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