SLProject 4.0.000
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...
 
class  AverageTiming
 Singleton timing class for average measurement of different timing blocks in loops. More...
 
struct  AverageTimingBlock
 concatenation of average value and timer More...
 
class  ComputerInfos
 
class  CustomLog
 Logger interface. More...
 
class  FileLog
 

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...
 
void showSpinnerMsg (string msg)
 Shows the a spinner icon message. More...
 
void hideSpinnerMsg ()
 Hides the previous spinner icon message. 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...
 
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

Function Documentation

◆ abs()

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

◆ ceil()

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

◆ clamp()

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

◆ closestPowerOf2()

unsigned Utils::closestPowerOf2 ( unsigned  num)

Returns the closest power of 2 to a passed number.

◆ 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

◆ containsString()

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

Returns true if container contains the search string.

◆ deleteFile()

bool Utils::deleteFile ( string &  pathfilename)

Deletes a file on the filesystem.

◆ dirExists()

bool Utils::dirExists ( const string &  path)

Returns true if a directory exists.

◆ dumpFileSystemRec()

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

Dumps all folders and files recursovely.

◆ endsWithString()

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

Return true if the container string ends with the endStr.

◆ errorMsg()

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

Platform independent error message output.

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

◆ fileExists()

bool Utils::fileExists ( const string &  pathfilename)

Returns true if a file exists.

◆ findFile()

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

Tries to find a filename on various paths to check.

◆ floor()

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

◆ formatString()

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

Returns a formatted string as sprintf.

◆ fract()

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

◆ gcd()

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

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

◆ getAllNamesInDir()

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

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

◆ getAppsWritableDir()

string Utils::getAppsWritableDir ( string  appName)

Returns the writable configuration directory.

◆ getCurrentWorkingDir()

string Utils::getCurrentWorkingDir ( )

Returns the working directory.

◆ getDateTime1String()

string Utils::getDateTime1String ( )

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

◆ getDateTime2String()

string Utils::getDateTime2String ( )

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

◆ getDirName()

string Utils::getDirName ( const string &  pathFilename)

Strip last component from file name.

◆ getDirNamesInDir()

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

Returns a vector directory names with path in dir.

◆ getFileContent()

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

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

◆ getFileExt()

string Utils::getFileExt ( const string &  filename)

Returns the file extension without dot in lower case.

◆ getFileName()

string Utils::getFileName ( const string &  pathFilename)

Returns the filename of path-filename string.

◆ getFileNamesInDir()

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

Returns a vector of sorted filesnames in dirName.

◆ getFileNameWOExt()

string Utils::getFileNameWOExt ( const string &  pathFilename)

Returns the filename without extension.

◆ getFileSize() [1/2]

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

Returns the file size in bytes.

◆ getFileSize() [2/2]

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

◆ getHostName()

string Utils::getHostName ( )

Returns the computer name.

◆ getLocalTimeString()

string Utils::getLocalTimeString ( )

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

◆ getPath()

string Utils::getPath ( const string &  pathFilename)

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

◆ getStringLines()

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

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

◆ hideSpinnerMsg()

void Utils::hideSpinnerMsg ( )

Hides the previous spinner icon message.

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

◆ isPowerOf2()

bool Utils::isPowerOf2 ( unsigned int  a)
inline

Returns true if a number is of power of 2.

◆ lcm()

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

◆ lerp()

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

◆ log()

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

logs a formatted string platform independently

◆ 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

◆ makeDir()

bool Utils::makeDir ( const string &  path)

Creates a directory with given path.

◆ makeDirRecurse() [1/2]

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

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

◆ mix()

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

◆ mod()

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

◆ nextPowerOf2()

unsigned Utils::nextPowerOf2 ( unsigned  num)

Returns the next power of 2 to a passed number.

◆ pulse()

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

◆ random() [1/2]

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

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

◆ random() [2/2]

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

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

◆ readTextFileIntoString()

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

Reads a text file into a string and returns it.

◆ removeDir()

void Utils::removeDir ( const string &  path)

RemoveDir deletes a directory with given path.

◆ removeFile()

void Utils::removeFile ( const string &  path)

RemoveFile deletes a file with given path.

◆ replaceNonFilenameChars()

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

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

◆ replaceString()

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

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

◆ showSpinnerMsg()

void Utils::showSpinnerMsg ( string  msg)

Shows the a spinner icon message.

◆ sign()

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

◆ splitString()

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

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

◆ startsWithString()

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

Return true if the container string starts with the startStr.

◆ step()

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

◆ toLowerString()

string Utils::toLowerString ( string  s)

Returns a string in lower case.

◆ toString() [1/2]

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

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

◆ toString() [2/2]

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

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

◆ toUpperString()

string Utils::toUpperString ( string  s)

Returns a string in upper case.

◆ trimLeftString()

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

trims a string at the left end

◆ trimRightString()

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

trims a string at the right end

◆ trimString()

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

Trims a string at both end.

◆ unifySlashes()

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

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

◆ warnMsg()

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

Platform independent warn message output.

◆ writeStringIntoTextFile()

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

Writes a string into a text file.

Variable Documentation

◆ customLog

std::unique_ptr< CustomLog > Utils::customLog

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

◆ DEG2RAD

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

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

◆ HALFPI

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

◆ ONEOVERPI

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

◆ PI

const float Utils::PI = 3.14159265358979f
static

◆ RAD2DEG

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

◆ TWOPI

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