SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
Utils Namespace Reference

Utils provides utilities for string & file handling, logging and math functions. More...

Classes

class  Averaged
 Averaged template class provides an average value from a fixed size array. More...
 
struct  AverageTimingBlock
 concatenation of average value and timer More...
 
class  AverageTiming
 Singleton timing class for average measurement of different timing blocks in loops. More...
 
class  CustomLog
 Logger interface. More...
 
class  FileLog
 File logging class. More...
 
class  ComputerInfos
 Class for holding computer information. More...
 

Typedefs

typedef Utils::Averaged< float > AvgFloat
 

Functions

string toString (float f, int roundedDecimals=1)
 Returns a string from a float with max. one trailing zero. More...
 
string toString (double d, int roundedDecimals=1)
 Returns a string from a double with max. one trailing zero. More...
 
string toLowerString (string s)
 Returns a string in lower case. More...
 
string toUpperString (string s)
 Returns a string in upper case. More...
 
string trimString (const string &s, const string &drop=" ")
 Trims a string at both end. More...
 
string trimRightString (const string &s, const string &drop)
 trims a string at the right end More...
 
string trimLeftString (const string &s, const string &drop)
 trims a string at the left end More...
 
void splitString (const string &s, char delimiter, vector< string > &splits)
 Splits an input string at a delimiter character into a string vector. More...
 
void replaceString (string &source, const string &from, const string &to)
 Replaces in the source string the from string by the to string. More...
 
vector< string > getStringLines (const string &multiLineString)
 Returns a vector of string one per line of a multiline string. More...
 
string readTextFileIntoString (const char *logTag, const string &pathAndFilename)
 Reads a text file into a string and returns it. More...
 
void writeStringIntoTextFile (const char *logTag, const string &stringToWrite, const string &pathAndFilename)
 Writes a string into a text file. More...
 
string replaceNonFilenameChars (string source, char replaceChar='-')
 replaces non-filename characters: /|?%*:"<>' More...
 
string getLocalTimeString ()
 Returns local time as string like "Wed Feb 13 15:46:11 2019". More...
 
string getDateTime1String ()
 Returns local time as string like "13.02.19-15:46". More...
 
string getDateTime2String ()
 Returns local time as string like "20190213-154611". More...
 
string getHostName ()
 Returns the computer name. More...
 
string formatString (string fmt_str,...)
 Returns a formatted string as sprintf. More...
 
bool containsString (const string &container, const string &search)
 Returns true if container contains the search string. More...
 
bool startsWithString (const string &container, const string &startStr)
 Return true if the container string starts with the startStr. More...
 
bool endsWithString (const string &container, const string &endStr)
 Return true if the container string ends with the endStr. More...
 
string unifySlashes (const string &inputDir, bool withTrailingSlash=true)
 Returns the inputDir string with unified forward slashes, e.g.: "dirA/dirB/". More...
 
string getPath (const string &pathFilename)
 Returns the path w. '\' of path-filename string. More...
 
bool getFileContent (const string &fileName, vector< string > &vecOfStrings)
 Returns true if content of file could be put in a vector of strings. More...
 
bool compareNatural (const string &a, const string &b)
 Naturally compares two strings (used for filename sorting) More...
 
string getFileName (const string &pathFilename)
 Returns the filename of path-filename string. More...
 
string getDirName (const string &pathFilename)
 Strip last component from file name. More...
 
string getFileNameWOExt (const string &pathFilename)
 Returns the filename without extension. More...
 
string getFileExt (const string &filename)
 Returns the file extension without dot in lower case. More...
 
vector< string > getDirNamesInDir (const string &dirName, bool fullPath=true)
 Returns a vector directory names with path in dir. More...
 
vector< string > getAllNamesInDir (const string &dirName, bool fullPath=true)
 Returns a vector of sorted names (files and directories) with path in dir. More...
 
vector< string > getFileNamesInDir (const string &dirName, bool fullPath=true)
 Returns a vector of sorted filesnames in dirName. More...
 
bool dirExists (const string &path)
 Returns true if a directory exists. More...
 
bool makeDir (const string &path)
 Creates a directory with given path. More...
 
bool makeDirRecurse (std::string path)
 
void removeDir (const string &path)
 RemoveDir deletes a directory with given path. More...
 
void removeFile (const string &path)
 RemoveFile deletes a file with given path. More...
 
bool fileExists (const string &pathfilename)
 Returns true if a file exists. More...
 
unsigned int getFileSize (const string &filename)
 Returns the file size in bytes. More...
 
unsigned int getFileSize (std::ifstream &fs)
 
string getAppsWritableDir (string appName="SLProject")
 Returns the writable configuration directory. More...
 
string getCurrentWorkingDir ()
 Returns the working directory. More...
 
bool deleteFile (string &pathfilename)
 Deletes a file on the filesystem. More...
 
void loopFileSystemRec (const string &path, function< void(string path, string baseName, int depth)> processFile, function< void(string path, string baseName, int depth)> processDir, const int depth=0)
 process all files and folders recursively naturally sorted More...
 
void dumpFileSystemRec (const char *logtag, const string &folderpath)
 Dumps all folders and files recursovely. More...
 
string findFile (const string &filename, const vector< string > &pathsToCheck)
 Tries to find a filename on various paths to check. More...
 
void initFileLog (const string &logDir, bool forceFlush)
 
void log (const char *tag, const char *format,...)
 logs a formatted string platform independently More...
 
void exitMsg (const char *tag, const char *msg, int line, const char *file)
 Terminates the application with a message. No leak checking. More...
 
void warnMsg (const char *tag, const char *msg, int line, const char *file)
 Platform independent warn message output. More...
 
void errorMsg (const char *tag, const char *msg, int line, const char *file)
 Platform independent error message output. More...
 
unsigned int maxThreads ()
 Returns in release config the max. NO. of threads otherwise 1. More...
 
int gcd (int a, int b)
 Greatest common divisor of two integer numbers (ggT = grösster gemeinsame Teiler) More...
 
int lcm (int a, int b)
 
unsigned closestPowerOf2 (unsigned num)
 Returns the closest power of 2 to a passed number. More...
 
unsigned nextPowerOf2 (unsigned num)
 Returns the next power of 2 to a passed number. More...
 
bool makeDirRecurse (string path)
 Creates a directory with given path recursively. More...
 
void initFileLog (const std::string &logDir, bool forceFlush)
 Instantiates FileLog instance. More...
 
template<class T >
sign (T a)
 
template<class T >
floor (T a)
 
template<class T >
ceil (T a)
 
template<class T >
fract (T a)
 
template<class T >
abs (T a)
 
template<class T >
mod (T a, T b)
 
