Mercurial > minori
annotate src/core/filesystem.cpp @ 59:327e9a5c72f1
filesystem: add basename and stem functions
| author | Paper <mrpapersonic@gmail.com> |
|---|---|
| date | Thu, 28 Sep 2023 13:45:45 -0400 |
| parents | 4b05bc7668eb |
| children | d417e9381ca5 |
| rev | line source |
|---|---|
| 9 | 1 #ifdef WIN32 |
| 15 | 2 # include <shlobj.h> |
| 5 | 3 #elif defined(MACOSX) |
| 15 | 4 # include "sys/osx/filesystem.h" |
| 7 | 5 #elif defined(__linux__) |
| 15 | 6 # include <pwd.h> |
| 7 # include <sys/types.h> | |
| 2 | 8 #endif |
| 11 | 9 |
| 10 #ifdef WIN32 | |
| 15 | 11 # define DELIM "\\" |
| 11 | 12 #else |
| 15 | 13 # define DELIM "/" |
| 14 # include <errno.h> | |
| 15 # include <unistd.h> | |
|
18
28d8f4c0ae12
*nix: add missing header file for stat
Paper <mrpapersonic@gmail.com>
parents:
15
diff
changeset
|
16 # include <sys/stat.h> |
| 11 | 17 #endif |
| 18 | |
| 36 | 19 #include "core/filesystem.h" |
| 15 | 20 #include "core/config.h" |
| 2 | 21 #include <limits.h> |
| 22 | |
| 11 | 23 namespace Filesystem { |
| 24 | |
|
59
327e9a5c72f1
filesystem: add basename and stem functions
Paper <mrpapersonic@gmail.com>
parents:
45
diff
changeset
|
25 bool CreateDirectories(const std::string& path) { |
| 11 | 26 std::string temp = ""; |
| 27 size_t start; | |
| 28 size_t end = 0; | |
|
44
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
36
diff
changeset
|
29 temp.append(path.substr(0, path.find_first_not_of(DELIM, end))); |
| 11 | 30 |
| 15 | 31 while ((start = path.find_first_not_of(DELIM, end)) != std::string::npos) { |
| 11 | 32 end = path.find(DELIM, start); |
| 33 temp.append(path.substr(start, end - start)); | |
| 34 #ifdef WIN32 | |
| 35 if (!CreateDirectoryA(temp.c_str(), NULL) && GetLastError() == ERROR_PATH_NOT_FOUND) | |
| 36 /* ERROR_PATH_NOT_FOUND should NOT happen here */ | |
| 37 return false; | |
| 38 #else | |
|
44
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
36
diff
changeset
|
39 struct stat st; |
|
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
36
diff
changeset
|
40 if (stat(temp.c_str(), &st) == -1) |
|
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
36
diff
changeset
|
41 mkdir(temp.c_str(), 0755); |
| 11 | 42 #endif |
| 43 temp.append(DELIM); | |
| 44 } | |
| 45 return true; | |
| 46 } | |
| 47 | |
|
59
327e9a5c72f1
filesystem: add basename and stem functions
Paper <mrpapersonic@gmail.com>
parents:
45
diff
changeset
|
48 bool Exists(const std::string& path) { |
| 11 | 49 #if WIN32 |
| 50 return GetFileAttributes(path.c_str()) != INVALID_FILE_ATTRIBUTES; | |
| 51 #else | |
| 52 struct stat st; | |
| 53 return stat(path.c_str(), &st) == 0; | |
| 54 #endif | |
| 55 } | |
| 56 | |
|
59
327e9a5c72f1
filesystem: add basename and stem functions
Paper <mrpapersonic@gmail.com>
parents:
45
diff
changeset
|
57 std::string Basename(const std::string& path) { |
|
327e9a5c72f1
filesystem: add basename and stem functions
Paper <mrpapersonic@gmail.com>
parents:
45
diff
changeset
|
58 return path.substr(0, path.find_last_of(DELIM)); |
|
327e9a5c72f1
filesystem: add basename and stem functions
Paper <mrpapersonic@gmail.com>
parents:
45
diff
changeset
|
59 } |
|
327e9a5c72f1
filesystem: add basename and stem functions
Paper <mrpapersonic@gmail.com>
parents:
45
diff
changeset
|
60 |
|
327e9a5c72f1
filesystem: add basename and stem functions
Paper <mrpapersonic@gmail.com>
parents:
45
diff
changeset
|
61 std::string Stem(const std::string& path) { |
|
327e9a5c72f1
filesystem: add basename and stem functions
Paper <mrpapersonic@gmail.com>
parents:
45
diff
changeset
|
62 std::string basename = Basename(path); |
|
327e9a5c72f1
filesystem: add basename and stem functions
Paper <mrpapersonic@gmail.com>
parents:
45
diff
changeset
|
63 return basename.substr(0, path.find_last_of(".")); |
|
327e9a5c72f1
filesystem: add basename and stem functions
Paper <mrpapersonic@gmail.com>
parents:
45
diff
changeset
|
64 } |
|
327e9a5c72f1
filesystem: add basename and stem functions
Paper <mrpapersonic@gmail.com>
parents:
45
diff
changeset
|
65 |
|
327e9a5c72f1
filesystem: add basename and stem functions
Paper <mrpapersonic@gmail.com>
parents:
45
diff
changeset
|
66 std::string GetParentDirectory(const std::string& path) { |
|
327e9a5c72f1
filesystem: add basename and stem functions
Paper <mrpapersonic@gmail.com>
parents:
45
diff
changeset
|
67 return path.substr(0, path.find_last_of(DELIM)); |
| 11 | 68 } |
| 69 | |
|
45
4b05bc7668eb
filesystem: split config path into dotpath and config, add anime db path
Paper <mrpapersonic@gmail.com>
parents:
44
diff
changeset
|
70 std::string GetDotPath(void) { |
| 11 | 71 std::string ret = ""; |
| 9 | 72 #ifdef WIN32 |
| 73 char buf[PATH_MAX + 1]; | |
| 11 | 74 if (SHGetFolderPathAndSubDir(NULL, CSIDL_APPDATA | CSIDL_FLAG_CREATE, NULL, 0, CONFIG_DIR, buf) == S_OK) { |
| 75 ret += buf; | |
| 76 } | |
| 2 | 77 #elif defined(MACOSX) |
| 11 | 78 ret += osx::GetApplicationSupportDirectory(); |
|
45
4b05bc7668eb
filesystem: split config path into dotpath and config, add anime db path
Paper <mrpapersonic@gmail.com>
parents:
44
diff
changeset
|
79 ret += DELIM CONFIG_DIR; |
| 2 | 80 #else // just assume POSIX |
| 7 | 81 if (getenv("HOME") != NULL) |
| 11 | 82 ret += getenv("HOME"); |
| 15 | 83 # ifdef __linux__ |
| 7 | 84 else |
| 11 | 85 ret += getpwuid(getuid())->pw_dir; |
| 15 | 86 # endif // __linux__ |
| 11 | 87 if (!ret.empty()) |
|
45
4b05bc7668eb
filesystem: split config path into dotpath and config, add anime db path
Paper <mrpapersonic@gmail.com>
parents:
44
diff
changeset
|
88 ret += DELIM ".config" DELIM CONFIG_DIR; |
| 15 | 89 #endif // !WIN32 && !MACOSX |
| 11 | 90 return ret; |
| 2 | 91 } |
| 11 | 92 |
|
45
4b05bc7668eb
filesystem: split config path into dotpath and config, add anime db path
Paper <mrpapersonic@gmail.com>
parents:
44
diff
changeset
|
93 std::string GetConfigPath(void) { |
|
4b05bc7668eb
filesystem: split config path into dotpath and config, add anime db path
Paper <mrpapersonic@gmail.com>
parents:
44
diff
changeset
|
94 std::string ret = ""; |
|
4b05bc7668eb
filesystem: split config path into dotpath and config, add anime db path
Paper <mrpapersonic@gmail.com>
parents:
44
diff
changeset
|
95 ret += GetDotPath(); |
|
4b05bc7668eb
filesystem: split config path into dotpath and config, add anime db path
Paper <mrpapersonic@gmail.com>
parents:
44
diff
changeset
|
96 if (!ret.empty()) |
|
4b05bc7668eb
filesystem: split config path into dotpath and config, add anime db path
Paper <mrpapersonic@gmail.com>
parents:
44
diff
changeset
|
97 ret += DELIM CONFIG_NAME; |
|
4b05bc7668eb
filesystem: split config path into dotpath and config, add anime db path
Paper <mrpapersonic@gmail.com>
parents:
44
diff
changeset
|
98 return ret; |
|
4b05bc7668eb
filesystem: split config path into dotpath and config, add anime db path
Paper <mrpapersonic@gmail.com>
parents:
44
diff
changeset
|
99 } |
|
4b05bc7668eb
filesystem: split config path into dotpath and config, add anime db path
Paper <mrpapersonic@gmail.com>
parents:
44
diff
changeset
|
100 |
|
4b05bc7668eb
filesystem: split config path into dotpath and config, add anime db path
Paper <mrpapersonic@gmail.com>
parents:
44
diff
changeset
|
101 std::string GetAnimeDBPath(void) { |
|
4b05bc7668eb
filesystem: split config path into dotpath and config, add anime db path
Paper <mrpapersonic@gmail.com>
parents:
44
diff
changeset
|
102 std::string ret = ""; |
|
4b05bc7668eb
filesystem: split config path into dotpath and config, add anime db path
Paper <mrpapersonic@gmail.com>
parents:
44
diff
changeset
|
103 ret += GetDotPath(); |
|
4b05bc7668eb
filesystem: split config path into dotpath and config, add anime db path
Paper <mrpapersonic@gmail.com>
parents:
44
diff
changeset
|
104 if (!ret.empty()) |
|
4b05bc7668eb
filesystem: split config path into dotpath and config, add anime db path
Paper <mrpapersonic@gmail.com>
parents:
44
diff
changeset
|
105 ret += DELIM "anime" DELIM "db.json"; |
|
4b05bc7668eb
filesystem: split config path into dotpath and config, add anime db path
Paper <mrpapersonic@gmail.com>
parents:
44
diff
changeset
|
106 return ret; |
|
4b05bc7668eb
filesystem: split config path into dotpath and config, add anime db path
Paper <mrpapersonic@gmail.com>
parents:
44
diff
changeset
|
107 } |
|
4b05bc7668eb
filesystem: split config path into dotpath and config, add anime db path
Paper <mrpapersonic@gmail.com>
parents:
44
diff
changeset
|
108 |
| 15 | 109 } // namespace Filesystem |
