swift
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 
24 namespace 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()) { return; }
37 
38  char xplanePath[512];
39  XPLMGetSystemPath(xplanePath);
40 #ifdef APL
41  if (XPLMIsFeatureEnabled("XPLM_USE_NATIVE_PATHS") == 0)
42  {
43  HFS2PosixPath(xplanePath, xplanePath, sizeof(xplanePath));
44  }
45  g_sep = "/";
46 #else
47  g_sep = XPLMGetDirectorySeparator();
48 #endif
49  g_xplanePath = xplanePath;
50  }
51 
52  void Logger::print(const std::string &filePath, int line, MsgType type, const std::string &message)
53  {
54  (void)line;
55  (void)type;
56  (void)filePath;
57 
58  assert(!filePath.empty());
59  std::ostringstream ss;
60  ss << "xswiftbus: ";
61 
62 #if defined(XSWIFTBUS_ENABLE_TRACE_LOG)
63  switch (type)
64  {
65  case DebugMsg: ss << "Debug"; break;
66  case InfoMsg: ss << "Info"; break;
67  case WarningMsg: ss << "Warning"; break;
68  case ErrorMsg: ss << "Error"; break;
69  }
70  ss << ' ';
71 
72  std::string seperator = "/\\";
73  std::size_t sepPos = filePath.find_last_of(seperator);
74  if (sepPos != std::string::npos) { ss << filePath.substr(sepPos + 1, filePath.size() - 1); }
75  else { ss << filePath; }
76  ss << ' ';
77 
78  ss << line;
79  ss << " : ";
80 #endif // XSWIFTBUS_ENABLE_TRACE_LOG
81 
82  ss << message;
83  ss << "\n";
84 
85  const std::string buffer = ss.str();
86  XPLMDebugString(buffer.c_str());
87  }
88 
89 #ifdef APL
90  template <typename T>
91  struct CFSmartPtr
92  {
93  CFSmartPtr(T p) : p_(p) {}
94  ~CFSmartPtr()
95  {
96  if (p_) CFRelease(p_);
97  }
98  operator T() { return p_; }
99  T p_;
100  };
101 
102 # ifdef __clang__
103 # pragma clang diagnostic push
104 # pragma clang diagnostic ignored "-Wdeprecated-declarations"
105 # endif
106 
107  int HFS2PosixPath(const char *path, char *result, int resultLen)
108  {
109  bool is_dir = (path[strlen(path) - 1] == ':');
110 
111  CFSmartPtr<CFStringRef> inStr(CFStringCreateWithCString(kCFAllocatorDefault, path, kCFStringEncodingMacRoman));
112  if (inStr == nullptr) return -1;
113 
114  CFSmartPtr<CFURLRef> url(CFURLCreateWithFileSystemPath(kCFAllocatorDefault, inStr, kCFURLHFSPathStyle, 0));
115  if (url == nullptr) return -1;
116 
117  CFSmartPtr<CFStringRef> outStr(CFURLCopyFileSystemPath(url, kCFURLPOSIXPathStyle));
118  if (outStr == nullptr) return -1;
119 
120  if (!CFStringGetCString(outStr, result, resultLen, kCFStringEncodingMacRoman)) return -1;
121 
122  if (is_dir) strcat(result, "/");
123 
124  return 0;
125  }
126 
127 # ifdef __clang__
128 # pragma clang diagnostic pop
129 # endif
130 #endif // APL
131 } // namespace XSwiftBus
132 
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.