template<class T >
step (T edge, T x)
 
template<class T >
pulse (T a, T b, T x)
 
template<class T >
clamp (T a, T min, T max)
 
template<class T >
mix (T mix, T a, T b)
 
template<class T >
lerp (T x, T a, T b)
 
bool isPowerOf2 (unsigned int a)
 Returns true if a number is of power of 2. More...
 
float random (float min, float max)
 Returns a uniform distributed random float number between min and max. More...
 
int random (int min, int max)
 Returns a uniform distributed random int number between min and max. More...
 

Variables

std::unique_ptr< CustomLogcustomLog
 custom log instance, e.g. log to a ui log window More...
 
bool onlyErrorLogs = false
 if this flag is set to true all calls to log get ignored More...
 
static std::unique_ptr< FileLogfileLog
 
static const float PI = 3.14159265358979f
 
static const float RAD2DEG = 180.0f / PI
 
static const float DEG2RAD = PI / 180.0f
 
static const float TWOPI = 2.0f * PI
 
static const float ONEOVERPI = 1.0f / PI
 
static const float HALFPI = PI * 0.5f
 

Detailed Description

Utils provides utilities for string & file handling, logging and math functions.

Function are grouped into sections:

  • String Handling Functions
  • File Handling Functions
  • Logging Functions
  • Math Constants and Functions

Typedef Documentation

◆ AvgFloat

Definition at line 85 of file Averaged.h.

Function Documentation

◆ abs()

template<class T >
T Utils::abs ( a)
inline

Definition at line 249 of file Utils.h.

249 {return (a >= 0) ? a : -a;}

◆ ceil()

template<class T >
T Utils::ceil ( a)
inline

Definition at line 247 of file Utils.h.

247 {return (T)((int)a + ((a > 0 && a != (int)(a))));}

◆ clamp()

template<class T >
T Utils::clamp ( a,
min,
max 
)
inline

Definition at line 253 of file Utils.h.

253 {return (a < min) ? min : (a > max) ? max : a;}

◆ closestPowerOf2()

unsigned Utils::closestPowerOf2 ( unsigned  num)

Returns the closest power of 2 to a passed number.

Definition at line 1221 of file Utils.cpp.

1222 {
1223  unsigned nextPow2 = 1;
1224  if (num <= 0) return 1;
1225 
1226  while (nextPow2 <= num)
1227  nextPow2 <<= 1;
1228  unsigned prevPow2 = nextPow2 >> 1;
1229 
1230  if (num - prevPow2 < nextPow2 - num)
1231  return prevPow2;
1232  else
1233  return nextPow2;
1234 }

◆ compareNatural()

bool Utils::compareNatural ( const string &  a,
const string &  b 
)

Naturally compares two strings (used for filename sorting)

String comparison as most filesystem do it. Source: https://www.o-rho.com/naturalsort

std::sort compareNatural


1.txt 1.txt 10.txt 1_t.txt 1_t.txt 10.txt 20 20 20.txt 20.txt ABc ABc aBCd aBCd aBCd(01) aBCd(1) aBCd(1) aBCd(01) aBCd(12) aBCd(2) aBCd(2) aBCd(12) aBc aBc aBcd aBcd aaA aaA aaa aaa z10.txt z2.txt z100.txt z10.txt z2.txt z100.txt

Definition at line 464 of file Utils.cpp.

465 {
466  const char* p1 = a.c_str();
467  const char* p2 = b.c_str();
468  const unsigned short st_scan = 0;
469  const unsigned short st_alpha = 1;
470  const unsigned short st_numeric = 2;
471  unsigned short state = st_scan;
472  const char* numstart1 = nullptr;
473  const char* numstart2 = nullptr;
474  const char* numend1 = nullptr;
475  const char* numend2 = nullptr;
476  unsigned long sz1 = 0;
477  unsigned long sz2 = 0;
478 
479  while (*p1 && *p2)
480  {
481  switch (state)
482  {
483  case st_scan:
484  if (!isdigit(*p1) && !isdigit(*p2))
485  {
486  state = st_alpha;
487  if (*p1 == *p2)
488  {
489  p1++;
490  p2++;
491  }
492  else
493  return *p1 < *p2;
494  }
495  else if (isdigit(*p1) && !isdigit(*p2))
496  return true;
497  else if (!isdigit(*p1) && isdigit(*p2))
498  return false;
499  else
500  {
501  state = st_numeric;
502  if (sz1 == 0)
503  while (*p1 == '0')
504  {
505  p1++;
506  sz1++;
507  }
508  else
509  while (*p1 == '0') p1++;
510  if (sz2 == 0)
511  while (*p2 == '0')
512  {
513  p2++;
514  sz2++;
515  }
516  else
517  while (*p2 == '0') p2++;
518  if (sz1 == sz2)
519  {
520  sz1 = 0;
521  sz2 = 0;
522  }
523  if (!isdigit(*p1)) p1--;
524  if (!isdigit(*p2)) p2--;
525  numstart1 = p1;
526  numstart2 = p2;
527  numend1 = numstart1;
528  numend2 = numstart2;
529  }
530  break;
531  case st_alpha:
532  if (!isdigit(*p1) && !isdigit(*p2))
533  {
534  if (*p1 == *p2)
535  {
536  p1++;
537  p2++;
538  }
539  else
540  return *p1 < *p2;
541  }
542  else
543  state = st_scan;
544  break;
545  case st_numeric:
546  while (isdigit(*p1)) numend1 = p1++;
547  while (isdigit(*p2)) numend2 = p2++;
548  if (numend1 - numstart1 == numend2 - numstart2 &&
549  !strncmp(numstart1, numstart2, numend2 - numstart2 + 1))
550  state = st_scan;
551  else
552  {
553  if (numend1 - numstart1 != numend2 - numstart2)
554  return numend1 - numstart1 < numend2 - numstart2;
555  while (*numstart1 && *numstart2)
556  {
557  if (*numstart1 != *numstart2) return *numstart1 < *numstart2;
558  numstart1++;
559  numstart2++;
560  }
561  }
562  break;
563  default: break;
564  }
565  }
566  if (sz1 < sz2) return true;
567  if (sz1 > sz2) return false;
568  if (*p1 == 0 && *p2 != 0) return true;
569  if (*p1 != 0 && *p2 == 0) return false;
570  return false;
571 }

◆ containsString()

bool Utils::containsString ( const string &  container,
const string &  search 
)

Returns true if container contains the search string.

Definition at line 345 of file Utils.cpp.

346 {
347  return (container.find(search) != string::npos);
348 }

◆ deleteFile()

bool Utils::deleteFile ( string &  pathfilename)

Deletes a file on the filesystem.

