Mercurial > minori
comparison src/core/filesystem.cc @ 135:0a458cb26ff4
filesystem: move to using std::filesystem after C++17 switch
old compilers will croak compiling this, but it's not like we
*really* need to support them (they probably croak compiling
Qt as well)
author | Paper <mrpapersonic@gmail.com> |
---|---|
date | Thu, 09 Nov 2023 18:01:56 -0500 |
parents | 54c9d36207db |
children | 7d3ad9529c4c |
comparison
equal
deleted
inserted
replaced
134:54c9d36207db | 135:0a458cb26ff4 |
---|---|
17 #endif | 17 #endif |
18 | 18 |
19 #include "core/filesystem.h" | 19 #include "core/filesystem.h" |
20 #include "core/config.h" | 20 #include "core/config.h" |
21 #include "core/strings.h" | 21 #include "core/strings.h" |
22 #include <filesystem> | |
22 #include <limits.h> | 23 #include <limits.h> |
23 | |
24 /* TODO: remove this and use std::filesystem */ | |
25 | 24 |
26 namespace Filesystem { | 25 namespace Filesystem { |
27 | 26 |
28 Path::Path() { | 27 /* this runs fs::create_directories() on the |
29 _path = ""; | 28 PARENT directory. */ |
30 } | 29 void CreateDirectories(const std::filesystem::path& path) { |
31 Path::Path(const std::string& path) { | 30 std::filesystem::create_directories(path.parent_path()); |
32 _path = path; | |
33 } | |
34 Path::Path(const Path& path) { | |
35 _path = path.GetPath(); | |
36 } | 31 } |
37 | 32 |
38 bool Path::CreateDirectories() const { | 33 std::filesystem::path GetDotPath() { |
39 std::string temp = ""; | |
40 size_t start; | |
41 size_t end = 0; | |
42 temp.append(_path.substr(0, _path.find_first_not_of(DELIM, end))); | |
43 | |
44 while ((start = _path.find_first_not_of(DELIM, end)) != std::string::npos) { | |
45 end = _path.find(DELIM, start); | |
46 temp.append(_path.substr(start, end - start)); | |
47 #ifdef WIN32 | 34 #ifdef WIN32 |
48 if (!CreateDirectoryW(Strings::ToWstring(temp).c_str(), NULL) && GetLastError() == ERROR_PATH_NOT_FOUND) | 35 std::filesystem::path path; |
49 /* ERROR_PATH_NOT_FOUND should NOT happen here */ | 36 wchar_t* buf; |
50 return false; | 37 if (SHGetKnownFolderPath(FOLDERID_RoamingAppData, KF_FLAG_CREATE, NULL, &buf) == S_OK) |
51 #else | 38 path = buf; |
52 struct stat st; | 39 else |
53 if (stat(temp.c_str(), &st) == -1) | 40 return std::filesystem::path(); |
54 mkdir(temp.c_str(), 0755); | 41 CoTaskMemFree(buf); |
55 #endif | 42 return path / CONFIG_DIR; |
56 temp.append(DELIM); | 43 #elif defined(MACOSX) |
57 } | 44 return std::filesystem::path(osx::GetApplicationSupportDirectory()) / CONFIG_DIR; |
58 return true; | 45 #else // just assume POSIX |
46 const char* home = getenv("HOME"); | |
47 if (home != NULL) | |
48 path = home; | |
49 # ifdef __linux__ | |
50 else | |
51 path = getpwuid(getuid())->pw_dir; | |
52 # endif // __linux__ | |
53 if (!path.empty()) | |
54 return path / ".config"; | |
55 else | |
56 return std::filesystem::path(); | |
57 #endif // !WIN32 && !MACOSX | |
58 return path / CONFIG_DIR; | |
59 } | 59 } |
60 | 60 |
61 bool Path::Exists() const { | 61 std::filesystem::path GetConfigPath() { |
62 #ifdef WIN32 | 62 return GetDotPath() / CONFIG_NAME; |
63 std::wstring buf = Strings::ToWstring(_path); | |
64 return GetFileAttributesW(buf.c_str()) != INVALID_FILE_ATTRIBUTES; | |
65 #else | |
66 struct stat st; | |
67 return stat(_path.c_str(), &st) == 0; | |
68 #endif | |
69 } | 63 } |
70 | 64 |
71 std::string Path::Basename() const { | 65 std::filesystem::path GetPlayersPath() { |
72 unsigned long long pos = _path.find_last_of(DELIM); | 66 return GetDotPath() / "recognition" / "players.json"; |
73 return pos != std::string::npos ? _path.substr(pos + 1, _path.length()) : ""; | |
74 } | 67 } |
75 | 68 |
76 std::string Path::Stem() const { | 69 std::filesystem::path GetExtensionsPath() { |
77 std::string basename = Basename(); | 70 return GetDotPath() / "recognition" / "extensions.json"; |
78 unsigned long long pos = basename.find_last_of("."); | |
79 return pos != std::string::npos ? basename.substr(0, pos) : ""; | |
80 } | 71 } |
81 | 72 |
82 std::string Path::Extension() const { | 73 std::filesystem::path GetAnimeDBPath() { |
83 std::string basename = Basename(); | 74 return GetDotPath() / "anime" / "db.json"; |
84 unsigned long long pos = basename.find_last_of("."); | |
85 return pos != std::string::npos ? basename.substr(pos + 1, basename.length()) : ""; | |
86 } | |
87 | |
88 Path Path::GetParent() const { | |
89 return _path.substr(0, _path.find_last_of(DELIM)); | |
90 } | |
91 | |
92 void Path::SetPath(const std::string& path) { | |
93 _path = path; | |
94 } | |
95 | |
96 std::string Path::GetPath() const { | |
97 return _path; | |
98 } | |
99 | |
100 Path GetDotPath(void) { | |
101 std::string ret = ""; | |
102 #ifdef WIN32 | |
103 std::wstring buf(MAX_PATH, '\0'); | |
104 if (SHGetFolderPathAndSubDirW(NULL, CSIDL_APPDATA | CSIDL_FLAG_CREATE, NULL, 0, CONFIG_WDIR, &buf.front()) == | |
105 S_OK) { | |
106 buf.resize(buf.find('\0')); | |
107 ret += Strings::ToUtf8String(buf); | |
108 } | |
109 #elif defined(MACOSX) | |
110 ret += osx::GetApplicationSupportDirectory(); | |
111 ret += DELIM CONFIG_DIR; | |
112 #else // just assume POSIX | |
113 if (getenv("HOME") != NULL) | |
114 ret += getenv("HOME"); | |
115 # ifdef __linux__ | |
116 else | |
117 ret += getpwuid(getuid())->pw_dir; | |
118 # endif // __linux__ | |
119 if (!ret.empty()) | |
120 ret += DELIM ".config" DELIM CONFIG_DIR; | |
121 #endif // !WIN32 && !MACOSX | |
122 return ret; | |
123 } | |
124 | |
125 Path GetConfigPath(void) { | |
126 std::string ret = ""; | |
127 ret += GetDotPath().GetPath(); | |
128 if (!ret.empty()) | |
129 ret += DELIM CONFIG_NAME; | |
130 return ret; | |
131 } | |
132 | |
133 Path GetPlayersPath(void) { | |
134 std::string ret = ""; | |
135 ret += GetDotPath().GetPath(); | |
136 if (!ret.empty()) | |
137 ret += DELIM "players.json"; | |
138 return ret; | |
139 } | |
140 | |
141 Path GetExtensionsPath(void) { | |
142 std::string ret = ""; | |
143 ret += GetDotPath().GetPath(); | |
144 if (!ret.empty()) | |
145 ret += DELIM "extensions.json"; | |
146 return ret; | |
147 } | |
148 | |
149 Path GetAnimeDBPath(void) { | |
150 std::string ret = ""; | |
151 ret += GetDotPath().GetPath(); | |
152 if (!ret.empty()) | |
153 ret += DELIM "anime" DELIM "db.json"; | |
154 return ret; | |
155 } | 75 } |
156 | 76 |
157 } // namespace Filesystem | 77 } // namespace Filesystem |