Mercurial > minori
changeset 60:d417e9381ca5
filesystem: WIP class-ification of paths
author | Paper <mrpapersonic@gmail.com> |
---|---|
date | Fri, 29 Sep 2023 13:52:50 -0400 |
parents | 327e9a5c72f1 |
children | 327568ad9be9 |
files | src/core/filesystem.cpp |
diffstat | 1 files changed, 39 insertions(+), 15 deletions(-) [+] |
line wrap: on
line diff
--- a/src/core/filesystem.cpp Thu Sep 28 13:45:45 2023 -0400 +++ b/src/core/filesystem.cpp Fri Sep 29 13:52:50 2023 -0400 @@ -22,15 +22,34 @@ namespace Filesystem { -bool CreateDirectories(const std::string& path) { +/* FIXME: This is a very C-like way of doing this. + Make a path class. */ +class Path { + public: + Path(); + Path(std::string path); + Path(const Path& path); + bool CreateDirectories() const; + bool Exists() const; + std::string Basename(); + std::string Stem(); + std::string Extension(); + Path GetParent(); + void SetPath(); + + private: + std::string _path; +} + +bool Path::CreateDirectories() const { std::string temp = ""; size_t start; size_t end = 0; - temp.append(path.substr(0, path.find_first_not_of(DELIM, end))); + temp.append(_path.substr(0, _path.find_first_not_of(DELIM, end))); - while ((start = path.find_first_not_of(DELIM, end)) != std::string::npos) { - end = path.find(DELIM, start); - temp.append(path.substr(start, end - start)); + while ((start = _path.find_first_not_of(DELIM, end)) != std::string::npos) { + end = _path.find(DELIM, start); + temp.append(_path.substr(start, end - start)); #ifdef WIN32 if (!CreateDirectoryA(temp.c_str(), NULL) && GetLastError() == ERROR_PATH_NOT_FOUND) /* ERROR_PATH_NOT_FOUND should NOT happen here */ @@ -45,26 +64,31 @@ return true; } -bool Exists(const std::string& path) { +bool Path::Exists() const { #if WIN32 - return GetFileAttributes(path.c_str()) != INVALID_FILE_ATTRIBUTES; + return GetFileAttributes(_path.c_str()) != INVALID_FILE_ATTRIBUTES; #else struct stat st; - return stat(path.c_str(), &st) == 0; + return stat(_path.c_str(), &st) == 0; #endif } -std::string Basename(const std::string& path) { - return path.substr(0, path.find_last_of(DELIM)); +std::string Path::Basename() const { + return _path.substr(0, path.find_last_of(DELIM)); } -std::string Stem(const std::string& path) { - std::string basename = Basename(path); - return basename.substr(0, path.find_last_of(".")); +std::string Path::Stem() const { + std::string basename = Basename(); + return basename.substr(0, basename.find_last_of(".")); } -std::string GetParentDirectory(const std::string& path) { - return path.substr(0, path.find_last_of(DELIM)); +std::string Path::Extension() const { + std::string basename = Basename(_path); + return basename.substr(basename.find_last_of("."), basename.end()); +} + +std::string Path::GetParent() const { + return _path.substr(0, _path.find_last_of(DELIM)); } std::string GetDotPath(void) {