Definition at line 1008 of file Utils.cpp.

1009 {
1010  if (fileExists(pathfilename))
1011  return remove(pathfilename.c_str()) != 0;
1012  return false;
1013 }
bool fileExists(const string &pathfilename)
Returns true if a file exists.
Definition: Utils.cpp:897

◆ dirExists()

bool Utils::dirExists ( const string &  path)

Returns true if a directory exists.

Definition at line 790 of file Utils.cpp.

791 {
792 #if defined(__EMSCRIPTEN__)
793  return true;
794 #elif defined(USE_STD_FILESYSTEM)
795  return fs::exists(path) && fs::is_directory(path);
796 #else
797  struct stat info
798  {
799  };
800  if (stat(path.c_str(), &info) != 0)
801  return false;
802  else if (info.st_mode & S_IFDIR)
803  return true;
804  else
805  return false;
806 #endif
807 }
bool exists(std::string path, SLIOStreamKind kind)
Checks whether a given file exists.

◆ dumpFileSystemRec()

void Utils::dumpFileSystemRec ( const char *  logtag,
const string &  folderPath 
)

Dumps all folders and files recursovely.

Definition at line 1051 of file Utils.cpp.

1052 {
1053  const char* tab = " ";
1054 
1056  folderPath,
1057  [logtag, tab](string path, string baseName, int depth) -> void {
1058  string indent;
1059  for (int d = 0; d < depth; ++d)
1060  indent += tab;
1061  string indentFolderName = indent + baseName;
1062  Utils::log(logtag, "%s", indentFolderName.c_str());
1063  },
1064  [logtag, tab](string path, string baseName, int depth) -> void {
1065  string indent;
1066  for (int d = 0; d < depth; ++d)
1067  indent += tab;
1068  string indentFolderName = indent + "[" + baseName + "]";
1069  Utils::log(logtag, "%s", indentFolderName.c_str());
1070  });
1071 }
void loopFileSystemRec(const string &path, function< void(string path, string baseName, int depth)> processFile, function< void(string path, string baseName, int depth)> processDir, const int depth)
process all files and folders recursively naturally sorted
Definition: Utils.cpp:1016
void log(const char *tag, const char *format,...)
logs a formatted string platform independently
Definition: Utils.cpp:1103

◆ endsWithString()

bool Utils::endsWithString ( const string &  container,
const string &  endStr 
)

Return true if the container string ends with the endStr.

Definition at line 357 of file Utils.cpp.

358 {
359  if (container.length() >= endStr.length())
360  return (0 == container.compare(container.length() - endStr.length(),
361  endStr.length(),
362  endStr));
363  else
364  return false;
365 }

◆ errorMsg()

void Utils::errorMsg ( const char *  tag,
const char *  msg,
const int  line,
const char *  file 
)

Platform independent error message output.

Definition at line 1168 of file Utils.cpp.

1172 {
1173 #if defined(ANDROID) || defined(ANDROID_NDK)
1174  __android_log_print(ANDROID_LOG_ERROR,
1175  tag,
1176  "Error: %s at line %d in %s\n",
1177  msg,
1178  line,
1179  file);
1180 #else
1181  std::cout << "--------------------------------\n"
1182  << "Error:\n"
1183  << "Tag: " << tag << '\n'
1184  << "Location: " << file << ":" << line << '\n'
1185  << "Message: " << msg << '\n'
1186  << "--------------------------------" << std::endl;
1187 #endif
1188 }

◆ exitMsg()

void Utils::exitMsg ( const char *  tag,
const char *  msg,
const int  line,
const char *  file 
)

Terminates the application with a message. No leak checking.

Definition at line 1135 of file Utils.cpp.

1139 {
1140  errorMsg(tag, msg, line, file);
1141  exit(-1);
1142 }
void errorMsg(const char *tag, const char *msg, const int line, const char *file)
Platform independent error message output.
Definition: Utils.cpp:1168

◆ fileExists()

bool Utils::fileExists ( const string &  pathfilename)

Returns true if a file exists.

Definition at line 897 of file Utils.cpp.

898 {
899 #if defined(__EMSCRIPTEN__)
900  return false;
901 #elif defined(USE_STD_FILESYSTEM)
902  return fs::exists(pathfilename);
903 #else
904  struct stat info
905  {
906  };
907  return (stat(pathfilename.c_str(), &info) == 0) && ((info.st_mode & S_IFDIR) == 0);
908 #endif
909 }

◆ findFile()

string Utils::findFile ( const string &  filename,
const vector< string > &  pathsToCheck 
)

Tries to find a filename on various paths to check.

Definition at line 1077 of file Utils.cpp.

1078 {
1079  if (Utils::fileExists(filename))
1080  return filename;
1081 
1082  // Check file existence
1083  for (const auto& path : pathsToCheck)
1084  {
1085  string pathPlusFilename = Utils::unifySlashes(path) + filename;
1086  if (Utils::fileExists(pathPlusFilename))
1087  return pathPlusFilename;
1088  }
1089  return "";
1090 }
string unifySlashes(const string &inputDir, bool withTrailingSlash)
Returns the inputDir string with unified forward slashes, e.g.: "dirA/dirB/".
Definition: Utils.cpp:368

◆ floor()

template<class T >
T Utils::floor ( a)
inline

Definition at line 246 of file Utils.h.

246 {return (T)((int)a - ((a < 0 && a != (int)(a))));}

◆ formatString()

string Utils::formatString ( string  fmt_str,
  ... 
)

Returns a formatted string as sprintf.

Definition at line 320 of file Utils.cpp.

321 {
322  // Reserve two times as much as the length of the fmt_str
323  int final_n = 0;
324  int n = ((int)fmt_str.size()) * 2;
325 
326  string str;
327  std::unique_ptr<char[]> formatted;
328  va_list ap;
329  while (true)
330  {
331  formatted.reset(new char[n]);
332  strcpy(&formatted[0], fmt_str.c_str());
333  va_start(ap, fmt_str);
334  final_n = vsnprintf(&formatted[0], (unsigned long)n, fmt_str.c_str(), ap);
335  va_end(ap);
336  if (final_n < 0 || final_n >= n)
337  n += abs(final_n - n + 1);
338  else
339  break;
340  }
341  return string(formatted.get());
342 }
T abs(T a)
Definition: Utils.h:249

◆ fract()

template<class T >
T Utils::fract ( a)
inline

Definition at line 248 of file Utils.h.

248 {return a - floor(a);}
T floor(T a)
Definition: Utils.h:246

◆ gcd()

int Utils::gcd ( int  a,
int  b 
)

Greatest common divisor of two integer numbers (ggT = grösster gemeinsame Teiler)

