Cleanup a couple of classes

This commit is contained in:
Tomas Jakobsson 2017-11-22 22:29:52 +01:00
parent ca046f75f1
commit cfaa7c19d4
13 changed files with 172 additions and 179 deletions

View file

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

View file

@ -14,53 +14,53 @@ namespace Utils
{
namespace FileSystem
{
bool createDirectory(const std::string& path)
bool createDirectory(const std::string& _path)
{
// don't create if it already exists
if(exists(path))
if(exists(_path))
return true;
// convert '\\' to '/'
fixSeparators(path);
makeGeneric(_path);
// try to create directory
if(mkdir(path.c_str(), 0755) == 0)
if(mkdir(_path.c_str(), 0755) == 0)
return true;
// 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
if(parent != path)
if(parent != _path)
createDirectory(parent);
// 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
void fixSeparators(const std::string& path)
void makeGeneric(const std::string& _path)
{
char* p = nullptr;
// convert '\\' to '/'
for(p = (char*)path.c_str() + 1; *p; ++p)
for(p = (char*)_path.c_str() + 1; *p; ++p)
{
if(*p == '\\')
*p = '/';
}
} // fixSeparators
} // makeGeneric
std::string escapePath(const std::string& path)
std::string escapePath(const std::string& _path)
{
#ifdef WIN32
// windows escapes stuff by just putting everything in quotes
return '"' + path + '"';
return '"' + _path + '"';
#else // WIN32
// 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* invalidChar = invalidChars;
@ -83,14 +83,14 @@ namespace Utils
} // escapePath
std::string getParent(const std::string& path)
std::string getParent(const std::string& _path)
{
// convert '\\' to '/'
fixSeparators(path);
makeGeneric(_path);
// make a copy of the path
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
while(len > 1)
@ -103,18 +103,18 @@ namespace Utils
}
// no parent found
return path;
return _path;
} // getParent
std::string getFileName(const std::string& path)
std::string getFileName(const std::string& _path)
{
// convert '\\' to '/'
fixSeparators(path);
makeGeneric(_path);
// make a copy of the path
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
while(len > 1)
@ -125,13 +125,13 @@ namespace Utils
}
// no '/' found, entire path is a filename
return path;
return _path;
} // 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
if(fileName == ".")
@ -156,10 +156,10 @@ namespace Utils
} // getStem
bool exists(const std::string& path)
bool exists(const std::string& _path)
{
struct stat info;
return (stat(path.c_str(), &info) == 0);
return (stat(_path.c_str(), &info) == 0);
} // exists

View file

@ -1,6 +1,6 @@
#pragma once
#ifndef ES_CORE_FILE_SYSTEM_UTIL_H
#define ES_CORE_FILE_SYSTEM_UTIL_H
#ifndef ES_CORE_UTILS_FILE_SYSTEM_UTIL_H
#define ES_CORE_UTILS_FILE_SYSTEM_UTIL_H
#include <string>
@ -8,16 +8,16 @@ namespace Utils
{
namespace FileSystem
{
bool createDirectory(const std::string& path);
void fixSeparators(const std::string& path);
std::string escapePath(const std::string& path);
std::string getParent(const std::string& path);
std::string getFileName(const std::string& path);
std::string getStem(const std::string& path);
bool exists(const std::string& path);
bool createDirectory(const std::string& _path);
void makeGeneric (const std::string& _path);
std::string escapePath (const std::string& _path);
std::string getParent (const std::string& _path);
std::string getFileName (const std::string& _path);
std::string getStem (const std::string& _path);
bool exists (const std::string& _path);
} // FileSystem::
} // Utils::FileSystem::
} // 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
void trim(std::string& _string)
std::string trim(const std::string& _path)
{
if(_string.size())
{
size_t cursorB = 0;
size_t cursorE = _string.size();
const size_t pathBegin = _path.find_first_not_of(" \t");
const size_t pathEnd = _path.find_last_not_of(" \t");
while((cursorB < _string.size()) && _string[cursorB] == ' ')
++cursorB;
if(pathBegin == std::string::npos)
return "";
while((cursorE > 0) && _string[cursorE - 1] == ' ')
--cursorE;
_string.erase(_string.begin() + cursorE, _string.end());
_string.erase(_string.begin(), _string.begin() + cursorB);
}
return _path.substr(pathBegin, pathEnd - pathBegin + 1);
} // trim

View file

@ -1,6 +1,6 @@
#pragma once
#ifndef ES_CORE_STRING_UTIL_H
#define ES_CORE_STRING_UTIL_H
#ifndef ES_CORE_UTILS_STRING_UTIL_H
#define ES_CORE_UTILS_STRING_UTIL_H
#include <string>
@ -13,10 +13,10 @@ namespace Utils
size_t nextCursor (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);
void trim(std::string& _string);
std::string trim (const std::string& _path);
} // String::
} // Utils::String::
} // Utils::
#endif // ES_CORE_STRING_UTIL_H
#endif // ES_CORE_UTILS_STRING_UTIL_H