Merge pull request #318 from tomaz82/clean

Cleanup a couple of classes
This commit is contained in:
Jools Wills 2017-12-01 17:39:54 +00:00 committed by GitHub
commit 8cfc926b26
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 172 additions and 179 deletions

View file

@ -3,8 +3,8 @@
#define ES_CORE_MATH_MISC_H #define ES_CORE_MATH_MISC_H
#define ES_PI (3.1415926535897932384626433832795028841971693993751058209749445923) #define ES_PI (3.1415926535897932384626433832795028841971693993751058209749445923)
#define ES_RAD_TO_DEG(x) ((x) * (180.0 / ES_PI)) #define ES_RAD_TO_DEG(_x) ((_x) * (180.0 / ES_PI))
#define ES_DEG_TO_RAD(x) ((x) * (ES_PI / 180.0)) #define ES_DEG_TO_RAD(_x) ((_x) * (ES_PI / 180.0))
namespace Math namespace Math
{ {

View file

@ -14,53 +14,53 @@ namespace Utils
{ {
namespace FileSystem namespace FileSystem
{ {
bool createDirectory(const std::string& path) bool createDirectory(const std::string& _path)
{ {
// don't create if it already exists // don't create if it already exists
if(exists(path)) if(exists(_path))
return true; return true;
// convert '\\' to '/' // convert '\\' to '/'
fixSeparators(path); makeGeneric(_path);
// try to create directory // try to create directory
if(mkdir(path.c_str(), 0755) == 0) if(mkdir(_path.c_str(), 0755) == 0)
return true; return true;
// failed to create directory, try to create the parent // failed to create directory, try to create the parent
std::string parent = getParent(path); std::string parent = getParent(_path);
// only try to create parent if it's not identical to path // only try to create parent if it's not identical to path
if(parent != path) if(parent != _path)
createDirectory(parent); createDirectory(parent);
// try to create directory again now that the parent should exist // try to create directory again now that the parent should exist
return (mkdir(path.c_str(), 0755) == 0); return (mkdir(_path.c_str(), 0755) == 0);
} // createDirectory } // createDirectory
void fixSeparators(const std::string& path) void makeGeneric(const std::string& _path)
{ {
char* p = nullptr; char* p = nullptr;
// convert '\\' to '/' // convert '\\' to '/'
for(p = (char*)path.c_str() + 1; *p; ++p) for(p = (char*)_path.c_str() + 1; *p; ++p)
{ {
if(*p == '\\') if(*p == '\\')
*p = '/'; *p = '/';
} }
} // fixSeparators } // makeGeneric
std::string escapePath(const std::string& path) std::string escapePath(const std::string& _path)
{ {
#ifdef WIN32 #ifdef WIN32
// windows escapes stuff by just putting everything in quotes // windows escapes stuff by just putting everything in quotes
return '"' + path + '"'; return '"' + _path + '"';
#else // WIN32 #else // WIN32
// insert a backslash before most characters that would mess up a bash path // insert a backslash before most characters that would mess up a bash path
std::string escapedPath = path; std::string escapedPath = _path;
const char* invalidChars = "\\ '\"!$^&*(){}[]?;<>"; const char* invalidChars = "\\ '\"!$^&*(){}[]?;<>";
const char* invalidChar = invalidChars; const char* invalidChar = invalidChars;
@ -83,14 +83,14 @@ namespace Utils
} // escapePath } // escapePath
std::string getParent(const std::string& path) std::string getParent(const std::string& _path)
{ {
// convert '\\' to '/' // convert '\\' to '/'
fixSeparators(path); makeGeneric(_path);
// make a copy of the path // make a copy of the path
char temp[512]; char temp[512];
size_t len = snprintf(temp, sizeof(temp), "%s", path.c_str()); size_t len = snprintf(temp, sizeof(temp), "%s", _path.c_str());
// find last '/' and end the new path // find last '/' and end the new path
while(len > 1) while(len > 1)
@ -103,18 +103,18 @@ namespace Utils
} }
// no parent found // no parent found
return path; return _path;
} // getParent } // getParent
std::string getFileName(const std::string& path) std::string getFileName(const std::string& _path)
{ {
// convert '\\' to '/' // convert '\\' to '/'
fixSeparators(path); makeGeneric(_path);
// make a copy of the path // make a copy of the path
char temp[512]; char temp[512];
size_t len = snprintf(temp, sizeof(temp), "%s", path.c_str()); size_t len = snprintf(temp, sizeof(temp), "%s", _path.c_str());
// find last '/' and return the filename // find last '/' and return the filename
while(len > 1) while(len > 1)
@ -125,13 +125,13 @@ namespace Utils
} }
// no '/' found, entire path is a filename // no '/' found, entire path is a filename
return path; return _path;
} // getFileName } // getFileName
std::string getStem(const std::string& path) std::string getStem(const std::string& _path)
{ {
std::string fileName = getFileName(path); std::string fileName = getFileName(_path);
// empty fileName // empty fileName
if(fileName == ".") if(fileName == ".")
@ -156,10 +156,10 @@ namespace Utils
} // getStem } // getStem
bool exists(const std::string& path) bool exists(const std::string& _path)
{ {
struct stat info; struct stat info;
return (stat(path.c_str(), &info) == 0); return (stat(_path.c_str(), &info) == 0);
} // exists } // exists

View file

@ -1,6 +1,6 @@
#pragma once #pragma once
#ifndef ES_CORE_FILE_SYSTEM_UTIL_H #ifndef ES_CORE_UTILS_FILE_SYSTEM_UTIL_H
#define ES_CORE_FILE_SYSTEM_UTIL_H #define ES_CORE_UTILS_FILE_SYSTEM_UTIL_H
#include <string> #include <string>
@ -8,16 +8,16 @@ namespace Utils
{ {
namespace FileSystem namespace FileSystem
{ {
bool createDirectory(const std::string& path); bool createDirectory(const std::string& _path);
void fixSeparators(const std::string& path); void makeGeneric (const std::string& _path);
std::string escapePath(const std::string& path); std::string escapePath (const std::string& _path);
std::string getParent(const std::string& path); std::string getParent (const std::string& _path);
std::string getFileName(const std::string& path); std::string getFileName (const std::string& _path);
std::string getStem(const std::string& path); std::string getStem (const std::string& _path);
bool exists(const std::string& path); bool exists (const std::string& _path);
} // FileSystem:: } // Utils::FileSystem::
} // Utils:: } // Utils::
#endif // ES_CORE_FILE_SYSTEM_UTIL_H #endif // ES_CORE_UTILS_FILE_SYSTEM_UTIL_H

View file

@ -132,22 +132,15 @@ namespace Utils
} // moveCursor } // moveCursor
void trim(std::string& _string) std::string trim(const std::string& _path)
{ {
if(_string.size()) const size_t pathBegin = _path.find_first_not_of(" \t");
{ const size_t pathEnd = _path.find_last_not_of(" \t");
size_t cursorB = 0;
size_t cursorE = _string.size();
while((cursorB < _string.size()) && _string[cursorB] == ' ') if(pathBegin == std::string::npos)
++cursorB; return "";
while((cursorE > 0) && _string[cursorE - 1] == ' ') return _path.substr(pathBegin, pathEnd - pathBegin + 1);
--cursorE;
_string.erase(_string.begin() + cursorE, _string.end());
_string.erase(_string.begin(), _string.begin() + cursorB);
}
} // trim } // trim

View file

@ -1,6 +1,6 @@
#pragma once #pragma once
#ifndef ES_CORE_STRING_UTIL_H #ifndef ES_CORE_UTILS_STRING_UTIL_H
#define ES_CORE_STRING_UTIL_H #define ES_CORE_UTILS_STRING_UTIL_H
#include <string> #include <string>
@ -13,10 +13,10 @@ namespace Utils
size_t nextCursor (const std::string& _string, const size_t _cursor); size_t nextCursor (const std::string& _string, const size_t _cursor);
size_t prevCursor (const std::string& _string, const size_t _cursor); size_t prevCursor (const std::string& _string, const size_t _cursor);
size_t moveCursor (const std::string& _string, const size_t _cursor, const int _amount); size_t moveCursor (const std::string& _string, const size_t _cursor, const int _amount);
void trim(std::string& _string); std::string trim (const std::string& _path);
} // String:: } // Utils::String::
} // Utils:: } // Utils::
#endif // ES_CORE_STRING_UTIL_H #endif // ES_CORE_UTILS_STRING_UTIL_H