Mercurial > minori
annotate src/core/filesystem.cpp @ 62:4c6dd5999b39
*: update
1. updated animia
2. use widestrings for filesystem on Windows
author | Paper <mrpapersonic@gmail.com> |
---|---|
date | Sun, 01 Oct 2023 06:16:06 -0400 |
parents | 327568ad9be9 |
children | 3d2decf093bb |
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 | |
61
327568ad9be9
core/fs: finish class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
60
diff
changeset
|
26 Path::Path() { _path = ""; } |
327568ad9be9
core/fs: finish class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
60
diff
changeset
|
27 Path::Path(const std::string& path) { _path = path; } |
327568ad9be9
core/fs: finish class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
60
diff
changeset
|
28 Path::Path(const Path& path) { _path = path.GetPath(); } |
60
d417e9381ca5
filesystem: WIP class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
59
diff
changeset
|
29 |
d417e9381ca5
filesystem: WIP class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
59
diff
changeset
|
30 bool Path::CreateDirectories() const { |
11 | 31 std::string temp = ""; |
32 size_t start; | |
33 size_t end = 0; | |
60
d417e9381ca5
filesystem: WIP class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
59
diff
changeset
|
34 temp.append(_path.substr(0, _path.find_first_not_of(DELIM, end))); |
11 | 35 |
60
d417e9381ca5
filesystem: WIP class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
59
diff
changeset
|
36 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
|
37 end = _path.find(DELIM, start); |
d417e9381ca5
filesystem: WIP class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
59
diff
changeset
|
38 temp.append(_path.substr(start, end - start)); |
11 | 39 #ifdef WIN32 |
62 | 40 if (!CreateDirectoryW(Strings::ToWstring(temp).c_str(), NULL) && GetLastError() == ERROR_PATH_NOT_FOUND) |
11 | 41 /* ERROR_PATH_NOT_FOUND should NOT happen here */ |
42 return false; | |
43 #else | |
44
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
36
diff
changeset
|
44 struct stat st; |
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
36
diff
changeset
|
45 if (stat(temp.c_str(), &st) == -1) |
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
36
diff
changeset
|
46 mkdir(temp.c_str(), 0755); |
11 | 47 #endif |
48 temp.append(DELIM); | |
49 } | |
50 return true; | |
51 } | |
52 | |
60
d417e9381ca5
filesystem: WIP class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
59
diff
changeset
|
53 bool Path::Exists() const { |
11 | 54 #if WIN32 |
62 | 55 std::wstring buf = Strings::ToWstring(_path); |
56 return GetFileAttributesW(buf.c_str()) != INVALID_FILE_ATTRIBUTES; | |
11 | 57 #else |
58 struct stat st; | |
60
d417e9381ca5
filesystem: WIP class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
59
diff
changeset
|
59 return stat(_path.c_str(), &st) == 0; |
11 | 60 #endif |
61 } | |
62 | |
60
d417e9381ca5
filesystem: WIP class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
59
diff
changeset
|
63 std::string Path::Basename() const { |
61
327568ad9be9
core/fs: finish class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
60
diff
changeset
|
64 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
|
65 } |
327e9a5c72f1
filesystem: add basename and stem functions
Paper <mrpapersonic@gmail.com>
parents:
45
diff
changeset
|
66 |
60
d417e9381ca5
filesystem: WIP class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
59
diff
changeset
|
67 std::string Path::Stem() const { |
d417e9381ca5
filesystem: WIP class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
59
diff
changeset
|
68 std::string basename = Basename(); |
d417e9381ca5
filesystem: WIP class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
59
diff
changeset
|
69 return basename.substr(0, basename.find_last_of(".")); |
59
327e9a5c72f1
filesystem: add basename and stem functions
Paper <mrpapersonic@gmail.com>
parents:
45
diff
changeset
|
70 } |
327e9a5c72f1
filesystem: add basename and stem functions
Paper <mrpapersonic@gmail.com>
parents:
45
diff
changeset
|
71 |
60
d417e9381ca5
filesystem: WIP class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
59
diff
changeset
|
72 std::string Path::Extension() const { |
61
327568ad9be9
core/fs: finish class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
60
diff
changeset
|
73 std::string basename = Basename(); |
327568ad9be9
core/fs: finish class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
60
diff
changeset
|
74 return basename.substr(basename.find_last_of("."), basename.length()); |
60
d417e9381ca5
filesystem: WIP class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
59
diff
changeset
|
75 } |
d417e9381ca5
filesystem: WIP class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
59
diff
changeset
|
76 |
61
327568ad9be9
core/fs: finish class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
60
diff
changeset
|
77 Path Path::GetParent() const { |
60
d417e9381ca5
filesystem: WIP class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
59
diff
changeset
|
78 return _path.substr(0, _path.find_last_of(DELIM)); |
11 | 79 } |
80 | |
61
327568ad9be9
core/fs: finish class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
60
diff
changeset
|
81 void Path::SetPath(const std::string& path) { |
327568ad9be9
core/fs: finish class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
60
diff
changeset
|
82 _path = path; |
327568ad9be9
core/fs: finish class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
60
diff
changeset
|
83 } |
327568ad9be9
core/fs: finish class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
60
diff
changeset
|
84 |
327568ad9be9
core/fs: finish class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
60
diff
changeset
|
85 std::string Path::GetPath() const { |
327568ad9be9
core/fs: finish class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
60
diff
changeset
|
86 return _path; |
327568ad9be9
core/fs: finish class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
60
diff
changeset
|
87 } |
327568ad9be9
core/fs: finish class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
60
diff
changeset
|
88 |
327568ad9be9
core/fs: finish class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
60
diff
changeset
|
89 Path GetDotPath(void) { |
11 | 90 std::string ret = ""; |
9 | 91 #ifdef WIN32 |
62 | 92 std::wstring buf(MAX_PATH, '\0'); |
93 if (SHGetFolderPathAndSubDirW(NULL, CSIDL_APPDATA | CSIDL_FLAG_CREATE, NULL, 0, CONFIG_WDIR, &buf.front()) == S_OK) { | |
94 buf.resize(buf.find('\0')); | |
95 ret += Strings::ToUtf8String(buf); | |
11 | 96 } |
2 | 97 #elif defined(MACOSX) |
11 | 98 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
|
99 ret += DELIM CONFIG_DIR; |
2 | 100 #else // just assume POSIX |
7 | 101 if (getenv("HOME") != NULL) |
11 | 102 ret += getenv("HOME"); |
15 | 103 # ifdef __linux__ |
7 | 104 else |
11 | 105 ret += getpwuid(getuid())->pw_dir; |
15 | 106 # endif // __linux__ |
11 | 107 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
|
108 ret += DELIM ".config" DELIM CONFIG_DIR; |
15 | 109 #endif // !WIN32 && !MACOSX |
11 | 110 return ret; |
2 | 111 } |
11 | 112 |
61
327568ad9be9
core/fs: finish class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
60
diff
changeset
|
113 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
|
114 std::string ret = ""; |
61
327568ad9be9
core/fs: finish class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
60
diff
changeset
|
115 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
|
116 if (!ret.empty()) |
4b05bc7668eb
filesystem: split config path into dotpath and config, add anime db path
Paper <mrpapersonic@gmail.com>
parents:
44
diff
changeset
|
117 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
|
118 return ret; |
4b05bc7668eb
filesystem: split config path into dotpath and config, add anime db path
Paper <mrpapersonic@gmail.com>
parents:
44
diff
changeset
|
119 } |
4b05bc7668eb
filesystem: split config path into dotpath and config, add anime db path
Paper <mrpapersonic@gmail.com>
parents:
44
diff
changeset
|
120 |
61
327568ad9be9
core/fs: finish class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
60
diff
changeset
|
121 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
|
122 std::string ret = ""; |
61
327568ad9be9
core/fs: finish class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
60
diff
changeset
|
123 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
|
124 if (!ret.empty()) |
4b05bc7668eb
filesystem: split config path into dotpath and config, add anime db path
Paper <mrpapersonic@gmail.com>
parents:
44
diff
changeset
|
125 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
|
126 return ret; |
4b05bc7668eb
filesystem: split config path into dotpath and config, add anime db path
Paper <mrpapersonic@gmail.com>
parents:
44
diff
changeset
|
127 } |
4b05bc7668eb
filesystem: split config path into dotpath and config, add anime db path
Paper <mrpapersonic@gmail.com>
parents:
44
diff
changeset
|
128 |
15 | 129 } // namespace Filesystem |