Files
pilotclient/src/xswiftbus/utils.cpp
2020-08-02 23:01:29 +01:00

149 lines
3.6 KiB
C++

/* Copyright (C) 2016
* swift Project Community / Contributors
*
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
* directory of this distribution. No part of swift project, including this file, may be copied, modified, propagated,
* or distributed except according to the terms contained in the LICENSE file.
*/
//! \cond PRIVATE
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include "utils.h"
#include <XPMPMultiplayer.h>
#include <XPLM/XPLMUtilities.h>
#include <XPLM/XPLMPlugin.h>
#include <string>
#include <sstream>
#include <cassert>
#ifdef APL
#include <Carbon/Carbon.h>
#endif
namespace XSwiftBus
{
std::string g_xplanePath;
std::string g_sep;
#ifdef APL
int HFS2PosixPath(const char *path, char *result, int resultLen);
#endif
//! Init global xplane path
void initXPlanePath()
{
if (!g_xplanePath.empty() && !g_sep.empty()) { return; }
char xplanePath[512];
XPLMGetSystemPath(xplanePath);
#ifdef APL
if (XPLMIsFeatureEnabled("XPLM_USE_NATIVE_PATHS") == 0)
{
HFS2PosixPath(xplanePath, xplanePath, sizeof(xplanePath));
}
g_sep = "/";
#else
g_sep = XPLMGetDirectorySeparator();
#endif
g_xplanePath = xplanePath;
}
void Logger::print(const std::string &filePath, int line, MsgType type, const std::string &message)
{
(void) line;
(void) type;
(void) filePath;
assert(!filePath.empty());
std::ostringstream ss;
ss << "xswiftbus: ";
#if defined(XSWIFTBUS_ENABLE_TRACE_LOG)
switch (type)
{
case DebugMsg:
ss << "Debug";
break;
case InfoMsg:
ss << "Info";
break;
case WarningMsg:
ss << "Warning";
break;
case ErrorMsg:
ss << "Error";
break;
}
ss << ' ';
std::string seperator = "/\\";
std::size_t sepPos = filePath.find_last_of(seperator);
if (sepPos != std::string::npos)
{
ss << filePath.substr(sepPos + 1, filePath.size() - 1);
}
else
{
ss << filePath;
}
ss << ' ';
ss << line;
ss << " : ";
#endif //XSWIFTBUS_ENABLE_TRACE_LOG
ss << message;
ss << "\n";
const std::string buffer = ss.str();
XPLMDebugString(buffer.c_str());
}
#ifdef APL
template <typename T>
struct CFSmartPtr
{
CFSmartPtr(T p) : p_(p) {}
~CFSmartPtr() { if (p_) CFRelease(p_); }
operator T () { return p_; }
T p_;
};
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
#endif
int HFS2PosixPath(const char *path, char *result, int resultLen)
{
bool is_dir = (path[strlen(path)-1] == ':');
CFSmartPtr<CFStringRef> inStr(CFStringCreateWithCString(kCFAllocatorDefault, path ,kCFStringEncodingMacRoman));
if (inStr == nullptr) return -1;
CFSmartPtr<CFURLRef> url(CFURLCreateWithFileSystemPath(kCFAllocatorDefault, inStr, kCFURLHFSPathStyle,0));
if (url == nullptr) return -1;
CFSmartPtr<CFStringRef> outStr(CFURLCopyFileSystemPath(url, kCFURLPOSIXPathStyle));
if (outStr == nullptr) return -1;
if (!CFStringGetCString(outStr, result, resultLen, kCFStringEncodingMacRoman))
return -1;
if(is_dir) strcat(result, "/");
return 0;
}
#ifdef __clang__
#pragma clang diagnostic pop
#endif
#endif //APL
}
//! \endcond