10 #ifndef CPLVRLAB_UTILS_H 
   11 #define CPLVRLAB_UTILS_H 
   23 using std::stringstream;
 
   44 string toString(
float f, 
int roundedDecimals = 1);
 
   47 string toString(
double d, 
int roundedDecimals = 1);
 
   56 string trimString(
const string& 
s, 
const string& drop = 
" ");
 
   65 void splitString(
const string& 
s, 
char delimiter, vector<string>& splits);
 
   68 void replaceString(
string& source, 
const string& from, 
const string& to);
 
   75                               const string& pathAndFilename);
 
   79                              const string& stringToWrite,
 
   80                              const string& pathAndFilename);
 
  101 bool containsString(
const string& container, 
const string& search);
 
  107 bool endsWithString(
const string& container, 
const string& endStr);
 
  110 string unifySlashes(
const string& inputDir, 
bool withTrailingSlash = 
true);
 
  114                     vector<string>& vecOfStrings);
 
  124 string getPath(
const string& pathFilename);
 
  133 string getDirName(
const string& pathFilename);
 
  139 vector<string> 
getDirNamesInDir(
const string& dirName, 
bool fullPath = 
true);
 
  142 vector<string> 
getAllNamesInDir(
const string& dirName, 
bool fullPath = 
true);
 
  155 bool makeDir(
const string& path);
 
  180                        function<
void(
string path, 
string baseName, 
int depth)> processFile,
 
  181                        function<
void(
string path, 
string baseName, 
int depth)> processDir,
 
  182                        const int                                               depth = 0);
 
  186                        const string& folderpath);
 
  189 string findFile(
const string&         filename,
 
  190                 const vector<string>& pathsToCheck);
 
  207 extern std::unique_ptr<CustomLog> 
customLog;
 
  210 void log(
const char* tag, 
const char* format, ...);
 
  213 [[noreturn]] 