Definition at line 1207 of file Utils.cpp.

1208 {
1209  if (b == 0)
1210  return a;
1211  return gcd(b, a % b);
1212 }
int gcd(int a, int b)
Greatest common divisor of two integer numbers (ggT = grösster gemeinsame Teiler)
Definition: Utils.cpp:1207

◆ getAllNamesInDir()

vector< string > Utils::getAllNamesInDir ( const string &  dirName,
bool  fullPath 
)

Returns a vector of sorted names (files and directories) with path in dir.

Definition at line 691 of file Utils.cpp.

692 {
693  vector<string> filePathNames;
694 
695 #if defined(USE_STD_FILESYSTEM)
696  if (fs::exists(dirName) && fs::is_directory(dirName))
697  {
698  for (const auto& entry : fs::directory_iterator(dirName))
699  {
700  auto filename = entry.path().filename();
701  if (fullPath)
702  filePathNames.push_back(dirName + "/" + filename.u8string());
703  else
704  filePathNames.push_back(filename.u8string());
705  }
706  }
707 #else
708 # if defined(TARGET_OS_IOS) && (TARGET_OS_IOS == 1)
709  return Utils_iOS::getAllNamesInDir(dirName, fullPath);
710 # else
711  DIR* dir = opendir(dirName.c_str());
712 
713  if (dir)
714  {
715  struct dirent* dirContent = nullptr;
716 
717  while ((dirContent = readdir(dir)) != nullptr)
718  {
719  string name(dirContent->d_name);
720  if (name != "." && name != "..")
721  {
722  if (fullPath)
723  filePathNames.push_back(dirName + "/" + name);
724  else
725  filePathNames.push_back(name);
726  }
727  }
728  closedir(dir);
729  }
730 # endif
731 #endif
732 
733  return filePathNames;
734 }
static std::vector< std::string > getAllNamesInDir(const std::string &dirName, bool fullPath=true)
Returns all files and folders in a directory as a vector.

◆ getAppsWritableDir()

string Utils::getAppsWritableDir ( string  appName)

Returns the writable configuration directory.

Definition at line 942 of file Utils.cpp.

943 {
944 #if defined(_WIN32)
945  string appData = getenv("APPDATA");
946  string configDir = appData + "/" + appName;
947  replaceString(configDir, "\\", "/");
948  if (!dirExists(configDir))
949  makeDir(configDir.c_str());
950  return configDir + "/";
951 #elif defined(__APPLE__)
952  string home = getenv("HOME");
953  string appData = home + "/Library/Application Support";
954  string configDir = appData + "/" + appName;
955  if (!dirExists(configDir))
956  mkdir(configDir.c_str(), S_IRWXU);
957  return configDir + "/";
958 #elif defined(ANDROID) || defined(ANDROID_NDK)
959  // @todo Where is the app data path on Andoroid?
960 #elif defined(linux) || defined(__linux) || defined(__linux__)
961  // @todo Where is the app data path on Linux?
962  string home = getenv("HOME");
963  string configDir = home + "/." + appName;
964  if (!dirExists(configDir))
965  mkdir(configDir.c_str(), S_IRWXU | S_IRWXG | S_IRWXO);
966  return configDir + "/";
967 #elif defined(__EMSCRIPTEN__)
968  return "?";
969 #else
970 # error "No port to this OS"
971 #endif
972  return "";
973 }
bool dirExists(const string &path)
Returns true if a directory exists.
Definition: Utils.cpp:790
bool makeDir(const string &path)
Creates a directory with given path.
Definition: Utils.cpp:810
void replaceString(string &source, const string &from, const string &to)
Replaces in the source string the from string by the to string.
Definition: Utils.cpp:170

◆ getCurrentWorkingDir()

string Utils::getCurrentWorkingDir ( )

Returns the working directory.

Definition at line 976 of file Utils.cpp.

977 {
978 #if defined(_WIN32)
979 # if defined(USE_STD_FILESYSTEM)
980  return fs::current_path().u8string();
981 # else
982  int size = 256;
983  char* buffer = (char*)malloc(size);
984  if (_getcwd(buffer, size) == buffer)
985  {
986  string dir = buffer;
987  replaceString(dir, "\\", "/");
988  return dir + "/";
989  }
990 
991  free(buffer);
992  return "";
993 # endif
994 #elif !defined(__EMSCRIPTEN__)
995  size_t size = 256;
996  char* buffer = (char*)malloc(size);
997  if (getcwd(buffer, size) == buffer)
998  return string(buffer) + "/";
999 
1000  free(buffer);
1001  return "";
1002 #else
1003  return "/";
1004 #endif
1005 }

◆ getDateTime1String()

string Utils::getDateTime1String ( )

Returns local time as string like "13.02.19-15:46".

Definition at line 269 of file Utils.cpp.

270 {
271  time_t tm = 0;
272  time(&tm);
273  struct tm* t = localtime(&tm);
274 
275  static char shortTime[50];
276  snprintf(shortTime,
277  sizeof(shortTime),
278  "%.2d.%.2d.%.2d-%.2d:%.2d",
279  t->tm_mday,
280  t->tm_mon + 1,
281  t->tm_year - 100,
282  t->tm_hour,
283  t->tm_min);
284 
285  return string(shortTime);
286 }

◆ getDateTime2String()

string Utils::getDateTime2String ( )

Returns local time as string like "20190213-154611".

Definition at line 289 of file Utils.cpp.

290 {
291  time_t tm = 0;
292  time(&tm);
293  struct tm* t = localtime(&tm);
294 
295  static char shortTime[50];
296  snprintf(shortTime,
297  sizeof(shortTime),
298  "%.4d%.2d%.2d-%.2d%.2d%.2d",
299  1900 + t->tm_year,
300  t->tm_mon + 1,
301  t->tm_mday,
302  t->tm_hour,
303  t->tm_min,
304  t->tm_sec);
305 
306  return string(shortTime);
307 }

◆ getDirName()

string Utils::getDirName ( const string &  pathFilename)

Strip last component from file name.

Definition at line 598 of file Utils.cpp.

599 {
600  size_t i1 = pathFilename.rfind('\\', pathFilename.length());
601  size_t i2 = pathFilename.rfind('/', pathFilename.length());
602  int i = -1;
603 
604  if (i1 != string::npos && i2 != string::npos)
605  i = (int)std::max(i1, i2);
606  else if (i1 != string::npos)
607  i = (int)i1;
608  else if (i2 != string::npos)
609  i = (int)i2;
610 
611  return pathFilename.substr(0, i + 1);
612 }

◆ getDirNamesInDir()

vector< string > Utils::getDirNamesInDir ( const string &  dirName,
bool  fullPath 
)

