9
|
1 #ifdef WIN32
|
2
|
2 #include <shlobj.h>
|
5
|
3 #elif defined(MACOSX)
|
|
4 #include "sys/osx/filesystem.h"
|
7
|
5 #elif defined(__linux__)
|
9
|
6 #include <pwd.h>
|
7
|
7 #include <sys/types.h>
|
2
|
8 #endif
|
11
|
9
|
|
10 #ifdef WIN32
|
|
11 #define DELIM "\\"
|
|
12 #else
|
|
13 #define DELIM "/"
|
|
14 #include <unistd.h>
|
|
15 #include <errno.h>
|
|
16 #endif
|
|
17
|
9
|
18 #include "core/filesystem.h"
|
11
|
19 #include "core/config.h"
|
2
|
20 #include <limits.h>
|
|
21
|
11
|
22 namespace Filesystem {
|
|
23
|
|
24 bool CreateDirectories(std::string path) {
|
|
25 std::string temp = "";
|
|
26 size_t start;
|
|
27 size_t end = 0;
|
|
28
|
|
29 while ((start = path.find_first_not_of(DELIM, end)) != std::string::npos)
|
|
30 {
|
|
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");
|
7
|
76 #ifdef __linux__
|
|
77 else
|
11
|
78 ret += getpwuid(getuid())->pw_dir;
|
7
|
79 #endif // __linux__
|
11
|
80 if (!ret.empty())
|
|
81 ret += DELIM ".config" DELIM CONFIG_DIR DELIM CONFIG_NAME;
|
9
|
82 #endif // !WIN32 && !MACOSX
|
11
|
83 return ret;
|
2
|
84 }
|
11
|
85
|
|
86 }
|