Mercurial > minori
comparison src/core/filesystem.cc @ 81:9b2b41f83a5e
boring: mass rename to cc
because this is a very unix-y project, it makes more sense to use the
'cc' extension
author | Paper <mrpapersonic@gmail.com> |
---|---|
date | Mon, 23 Oct 2023 12:07:27 -0400 |
parents | src/core/filesystem.cpp@3364fadc8a36 |
children | c8c72278f6fd |
comparison
equal
deleted
inserted
replaced
80:825506f0e221 | 81:9b2b41f83a5e |
---|---|
1 #ifdef WIN32 | |
2 # include <shlobj.h> | |
3 #elif defined(MACOSX) | |
4 # include "sys/osx/filesystem.h" | |
5 #elif defined(__linux__) | |
6 # include <pwd.h> | |
7 # include <sys/types.h> | |
8 #endif | |
9 | |
10 #ifdef WIN32 | |
11 # define DELIM "\\" | |
12 #else | |
13 # define DELIM "/" | |
14 # include <errno.h> | |
15 # include <unistd.h> | |
16 # include <sys/stat.h> | |
17 #endif | |
18 | |
19 #include "core/filesystem.h" | |
20 #include "core/config.h" | |
21 #include "core/strings.h" | |
22 #include <limits.h> | |
23 | |
24 namespace Filesystem { | |
25 | |
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 } | |
35 | |
36 bool Path::CreateDirectories() const { | |
37 std::string temp = ""; | |
38 size_t start; | |
39 size_t end = 0; | |
40 temp.append(_path.substr(0, _path.find_first_not_of(DELIM, end))); | |
41 | |
42 while ((start = _path.find_first_not_of(DELIM, end)) != std::string::npos) { | |
43 end = _path.find(DELIM, start); | |
44 temp.append(_path.substr(start, end - start)); | |
45 #ifdef WIN32 | |
46 if (!CreateDirectoryW(Strings::ToWstring(temp).c_str(), NULL) && GetLastError() == ERROR_PATH_NOT_FOUND) | |
47 /* ERROR_PATH_NOT_FOUND should NOT happen here */ | |
48 return false; | |
49 #else | |
50 struct stat st; | |
51 if (stat(temp.c_str(), &st) == -1) | |
52 mkdir(temp.c_str(), 0755); | |
53 #endif | |
54 temp.append(DELIM); | |
55 } | |
56 return true; | |
57 } | |
58 | |
59 bool Path::Exists() const { | |
60 #if WIN32 | |
61 std::wstring buf = Strings::ToWstring(_path); | |
62 return GetFileAttributesW(buf.c_str()) != INVALID_FILE_ATTRIBUTES; | |
63 #else | |
64 struct stat st; | |
65 return stat(_path.c_str(), &st) == 0; | |
66 #endif | |
67 } | |
68 | |
69 std::string Path::Basename() const { | |
70 unsigned long long pos = _path.find_last_of(DELIM); | |
71 return pos != std::string::npos ? _path.substr(pos + 1, _path.length()) : ""; | |
72 } | |
73 | |
74 std::string Path::Stem() const { | |
75 std::string basename = Basename(); | |
76 unsigned long long pos = basename.find_last_of("."); | |
77 return pos != std::string::npos ? basename.substr(0, pos) : ""; | |
78 } | |
79 | |
80 std::string Path::Extension() const { | |
81 std::string basename = Basename(); | |
82 unsigned long long pos = basename.find_last_of("."); | |
83 return pos != std::string::npos ? basename.substr(pos + 1, basename.length()) : ""; | |
84 } | |
85 | |
86 Path Path::GetParent() const { | |
87 return _path.substr(0, _path.find_last_of(DELIM)); | |
88 } | |
89 | |
90 void Path::SetPath(const std::string& path) { | |
91 _path = path; | |
92 } | |
93 | |
94 std::string Path::GetPath() const { | |
95 return _path; | |
96 } | |
97 | |
98 Path GetDotPath(void) { | |
99 std::string ret = ""; | |
100 #ifdef WIN32 | |
101 std::wstring buf(MAX_PATH, '\0'); | |
102 if (SHGetFolderPathAndSubDirW(NULL, CSIDL_APPDATA | CSIDL_FLAG_CREATE, NULL, 0, CONFIG_WDIR, &buf.front()) == | |
103 S_OK) { | |
104 buf.resize(buf.find('\0')); | |
105 ret += Strings::ToUtf8String(buf); | |
106 } | |
107 #elif defined(MACOSX) | |
108 ret += osx::GetApplicationSupportDirectory(); | |
109 ret += DELIM CONFIG_DIR; | |
110 #else // just assume POSIX | |
111 if (getenv("HOME") != NULL) | |
112 ret += getenv("HOME"); | |
113 # ifdef __linux__ | |
114 else | |
115 ret += getpwuid(getuid())->pw_dir; | |
116 # endif // __linux__ | |
117 if (!ret.empty()) | |
118 ret += DELIM ".config" DELIM CONFIG_DIR; | |
119 #endif // !WIN32 && !MACOSX | |
120 return ret; | |
121 } | |
122 | |
123 Path GetConfigPath(void) { | |
124 std::string ret = ""; | |
125 ret += GetDotPath().GetPath(); | |
126 if (!ret.empty()) | |
127 ret += DELIM CONFIG_NAME; | |
128 return ret; | |
129 } | |
130 | |
131 Path GetAnimeDBPath(void) { | |
132 std::string ret = ""; | |
133 ret += GetDotPath().GetPath(); | |
134 if (!ret.empty()) | |
135 ret += DELIM "anime" DELIM "db.json"; | |
136 return ret; | |
137 } | |
138 | |
139 } // namespace Filesystem |