Returns a vector directory names with path in dir.

Definition at line 638 of file Utils.cpp.

639 {
640  vector<string> filePathNames;
641 
642 #if defined(USE_STD_FILESYSTEM)
643  if (fs::exists(dirName) && fs::is_directory(dirName))
644  {
645  for (const auto& entry : fs::directory_iterator(dirName))
646  {
647  auto filename = entry.path().filename();
648  if (fs::is_directory(entry.status()))
649  {
650  if (fullPath)
651  filePathNames.push_back(dirName + "/" + filename.u8string());
652  else
653  filePathNames.push_back(filename.u8string());
654  }
655  }
656  }
657 #else
658  DIR* dir = opendir(dirName.c_str());
659 
660  if (dir)
661  {
662  struct dirent* dirContent = nullptr;
663 
664  while ((dirContent = readdir(dir)) != nullptr)
665  {
666  string name(dirContent->d_name);
667 
668  if (name != "." && name != "..")
669  {
670  struct stat path_stat
671  {
672  };
673  stat((dirName + name).c_str(), &path_stat);
674  if (!S_ISREG(path_stat.st_mode))
675  {
676  if (fullPath)
677  filePathNames.push_back(dirName + "/" + name);
678  else
679  filePathNames.push_back(name);
680  }
681  }
682  }
683  closedir(dir);
684  }
685 #endif
686 
687  return filePathNames;
688 }

◆ getFileContent()

bool Utils::getFileContent ( const string &  fileName,
vector< string > &  vecOfStrings 
)

Returns true if content of file could be put in a vector of strings.

Definition at line 411 of file Utils.cpp.

413 {
414 
415  // Open the File
416  std::ifstream in(fileName.c_str());
417 
418  // Check if object is valid
419  if (!in)
420  {
421  std::cerr << "Cannot open the File : " << fileName << std::endl;
422  return false;
423  }
424 
425  // Read the next line from File untill it reaches the end.
426  std::string str;
427  while (std::getline(in, str))
428  {
429  // Line contains string of length > 0 then save it in vector
430  if (!str.empty())
431  vecOfStrings.push_back(str);
432  }
433 
434  // Close The File
435  in.close();
436  return true;
437 }

◆ getFileExt()

string Utils::getFileExt ( const string &  filename)

Returns the file extension without dot in lower case.

Definition at line 629 of file Utils.cpp.

630 {
631  size_t i = filename.rfind('.', filename.length());
632  if (i != string::npos)
633  return toLowerString(filename.substr(i + 1, filename.length() - i));
634  return ("");
635 }
string toLowerString(string s)
Returns a string in lower case.
Definition: Utils.cpp:112

◆ getFileName()

string Utils::getFileName ( const string &  pathFilename)

Returns the filename of path-filename string.

Definition at line 580 of file Utils.cpp.

581 {
582  size_t i1 = pathFilename.rfind('\\', pathFilename.length());
583  size_t i2 = pathFilename.rfind('/', pathFilename.length());
584  int i = -1;
585 
586  if (i1 != string::npos && i2 != string::npos)
587  i = (int)std::max(i1, i2);
588  else if (i1 != string::npos)
589  i = (int)i1;
590  else if (i2 != string::npos)
591  i = (int)i2;
592 
593  return pathFilename.substr(i + 1, pathFilename.length() - i);
594 }

◆ getFileNamesInDir()

vector< string > Utils::getFileNamesInDir ( const string &  dirName,
bool  fullPath 
)

Returns a vector of sorted filesnames in dirName.

Definition at line 737 of file Utils.cpp.

738 {
739  vector<string> filePathNames;
740 
741 #if defined(USE_STD_FILESYSTEM)
742  if (fs::exists(dirName) && fs::is_directory(dirName))
743  {
744  for (const auto& entry : fs::directory_iterator(dirName))
745  {
746  auto filename = entry.path().filename();
747  if (fs::is_regular_file(entry.status()))
748  {
749  if (fullPath)
750  filePathNames.push_back(dirName + "/" + filename.u8string());
751  else
752  filePathNames.push_back(filename.u8string());
753  }
754  }
755  }
756 #else
757  // todo: does this part also return directories? It should only return file names..
758  DIR* dir = opendir(dirName.c_str());
759 
760  if (dir)
761  {
762  struct dirent* dirContent = nullptr;
763 
764  while ((dirContent = readdir(dir)) != nullptr)
765  {
766  string name(dirContent->d_name);
767  if (name != "." && name != "..")
768  {
769  struct stat path_stat
770  {
771  };
772  stat((dirName + name).c_str(), &path_stat);
773  if (S_ISREG(path_stat.st_mode))
774  {
775  if (fullPath)
776  filePathNames.push_back(dirName + name);
777  else
778  filePathNames.push_back(name);
779  }
780  }
781  }
782  closedir(dir);
783  }
784 #endif
785 
786  return filePathNames;
787 }

◆ getFileNameWOExt()

string Utils::getFileNameWOExt ( const string &  pathFilename)

Returns the filename without extension.

Definition at line 616 of file Utils.cpp.

617 {
618  string filename = getFileName(pathFilename);
619  size_t i = filename.rfind('.', filename.length());
620  if (i != string::npos)
621  {
622  return (filename.substr(0, i));
623  }
624 
625  return (filename);
626 }
string getFileName(const string &pathFilename)
Returns the filename of path-filename string.
Definition: Utils.cpp:580

◆ getFileSize() [1/2]

unsigned int Utils::getFileSize ( const string &  pathfilename)

Returns the file size in bytes.

Definition at line 912 of file Utils.cpp.

913 {
914 #if defined(USE_STD_FILESYSTEM)
915  if (fs::exists(pathfilename))
916  return (unsigned int)fs::file_size(pathfilename);
917  else
918  return 0;
919 #else
920  struct stat st
921  {
922  };
923  if (stat(pathfilename.c_str(), &st) != 0)
924  return 0;
925  return (unsigned int)st.st_size;
926 #endif
927 }

◆ getFileSize() [2/2]

unsigned int Utils::getFileSize ( std::ifstream &  fs)

Definition at line 930 of file Utils.cpp.

931 {
932  fs.seekg(0, std::ios::beg);
933  std::streampos begin = fs.tellg();
934  fs.seekg(0, std::ios::end);
935  std::streampos end = fs.tellg();
936  fs.seekg(0, std::ios::beg);
937  return (unsigned int)(end - begin);
938 }

◆ getHostName()

string Utils::getHostName ( )

Returns the computer name.

Definition at line 310 of file Utils.cpp.

