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

Implementation of various utility functions defined in Utils.h. More...

#include <Utils.h>
#include <cstddef>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <string>
#include <cstdarg>
#include <cstring>
#include <utility>
#include <vector>
#include <algorithm>
#include <thread>
#include <asio.hpp>
#include <asio/ip/tcp.hpp>
Include dependency graph for Utils.cpp:

Go to the source code of this file.

Namespaces

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

Functions

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

Variables

std::unique_ptr< CustomLog > Utils::customLog
 custom log instance, e.g. log to a ui log window More...
 
bool Utils::onlyErrorLogs = false
 if this flag is set to true all calls to log get ignored More...
 

Detailed Description

Implementation of various utility functions defined in Utils.h.

Date
May 2019
Authors
Marcus Hudritsch
Remarks
Please use clangformat to format the code. See more code style on https://github.com/cpvrlab/SLProject4/wiki/SLProject-Coding-Style

Definition in file Utils.cpp.