Mercurial > minori
comparison src/core/filesystem.cpp @ 61:327568ad9be9
core/fs: finish class-ification of paths
| author | Paper <mrpapersonic@gmail.com> |
|---|---|
| date | Fri, 29 Sep 2023 15:52:31 -0400 |
| parents | d417e9381ca5 |
| children | 4c6dd5999b39 |
comparison
equal
deleted
inserted
replaced
| 60:d417e9381ca5 | 61:327568ad9be9 |
|---|---|
| 20 #include "core/config.h" | 20 #include "core/config.h" |
| 21 #include <limits.h> | 21 #include <limits.h> |
| 22 | 22 |
| 23 namespace Filesystem { | 23 namespace Filesystem { |
| 24 | 24 |
| 25 /* FIXME: This is a very C-like way of doing this. | 25 Path::Path() { _path = ""; } |
| 26 Make a path class. */ | 26 Path::Path(const std::string& path) { _path = path; } |
| 27 class Path { | 27 Path::Path(const Path& path) { _path = path.GetPath(); } |
| 28 public: | |
| 29 Path(); | |
| 30 Path(std::string path); | |
| 31 Path(const Path& path); | |
| 32 bool CreateDirectories() const; | |
| 33 bool Exists() const; | |
| 34 std::string Basename(); | |
| 35 std::string Stem(); | |
| 36 std::string Extension(); | |
| 37 Path GetParent(); | |
| 38 void SetPath(); | |
| 39 | |
| 40 private: | |
| 41 std::string _path; | |
| 42 } | |
| 43 | 28 |
| 44 bool Path::CreateDirectories() const { | 29 bool Path::CreateDirectories() const { |
| 45 std::string temp = ""; | 30 std::string temp = ""; |
| 46 size_t start; | 31 size_t start; |
| 47 size_t end = 0; | 32 size_t end = 0; |
| 72 return stat(_path.c_str(), &st) == 0; | 57 return stat(_path.c_str(), &st) == 0; |
| 73 #endif | 58 #endif |
| 74 } | 59 } |
| 75 | 60 |
| 76 std::string Path::Basename() const { | 61 std::string Path::Basename() const { |
| 77 return _path.substr(0, path.find_last_of(DELIM)); | 62 return _path.substr(0, _path.find_last_of(DELIM)); |
| 78 } | 63 } |
| 79 | 64 |
| 80 std::string Path::Stem() const { | 65 std::string Path::Stem() const { |
| 81 std::string basename = Basename(); | 66 std::string basename = Basename(); |
| 82 return basename.substr(0, basename.find_last_of(".")); | 67 return basename.substr(0, basename.find_last_of(".")); |
| 83 } | 68 } |
| 84 | 69 |
| 85 std::string Path::Extension() const { | 70 std::string Path::Extension() const { |
| 86 std::string basename = Basename(_path); | 71 std::string basename = Basename(); |
| 87 return basename.substr(basename.find_last_of("."), basename.end()); | 72 return basename.substr(basename.find_last_of("."), basename.length()); |
| 88 } | 73 } |
| 89 | 74 |
| 90 std::string Path::GetParent() const { | 75 Path Path::GetParent() const { |
| 91 return _path.substr(0, _path.find_last_of(DELIM)); | 76 return _path.substr(0, _path.find_last_of(DELIM)); |
| 92 } | 77 } |
| 93 | 78 |
| 94 std::string GetDotPath(void) { | 79 void Path::SetPath(const std::string& path) { |
| 80 _path = path; | |
| 81 } | |
| 82 | |
| 83 std::string Path::GetPath() const { | |
| 84 return _path; | |
| 85 } | |
| 86 | |
| 87 Path GetDotPath(void) { | |
| 95 std::string ret = ""; | 88 std::string ret = ""; |
| 96 #ifdef WIN32 | 89 #ifdef WIN32 |
| 97 char buf[PATH_MAX + 1]; | 90 char buf[PATH_MAX + 1]; |
| 98 if (SHGetFolderPathAndSubDir(NULL, CSIDL_APPDATA | CSIDL_FLAG_CREATE, NULL, 0, CONFIG_DIR, buf) == S_OK) { | 91 if (SHGetFolderPathAndSubDir(NULL, CSIDL_APPDATA | CSIDL_FLAG_CREATE, NULL, 0, CONFIG_DIR, buf) == S_OK) { |
| 99 ret += buf; | 92 ret += buf; |
| 112 ret += DELIM ".config" DELIM CONFIG_DIR; | 105 ret += DELIM ".config" DELIM CONFIG_DIR; |
| 113 #endif // !WIN32 && !MACOSX | 106 #endif // !WIN32 && !MACOSX |
| 114 return ret; | 107 return ret; |
| 115 } | 108 } |
| 116 | 109 |
| 117 std::string GetConfigPath(void) { | 110 Path GetConfigPath(void) { |
| 118 std::string ret = ""; | 111 std::string ret = ""; |
| 119 ret += GetDotPath(); | 112 ret += GetDotPath().GetPath(); |
| 120 if (!ret.empty()) | 113 if (!ret.empty()) |
| 121 ret += DELIM CONFIG_NAME; | 114 ret += DELIM CONFIG_NAME; |
| 122 return ret; | 115 return ret; |
| 123 } | 116 } |
| 124 | 117 |
| 125 std::string GetAnimeDBPath(void) { | 118 Path GetAnimeDBPath(void) { |
| 126 std::string ret = ""; | 119 std::string ret = ""; |
| 127 ret += GetDotPath(); | 120 ret += GetDotPath().GetPath(); |
| 128 if (!ret.empty()) | 121 if (!ret.empty()) |
| 129 ret += DELIM "anime" DELIM "db.json"; | 122 ret += DELIM "anime" DELIM "db.json"; |
| 130 return ret; | 123 return ret; |
| 131 } | 124 } |
| 132 | 125 |
