Mercurial > minori
annotate src/core/filesystem.cpp @ 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 |
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 | |
60
d417e9381ca5
filesystem: WIP class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
59
diff
changeset
|
25 /* FIXME: This is a very C-like way of doing this. |
d417e9381ca5
filesystem: WIP class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
59
diff
changeset
|
26 Make a path class. */ |
d417e9381ca5
filesystem: WIP class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
59
diff
changeset
|
27 class Path { |
d417e9381ca5
filesystem: WIP class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
59
diff
changeset
|
28 public: |
d417e9381ca5
filesystem: WIP class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
59
diff
changeset
|
29 Path(); |
d417e9381ca5
filesystem: WIP class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
59
diff
changeset
|
30 Path(std::string path); |
d417e9381ca5
filesystem: WIP class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
59
diff
changeset
|
31 Path(const Path& path); |
d417e9381ca5
filesystem: WIP class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
59
diff
changeset
|
32 bool CreateDirectories() const; |
d417e9381ca5
filesystem: WIP class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
59
diff
changeset
|
33 bool Exists() const; |
d417e9381ca5
filesystem: WIP class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
59
diff
changeset
|
34 std::string Basename(); |
d417e9381ca5
filesystem: WIP class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
59
diff
changeset
|
35 std::string Stem(); |
d417e9381ca5
filesystem: WIP class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
59
diff
changeset
|
36 std::string Extension(); |
d417e9381ca5
filesystem: WIP class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
59
diff
changeset
|
37 Path GetParent(); |
d417e9381ca5
filesystem: WIP class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
59
diff
changeset
|
38 void SetPath(); |
d417e9381ca5
filesystem: WIP class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
59
diff
changeset
|
39 |
d417e9381ca5
filesystem: WIP class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
59
diff
changeset
|
40 private: |
d417e9381ca5
filesystem: WIP class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
59
diff
changeset
|
41 std::string _path; |
d417e9381ca5
filesystem: WIP class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
59
diff
changeset
|
42 } |
d417e9381ca5
filesystem: WIP class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
59
diff
changeset
|
43 |
d417e9381ca5
filesystem: WIP class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
59
diff
changeset
|
44 bool Path::CreateDirectories() const { |
11 | 45 std::string temp = ""; |
46 size_t start; | |
47 size_t end = 0; | |
60
d417e9381ca5
filesystem: WIP class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
59
diff
changeset
|
48 temp.append(_path.substr(0, _path.find_first_not_of(DELIM, end))); |
11 | 49 |
60
d417e9381ca5
filesystem: WIP class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
59
diff
changeset
|
50 while ((start = _path.find_first_not_of(DELIM, end)) != std::string::npos) { |
d417e9381ca5
filesystem: WIP class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
59
diff
changeset
|
51 end = _path.find(DELIM, start); |
d417e9381ca5
filesystem: WIP class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
59
diff
changeset
|
52 temp.append(_path.substr(start, end - start)); |
11 | 53 #ifdef WIN32 |
54 if (!CreateDirectoryA(temp.c_str(), NULL) && GetLastError() == ERROR_PATH_NOT_FOUND) | |
55 /* ERROR_PATH_NOT_FOUND should NOT happen here */ | |
56 return false; | |
57 #else | |
44
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
36
diff
changeset
|
58 struct stat st; |
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
36
diff
changeset
|
59 if (stat(temp.c_str(), &st) == -1) |
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
36
diff
changeset
|
60 mkdir(temp.c_str(), 0755); |
11 | 61 #endif |
62 temp.append(DELIM); | |
63 } | |
64 return true; | |
65 } | |
66 | |
60
d417e9381ca5
filesystem: WIP class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
59
diff
changeset
|
67 bool Path::Exists() const { |
11 | 68 #if WIN32 |
60
d417e9381ca5
filesystem: WIP class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
59
diff
changeset
|
69 return GetFileAttributes(_path.c_str()) != INVALID_FILE_ATTRIBUTES; |
11 | 70 #else |
71 struct stat st; | |
60
d417e9381ca5
filesystem: WIP class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
59
diff
changeset
|
72 return stat(_path.c_str(), &st) == 0; |
11 | 73 #endif |
74 } | |
75 | |
60
d417e9381ca5
filesystem: WIP class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
59
diff
changeset
|
76 std::string Path::Basename() const { |
d417e9381ca5
filesystem: WIP class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
59
diff
changeset
|
77 return _path.substr(0, path.find_last_of(DELIM)); |
59
327e9a5c72f1
filesystem: add basename and stem functions
Paper <mrpapersonic@gmail.com>
parents:
45
diff
changeset
|
78 } |
327e9a5c72f1
filesystem: add basename and stem functions
Paper <mrpapersonic@gmail.com>
parents:
45
diff
changeset
|
79 |
60
d417e9381ca5
filesystem: WIP class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
59
diff
changeset
|
80 std::string Path::Stem() const { |
d417e9381ca5
filesystem: WIP class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
59
diff
changeset
|
81 std::string basename = Basename(); |
d417e9381ca5
filesystem: WIP class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
59
diff
changeset
|
82 return basename.substr(0, basename.find_last_of(".")); |
59
327e9a5c72f1
filesystem: add basename and stem functions
Paper <mrpapersonic@gmail.com>
parents:
45
diff
changeset
|
83 } |
327e9a5c72f1
filesystem: add basename and stem functions
Paper <mrpapersonic@gmail.com>
parents:
45
diff
changeset
|
84 |
60
d417e9381ca5
filesystem: WIP class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
59
diff
changeset
|
85 std::string Path::Extension() const { |
d417e9381ca5
filesystem: WIP class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
59
diff
changeset
|
86 std::string basename = Basename(_path); |
d417e9381ca5
filesystem: WIP class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
59
diff
changeset
|
87 return basename.substr(basename.find_last_of("."), basename.end()); |
d417e9381ca5
filesystem: WIP class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
59
diff
changeset
|
88 } |
d417e9381ca5
filesystem: WIP class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
59
diff
changeset
|
89 |
d417e9381ca5
filesystem: WIP class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
59
diff
changeset
|
90 std::string Path::GetParent() const { |
d417e9381ca5
filesystem: WIP class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
59
diff
changeset
|
91 return _path.substr(0, _path.find_last_of(DELIM)); |
11 | 92 } |
93 | |
45
4b05bc7668eb
filesystem: split config path into dotpath and config, add anime db path
Paper <mrpapersonic@gmail.com>
parents:
44
diff
changeset
|
94 std::string GetDotPath(void) { |
11 | 95 std::string ret = ""; |
9 | 96 #ifdef WIN32 |
97 char buf[PATH_MAX + 1]; | |
11 | 98 if (SHGetFolderPathAndSubDir(NULL, CSIDL_APPDATA | CSIDL_FLAG_CREATE, NULL, 0, CONFIG_DIR, buf) == S_OK) { |
99 ret += buf; | |
100 } | |
2 | 101 #elif defined(MACOSX) |
11 | 102 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
|
103 ret += DELIM CONFIG_DIR; |
2 | 104 #else // just assume POSIX |
7 | 105 if (getenv("HOME") != NULL) |
11 | 106 ret += getenv("HOME"); |
15 | 107 # ifdef __linux__ |
7 | 108 else |
11 | 109 ret += getpwuid(getuid())->pw_dir; |
15 | 110 # endif // __linux__ |
11 | 111 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
|
112 ret += DELIM ".config" DELIM CONFIG_DIR; |
15 | 113 #endif // !WIN32 && !MACOSX |
11 | 114 return ret; |
2 | 115 } |
11 | 116 |
45
4b05bc7668eb
filesystem: split config path into dotpath and config, add anime db path
Paper <mrpapersonic@gmail.com>
parents:
44
diff
changeset
|
117 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
|
118 std::string ret = ""; |
4b05bc7668eb
filesystem: split config path into dotpath and config, add anime db path
Paper <mrpapersonic@gmail.com>
parents:
44
diff
changeset
|
119 ret += GetDotPath(); |
4b05bc7668eb
filesystem: split config path into dotpath and config, add anime db path
Paper <mrpapersonic@gmail.com>
parents:
44
diff
changeset
|
120 if (!ret.empty()) |
4b05bc7668eb
filesystem: split config path into dotpath and config, add anime db path
Paper <mrpapersonic@gmail.com>
parents:
44
diff
changeset
|
121 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
|
122 return ret; |
4b05bc7668eb
filesystem: split config path into dotpath and config, add anime db path
Paper <mrpapersonic@gmail.com>
parents:
44
diff
changeset
|
123 } |
4b05bc7668eb
filesystem: split config path into dotpath and config, add anime db path
Paper <mrpapersonic@gmail.com>
parents:
44
diff
changeset
|
124 |
4b05bc7668eb
filesystem: split config path into dotpath and config, add anime db path
Paper <mrpapersonic@gmail.com>
parents:
44
diff
changeset
|
125 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
|
126 std::string ret = ""; |
4b05bc7668eb
filesystem: split config path into dotpath and config, add anime db path
Paper <mrpapersonic@gmail.com>
parents:
44
diff
changeset
|
127 ret += GetDotPath(); |
4b05bc7668eb
filesystem: split config path into dotpath and config, add anime db path
Paper <mrpapersonic@gmail.com>
parents:
44
diff
changeset
|
128 if (!ret.empty()) |
4b05bc7668eb
filesystem: split config path into dotpath and config, add anime db path
Paper <mrpapersonic@gmail.com>
parents:
44
diff
changeset
|
129 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
|
130 return ret; |
4b05bc7668eb
filesystem: split config path into dotpath and config, add anime db path
Paper <mrpapersonic@gmail.com>
parents:
44
diff
changeset
|
131 } |
4b05bc7668eb
filesystem: split config path into dotpath and config, add anime db path
Paper <mrpapersonic@gmail.com>
parents:
44
diff
changeset
|
132 |
15 | 133 } // namespace Filesystem |