311 {
312 #ifndef __EMSCRIPTEN__
313  return asio::ip::host_name();
314 #else
315  return "0.0.0.0";
316 #endif
317 }

◆ getLocalTimeString()

string Utils::getLocalTimeString ( )

Returns local time as string like "Wed Feb 13 15:46:11 2019".

Definition at line 258 of file Utils.cpp.

259 {
260  time_t tm = 0;
261  time(&tm);
262  struct tm* t2 = localtime(&tm);
263  char buf[1024];
264  strftime(buf, sizeof(buf), "%c", t2);
265  return string(buf);
266 }

◆ getPath()

string Utils::getPath ( const string &  pathFilename)

Returns the path w. '\' of path-filename string.

Definition at line 392 of file Utils.cpp.

393 {
394  size_t i1 = pathFilename.rfind('\\', pathFilename.length());
395  size_t i2 = pathFilename.rfind('/', pathFilename.length());
396  if ((i1 != string::npos && i2 == string::npos) ||
397  (i1 != string::npos && i1 > i2))
398  {
399  return (pathFilename.substr(0, i1 + 1));
400  }
401 
402  if ((i2 != string::npos && i1 == string::npos) ||
403  (i2 != string::npos && i2 > i1))
404  {
405  return (pathFilename.substr(0, i2 + 1));
406  }
407  return pathFilename;
408 }

◆ getStringLines()

vector< string > Utils::getStringLines ( const string &  multiLineString)

Returns a vector of string one per line of a multiline string.

Definition at line 195 of file Utils.cpp.

196 {
197  std::stringstream stream(multiLineString);
198  std::vector<std::string> res;
199  while (1)
200  {
201  std::string line;
202  std::getline(stream, line);
203  line = Utils::trimString(line, "\r");
204  res.push_back(line);
205  if (!stream.good())
206  break;
207  }
208  return res;
209 }
string trimString(const string &s, const string &drop)
Trims a string at both end.
Definition: Utils.cpp:128

◆ initFileLog() [1/2]

void Utils::initFileLog ( const std::string &  logDir,
bool  forceFlush 
)

Instantiates FileLog instance.

◆ initFileLog() [2/2]

void Utils::initFileLog ( const string &  logDir,
bool  forceFlush 
)

Definition at line 1097 of file Utils.cpp.

1098 {
1099  fileLog = std::make_unique<FileLog>(logDir, forceFlush);
1100 }
static std::unique_ptr< FileLog > fileLog
Definition: Utils.h:198

◆ isPowerOf2()

bool Utils::isPowerOf2 ( unsigned int  a)
inline

Returns true if a number is of power of 2.

Definition at line 259 of file Utils.h.

260 {
261  return a == 1 || (a & (a - 1)) == 0;
262 }

◆ lcm()

int Utils::lcm ( int  a,
int  b 
)

Definition at line 1215 of file Utils.cpp.

1216 {
1217  return (a * b) / Utils::gcd(a, b);
1218 }

◆ lerp()

template<class T >
T Utils::lerp ( x,
a,
b 
)
inline

Definition at line 255 of file Utils.h.

255 {return (a + x * (b - a));}

◆ log()

void Utils::log ( const char *  tag,
const char *  format,
  ... 
)

logs a formatted string platform independently

Definition at line 1103 of file Utils.cpp.

1104 {
1105  char log[4096];
1106 
1107  va_list argptr;
1108  va_start(argptr, format);
1109  vsnprintf(log, sizeof(log), format, argptr);
1110  va_end(argptr);
1111 
1112  char msg[4096];
1113  strcpy(msg, tag);
1114  strcat(msg, ": ");
1115  strcat(msg, log);
1116  strcat(msg, "\n");
1117 
1118  if (fileLog)
1119  fileLog->post(msg);
1120 
1121  if (customLog)
1122  customLog->post(msg);
1123 
1125  return;
1126 
1127 #if defined(ANDROID) || defined(ANDROID_NDK)
1128  __android_log_print(ANDROID_LOG_INFO, tag, msg);
1129 #else
1130  std::cout << msg << std::flush;
1131 #endif
1132 }
std::unique_ptr< CustomLog > customLog
custom log instance, e.g. log to a ui log window
Definition: Utils.cpp:82
bool onlyErrorLogs
if this flag is set to true all calls to log get ignored
Definition: Utils.cpp:84

◆ loopFileSystemRec()

void Utils::loopFileSystemRec ( const string &  path,
function< void(string path, string baseName, int depth)>  processFile,
function< void(string path, string baseName, int depth)>  processDir,
const int  depth 
)

process all files and folders recursively naturally sorted

Definition at line 1016 of file Utils.cpp.

1020 {
1021  // be sure that the folder slashes are correct
1022  string folder = unifySlashes(path);
1023 
1024  if (dirExists(folder))
1025  {
1026  vector<string> unsortedNames = getAllNamesInDir(folder);
1027 
1028  processDir(getDirName(trimRightString(folder, "/")),
1029  getFileName(trimRightString(folder, "/")),
1030  depth);
1031  sort(unsortedNames.begin(), unsortedNames.end(), Utils::compareNatural);
1032 
1033  for (const auto& fileOrFolder : unsortedNames)
1034  {
1035  if (dirExists(fileOrFolder))
1036  loopFileSystemRec(fileOrFolder, processFile, processDir, depth + 1);
1037  else
1038  processFile(folder, getFileName(fileOrFolder), depth);
1039  }
1040  }
1041  else
1042  {
1043  processFile(getDirName(trimRightString(path, "/")),
1044  getFileName(trimRightString(path, "/")),
1045  depth);
1046  }
1047 }
bool compareNatural(const string &a, const string &b)
Naturally compares two strings (used for filename sorting)
Definition: Utils.cpp:464
string getDirName(const string &pathFilename)
Strip last component from file name.
Definition: Utils.cpp:598
vector< string > getAllNamesInDir(const string &dirName, bool fullPath)
Returns a vector of sorted names (files and directories) with path in dir.
Definition: Utils.cpp:691
string trimRightString(const string &s, const string &drop)
trims a string at the right end
Definition: Utils.cpp:136

◆ makeDir()

bool Utils::makeDir ( const string &  path)

Creates a directory with given path.

Definition at line 810 of file Utils.cpp.

