Mercurial > minori
annotate src/core/filesystem.cpp @ 45:4b05bc7668eb
filesystem: split config path into dotpath and config, add anime db path
EVENTUALLY I'll get around to saving the anime db and list...
author | Paper <mrpapersonic@gmail.com> |
---|---|
date | Fri, 22 Sep 2023 15:21:55 -0400 |
parents | 619cbd6e69f9 |
children | 327e9a5c72f1 |
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 | |
25 bool CreateDirectories(std::string path) { | |
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 | |
48 bool Exists(std::string path) { | |
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 | |
57 std::string GetParentDirectory(std::string path) { | |
58 size_t pos = 0; | |
59 pos = path.find_last_of(DELIM); | |
60 return path.substr(0, pos); | |
61 } | |
62 | |
45
4b05bc7668eb
filesystem: split config path into dotpath and config, add anime db path
Paper <mrpapersonic@gmail.com>
parents:
44
diff
changeset
|
63 std::string GetDotPath(void) { |
11 | 64 std::string ret = ""; |
9 | 65 #ifdef WIN32 |
66 char buf[PATH_MAX + 1]; | |
11 | 67 if (SHGetFolderPathAndSubDir(NULL, CSIDL_APPDATA | CSIDL_FLAG_CREATE, NULL, 0, CONFIG_DIR, buf) == S_OK) { |
68 ret += buf; | |
69 } | |
2 | 70 #elif defined(MACOSX) |
11 | 71 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
|
72 ret += DELIM CONFIG_DIR; |
2 | 73 #else // just assume POSIX |
7 | 74 if (getenv("HOME") != NULL) |
11 | 75 ret += getenv("HOME"); |
15 | 76 # ifdef __linux__ |
7 | 77 else |
11 | 78 ret += getpwuid(getuid())->pw_dir; |
15 | 79 # endif // __linux__ |
11 | 80 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
|
81 ret += DELIM ".config" DELIM CONFIG_DIR; |
15 | 82 #endif // !WIN32 && !MACOSX |
11 | 83 return ret; |
2 | 84 } |
11 | 85 |
45
4b05bc7668eb
filesystem: split config path into dotpath and config, add anime db path
Paper <mrpapersonic@gmail.com>
parents:
44
diff
changeset
|
86 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
|
87 std::string ret = ""; |
4b05bc7668eb
filesystem: split config path into dotpath and config, add anime db path
Paper <mrpapersonic@gmail.com>
parents:
44
diff
changeset
|
88 ret += GetDotPath(); |
4b05bc7668eb
filesystem: split config path into dotpath and config, add anime db path
Paper <mrpapersonic@gmail.com>
parents:
44
diff
changeset
|
89 if (!ret.empty()) |
4b05bc7668eb
filesystem: split config path into dotpath and config, add anime db path
Paper <mrpapersonic@gmail.com>
parents:
44
diff
changeset
|
90 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
|
91 return ret; |
4b05bc7668eb
filesystem: split config path into dotpath and config, add anime db path
Paper <mrpapersonic@gmail.com>
parents:
44
diff
changeset
|
92 } |
4b05bc7668eb
filesystem: split config path into dotpath and config, add anime db path
Paper <mrpapersonic@gmail.com>
parents:
44
diff
changeset
|
93 |
4b05bc7668eb
filesystem: split config path into dotpath and config, add anime db path
Paper <mrpapersonic@gmail.com>
parents:
44
diff
changeset
|
94 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
|
95 std::string ret = ""; |
4b05bc7668eb
filesystem: split config path into dotpath and config, add anime db path
Paper <mrpapersonic@gmail.com>
parents:
44
diff
changeset
|
96 ret += GetDotPath(); |
4b05bc7668eb
filesystem: split config path into dotpath and config, add anime db path
Paper <mrpapersonic@gmail.com>
parents:
44
diff
changeset
|
97 if (!ret.empty()) |
4b05bc7668eb
filesystem: split config path into dotpath and config, add anime db path
Paper <mrpapersonic@gmail.com>
parents:
44
diff
changeset
|
98 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
|
99 return ret; |
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 |
15 | 102 } // namespace Filesystem |