swift
Loading...
Searching...
No Matches
utils.cpp
1// SPDX-FileCopyrightText: Copyright (C) 2016 swift Project Community / Contributors
2// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3
5
6#ifndef NOMINMAX
7# define NOMINMAX
8#endif
9
10#include "utils.h"
11
12#include <XPLM/XPLMPlugin.h>
13#include <XPLM/XPLMUtilities.h>
14#include <XPMPMultiplayer.h>
15
16#include <cassert>
17#include <sstream>
18#include <string>
19
20#ifdef APL
21# include <Carbon/Carbon.h>
22#endif
23
24namespace XSwiftBus
25{
26 std::string g_xplanePath;
27 std::string g_sep;
28
29#ifdef APL
30 int HFS2PosixPath(const char *path, char *result, int resultLen);
31#endif
32
34 void initXPlanePath()
35 {
36 if (!g_xplanePath.empty() && !g_sep.empty())
37 {
38 return;
39 }
40
41 char xplanePath[512];
42 XPLMGetSystemPath(xplanePath);
43#ifdef APL
44 if (XPLMIsFeatureEnabled("XPLM_USE_NATIVE_PATHS") == 0)
45 {
46 HFS2PosixPath(xplanePath, xplanePath, sizeof(xplanePath));
47 }
48 g_sep = "/";
49#else
50 g_sep = XPLMGetDirectorySeparator();
51#endif
52 g_xplanePath = xplanePath;
53 }
54
55 void Logger::print(const std::string &filePath, int line, MsgType type, const std::string &message)
56 {
57 (void)line;
58 (void)type;
59 (void)filePath;
60
61 assert(!filePath.empty());
62 std::ostringstream ss;
63 ss << "xswiftbus: ";
64
65#if defined(XSWIFTBUS_ENABLE_TRACE_LOG)
66 switch (type)
67 {
68 case DebugMsg: ss << "Debug"; break;
69 case InfoMsg: ss << "Info"; break;
70 case WarningMsg: ss << "Warning"; break;
71 case ErrorMsg: ss << "Error"; break;
72 }
73 ss << ' ';
74
75 std::string seperator = "/\\";
76 std::size_t sepPos = filePath.find_last_of(seperator);
77 if (sepPos != std::string::npos)
78 {
79 ss << filePath.substr(sepPos + 1, filePath.size() - 1);
80 }
81 else
82 {
83 ss << filePath;
84 }
85 ss << ' ';
86
87 ss << line;
88 ss << " : ";
89#endif // XSWIFTBUS_ENABLE_TRACE_LOG
90
91 ss << message;
92 ss << "\n";
93
94 const std::string buffer = ss.str();
95 XPLMDebugString(buffer.c_str());
96 }
97
98#ifdef APL
99 template <typename T>
100 struct CFSmartPtr
101 {
102 CFSmartPtr(T p) : p_(p) {}
103 ~CFSmartPtr()
104 {
105 if (p_) CFRelease(p_);
106 }
107 operator T() { return p_; }
108 T p_;
109 };
110
111# ifdef __clang__
112# pragma clang diagnostic push
113# pragma clang diagnostic ignored "-Wdeprecated-declarations"
114# endif
115
116 int HFS2PosixPath(const char *path, char *result, int resultLen)
117 {
118 bool is_dir = (path[strlen(path) - 1] == ':');
119
120 CFSmartPtr<CFStringRef> inStr(CFStringCreateWithCString(kCFAllocatorDefault, path, kCFStringEncodingMacRoman));
121 if (inStr == nullptr) return -1;
122
123 CFSmartPtr<CFURLRef> url(CFURLCreateWithFileSystemPath(kCFAllocatorDefault, inStr, kCFURLHFSPathStyle, 0));
124 if (url == nullptr) return -1;
125
126 CFSmartPtr<CFStringRef> outStr(CFURLCopyFileSystemPath(url, kCFURLPOSIXPathStyle));
127 if (outStr == nullptr) return -1;
128
129 if (!CFStringGetCString(outStr, result, resultLen, kCFStringEncodingMacRoman)) return -1;
130
131 if (is_dir) strcat(result, "/");
132
133 return 0;
134 }
135
136# ifdef __clang__
137# pragma clang diagnostic pop
138# endif
139#endif // APL
140} // namespace XSwiftBus
141
static void print(const std::string &filePath, int line, MsgType type, const std::string &message)
Print message to X-Plane log.
Plugin loaded by X-Plane which publishes a DBus service.
Definition command.h:14
std::string g_xplanePath
Absolute xplane path.
std::string g_sep
Platform specific dir separator.
void initXPlanePath()
Init global xplane path.