Mercurial > minori
annotate src/core/filesystem.cc @ 118:39521c47c7a3
*: another huge megacommit, SORRY
The torrents page works a lot better now
Added the edit option to the anime list right click menu
Vectorized currently playing files
Available player and extensions are now loaded at runtime
from files in (dotpath)/players.json and (dotpath)/extensions.json
These paths are not permanent and will likely be moved to
(dotpath)/recognition
...
...
...
author | Paper <mrpapersonic@gmail.com> |
---|---|
date | Tue, 07 Nov 2023 23:40:54 -0500 |
parents | c8c72278f6fd |
children | 54c9d36207db |
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" |
62 | 21 #include "core/strings.h" |
2 | 22 #include <limits.h> |
23 | |
11 | 24 namespace Filesystem { |
25 | |
63 | 26 Path::Path() { |
27 _path = ""; | |
28 } | |
29 Path::Path(const std::string& path) { | |
30 _path = path; | |
31 } | |
32 Path::Path(const Path& path) { | |
33 _path = path.GetPath(); | |
34 } | |
60
d417e9381ca5
filesystem: WIP class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
59
diff
changeset
|
35 |
d417e9381ca5
filesystem: WIP class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
59
diff
changeset
|
36 bool Path::CreateDirectories() const { |
11 | 37 std::string temp = ""; |
38 size_t start; | |
39 size_t end = 0; | |
60
d417e9381ca5
filesystem: WIP class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
59
diff
changeset
|
40 temp.append(_path.substr(0, _path.find_first_not_of(DELIM, end))); |
11 | 41 |
60
d417e9381ca5
filesystem: WIP class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
59
diff
changeset
|
42 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
|
43 end = _path.find(DELIM, start); |
d417e9381ca5
filesystem: WIP class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
59
diff
changeset
|
44 temp.append(_path.substr(start, end - start)); |
11 | 45 #ifdef WIN32 |
62 | 46 if (!CreateDirectoryW(Strings::ToWstring(temp).c_str(), NULL) && GetLastError() == ERROR_PATH_NOT_FOUND) |
11 | 47 /* ERROR_PATH_NOT_FOUND should NOT happen here */ |
48 return false; | |
49 #else | |
44
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
36
diff
changeset
|
50 struct stat st; |
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
36
diff
changeset
|
51 if (stat(temp.c_str(), &st) == -1) |
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
36
diff
changeset
|
52 mkdir(temp.c_str(), 0755); |
11 | 53 #endif |
54 temp.append(DELIM); | |
55 } | |
56 return true; | |
57 } | |
58 | |
60
d417e9381ca5
filesystem: WIP class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
59
diff
changeset
|
59 bool Path::Exists() const { |
106
c8c72278f6fd
*: #if -> #ifdef, remove outdated comments in sys/win32/dark_theme.cc
Paper <mrpapersonic@gmail.com>
parents:
81
diff
changeset
|
60 #ifdef WIN32 |
62 | 61 std::wstring buf = Strings::ToWstring(_path); |
62 return GetFileAttributesW(buf.c_str()) != INVALID_FILE_ATTRIBUTES; | |
11 | 63 #else |
64 struct stat st; | |
60
d417e9381ca5
filesystem: WIP class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
59
diff
changeset
|
65 return stat(_path.c_str(), &st) == 0; |
11 | 66 #endif |
67 } | |
68 | |
60
d417e9381ca5
filesystem: WIP class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
59
diff
changeset
|
69 std::string Path::Basename() const { |
64 | 70 unsigned long long pos = _path.find_last_of(DELIM); |
76 | 71 return pos != std::string::npos ? _path.substr(pos + 1, _path.length()) : ""; |
59
327e9a5c72f1
filesystem: add basename and stem functions
Paper <mrpapersonic@gmail.com>
parents:
45
diff
changeset
|
72 } |
327e9a5c72f1
filesystem: add basename and stem functions
Paper <mrpapersonic@gmail.com>
parents:
45
diff
changeset
|
73 |
60
d417e9381ca5
filesystem: WIP class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
59
diff
changeset
|
74 std::string Path::Stem() const { |
d417e9381ca5
filesystem: WIP class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
59
diff
changeset
|
75 std::string basename = Basename(); |
64 | 76 unsigned long long pos = basename.find_last_of("."); |
77 return pos != std::string::npos ? basename.substr(0, pos) : ""; | |
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::Extension() const { |
61
327568ad9be9
core/fs: finish class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
60
diff
changeset
|
81 std::string basename = Basename(); |
64 | 82 unsigned long long pos = basename.find_last_of("."); |
76 | 83 return pos != std::string::npos ? basename.substr(pos + 1, basename.length()) : ""; |
60
d417e9381ca5
filesystem: WIP class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
59
diff
changeset
|
84 } |
d417e9381ca5
filesystem: WIP class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
59
diff
changeset
|
85 |
61
327568ad9be9
core/fs: finish class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
60
diff
changeset
|
86 Path Path::GetParent() const { |
60
d417e9381ca5
filesystem: WIP class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
59
diff
changeset
|
87 return _path.substr(0, _path.find_last_of(DELIM)); |
11 | 88 } |
89 | |
61
327568ad9be9
core/fs: finish class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
60
diff
changeset
|
90 void Path::SetPath(const std::string& path) { |
327568ad9be9
core/fs: finish class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
60
diff
changeset
|
91 _path = path; |
327568ad9be9
core/fs: finish class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
60
diff
changeset
|
92 } |
327568ad9be9
core/fs: finish class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
60
diff
changeset
|
93 |
327568ad9be9
core/fs: finish class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
60
diff
changeset
|
94 std::string Path::GetPath() const { |
327568ad9be9
core/fs: finish class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
60
diff
changeset
|
95 return _path; |
327568ad9be9
core/fs: finish class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
60
diff
changeset
|
96 } |
327568ad9be9
core/fs: finish class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
60
diff
changeset
|
97 |
327568ad9be9
core/fs: finish class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
60
diff
changeset
|
98 Path GetDotPath(void) { |
11 | 99 std::string ret = ""; |
9 | 100 #ifdef WIN32 |
62 | 101 std::wstring buf(MAX_PATH, '\0'); |
63 | 102 if (SHGetFolderPathAndSubDirW(NULL, CSIDL_APPDATA | CSIDL_FLAG_CREATE, NULL, 0, CONFIG_WDIR, &buf.front()) == |
103 S_OK) { | |
62 | 104 buf.resize(buf.find('\0')); |
105 ret += Strings::ToUtf8String(buf); | |
11 | 106 } |
2 | 107 #elif defined(MACOSX) |
11 | 108 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
|
109 ret += DELIM CONFIG_DIR; |
2 | 110 #else // just assume POSIX |
7 | 111 if (getenv("HOME") != NULL) |
11 | 112 ret += getenv("HOME"); |
15 | 113 # ifdef __linux__ |
7 | 114 else |
11 | 115 ret += getpwuid(getuid())->pw_dir; |
15 | 116 # endif // __linux__ |
11 | 117 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
|
118 ret += DELIM ".config" DELIM CONFIG_DIR; |
15 | 119 #endif // !WIN32 && !MACOSX |
11 | 120 return ret; |
2 | 121 } |
11 | 122 |
61
327568ad9be9
core/fs: finish class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
60
diff
changeset
|
123 Path GetConfigPath(void) { |
45
4b05bc7668eb
filesystem: split config path into dotpath and config, add anime db path
Paper <mrpapersonic@gmail.com>
parents:
44
diff
changeset
|
124 std::string ret = ""; |
61
327568ad9be9
core/fs: finish class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
60
diff
changeset
|
125 ret += GetDotPath().GetPath(); |
45
4b05bc7668eb
filesystem: split config path into dotpath and config, add anime db path
Paper <mrpapersonic@gmail.com>
parents:
44
diff
changeset
|
126 if (!ret.empty()) |
4b05bc7668eb
filesystem: split config path into dotpath and config, add anime db path
Paper <mrpapersonic@gmail.com>
parents:
44
diff
changeset
|
127 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
|
128 return ret; |
4b05bc7668eb
filesystem: split config path into dotpath and config, add anime db path
Paper <mrpapersonic@gmail.com>
parents:
44
diff
changeset
|
129 } |
4b05bc7668eb
filesystem: split config path into dotpath and config, add anime db path
Paper <mrpapersonic@gmail.com>
parents:
44
diff
changeset
|
130 |
118
39521c47c7a3
*: another huge megacommit, SORRY
Paper <mrpapersonic@gmail.com>
parents:
106
diff
changeset
|
131 Path GetPlayersPath(void) { |
39521c47c7a3
*: another huge megacommit, SORRY
Paper <mrpapersonic@gmail.com>
parents:
106
diff
changeset
|
132 std::string ret = ""; |
39521c47c7a3
*: another huge megacommit, SORRY
Paper <mrpapersonic@gmail.com>
parents:
106
diff
changeset
|
133 ret += GetDotPath().GetPath(); |
39521c47c7a3
*: another huge megacommit, SORRY
Paper <mrpapersonic@gmail.com>
parents:
106
diff
changeset
|
134 if (!ret.empty()) |
39521c47c7a3
*: another huge megacommit, SORRY
Paper <mrpapersonic@gmail.com>
parents:
106
diff
changeset
|
135 ret += DELIM "players.json"; |
39521c47c7a3
*: another huge megacommit, SORRY
Paper <mrpapersonic@gmail.com>
parents:
106
diff
changeset
|
136 return ret; |
39521c47c7a3
*: another huge megacommit, SORRY
Paper <mrpapersonic@gmail.com>
parents:
106
diff
changeset
|
137 } |
39521c47c7a3
*: another huge megacommit, SORRY
Paper <mrpapersonic@gmail.com>
parents:
106
diff
changeset
|
138 |
39521c47c7a3
*: another huge megacommit, SORRY
Paper <mrpapersonic@gmail.com>
parents:
106
diff
changeset
|
139 Path GetExtensionsPath(void) { |
39521c47c7a3
*: another huge megacommit, SORRY
Paper <mrpapersonic@gmail.com>
parents:
106
diff
changeset
|
140 std::string ret = ""; |
39521c47c7a3
*: another huge megacommit, SORRY
Paper <mrpapersonic@gmail.com>
parents:
106
diff
changeset
|
141 ret += GetDotPath().GetPath(); |
39521c47c7a3
*: another huge megacommit, SORRY
Paper <mrpapersonic@gmail.com>
parents:
106
diff
changeset
|
142 if (!ret.empty()) |
39521c47c7a3
*: another huge megacommit, SORRY
Paper <mrpapersonic@gmail.com>
parents:
106
diff
changeset
|
143 ret += DELIM "extensions.json"; |
39521c47c7a3
*: another huge megacommit, SORRY
Paper <mrpapersonic@gmail.com>
parents:
106
diff
changeset
|
144 return ret; |
39521c47c7a3
*: another huge megacommit, SORRY
Paper <mrpapersonic@gmail.com>
parents:
106
diff
changeset
|
145 } |
39521c47c7a3
*: another huge megacommit, SORRY
Paper <mrpapersonic@gmail.com>
parents:
106
diff
changeset
|
146 |
61
327568ad9be9
core/fs: finish class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
60
diff
changeset
|
147 Path GetAnimeDBPath(void) { |
45
4b05bc7668eb
filesystem: split config path into dotpath and config, add anime db path
Paper <mrpapersonic@gmail.com>
parents:
44
diff
changeset
|
148 std::string ret = ""; |
61
327568ad9be9
core/fs: finish class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
60
diff
changeset
|
149 ret += GetDotPath().GetPath(); |
45
4b05bc7668eb
filesystem: split config path into dotpath and config, add anime db path
Paper <mrpapersonic@gmail.com>
parents:
44
diff
changeset
|
150 if (!ret.empty()) |
4b05bc7668eb
filesystem: split config path into dotpath and config, add anime db path
Paper <mrpapersonic@gmail.com>
parents:
44
diff
changeset
|
151 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
|
152 return ret; |
4b05bc7668eb
filesystem: split config path into dotpath and config, add anime db path
Paper <mrpapersonic@gmail.com>
parents:
44
diff
changeset
|
153 } |
4b05bc7668eb
filesystem: split config path into dotpath and config, add anime db path
Paper <mrpapersonic@gmail.com>
parents:
44
diff
changeset
|
154 |
15 | 155 } // namespace Filesystem |