Mercurial > minori
annotate src/core/filesystem.cpp @ 37:9ae9365dd4ea
window.cpp: fix sidebar on Linux
author | Paper <mrpapersonic@gmail.com> |
---|---|
date | Thu, 21 Sep 2023 17:15:43 -0400 |
parents | 2743011a6042 |
children | 619cbd6e69f9 |
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" |
2 | 21 #include <limits.h> |
22 | |
11 | 23 namespace Filesystem { |
24 | |
25 bool CreateDirectories(std::string path) { | |
26 std::string temp = ""; | |
27 size_t start; | |
28 size_t end = 0; | |
29 | |
15 | 30 while ((start = path.find_first_not_of(DELIM, end)) != std::string::npos) { |
11 | 31 end = path.find(DELIM, start); |
32 temp.append(path.substr(start, end - start)); | |
33 #ifdef WIN32 | |
34 if (!CreateDirectoryA(temp.c_str(), NULL) && GetLastError() == ERROR_PATH_NOT_FOUND) | |
35 /* ERROR_PATH_NOT_FOUND should NOT happen here */ | |
36 return false; | |
37 #else | |
38 if (mkdir(temp.c_str(), 0755)) | |
39 return false; | |
40 #endif | |
41 temp.append(DELIM); | |
42 } | |
43 return true; | |
44 } | |
45 | |
46 bool Exists(std::string path) { | |
47 #if WIN32 | |
48 return GetFileAttributes(path.c_str()) != INVALID_FILE_ATTRIBUTES; | |
49 #else | |
50 struct stat st; | |
51 return stat(path.c_str(), &st) == 0; | |
52 #endif | |
53 } | |
54 | |
55 std::string GetParentDirectory(std::string path) { | |
56 size_t pos = 0; | |
57 pos = path.find_last_of(DELIM); | |
58 return path.substr(0, pos); | |
59 } | |
60 | |
61 std::string GetConfigPath(void) { | |
62 std::string ret = ""; | |
9 | 63 #ifdef WIN32 |
64 char buf[PATH_MAX + 1]; | |
11 | 65 if (SHGetFolderPathAndSubDir(NULL, CSIDL_APPDATA | CSIDL_FLAG_CREATE, NULL, 0, CONFIG_DIR, buf) == S_OK) { |
66 ret += buf; | |
67 ret += DELIM CONFIG_NAME; | |
68 } | |
2 | 69 #elif defined(MACOSX) |
11 | 70 /* pass all of our problems to objc++ code */ |
71 ret += osx::GetApplicationSupportDirectory(); | |
72 ret += DELIM CONFIG_DIR DELIM CONFIG_NAME; | |
2 | 73 #else // just assume POSIX |
7 | 74 if (getenv("HOME") != NULL) |
11 | 75 ret += getenv("HOME"); |
15 | 76 # ifdef __linux__ |
7 | 77 else |
11 | 78 ret += getpwuid(getuid())->pw_dir; |
15 | 79 # endif // __linux__ |
11 | 80 if (!ret.empty()) |
81 ret += DELIM ".config" DELIM CONFIG_DIR DELIM CONFIG_NAME; | |
15 | 82 #endif // !WIN32 && !MACOSX |
11 | 83 return ret; |
2 | 84 } |
11 | 85 |
15 | 86 } // namespace Filesystem |