811 {
812 #if defined(USE_STD_FILESYSTEM)
813  return fs::create_directories(path);
814 #else
815 # if defined(_WIN32)
816  return _mkdir(path.c_str());
817 # else
818  int failed = mkdir(path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
819  bool result = !failed;
820  return result;
821 # endif
822 #endif
823 }

◆ makeDirRecurse() [1/2]

bool Utils::makeDirRecurse ( std::string  path)

Definition at line 826 of file Utils.cpp.

827 {
828  std::string delimiter = "/";
829 
830  size_t pos = 0;
831  std::string token;
832 
833  std::string createdPath;
834 
835  while ((pos = path.find(delimiter)) != std::string::npos)
836  {
837  createdPath += path.substr(0, pos) + "/";
838 
839  if (!dirExists(createdPath))
840  {
841  if (!makeDir(createdPath))
842  {
843  return false;
844  }
845  }
846 
847  path.erase(0, pos + delimiter.length());
848  }
849 
850  return true;
851 }

◆ makeDirRecurse() [2/2]

bool Utils::makeDirRecurse ( string  path)

Creates a directory with given path recursively.

◆ maxThreads()

unsigned int Utils::maxThreads ( )

Returns in release config the max. NO. of threads otherwise 1.

Definition at line 1191 of file Utils.cpp.

1192 {
1193 #if defined(DEBUG) || defined(_DEBUG)
1194  return 1;
1195 #else
1196  return std::max(std::thread::hardware_concurrency(), 1U);
1197 #endif
1198 }

◆ mix()

template<class T >
T Utils::mix ( mix,
a,
b 
)
inline

Definition at line 254 of file Utils.h.

254 {return (1 - mix) * a + mix * b;}
T mix(T mix, T a, T b)
Definition: Utils.h:254

◆ mod()

template<class T >
T Utils::mod ( a,
b 
)
inline

Definition at line 250 of file Utils.h.

250 {return a - b * floor(a / b);}

◆ nextPowerOf2()

unsigned Utils::nextPowerOf2 ( unsigned  num)

Returns the next power of 2 to a passed number.

Definition at line 1237 of file Utils.cpp.

1238 {
1239  unsigned nextPow2 = 1;
1240  if (num == 0) return 1;
1241 
1242  while (nextPow2 <= num)
1243  nextPow2 <<= 1;
1244  return nextPow2;
1245 }

◆ pulse()

template<class T >
T Utils::pulse ( a,
b,
x 
)
inline

Definition at line 252 of file Utils.h.

252 {return (SL_step(a, x) - step(b, x));}
T step(T edge, T x)
Definition: Utils.h:251

◆ random() [1/2]

float Utils::random ( float  min,
float  max 
)
inline

Returns a uniform distributed random float number between min and max.

Definition at line 265 of file Utils.h.

266 {
267  return ((float)rand() / (float)RAND_MAX) * (max - min) + min;
268 }

◆ random() [2/2]

int Utils::random ( int  min,
int  max 
)
inline

Returns a uniform distributed random int number between min and max.

Definition at line 271 of file Utils.h.

272 {
273  return min + (rand() % (int)(max - min + 1));
274 }

◆ readTextFileIntoString()

string Utils::readTextFileIntoString ( const char *  logTag,
const string &  pathAndFilename 
)

Reads a text file into a string and returns it.

Definition at line 212 of file Utils.cpp.

213 {
214  fstream shaderFile(pathAndFilename.c_str(), std::ios::in);
215 
216  if (!shaderFile.is_open())
217  {
218  log(logTag,
219  "File open failed in readTextFileIntoString: %s",
220  pathAndFilename.c_str());
221  exit(1);
222  }
223 
224  std::stringstream buffer;
225  buffer << shaderFile.rdbuf();
226  return buffer.str();
227 }

◆ removeDir()

void Utils::removeDir ( const string &  path)

RemoveDir deletes a directory with given path.

Definition at line 854 of file Utils.cpp.

855 {
856 
857 #if defined(USE_STD_FILESYSTEM)
858  fs::remove_all(path);
859 #else
860 # if defined(_WIN32)
861  int ret = _rmdir(path.c_str());
862  if (ret != 0)
863  {
864  errno_t err;
865  _get_errno(&err);
866  log("Could not remove directory: %s\nErrno: %s\n", path.c_str(), strerror(errno));
867  }
868 # else
869  rmdir(path.c_str());
870 # endif
871 #endif
872 }

◆ removeFile()

void Utils::removeFile ( const string &  path)

RemoveFile deletes a file with given path.

Definition at line 875 of file Utils.cpp.

876 {
877  if (fileExists(path))
878  {
879 #if defined(USE_STD_FILESYSTEM)
880  fs::remove(path);
881 #else
882 # if defined(_WIN32)
883  DeleteFileA(path.c_str());
884 # else
885  unlink(path.c_str());
886 # endif
887 
888 #endif
889  }
890  else
891  log("Could not remove file : %s\nErrno: %s\n",
892  path.c_str(),
893  "file does not exist");
894 }

◆ replaceNonFilenameChars()

string Utils::replaceNonFilenameChars ( string  src,
const char  replaceChar 
)

replaces non-filename characters: /|?%*:"<>'

Definition at line 244 of file Utils.cpp.

245 {
246  std::replace(src.begin(), src.end(), '/', replaceChar);
247  std::replace(src.begin(), src.end(), '\\', replaceChar);
248  std::replace(src.begin(), src.end(), '|', replaceChar);
249  std::replace(src.begin(), src.end(), '?', replaceChar);
250  std::replace(src.begin(), src.end(), '%', replaceChar);
251  std::replace(src.begin(), src.end(), '*', replaceChar);
252  std::replace(src.begin(), src.end(), ':', replaceChar);
253  std::replace(src.begin(), src.end(), '"', replaceChar);
254  return src;
255 }

◆ replaceString()

void Utils::replaceString ( string &  source,
const string &  from,
const string &  to 
)

Replaces in the source string the from string by the to string.

Definition at line 170 of file Utils.cpp.

173 {
174  // Code from: http://stackoverflow.com/questions/2896600/
175  // how-to-replace-all-occurrences-of-a-character-in-string
176  string newString;
177  newString.reserve(source.length()); // avoids a few memory allocations
178 
179  string::size_type lastPos = 0;
180  string::size_type findPos = 0;
181 
182  while (string::npos != (findPos = source.find(from, lastPos)))
183  {
184  newString.append(source, lastPos, findPos - lastPos);
185  newString += to;
186  lastPos = findPos + from.length();
187  }
188 
189  // Care for the rest after last occurrence
190  newString += source.substr(lastPos);
191  source.swap(newString);
192 }

◆ sign()

template<class T >
T Utils::sign ( a)
inline

Definition at line 245 of file Utils.h.

245 {return (T)((a > 0) ? 1 : (a < 0) ? -1 : 0);}

◆ splitString()

void Utils::splitString ( const string &  s,
char  delimiter,
vector< string > &  splits 
)

Splits an input string at a delimiter character into a string vector.

Definition at line 152 of file Utils.cpp.

155 {
156  string::size_type i = 0;
157  string::size_type j = s.find(delimiter);
158 
159  while (j != string::npos)
160  {
161  splits.push_back(s.substr(i, j - i));
162  i = ++j;
163  j = s.find(delimiter, j);
164  if (j == string::npos)
165  splits.push_back(s.substr(i, s.length()));
166  }
167 }
The SLScene class represents the top level instance holding the scene structure.
Definition: SLScene.h:47

◆ startsWithString()

bool Utils::startsWithString ( const string &  container,
const string &  startStr 
)

Return true if the container string starts with the startStr.

Definition at line 351 of file Utils.cpp.

352 {
353  return container.find(startStr) == 0;
354 }

◆ step()

template<class T >
T Utils::step ( edge,
x 
)
inline

Definition at line 251 of file Utils.h.

251 {return (T)(x >= edge);}

◆ toLowerString()

string Utils::toLowerString ( string  s)

Returns a string in lower case.

Definition at line 112 of file Utils.cpp.

113 {
114  string cpy(std::move(s));
115  transform(cpy.begin(), cpy.end(), cpy.begin(), ::tolower);
116  return cpy;
117 }

◆ toString() [1/2]

string Utils::toString ( double  d,
int  roundedDecimals 
)

Returns a string from a double with max. one trailing zero.

Definition at line 102 of file Utils.cpp.

103 {
104  stringstream ss;
105  ss << std::fixed << std::setprecision(roundedDecimals) << d;
106  string num = ss.str();
107  if (num == "-0.0") num = "0.0";
108  return num;
109 }

◆ toString() [2/2]

string Utils::toString ( float  f,
int  roundedDecimals 
)

Returns a string from a float with max. one trailing zero.

Definition at line 92 of file Utils.cpp.

93 {
94  stringstream ss;
95  ss << std::fixed << std::setprecision(roundedDecimals) << f;
96  string num = ss.str();
97  if (num == "-0.0") num = "0.0";
98  return num;
99 }

◆ toUpperString()

string Utils::toUpperString ( string  s)

Returns a string in upper case.

Definition at line 120 of file Utils.cpp.

121 {
122  string cpy(std::move(s));
123  transform(cpy.begin(), cpy.end(), cpy.begin(), ::toupper);
124  return cpy;
125 }

◆ trimLeftString()

string Utils::trimLeftString ( const string &  s,
const string &  drop 
)

trims a string at the left end

Definition at line 144 of file Utils.cpp.

145 {
146  string r = s;
147  r = r.erase(r.find_first_not_of(drop) + 1);
148  return r;
149 }
SLScene * s
Definition: SLScene.h:31

◆ trimRightString()

string Utils::trimRightString ( const string &  s,
const string &  drop 
)

trims a string at the right end

Definition at line 136 of file Utils.cpp.

137 {
138  string r = s;
139  r = r.erase(r.find_last_not_of(drop) + 1);
140  return r;
141 }

◆ trimString()

string Utils::trimString ( const string &  s,
const string &  drop 
)

Trims a string at both end.

Definition at line 128 of file Utils.cpp.

129 {
130  string r = s;
131  r = r.erase(r.find_last_not_of(drop) + 1);
132  return r.erase(0, r.find_first_not_of(drop));
133 }

◆ unifySlashes()

string Utils::unifySlashes ( const string &  inputDir,
bool  withTrailingSlash 
)

Returns the inputDir string with unified forward slashes, e.g.: "dirA/dirB/".

Definition at line 368 of file Utils.cpp.

369 {
370  string copy = inputDir;
371  string curr;
372  string delimiter = "\\";
373  size_t pos = 0;
374  string token;
375  while ((pos = copy.find(delimiter)) != string::npos)
376  {
377  token = copy.substr(0, pos);
378  copy.erase(0, pos + delimiter.length());
379  curr.append(token);
380  curr.append("/");
381  }
382 
383  curr.append(copy);
384 
385  if (withTrailingSlash && !curr.empty() && curr.back() != '/')
386  curr.append("/");
387 
388  return curr;
389 }

◆ warnMsg()

void Utils::warnMsg ( const char *  tag,
const char *  msg,
const int  line,
const char *  file 
)

Platform independent warn message output.

Definition at line 1145 of file Utils.cpp.

1149 {
1150 #if defined(ANDROID) || defined(ANDROID_NDK)
1151  __android_log_print(ANDROID_LOG_WARN,
1152  tag,
1153  "Warning: %s at line %d in %s\n",
1154  msg,
1155  line,
1156  file);
1157 #else
1158  std::cout << "--------------------------------\n"
1159  << "Warning:\n"
1160  << "Tag: " << tag << '\n'
1161  << "Location: " << file << ":" << line << '\n'
1162  << "Message: " << msg << '\n'
1163  << "--------------------------------" << std::endl;
1164 #endif
1165 }

◆ writeStringIntoTextFile()

void Utils::writeStringIntoTextFile ( const char *  logTag,
const string &  stringToWrite,
const string &  pathAndFilename 
)

Writes a string into a text file.

Definition at line 230 of file Utils.cpp.

233 {
234  std::ofstream file(pathAndFilename);
235  file << stringToWrite;
236  if (file.bad())
237  log(logTag,
238  "Writing file failed in writeStringIntoTextFile: %s",
239  pathAndFilename.c_str());
240  file.close();
241 }

Variable Documentation

◆ customLog

std::unique_ptr< CustomLog > Utils::customLog

custom log instance, e.g. log to a ui log window

Definition at line 82 of file Utils.cpp.

◆ DEG2RAD

const float Utils::DEG2RAD = PI / 180.0f
static

Definition at line 239 of file Utils.h.

◆ fileLog

std::unique_ptr<FileLog> Utils::fileLog
static

FileLog Instance for logging to logfile. If it is instantiated the logging methods will also output into this file. Instantiate it with initFileLog function.

Definition at line 198 of file Utils.h.

◆ HALFPI

const float Utils::HALFPI = PI * 0.5f
static

Definition at line 242 of file Utils.h.

◆ ONEOVERPI

const float Utils::ONEOVERPI = 1.0f / PI
static

Definition at line 241 of file Utils.h.

◆ onlyErrorLogs

bool Utils::onlyErrorLogs = false

if this flag is set to true all calls to log get ignored

Definition at line 84 of file Utils.cpp.

◆ PI

const float Utils::PI = 3.14159265358979f
static

Definition at line 237 of file Utils.h.

◆ RAD2DEG

const float Utils::RAD2DEG = 180.0f / PI
static

Definition at line 238 of file Utils.h.

◆ TWOPI

const float Utils::TWOPI = 2.0f * PI
static

Definition at line 240 of file Utils.h.