void exitMsg(
const char* tag,
 
  237 static const float PI        = 3.14159265358979f;
 
  245 template<
class T> 
inline T 
sign(T a){
return (T)((a > 0) ? 1 : (a < 0) ? -1 : 0);}
 
  246 template<
class T> 
inline T 
floor(T a){
return (T)((int)a - ((a < 0 && a != (
int)(a))));}
 
  247 template<
class T> 
inline T 
ceil(T a){
return (T)((int)a + ((a > 0 && a != (
int)(a))));}
 
  249 template<
class T> 
inline T 
abs(T a){
return (a >= 0) ? a : -a;}
 
  250 template<
class T> 
inline T 
mod(T a, T b){
return a - b * 
floor(a / b);}
 
  251 template<
class T> 
inline T 
step(T edge, T x){
return (T)(x >= edge);}
 
  252 template<
class T> 
inline T 
pulse(T a, T b, T x){
return (SL_step(a, x) - 
step(b, x));}
 
  253 template<
class T> 
inline T 
clamp(T a, T min, T max){
return (a < min) ? min : (a > max) ? max : a;}
 
  254 template<
class T> 
inline T 
mix(T 
mix, T a, T b){
return (1 - 
mix) * a + 
mix * b;}
 
  255 template<
class T> 
inline T 
lerp(T x, T a, T b){
return (a + x * (b - a));}
 
  261     return a == 1 || (a & (a - 1)) == 0;
 
  265 inline float random(
float min, 
float max)
 
  267     return ((
float)rand() / (
float)RAND_MAX) * (max - min) + min;
 
  273     return min + (rand() % (int)(max - min + 1));
 
  277 int gcd(
int a, 
int b);
 
  294     static std::string 
os;
 
  297     static std::string 
id;
 
  299     static std::string 
get();
 
The SLScene class represents the top level instance holding the scene structure.
 
Class for holding computer information.
 
Utils provides utilities for string & file handling, logging and math functions.
 
string findFile(const string &filename, const vector< string > &pathsToCheck)
Tries to find a filename on various paths to check.
 
vector< string > getDirNamesInDir(const string &dirName, bool fullPath)
Returns a vector directory names with path in dir.
 
string getDateTime2String()
Returns local time as string like "20190213-154611".
 
static const float DEG2RAD
 
string unifySlashes(const string &inputDir, bool withTrailingSlash)
Returns the inputDir string with unified forward slashes, e.g.: "dirA/dirB/".
 
bool fileExists(const string &pathfilename)
Returns true if a file exists.
 
static const float HALFPI
 
bool dirExists(const string &path)
Returns true if a directory exists.
 
bool makeDir(const string &path)
Creates a directory with given path.
 
bool getFileContent(const string &fileName, vector< string > &vecOfStrings)
Returns true if content of file could be put in a vector of strings.
 
unsigned nextPowerOf2(unsigned num)
Returns the next power of 2 to a passed number.
 
void removeDir(const string &path)
RemoveDir deletes a directory with given path.
 
string getHostName()
Returns the computer name.
 
bool containsString(const string &container, const string &search)
Returns true if container contains the search string.
 
void dumpFileSystemRec(const char *logtag, const string &folderPath)
Dumps all folders and files recursovely.
 
vector< string > getStringLines(const string &multiLineString)
Returns a vector of string one per line of a multiline string.
 
void errorMsg(const char *tag, const char *msg, const int line, const char *file)
Platform independent error message output.
 
string getFileNameWOExt(const string &pathFilename)
Returns the filename without extension.
 
bool compareNatural(const string &a, const string &b)
Naturally compares two strings (used for filename sorting)
 
T clamp(T a, T min, T max)
 
string trimLeftString(const string &s, const string &drop)
trims a string at the left end
 
std::unique_ptr< CustomLog > customLog
custom log instance, e.g. log to a ui log window
 
unsigned int getFileSize(const string &pathfilename)
Returns the file size in bytes.
 
string formatString(string fmt_str,...)
Returns a formatted string as sprintf.
 
string getFileName(const string &pathFilename)
Returns the filename of path-filename string.
 
string replaceNonFilenameChars(string src, const char replaceChar)
replaces non-filename characters: /|?%*:"<>'
 
void splitString(const string &s, char delimiter, vector< string > &splits)
Splits an input string at a delimiter character into a string vector.
 
string getPath(const string &pathFilename)
Returns the path w. '\' of path-filename string.
 
void warnMsg(const char *tag, const char *msg, const int line, const char *file)
Platform independent warn message output.
 
static const float ONEOVERPI
 
void exitMsg(const char *tag, const char *msg, const int line, const char *file)
Terminates the application with a message. No leak checking.
 
string toUpperString(string s)
Returns a string in upper case.
 
static const float RAD2DEG
 
static std::unique_ptr< FileLog > fileLog
 
unsigned int maxThreads()
Returns in release config the max. NO. of threads otherwise 1.
 
vector< string > getFileNamesInDir(const string &dirName, bool fullPath)
Returns a vector of sorted filesnames in dirName.
 
string getDirName(const string &pathFilename)
Strip last component from file name.
 
void removeFile(const string &path)
RemoveFile deletes a file with given path.
 
vector< string > getAllNamesInDir(const string &dirName, bool fullPath)
Returns a vector of sorted names (files and directories) with path in dir.
 
string getCurrentWorkingDir()
Returns the working directory.
 
unsigned closestPowerOf2(unsigned num)
Returns the closest power of 2 to a passed number.
 
string getLocalTimeString()
Returns local time as string like "Wed Feb 13 15:46:11 2019".
 
string trimString(const string &s, const string &drop)
Trims a string at both end.
 
string getAppsWritableDir(string appName)
Returns the writable configuration directory.
 
int gcd(int a, int b)
Greatest common divisor of two integer numbers (ggT = grösster gemeinsame Teiler)
 
string trimRightString(const string &s, const string &drop)
trims a string at the right end
 
bool makeDirRecurse(std::string path)
 
bool startsWithString(const string &container, const string &startStr)
Return true if the container string starts with the startStr.
 
float random(float min, float max)
Returns a uniform distributed random float number between min and max.
 
bool isPowerOf2(unsigned int a)
Returns true if a number is of power of 2.
 
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
 
string toString(float f, int roundedDecimals)
Returns a string from a float with max. one trailing zero.
 
bool endsWithString(const string &container, const string &endStr)
Return true if the container string ends with the endStr.
 
void replaceString(string &source, const string &from, const string &to)
Replaces in the source string the from string by the to string.
 
bool deleteFile(string &pathfilename)
Deletes a file on the filesystem.
 
string readTextFileIntoString(const char *logTag, const string &pathAndFilename)
Reads a text file into a string and returns it.
 
string getDateTime1String()
Returns local time as string like "13.02.19-15:46".
 
bool onlyErrorLogs
if this flag is set to true all calls to log get ignored
 
string toLowerString(string s)
Returns a string in lower case.
 
void log(const char *tag, const char *format,...)
logs a formatted string platform independently
 
void writeStringIntoTextFile(const char *logTag, const string &stringToWrite, const string &pathAndFilename)
Writes a string into a text file.
 
void initFileLog(const string &logDir, bool forceFlush)
 
string getFileExt(const string &filename)
Returns the file extension without dot in lower case.