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