Mercurial > minori
comparison src/core/filesystem.cpp @ 11:fc1bf97c528b
*: use C++11 standard
I've been meaning to do this for a while, but I didn't want to reimplement
the filesystem code. Now we are on C++11 and most compilers from the past 5 centuries should support this now
author | Paper <mrpapersonic@gmail.com> |
---|---|
date | Sun, 17 Sep 2023 06:14:30 -0400 |
parents | 5c0397762b53 |
children | cde8f67a7c7d |
comparison
equal
deleted
inserted
replaced
10:4b198a111713 | 11:fc1bf97c528b |
---|---|
3 #elif defined(MACOSX) | 3 #elif defined(MACOSX) |
4 #include "sys/osx/filesystem.h" | 4 #include "sys/osx/filesystem.h" |
5 #elif defined(__linux__) | 5 #elif defined(__linux__) |
6 #include <pwd.h> | 6 #include <pwd.h> |
7 #include <sys/types.h> | 7 #include <sys/types.h> |
8 #endif | |
9 | |
10 #ifdef WIN32 | |
11 #define DELIM "\\" | |
12 #else | |
13 #define DELIM "/" | |
8 #include <unistd.h> | 14 #include <unistd.h> |
15 #include <errno.h> | |
9 #endif | 16 #endif |
17 | |
18 #include "core/filesystem.h" | |
10 #include "core/config.h" | 19 #include "core/config.h" |
11 #include "core/filesystem.h" | |
12 #include "core/strings.h" | |
13 #include <QMessageBox> | |
14 #include <filesystem> | |
15 #include <limits.h> | 20 #include <limits.h> |
16 | 21 |
17 std::filesystem::path get_config_path(void) { | 22 namespace Filesystem { |
18 std::filesystem::path cfg_path; | 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 = ""; | |
19 #ifdef WIN32 | 63 #ifdef WIN32 |
20 char buf[PATH_MAX + 1]; | 64 char buf[PATH_MAX + 1]; |
21 if (SHGetFolderPathAndSubDir(NULL, CSIDL_APPDATA | CSIDL_FLAG_CREATE, NULL, 0, CONFIG_DIR, buf) == S_OK) | 65 if (SHGetFolderPathAndSubDir(NULL, CSIDL_APPDATA | CSIDL_FLAG_CREATE, NULL, 0, CONFIG_DIR, buf) == S_OK) { |
22 cfg_path = std::filesystem::path(buf) / CONFIG_NAME; | 66 ret += buf; |
67 ret += DELIM CONFIG_NAME; | |
68 } | |
23 #elif defined(MACOSX) | 69 #elif defined(MACOSX) |
24 /* pass all of our problems to */ | 70 /* pass all of our problems to objc++ code */ |
25 cfg_path = std::filesystem::path(osx::GetApplicationSupportDirectory()) / CONFIG_DIR / CONFIG_NAME; | 71 ret += osx::GetApplicationSupportDirectory(); |
72 ret += DELIM CONFIG_DIR DELIM CONFIG_NAME; | |
26 #else // just assume POSIX | 73 #else // just assume POSIX |
27 if (getenv("HOME") != NULL) | 74 if (getenv("HOME") != NULL) |
28 cfg_path = std::filesystem::path(getenv("HOME")) / ".config" / CONFIG_DIR / CONFIG_NAME; | 75 ret += getenv("HOME"); |
29 #ifdef __linux__ | 76 #ifdef __linux__ |
30 else | 77 else |
31 cfg_path = std::filesystem::path(getpwuid(getuid())->pw_dir) / ".config" / CONFIG_DIR / CONFIG_NAME; | 78 ret += getpwuid(getuid())->pw_dir; |
32 #endif // __linux__ | 79 #endif // __linux__ |
80 if (!ret.empty()) | |
81 ret += DELIM ".config" DELIM CONFIG_DIR DELIM CONFIG_NAME; | |
33 #endif // !WIN32 && !MACOSX | 82 #endif // !WIN32 && !MACOSX |
34 return cfg_path; | 83 return ret; |
35 } | 84 } |
85 | |
86 } |