30 #define ERR_ASSERT "ASSERT FAILED: %s"
31 #define ERR_EXCEPTION "EXCEPTION CAUGHT: %s"
51 #define PATH_DELIM_STD '\\'
53 #define PATH_DELIM_STD '/'
60 bool IsDir (
const std::string& path);
68 bool CopyFileIfNewer(
const std::string& source,
const std::string& destDir);
74 std::istream&
safeGetline(std::istream& is, std::string& t);
89 #define WHITESPACE " \t\f\v\r\n"
98 s.erase(s.find_last_not_of(t) + 1);
106 s.erase(0, s.find_first_not_of(t));
118 inline std::string
leftOf(
const std::string& s,
const std::string& terminators)
120 return s.substr(0, std::min(s.find_first_of(terminators), s.size()));
124 std::vector<std::string>
str_tokenize (
const std::string s,
125 const std::string tokens,
126 bool bSkipEmpty =
true);
133 constexpr
double PI = 3.1415926535897932384626433832795028841971693993751;
146 {
return (_rad < T(0) ? T(360) : T(0)) + _rad * T(180) / T(
PI); }
151 {
return _deg * T(
PI) / T(180); }
155 inline T
sqr (
const T a) {
return a*a; }
159 inline T
dist (
const T x1,
const T y1,
const T z1,
160 const T x2,
const T y2,
const T z2)
162 return std::sqrt(
sqr(x1-x2) +
sqr(y1-y2) +
sqr(z1-z2));
167 {
return rad2deg(atan2(x,y)); }
177 {
return rad2deg(atan2(z2-z1,x2-x1) +
float(
PI/2.0)); }
180 float headDiff (
float head1,
float head2);
183 template <
class numT>
187 _head += std::ceil(_head/-numT(360)) * numT(360);
188 else if (_head >= numT(360))
189 _head -= std::floor(_head/numT(360)) * numT(360);
197 std::valarray<float>
HeadPitch2Vec (
const float head,
const float pitch);
200 std::valarray<float>
HeadPitchRoll2Normal(
const float head,
const float pitch,
const float roll);
242 #if defined(__clang__) || defined(__GNUC__)
243 #define XPMP2_FMTARGS(FMT) __attribute__((format(printf, FMT, FMT+1)))
245 #define XPMP2_FMTARGS(FMT)
259 const char*
LogGetString (
const char* szFile,
int ln,
const char* szFunc,
logLevelTy lvl,
const char* szMsg, va_list args );
271 #define LOG_MSG(lvl,...) { \
272 if (lvl >= XPMP2::glob.logLvl) \
273 {LogMsg(__FILE__, __LINE__, __func__, lvl, __VA_ARGS__);} \
279 #define LOG_MATCHING(lvl,...) { \
280 if (XPMP2::glob.bLogMdlMatch && lvl >= glob.logLvl) \
281 {LogMsg(__FILE__, __LINE__, __func__, lvl, __VA_ARGS__);} \
287 #define THROW_ERROR(...) \
288 throw XPMP2Error(__FILE__, __LINE__, __func__, __VA_ARGS__);
292 #define LOG_ASSERT(cond) \
294 THROW_ERROR(ERR_ASSERT,#cond); \
298 #define CATCH_AC(ac) \
299 catch (const std::exception& e) { \
300 LOG_MSG(logFATAL, ERR_EXCEPTION, e.what()); \
304 LOG_MSG(logFATAL, ERR_EXCEPTION, "<unknown>"); \
313 #if APL == 1 || LIN == 1
315 inline void strncpy_s(
char * dest,
size_t destsz,
const char * src,
size_t count)
317 strncpy(dest, src, std::min(destsz,count)); dest[destsz - 1] = 0;
321 inline struct tm *gmtime_s(
struct tm * result,
const time_t * time)
322 {
return gmtime_r(time, result); }
323 inline struct tm *localtime_s(
struct tm * result,
const time_t * time)
324 {
return localtime_r(time, result); }
329 #define STRCPY_S(dest,src) strncpy_s(dest,sizeof(dest),src,sizeof(dest)-1)
330 #define STRCPY_ATMOST(dest,src) strncpy_s(dest,sizeof(dest),strAtMost(src,sizeof(dest)-1).c_str(),sizeof(dest)-1)
334 inline int strerror_s(
char *buf,
size_t bufsz,
int errnum )
335 {
return strerror_r(errnum, buf, bufsz); }
337 inline int strerror_s(
char *buf,
size_t bufsz,
int errnum )
338 { strerror_r(errnum, buf, bufsz);
return 0; }
344 std::string
TOPOSIX (
const std::string& p);
346 std::string
FROMPOSIX (
const std::string& p);
349 inline std::string
TOPOSIX (
const std::string& p) {
return p; }
351 inline std::string
FROMPOSIX (
const std::string& p) {
return p; }
358 #define SET_THREAD_NAME(sName) SetThreadDescription(GetCurrentThread(), L##sName)
360 #define SET_THREAD_NAME(sName) pthread_setname_np(sName)
362 #define SET_THREAD_NAME(sName) pthread_setname_np(pthread_self(),sName)
365 #define SET_THREAD_NAME(sName)
#define WHITESPACE
Definition: Utilities.h:89
#define XPMP2_FMTARGS(FMT)
To apply printf-style warnings to our functions.
Definition: Utilities.h:245
Definition: XPMPAircraft.h:70
std::valarray< float > HeadPitch2Vec(const float head, const float pitch)
Convert heading/pitch to normalized x/y/z vector.
Definition: Utilities.cpp:675
T rad2deg(const T _rad)
Convert radians to degrees, normalized to [0..360)
Definition: Utilities.h:145
logLevelTy
Logging level.
Definition: Utilities.h:249
@ logFATAL
fatal is shortly before a crash
Definition: Utilities.h:254
@ logMSG
will always be output, no matter what has been configured, cannot be suppressed
Definition: Utilities.h:255
@ logDEBUG
Debug, highest level of detail.
Definition: Utilities.h:250
@ logWARN
warnings, i.e. unexpected, but uncritical events, maybe leading to unwanted display,...
Definition: Utilities.h:252
@ logERR
errors mean, aircraft can potentially not be displayed
Definition: Utilities.h:253
@ logINFO
regular info messages
Definition: Utilities.h:251
std::string leftOf(const std::string &s, const std::string &terminators)
Returns everything left of any of terminators.
Definition: Utilities.h:118
const char * LogGetString(const char *szPath, int ln, const char *szFunc, logLevelTy lvl, const char *szMsg, va_list args)
Returns ptr to static buffer filled with formatted log string.
Definition: Utilities.cpp:826
constexpr const char * RSRC_OBJ8DATAREFS
Definition: Utilities.h:38
std::string TOPOSIX(const std::string &p)
On Lin/Win there is no need for a conversion, but we do treat p now as std::string
Definition: Utilities.h:349
constexpr double PI
Pi.
Definition: Utilities.h:133
void LogMsg(const char *szPath, int ln, const char *szFunc, logLevelTy lvl, const char *szMsg,...)
Log Text to log file.
Definition: Utilities.cpp:866
bool CheckEverySoOften(float &_lastCheck, float _interval, float _now)
Convenience function to check on something at most every x seconds.
Definition: Utilities.cpp:782
std::string & ltrim(std::string &s, const char *t=WHITESPACE)
trimming of string (from left)
Definition: Utilities.h:104
std::string GetMiscNetwTimeStr(float _time)
Return the network time as a string like used in the XP's Log.txt.
Definition: Utilities.cpp:738
std::string FROMPOSIX(const std::string &p)
On Lin/Win there is no need for a conversion, but we do treat p now as std::string
Definition: Utilities.h:351
float angleLocCoord(float x1, float z1, float x2, float z2)
Angle of line from point (x1|z1) to point (x2|z2)
Definition: Utilities.h:176
void RemoveExtension(std::string &path)
Removes everything after the last dot, the dot including.
Definition: Utilities.cpp:490
std::list< std::string > GetDirContents(const std::string &path)
List of files in a directory (wrapper around XPLMGetDirectoryContents)
Definition: Utilities.cpp:421
constexpr const char * RSRC_RELATED
Definition: Utilities.h:34
std::valarray< float > HeadPitchRoll2Normal(const float head, const float pitch, const float roll)
Convert heading/pitch/roll to unit and normal vector, ie. returns 6 values, first 3 like HeadPitch2Ve...
Definition: Utilities.cpp:696
T sqr(const T a)
Square.
Definition: Utilities.h:155
float GetMiscNetwTime()
Get synched network time from X-Plane (sim/network/misc/network_time_sec) as used in Log....
Definition: Utilities.cpp:729
bool fequal(float a, float b)
Are these two float near-equal? (to avoid trying something like a == b)
Definition: Utilities.h:139
const std::string & GetXPSystemPath()
Returns XP's system directory, including a trailing slash.
Definition: Utilities.cpp:467
numT headNormalize(numT _head)
Normalize a heading value to [0..360), works for both float and double values.
Definition: Utilities.h:184
constexpr float EPSILON_F
Epsilon, a small number.
Definition: Utilities.h:136
const char * GetGraphicsDriverTxt()
Text string for current graphics driver in use.
Definition: Utilities.cpp:755
float headDiff(float head1, float head2)
(Shortest) difference between 2 angles: How much to turn to go from h1 to h2?
Definition: Utilities.cpp:655
bool ExistsFile(const std::string &filename)
Does a file path exist?
Definition: Utilities.cpp:337
bool CreateDir(const std::string &path)
Create directory if it does not exist.
Definition: Utilities.cpp:353
std::string & str_tolower(std::string &s)
change a std::string to uppercase
Definition: Utilities.cpp:601
bool CopyFileIfNewer(const std::string &source, const std::string &destDir)
Copy file if source is newer or destination missing.
Definition: Utilities.cpp:368
constexpr const char * RSRC_MAP_ICONS
Definition: Utilities.h:37
bool IsPaused()
X-Plane in a Pause state?
Definition: Utilities.cpp:768
bool IsViewExternal()
Is current X-Plane view an external view (outside a cockpit)?
Definition: Utilities.cpp:775
T dist(const T x1, const T y1, const T z1, const T x2, const T y2, const T z2)
Pythagorean distance between two points in a 3-D world.
Definition: Utilities.h:159
std::string & trim(std::string &s, const char *t=WHITESPACE)
trimming of string (from both ends)
Definition: Utilities.h:112
std::string StripXPSysDir(const std::string &path)
If a path starts with X-Plane's system directory it is stripped.
Definition: Utilities.cpp:480
T deg2rad(const T _deg)
Convert degree to radians.
Definition: Utilities.h:150
constexpr const char * RSRC_REL_OP
Definition: Utilities.h:35
std::string & rtrim(std::string &s, const char *t=WHITESPACE)
trimming of string (from right)
Definition: Utilities.h:96
bool IsDir(const std::string &path)
Is path a directory?
Definition: Utilities.cpp:344
float atan2deg(float x, float y)
atan2 converted to degrees: the angle between (0|0) and the given point
Definition: Utilities.h:166
int PrefsFuncIntDefault(const char *, const char *, int _default)
Default config function just always returns the provided default value.
Definition: Utilities.cpp:179
constexpr const char * RSRC_DOC8643
Definition: Utilities.h:36
std::vector< std::string > str_tokenize(const std::string s, const std::string tokens, bool bSkipEmpty)
separates string into tokens
Definition: Utilities.cpp:608
std::istream & safeGetline(std::istream &is, std::string &t)
Read a line from a text file, no matter if ending on CRLF or LF.
Definition: Utilities.cpp:454