| 77531130 |
Poul Sander |
2016-02-17 18:42 |
1 |
/* |
| 47fef8e8 |
sago007 |
2016-11-24 21:37 |
2 |
Its is under the MIT license, to encourage reuse by cut-and-paste. |
| 47fef8e8 |
sago007 |
2016-11-24 21:37 |
3 |
|
| 47fef8e8 |
sago007 |
2016-11-24 21:37 |
4 |
The original files are hosted here: https://github.com/sago007/PlatformFolders |
| 47fef8e8 |
sago007 |
2016-11-24 21:37 |
5 |
|
| 47fef8e8 |
sago007 |
2016-11-24 21:37 |
6 |
Copyright (c) 2015-2016 Poul Sander |
| 47fef8e8 |
sago007 |
2016-11-24 21:37 |
7 |
|
| 47fef8e8 |
sago007 |
2016-11-24 21:37 |
8 |
Permission is hereby granted, free of charge, to any person |
| 47fef8e8 |
sago007 |
2016-11-24 21:37 |
9 |
obtaining a copy of this software and associated documentation files |
| 47fef8e8 |
sago007 |
2016-11-24 21:37 |
10 |
(the "Software"), to deal in the Software without restriction, |
| 47fef8e8 |
sago007 |
2016-11-24 21:37 |
11 |
including without limitation the rights to use, copy, modify, merge, |
| 47fef8e8 |
sago007 |
2016-11-24 21:37 |
12 |
publish, distribute, sublicense, and/or sell copies of the Software, |
| 47fef8e8 |
sago007 |
2016-11-24 21:37 |
13 |
and to permit persons to whom the Software is furnished to do so, |
| 47fef8e8 |
sago007 |
2016-11-24 21:37 |
14 |
subject to the following conditions: |
| 47fef8e8 |
sago007 |
2016-11-24 21:37 |
15 |
|
| 47fef8e8 |
sago007 |
2016-11-24 21:37 |
16 |
The above copyright notice and this permission notice shall be |
| 47fef8e8 |
sago007 |
2016-11-24 21:37 |
17 |
included in all copies or substantial portions of the Software. |
| 47fef8e8 |
sago007 |
2016-11-24 21:37 |
18 |
|
| 47fef8e8 |
sago007 |
2016-11-24 21:37 |
19 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 47fef8e8 |
sago007 |
2016-11-24 21:37 |
20 |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 47fef8e8 |
sago007 |
2016-11-24 21:37 |
21 |
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 47fef8e8 |
sago007 |
2016-11-24 21:37 |
22 |
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 47fef8e8 |
sago007 |
2016-11-24 21:37 |
23 |
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 47fef8e8 |
sago007 |
2016-11-24 21:37 |
24 |
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 47fef8e8 |
sago007 |
2016-11-24 21:37 |
25 |
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 47fef8e8 |
sago007 |
2016-11-24 21:37 |
26 |
SOFTWARE. |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
27 |
*/ |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
28 |
|
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
29 |
#include "platform_folders.h" |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
30 |
#include <iostream> |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
31 |
#include <stdexcept> |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
32 |
#include <string.h> |
| 0494de59 |
sago007 |
2018-06-03 19:32 |
33 |
#include <cstdio> |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
34 |
#include <cstdlib> |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
35 |
|
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
36 |
#if defined(_WIN32) |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
37 |
#include <windows.h> |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
38 |
#include <shlobj.h> |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
39 |
|
| 0494de59 |
sago007 |
2018-06-03 19:32 |
40 |
static std::string win32_utf16_to_utf8(const wchar_t* wstr) { |
| f7573b59 |
sago007 |
2016-10-08 17:11 |
41 |
std::string res; |
| f7573b59 |
sago007 |
2016-10-08 17:11 |
42 |
// If the 6th parameter is 0 then WideCharToMultiByte returns the number of bytes needed to store the result. |
| f7573b59 |
sago007 |
2016-10-08 17:11 |
43 |
int actualSize = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL); |
| f7573b59 |
sago007 |
2016-10-08 17:11 |
44 |
if (actualSize > 0) { |
| f7573b59 |
sago007 |
2016-10-08 17:11 |
45 |
//If the converted UTF-8 string could not be in the initial buffer. Allocate one that can hold it. |
| f7573b59 |
sago007 |
2016-10-08 17:11 |
46 |
std::vector<char> buffer(actualSize); |
| f7573b59 |
sago007 |
2016-10-08 17:11 |
47 |
actualSize = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, &buffer[0], buffer.size(), NULL, NULL); |
| f7573b59 |
sago007 |
2016-10-08 17:11 |
48 |
res = buffer.data(); |
| f7573b59 |
sago007 |
2016-10-08 17:11 |
49 |
} |
| f7573b59 |
sago007 |
2016-10-08 17:11 |
50 |
if (actualSize == 0) { |
| f7573b59 |
sago007 |
2016-10-08 17:11 |
51 |
// WideCharToMultiByte return 0 for errors. |
| 0494de59 |
sago007 |
2018-06-03 19:32 |
52 |
const std::string errorMsg = "UTF16 to UTF8 failed with error code: " + GetLastError(); |
| f7573b59 |
sago007 |
2016-10-08 17:11 |
53 |
throw std::runtime_error(errorMsg.c_str()); |
| f7573b59 |
sago007 |
2016-10-08 17:11 |
54 |
} |
| f7573b59 |
sago007 |
2016-10-08 17:11 |
55 |
return res; |
| f7573b59 |
sago007 |
2016-10-08 17:11 |
56 |
} |
| f7573b59 |
sago007 |
2016-10-08 17:11 |
57 |
|
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
58 |
static std::string GetWindowsFolder(int folderId, const char* errorMsg) { |
| f7573b59 |
sago007 |
2016-10-08 17:11 |
59 |
wchar_t szPath[MAX_PATH]; |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
60 |
szPath[0] = 0; |
| 0494de59 |
sago007 |
2018-06-03 19:32 |
61 |
if ( !SUCCEEDED( SHGetFolderPathW( NULL, folderId, NULL, 0, szPath ) ) ) { |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
62 |
throw std::runtime_error(errorMsg); |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
63 |
} |
| f7573b59 |
sago007 |
2016-10-08 17:11 |
64 |
return win32_utf16_to_utf8(szPath); |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
65 |
} |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
66 |
|
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
67 |
static std::string GetAppData() { |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
68 |
return GetWindowsFolder(CSIDL_APPDATA, "RoamingAppData could not be found"); |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
69 |
} |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
70 |
|
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
71 |
static std::string GetAppDataCommon() { |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
72 |
return GetWindowsFolder(CSIDL_COMMON_APPDATA, "Common appdata could not be found"); |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
73 |
} |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
74 |
|
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
75 |
static std::string GetAppDataLocal() { |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
76 |
return GetWindowsFolder(CSIDL_LOCAL_APPDATA, "LocalAppData could not be found"); |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
77 |
} |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
78 |
#elif defined(__APPLE__) |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
79 |
#include <CoreServices/CoreServices.h> |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
80 |
|
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
81 |
static std::string GetMacFolder(OSType folderType, const char* errorMsg) { |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
82 |
std::string ret; |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
83 |
FSRef ref; |
| 47fef8e8 |
sago007 |
2016-11-24 21:37 |
84 |
char path[PATH_MAX]; |
| 47fef8e8 |
sago007 |
2016-11-24 21:37 |
85 |
OSStatus err = FSFindFolder( kUserDomain, folderType, kCreateFolder, &ref ); |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
86 |
if (err != noErr) { |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
87 |
throw std::runtime_error(errorMsg); |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
88 |
} |
| 47fef8e8 |
sago007 |
2016-11-24 21:37 |
89 |
FSRefMakePath( &ref, (UInt8*)&path, PATH_MAX ); |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
90 |
ret = path; |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
91 |
return ret; |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
92 |
} |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
93 |
|
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
94 |
#else |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
95 |
#include <map> |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
96 |
#include <fstream> |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
97 |
#include <pwd.h> |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
98 |
#include <unistd.h> |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
99 |
#include <sys/types.h> |
| 0494de59 |
sago007 |
2018-06-03 19:32 |
100 |
// For strlen and strtok |
| 0494de59 |
sago007 |
2018-06-03 19:32 |
101 |
#include <cstring> |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
102 |
//Typically Linux. For easy reading the comments will just say Linux but should work with most *nixes |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
103 |
|
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
104 |
static void throwOnRelative(const char* envName, const char* envValue) { |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
105 |
if (envValue[0] != '/') { |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
106 |
char buffer[200]; |
| 0494de59 |
sago007 |
2018-06-03 19:32 |
107 |
std::snprintf(buffer, sizeof(buffer), "Environment \"%s\" does not start with an '/'. XDG specifies that the value must be absolute. The current value is: \"%s\"", envName, envValue); |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
108 |
throw std::runtime_error(buffer); |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
109 |
} |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
110 |
} |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
111 |
|
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
112 |
/** |
| 55ea55dd |
sago007 |
2018-03-24 16:07 |
113 |
* Retrives the effective user's home dir. |
| 55ea55dd |
sago007 |
2018-03-24 16:07 |
114 |
* If the user is running as root we ignore the HOME environment. It works badly with sudo. |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
115 |
* Writing to $HOME as root implies security concerns that a multiplatform program cannot be assumed to handle. |
| 55ea55dd |
sago007 |
2018-03-24 16:07 |
116 |
* @return The home directory. HOME environment is respected for non-root users if it exists. |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
117 |
*/ |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
118 |
static std::string getHome() { |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
119 |
std::string res; |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
120 |
int uid = getuid(); |
| 0494de59 |
sago007 |
2018-06-03 19:32 |
121 |
const char* homeEnv = std::getenv("HOME"); |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
122 |
if ( uid != 0 && homeEnv) { |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
123 |
//We only acknowlegde HOME if not root. |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
124 |
res = homeEnv; |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
125 |
return res; |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
126 |
} |
| 0494de59 |
sago007 |
2018-06-03 19:32 |
127 |
struct passwd* pw = getpwuid(uid); |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
128 |
if (!pw) { |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
129 |
throw std::runtime_error("Unable to get passwd struct."); |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
130 |
} |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
131 |
const char* tempRes = pw->pw_dir; |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
132 |
if (!tempRes) { |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
133 |
throw std::runtime_error("User has no home directory"); |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
134 |
} |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
135 |
res = tempRes; |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
136 |
return res; |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
137 |
} |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
138 |
|
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
139 |
static std::string getLinuxFolderDefault(const char* envName, const char* defaultRelativePath) { |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
140 |
std::string res; |
| 0494de59 |
sago007 |
2018-06-03 19:32 |
141 |
const char* tempRes = std::getenv(envName); |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
142 |
if (tempRes) { |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
143 |
throwOnRelative(envName, tempRes); |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
144 |
res = tempRes; |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
145 |
return res; |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
146 |
} |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
147 |
res = getHome() + "/" + defaultRelativePath; |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
148 |
return res; |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
149 |
} |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
150 |
|
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
151 |
static void appendExtraFoldersTokenizer(const char* envName, const char* envValue, std::vector<std::string>& folders) { |
| 0494de59 |
sago007 |
2018-06-03 19:32 |
152 |
std::vector<char> buffer(envValue, envValue + std::strlen(envValue) + 1); |
| 0494de59 |
sago007 |
2018-06-03 19:32 |
153 |
char* p = std::strtok ( &buffer[0], ":"); |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
154 |
while (p != NULL) { |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
155 |
if (p[0] == '/') { |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
156 |
folders.push_back(p); |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
157 |
} |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
158 |
else { |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
159 |
//Unless the system is wrongly configured this should never happen... But of course some systems will be incorectly configured. |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
160 |
//The XDG documentation indicates that the folder should be ignored but that the program should continue. |
| f7573b59 |
sago007 |
2016-10-08 17:11 |
161 |
std::cerr << "Skipping path \"" << p << "\" in \"" << envName << "\" because it does not start with a \"/\"\n"; |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
162 |
} |
| 0494de59 |
sago007 |
2018-06-03 19:32 |
163 |
p = std::strtok (NULL, ":"); |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
164 |
} |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
165 |
} |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
166 |
|
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
167 |
static void appendExtraFolders(const char* envName, const char* defaultValue, std::vector<std::string>& folders) { |
| 0494de59 |
sago007 |
2018-06-03 19:32 |
168 |
const char* envValue = std::getenv(envName); |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
169 |
if (!envValue) { |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
170 |
envValue = defaultValue; |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
171 |
} |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
172 |
appendExtraFoldersTokenizer(envName, envValue, folders); |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
173 |
} |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
174 |
|
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
175 |
#endif |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
176 |
|
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
177 |
|
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
178 |
namespace sago { |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
179 |
|
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
180 |
std::string getDataHome() { |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
181 |
#if defined(_WIN32) |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
182 |
return GetAppData(); |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
183 |
#elif defined(__APPLE__) |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
184 |
return GetMacFolder(kApplicationSupportFolderType, "Failed to find the Application Support Folder"); |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
185 |
#else |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
186 |
return getLinuxFolderDefault("XDG_DATA_HOME", ".local/share"); |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
187 |
#endif |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
188 |
} |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
189 |
|
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
190 |
std::string getConfigHome() { |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
191 |
#if defined(_WIN32) |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
192 |
return GetAppData(); |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
193 |
#elif defined(__APPLE__) |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
194 |
return GetMacFolder(kApplicationSupportFolderType, "Failed to find the Application Support Folder"); |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
195 |
#else |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
196 |
return getLinuxFolderDefault("XDG_CONFIG_HOME", ".config"); |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
197 |
#endif |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
198 |
} |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
199 |
|
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
200 |
std::string getCacheDir() { |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
201 |
#if defined(_WIN32) |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
202 |
return GetAppDataLocal(); |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
203 |
#elif defined(__APPLE__) |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
204 |
return GetMacFolder(kCachedDataFolderType, "Failed to find the Application Support Folder"); |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
205 |
#else |
| 0494de59 |
sago007 |
2018-06-03 19:32 |
206 |
return getLinuxFolderDefault("XDG_CACHE_HOME", ".cache"); |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
207 |
#endif |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
208 |
} |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
209 |
|
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
210 |
void appendAdditionalDataDirectories(std::vector<std::string>& homes) { |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
211 |
#if defined(_WIN32) |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
212 |
homes.push_back(GetAppDataCommon()); |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
213 |
#elif defined(__APPLE__) |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
214 |
#else |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
215 |
appendExtraFolders("XDG_DATA_DIRS", "/usr/local/share/:/usr/share/", homes); |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
216 |
#endif |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
217 |
} |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
218 |
|
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
219 |
void appendAdditionalConfigDirectories(std::vector<std::string>& homes) { |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
220 |
#if defined(_WIN32) |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
221 |
homes.push_back(GetAppDataCommon()); |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
222 |
#elif defined(__APPLE__) |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
223 |
#else |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
224 |
appendExtraFolders("XDG_CONFIG_DIRS", "/etc/xdg", homes); |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
225 |
#endif |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
226 |
} |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
227 |
|
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
228 |
#if defined(_WIN32) |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
229 |
#elif defined(__APPLE__) |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
230 |
#else |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
231 |
struct PlatformFolders::PlatformFoldersData { |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
232 |
std::map<std::string, std::string> folders; |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
233 |
}; |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
234 |
|
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
235 |
static void PlatformFoldersAddFromFile(const std::string& filename, std::map<std::string, std::string>& folders) { |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
236 |
std::ifstream infile(filename.c_str()); |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
237 |
std::string line; |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
238 |
while (std::getline(infile, line)) { |
| 68f32f61 |
sago007 |
2018-06-24 16:44 |
239 |
if (line.length() == 0 || line.at(0) == '#' || line.substr(0, 4) != "XDG_" || line.find("_DIR") == std::string::npos) { |
| 68f32f61 |
sago007 |
2018-06-24 16:44 |
240 |
continue; |
| 68f32f61 |
sago007 |
2018-06-24 16:44 |
241 |
} |
| 68f32f61 |
sago007 |
2018-06-24 16:44 |
242 |
try { |
| 68f32f61 |
sago007 |
2018-06-24 16:44 |
243 |
std::size_t splitPos = line.find('='); |
| 68f32f61 |
sago007 |
2018-06-24 16:44 |
244 |
std::string key = line.substr(0, splitPos); |
| 68f32f61 |
sago007 |
2018-06-24 16:44 |
245 |
std::size_t valueStart = line.find('"', splitPos); |
| 68f32f61 |
sago007 |
2018-06-24 16:44 |
246 |
std::size_t valueEnd = line.find('"', valueStart+1); |
| 68f32f61 |
sago007 |
2018-06-24 16:44 |
247 |
std::string value = line.substr(valueStart+1, valueEnd - valueStart - 1); |
| 68f32f61 |
sago007 |
2018-06-24 16:44 |
248 |
folders[key] = value; |
| 68f32f61 |
sago007 |
2018-06-24 16:44 |
249 |
} catch (std::exception& e) { |
| 68f32f61 |
sago007 |
2018-06-24 16:44 |
250 |
std::cerr << "WARNING: Failed to process \"" << line << "\" from \"" << filename << "\". Error: "<< e.what() << "\n"; |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
251 |
continue; |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
252 |
} |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
253 |
} |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
254 |
} |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
255 |
|
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
256 |
static void PlatformFoldersFillData(std::map<std::string, std::string>& folders) { |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
257 |
folders["XDG_DOCUMENTS_DIR"] = "$HOME/Documents"; |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
258 |
folders["XDG_DESKTOP_DIR"] = "$HOME/Desktop"; |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
259 |
folders["XDG_DOWNLOAD_DIR"] = "$HOME/Downloads"; |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
260 |
folders["XDG_MUSIC_DIR"] = "$HOME/Music"; |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
261 |
folders["XDG_PICTURES_DIR"] = "$HOME/Pictures"; |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
262 |
folders["XDG_PUBLICSHARE_DIR"] = "$HOME/Public"; |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
263 |
folders["XDG_TEMPLATES_DIR"] = "$HOME/.Templates"; |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
264 |
folders["XDG_VIDEOS_DIR"] = "$HOME/Videos"; |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
265 |
PlatformFoldersAddFromFile( getConfigHome()+"/user-dirs.dirs", folders); |
| 55ea55dd |
sago007 |
2018-03-24 16:07 |
266 |
for (std::map<std::string, std::string>::iterator itr = folders.begin() ; itr != folders.end() ; ++itr ) { |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
267 |
std::string& value = itr->second; |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
268 |
if (value.compare(0, 5, "$HOME") == 0) { |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
269 |
value = getHome() + value.substr(5, std::string::npos); |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
270 |
} |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
271 |
} |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
272 |
} |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
273 |
#endif |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
274 |
|
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
275 |
PlatformFolders::PlatformFolders() { |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
276 |
#if defined(_WIN32) |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
277 |
#elif defined(__APPLE__) |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
278 |
#else |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
279 |
this->data = new PlatformFolders::PlatformFoldersData(); |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
280 |
try { |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
281 |
PlatformFoldersFillData(data->folders); |
| 0494de59 |
sago007 |
2018-06-03 19:32 |
282 |
} |
| 0494de59 |
sago007 |
2018-06-03 19:32 |
283 |
catch (...) { |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
284 |
delete this->data; |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
285 |
throw; |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
286 |
} |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
287 |
#endif |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
288 |
} |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
289 |
|
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
290 |
PlatformFolders::~PlatformFolders() { |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
291 |
#if defined(_WIN32) |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
292 |
#elif defined(__APPLE__) |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
293 |
#else |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
294 |
delete this->data; |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
295 |
#endif |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
296 |
} |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
297 |
|
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
298 |
std::string PlatformFolders::getDocumentsFolder() const { |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
299 |
#if defined(_WIN32) |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
300 |
return GetWindowsFolder(CSIDL_PERSONAL, "Failed to find My Documents folder"); |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
301 |
#elif defined(__APPLE__) |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
302 |
return GetMacFolder(kDocumentsFolderType, "Failed to find Documents Folder"); |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
303 |
#else |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
304 |
return data->folders["XDG_DOCUMENTS_DIR"]; |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
305 |
#endif |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
306 |
} |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
307 |
|
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
308 |
std::string PlatformFolders::getDesktopFolder() const { |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
309 |
#if defined(_WIN32) |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
310 |
return GetWindowsFolder(CSIDL_DESKTOP, "Failed to find Desktop folder"); |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
311 |
#elif defined(__APPLE__) |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
312 |
return GetMacFolder(kDesktopFolderType, "Failed to find Desktop folder"); |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
313 |
#else |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
314 |
return data->folders["XDG_DESKTOP_DIR"]; |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
315 |
#endif |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
316 |
} |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
317 |
|
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
318 |
std::string PlatformFolders::getPicturesFolder() const { |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
319 |
#if defined(_WIN32) |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
320 |
return GetWindowsFolder(CSIDL_MYPICTURES, "Failed to find My Pictures folder"); |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
321 |
#elif defined(__APPLE__) |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
322 |
return GetMacFolder(kPictureDocumentsFolderType, "Failed to find Picture folder"); |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
323 |
#else |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
324 |
return data->folders["XDG_PICTURES_DIR"]; |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
325 |
#endif |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
326 |
} |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
327 |
|
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
328 |
std::string PlatformFolders::getDownloadFolder1() const { |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
329 |
#if defined(_WIN32) |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
330 |
//Pre Vista. Files was downloaded to the desktop |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
331 |
return GetWindowsFolder(CSIDL_DESKTOP, "Failed to find My Downloads (Desktop) folder"); |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
332 |
#elif defined(__APPLE__) |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
333 |
return GetMacFolder(kDownloadsFolderType, "Failed to find Download folder"); |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
334 |
#else |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
335 |
return data->folders["XDG_DOWNLOAD_DIR"]; |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
336 |
#endif |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
337 |
} |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
338 |
|
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
339 |
std::string PlatformFolders::getMusicFolder() const { |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
340 |
#if defined(_WIN32) |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
341 |
return GetWindowsFolder(CSIDL_MYMUSIC, "Failed to find My Music folder"); |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
342 |
#elif defined(__APPLE__) |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
343 |
return GetMacFolder(kMusicDocumentsFolderType, "Failed to find Music folder"); |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
344 |
#else |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
345 |
return data->folders["XDG_MUSIC_DIR"]; |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
346 |
#endif |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
347 |
} |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
348 |
|
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
349 |
std::string PlatformFolders::getVideoFolder() const { |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
350 |
#if defined(_WIN32) |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
351 |
return GetWindowsFolder(CSIDL_MYVIDEO, "Failed to find My Video folder"); |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
352 |
#elif defined(__APPLE__) |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
353 |
return GetMacFolder(kMovieDocumentsFolderType, "Failed to find Movie folder"); |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
354 |
#else |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
355 |
return data->folders["XDG_VIDEOS_DIR"]; |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
356 |
#endif |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
357 |
} |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
358 |
|
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
359 |
std::string PlatformFolders::getSaveGamesFolder1() const { |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
360 |
#if defined(_WIN32) |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
361 |
//A dedicated Save Games folder was not introduced until Vista. For XP and older save games are most often saved in a normal folder named "My Games". |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
362 |
//Data that should not be user accessible should be placed under GetDataHome() instead |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
363 |
return GetWindowsFolder(CSIDL_PERSONAL, "Failed to find My Documents folder")+"\\My Games"; |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
364 |
#elif defined(__APPLE__) |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
365 |
return GetMacFolder(kApplicationSupportFolderType, "Failed to find Application Support Folder"); |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
366 |
#else |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
367 |
return getDataHome(); |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
368 |
#endif |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
369 |
} |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
370 |
|
| 0494de59 |
sago007 |
2018-06-03 19:32 |
371 |
std::string getDesktopFolder() { |
| 0494de59 |
sago007 |
2018-06-03 19:32 |
372 |
return PlatformFolders().getDesktopFolder(); |
| 0494de59 |
sago007 |
2018-06-03 19:32 |
373 |
} |
| 0494de59 |
sago007 |
2018-06-03 19:32 |
374 |
|
| 0494de59 |
sago007 |
2018-06-03 19:32 |
375 |
std::string getDocumentsFolder() { |
| 0494de59 |
sago007 |
2018-06-03 19:32 |
376 |
return PlatformFolders().getDocumentsFolder(); |
| 0494de59 |
sago007 |
2018-06-03 19:32 |
377 |
} |
| 0494de59 |
sago007 |
2018-06-03 19:32 |
378 |
|
| 0494de59 |
sago007 |
2018-06-03 19:32 |
379 |
std::string getDownloadFolder1() { |
| 0494de59 |
sago007 |
2018-06-03 19:32 |
380 |
return PlatformFolders().getDownloadFolder1(); |
| 0494de59 |
sago007 |
2018-06-03 19:32 |
381 |
} |
| 0494de59 |
sago007 |
2018-06-03 19:32 |
382 |
|
| 0494de59 |
sago007 |
2018-06-03 19:32 |
383 |
std::string getPicturesFolder() { |
| 0494de59 |
sago007 |
2018-06-03 19:32 |
384 |
return PlatformFolders().getPicturesFolder(); |
| 0494de59 |
sago007 |
2018-06-03 19:32 |
385 |
} |
| 0494de59 |
sago007 |
2018-06-03 19:32 |
386 |
|
| 0494de59 |
sago007 |
2018-06-03 19:32 |
387 |
std::string getMusicFolder() { |
| 0494de59 |
sago007 |
2018-06-03 19:32 |
388 |
return PlatformFolders().getMusicFolder(); |
| 0494de59 |
sago007 |
2018-06-03 19:32 |
389 |
} |
| 0494de59 |
sago007 |
2018-06-03 19:32 |
390 |
|
| 0494de59 |
sago007 |
2018-06-03 19:32 |
391 |
std::string getVideoFolder() { |
| 0494de59 |
sago007 |
2018-06-03 19:32 |
392 |
return PlatformFolders().getVideoFolder(); |
| 0494de59 |
sago007 |
2018-06-03 19:32 |
393 |
} |
| 0494de59 |
sago007 |
2018-06-03 19:32 |
394 |
|
| 0494de59 |
sago007 |
2018-06-03 19:32 |
395 |
std::string getSaveGamesFolder1() { |
| 0494de59 |
sago007 |
2018-06-03 19:32 |
396 |
return PlatformFolders().getSaveGamesFolder1(); |
| 0494de59 |
sago007 |
2018-06-03 19:32 |
397 |
} |
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
398 |
|
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
399 |
|
| 77531130 |
Poul Sander |
2016-02-17 18:42 |
400 |
} //namespace sago |
|
|
1970-01-01 00:00 |
401 |
|