annotate src/core/filesystem.cc @ 137:69db40272acd

dep/animia: [WIP] huge refactor this WILL NOT compile, because lots of code has been changed and every API in the original codebase has been removed. note that this api setup is not exactly permanent...
author Paper <mrpapersonic@gmail.com>
date Fri, 10 Nov 2023 13:52:47 -0500
parents 7d3ad9529c4c
children 28842a8d0c6b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
9
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 7
diff changeset
1 #ifdef WIN32
15
cde8f67a7c7d *: update, megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 11
diff changeset
2 # include <shlobj.h>
5
51ae25154b70 Fix OS X support code
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
3 #elif defined(MACOSX)
15
cde8f67a7c7d *: update, megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 11
diff changeset
4 # include "sys/osx/filesystem.h"
7
Paper <mrpapersonic@gmail.com>
parents: 5
diff changeset
5 #elif defined(__linux__)
15
cde8f67a7c7d *: update, megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 11
diff changeset
6 # include <pwd.h>
cde8f67a7c7d *: update, megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 11
diff changeset
7 # include <sys/types.h>
2
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
8 #endif
11
fc1bf97c528b *: use C++11 standard
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
9
fc1bf97c528b *: use C++11 standard
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
10 #ifdef WIN32
15
cde8f67a7c7d *: update, megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 11
diff changeset
11 # define DELIM "\\"
11
fc1bf97c528b *: use C++11 standard
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
12 #else
15
cde8f67a7c7d *: update, megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 11
diff changeset
13 # define DELIM "/"
cde8f67a7c7d *: update, megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 11
diff changeset
14 # include <errno.h>
cde8f67a7c7d *: update, megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 11
diff changeset
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
fc1bf97c528b *: use C++11 standard
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
17 #endif
fc1bf97c528b *: use C++11 standard
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
18
36
2743011a6042 *: mass update
Paper <mrpapersonic@gmail.com>
parents: 18
diff changeset
19 #include "core/filesystem.h"
15
cde8f67a7c7d *: update, megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 11
diff changeset
20 #include "core/config.h"
62
4c6dd5999b39 *: update
Paper <mrpapersonic@gmail.com>
parents: 61
diff changeset
21 #include "core/strings.h"
135
0a458cb26ff4 filesystem: move to using std::filesystem after C++17 switch
Paper <mrpapersonic@gmail.com>
parents: 134
diff changeset
22 #include <filesystem>
2
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
23 #include <limits.h>
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
24
11
fc1bf97c528b *: use C++11 standard
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
25 namespace Filesystem {
fc1bf97c528b *: use C++11 standard
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
26
135
0a458cb26ff4 filesystem: move to using std::filesystem after C++17 switch
Paper <mrpapersonic@gmail.com>
parents: 134
diff changeset
27 /* this runs fs::create_directories() on the
0a458cb26ff4 filesystem: move to using std::filesystem after C++17 switch
Paper <mrpapersonic@gmail.com>
parents: 134
diff changeset
28 PARENT directory. */
0a458cb26ff4 filesystem: move to using std::filesystem after C++17 switch
Paper <mrpapersonic@gmail.com>
parents: 134
diff changeset
29 void CreateDirectories(const std::filesystem::path& path) {
0a458cb26ff4 filesystem: move to using std::filesystem after C++17 switch
Paper <mrpapersonic@gmail.com>
parents: 134
diff changeset
30 std::filesystem::create_directories(path.parent_path());
11
fc1bf97c528b *: use C++11 standard
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
31 }
fc1bf97c528b *: use C++11 standard
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
32
135
0a458cb26ff4 filesystem: move to using std::filesystem after C++17 switch
Paper <mrpapersonic@gmail.com>
parents: 134
diff changeset
33 std::filesystem::path GetDotPath() {
9
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 7
diff changeset
34 #ifdef WIN32
135
0a458cb26ff4 filesystem: move to using std::filesystem after C++17 switch
Paper <mrpapersonic@gmail.com>
parents: 134
diff changeset
35 std::filesystem::path path;
0a458cb26ff4 filesystem: move to using std::filesystem after C++17 switch
Paper <mrpapersonic@gmail.com>
parents: 134
diff changeset
36 wchar_t* buf;
0a458cb26ff4 filesystem: move to using std::filesystem after C++17 switch
Paper <mrpapersonic@gmail.com>
parents: 134
diff changeset
37 if (SHGetKnownFolderPath(FOLDERID_RoamingAppData, KF_FLAG_CREATE, NULL, &buf) == S_OK)
0a458cb26ff4 filesystem: move to using std::filesystem after C++17 switch
Paper <mrpapersonic@gmail.com>
parents: 134
diff changeset
38 path = buf;
0a458cb26ff4 filesystem: move to using std::filesystem after C++17 switch
Paper <mrpapersonic@gmail.com>
parents: 134
diff changeset
39 else
0a458cb26ff4 filesystem: move to using std::filesystem after C++17 switch
Paper <mrpapersonic@gmail.com>
parents: 134
diff changeset
40 return std::filesystem::path();
0a458cb26ff4 filesystem: move to using std::filesystem after C++17 switch
Paper <mrpapersonic@gmail.com>
parents: 134
diff changeset
41 CoTaskMemFree(buf);
0a458cb26ff4 filesystem: move to using std::filesystem after C++17 switch
Paper <mrpapersonic@gmail.com>
parents: 134
diff changeset
42 return path / CONFIG_DIR;
2
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
43 #elif defined(MACOSX)
135
0a458cb26ff4 filesystem: move to using std::filesystem after C++17 switch
Paper <mrpapersonic@gmail.com>
parents: 134
diff changeset
44 return std::filesystem::path(osx::GetApplicationSupportDirectory()) / CONFIG_DIR;
2
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
45 #else // just assume POSIX
135
0a458cb26ff4 filesystem: move to using std::filesystem after C++17 switch
Paper <mrpapersonic@gmail.com>
parents: 134
diff changeset
46 const char* home = getenv("HOME");
0a458cb26ff4 filesystem: move to using std::filesystem after C++17 switch
Paper <mrpapersonic@gmail.com>
parents: 134
diff changeset
47 if (home != NULL)
0a458cb26ff4 filesystem: move to using std::filesystem after C++17 switch
Paper <mrpapersonic@gmail.com>
parents: 134
diff changeset
48 path = home;
15
cde8f67a7c7d *: update, megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 11
diff changeset
49 # ifdef __linux__
7
Paper <mrpapersonic@gmail.com>
parents: 5
diff changeset
50 else
135
0a458cb26ff4 filesystem: move to using std::filesystem after C++17 switch
Paper <mrpapersonic@gmail.com>
parents: 134
diff changeset
51 path = getpwuid(getuid())->pw_dir;
15
cde8f67a7c7d *: update, megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 11
diff changeset
52 # endif // __linux__
135
0a458cb26ff4 filesystem: move to using std::filesystem after C++17 switch
Paper <mrpapersonic@gmail.com>
parents: 134
diff changeset
53 if (!path.empty())
0a458cb26ff4 filesystem: move to using std::filesystem after C++17 switch
Paper <mrpapersonic@gmail.com>
parents: 134
diff changeset
54 return path / ".config";
0a458cb26ff4 filesystem: move to using std::filesystem after C++17 switch
Paper <mrpapersonic@gmail.com>
parents: 134
diff changeset
55 else
0a458cb26ff4 filesystem: move to using std::filesystem after C++17 switch
Paper <mrpapersonic@gmail.com>
parents: 134
diff changeset
56 return std::filesystem::path();
15
cde8f67a7c7d *: update, megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 11
diff changeset
57 #endif // !WIN32 && !MACOSX
45
4b05bc7668eb filesystem: split config path into dotpath and config, add anime db path
Paper <mrpapersonic@gmail.com>
parents: 44
diff changeset
58 }
4b05bc7668eb filesystem: split config path into dotpath and config, add anime db path
Paper <mrpapersonic@gmail.com>
parents: 44
diff changeset
59
135
0a458cb26ff4 filesystem: move to using std::filesystem after C++17 switch
Paper <mrpapersonic@gmail.com>
parents: 134
diff changeset
60 std::filesystem::path GetConfigPath() {
0a458cb26ff4 filesystem: move to using std::filesystem after C++17 switch
Paper <mrpapersonic@gmail.com>
parents: 134
diff changeset
61 return GetDotPath() / CONFIG_NAME;
118
39521c47c7a3 *: another huge megacommit, SORRY
Paper <mrpapersonic@gmail.com>
parents: 106
diff changeset
62 }
39521c47c7a3 *: another huge megacommit, SORRY
Paper <mrpapersonic@gmail.com>
parents: 106
diff changeset
63
135
0a458cb26ff4 filesystem: move to using std::filesystem after C++17 switch
Paper <mrpapersonic@gmail.com>
parents: 134
diff changeset
64 std::filesystem::path GetPlayersPath() {
0a458cb26ff4 filesystem: move to using std::filesystem after C++17 switch
Paper <mrpapersonic@gmail.com>
parents: 134
diff changeset
65 return GetDotPath() / "recognition" / "players.json";
118
39521c47c7a3 *: another huge megacommit, SORRY
Paper <mrpapersonic@gmail.com>
parents: 106
diff changeset
66 }
39521c47c7a3 *: another huge megacommit, SORRY
Paper <mrpapersonic@gmail.com>
parents: 106
diff changeset
67
135
0a458cb26ff4 filesystem: move to using std::filesystem after C++17 switch
Paper <mrpapersonic@gmail.com>
parents: 134
diff changeset
68 std::filesystem::path GetExtensionsPath() {
0a458cb26ff4 filesystem: move to using std::filesystem after C++17 switch
Paper <mrpapersonic@gmail.com>
parents: 134
diff changeset
69 return GetDotPath() / "recognition" / "extensions.json";
0a458cb26ff4 filesystem: move to using std::filesystem after C++17 switch
Paper <mrpapersonic@gmail.com>
parents: 134
diff changeset
70 }
0a458cb26ff4 filesystem: move to using std::filesystem after C++17 switch
Paper <mrpapersonic@gmail.com>
parents: 134
diff changeset
71
0a458cb26ff4 filesystem: move to using std::filesystem after C++17 switch
Paper <mrpapersonic@gmail.com>
parents: 134
diff changeset
72 std::filesystem::path GetAnimeDBPath() {
0a458cb26ff4 filesystem: move to using std::filesystem after C++17 switch
Paper <mrpapersonic@gmail.com>
parents: 134
diff changeset
73 return GetDotPath() / "anime" / "db.json";
45
4b05bc7668eb filesystem: split config path into dotpath and config, add anime db path
Paper <mrpapersonic@gmail.com>
parents: 44
diff changeset
74 }
4b05bc7668eb filesystem: split config path into dotpath and config, add anime db path
Paper <mrpapersonic@gmail.com>
parents: 44
diff changeset
75
15
cde8f67a7c7d *: update, megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 11
diff changeset
76 } // namespace Filesystem