SLProject  4.3.020
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 1218 of file Utils.cpp.

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

◆ 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 463 of file Utils.cpp.

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

◆ containsString()

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

Returns true if container contains the search string.

Definition at line 344 of file Utils.cpp.

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

◆ deleteFile()

bool Utils::deleteFile ( string &  pathfilename)

Deletes a file on the filesystem.

Definition at line 1005 of file Utils.cpp.

1006 {
1007  if (fileExists(pathfilename))
1008  return remove(pathfilename.c_str()) != 0;
1009  return false;
1010 }
bool fileExists(const string &pathfilename)
Returns true if a file exists.
Definition: Utils.cpp:894

◆ dirExists()

bool Utils::dirExists ( const string &  path)

Returns true if a directory exists.

Definition at line 789 of file Utils.cpp.

790 {
791 #if defined(__EMSCRIPTEN__)
792  return true;
793 #elif defined(USE_STD_FILESYSTEM)
794  return fs::exists(path) && fs::is_directory(path);
795 #else
796  struct stat info
797  {
798  };
799  if (stat(path.c_str(), &info) != 0)
800  return false;
801  else if (info.st_mode & S_IFDIR)
802  return true;
803  else
804  return false;
805 #endif
806 }
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 1048 of file Utils.cpp.

1049 {
1050  const char* tab = " ";
1051 
1053  folderPath,
1054  [logtag, tab](string path, string baseName, int depth) -> void {
1055  string indent;
1056  for (int d = 0; d < depth; ++d)
1057  indent += tab;
1058  string indentFolderName = indent + baseName;
1059  Utils::log(logtag, "%s", indentFolderName.c_str());
1060  },
1061  [logtag, tab](string path, string baseName, int depth) -> void {
1062  string indent;
1063  for (int d = 0; d < depth; ++d)
1064  indent += tab;
1065  string indentFolderName = indent + "[" + baseName + "]";
1066  Utils::log(logtag, "%s", indentFolderName.c_str());
1067  });
1068 }
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:1013
void log(const char *tag, const char *format,...)
logs a formatted string platform independently
Definition: Utils.cpp:1100

◆ endsWithString()

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

Return true if the container string ends with the endStr.

Definition at line 356 of file Utils.cpp.

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

◆ errorMsg()

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

Platform independent error message output.

Definition at line 1165 of file Utils.cpp.

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

◆ 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 1132 of file Utils.cpp.

1136 {
1137  errorMsg(tag, msg, line, file);
1138  exit(-1);
1139 }
void errorMsg(const char *tag, const char *msg, const int line, const char *file)
Platform independent error message output.
Definition: Utils.cpp:1165

◆ fileExists()

bool Utils::fileExists ( const string &  pathfilename)

Returns true if a file exists.

Definition at line 894 of file Utils.cpp.

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

◆ findFile()

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

Tries to find a filename on various paths to check.

Definition at line 1074 of file Utils.cpp.

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

◆ 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  std::unique_ptr<char[]> formatted;
327  va_list ap;
328  while (true)
329  {
330  formatted.reset(new char[n]);
331  strcpy(&formatted[0], fmt_str.c_str());
332  va_start(ap, fmt_str);
333  final_n = vsnprintf(&formatted[0], (unsigned long)n, fmt_str.c_str(), ap);
334  va_end(ap);
335  if (final_n < 0 || final_n >= n)
336  n += abs(final_n - n + 1);
337  else
338  break;
339  }
340  return string(formatted.get());
341 }
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 1204 of file Utils.cpp.

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

◆ 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 690 of file Utils.cpp.

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

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

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

◆ 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 597 of file Utils.cpp.

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

◆ getDirNamesInDir()

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

Returns a vector directory names with path in dir.

Definition at line 637 of file Utils.cpp.

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

◆ 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 410 of file Utils.cpp.

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

◆ getFileExt()

string Utils::getFileExt ( const string &  filename)

Returns the file extension without dot in lower case.

Definition at line 628 of file Utils.cpp.

629 {
630  size_t i = filename.rfind('.', filename.length());
631  if (i != string::npos)
632  return toLowerString(filename.substr(i + 1, filename.length() - i));
633  return ("");
634 }
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 579 of file Utils.cpp.

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

◆ getFileNamesInDir()

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

Returns a vector of sorted filesnames in dirName.

Definition at line 736 of file Utils.cpp.

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

◆ getFileNameWOExt()

string Utils::getFileNameWOExt ( const string &  pathFilename)

Returns the filename without extension.

Definition at line 615 of file Utils.cpp.

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

◆ getFileSize() [1/2]

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

Returns the file size in bytes.

Definition at line 909 of file Utils.cpp.

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

◆ getFileSize() [2/2]

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

Definition at line 927 of file Utils.cpp.

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

◆ 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 391 of file Utils.cpp.

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

◆ 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 1094 of file Utils.cpp.

1095 {
1096  fileLog = std::make_unique<FileLog>(logDir, forceFlush);
1097 }
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 1212 of file Utils.cpp.

1213 {
1214  return (a * b) / Utils::gcd(a, b);
1215 }

◆ 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 1100 of file Utils.cpp.

1101 {
1102  char log[4096];
1103 
1104  va_list argptr;
1105  va_start(argptr, format);
1106  vsnprintf(log, sizeof(log), format, argptr);
1107  va_end(argptr);
1108 
1109  char msg[4096];
1110  strcpy(msg, tag);
1111  strcat(msg, ": ");
1112  strcat(msg, log);
1113  strcat(msg, "\n");
1114 
1115  if (fileLog)
1116  fileLog->post(msg);
1117 
1118  if (customLog)
1119  customLog->post(msg);
1120 
1122  return;
1123 
1124 #if defined(ANDROID) || defined(ANDROID_NDK)
1125  __android_log_print(ANDROID_LOG_INFO, tag, msg);
1126 #else
1127  std::cout << msg << std::flush;
1128 #endif
1129 }
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 1013 of file Utils.cpp.

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

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

◆ makeDirRecurse() [1/2]

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

Definition at line 825 of file Utils.cpp.

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

◆ 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 1188 of file Utils.cpp.

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

◆ 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 1234 of file Utils.cpp.

1235 {
1236  unsigned nextPow2 = 1;
1237  if (num == 0) return 1;
1238 
1239  while (nextPow2 <= num)
1240  nextPow2 <<= 1;
1241  return nextPow2;
1242 }

◆ 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 851 of file Utils.cpp.

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

◆ removeFile()

void Utils::removeFile ( const string &  path)

RemoveFile deletes a file with given path.

Definition at line 872 of file Utils.cpp.

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

◆ 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 350 of file Utils.cpp.

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

◆ 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 367 of file Utils.cpp.

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

◆ warnMsg()

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

Platform independent warn message output.

Definition at line 1142 of file Utils.cpp.

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

◆ 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.