git repos / PlatformFolders

blame: sago/platform_folders.cpp

normal view · raw

1213f3f3 sago007 2015-09-19 19:24 1
/*
d2b67cec sago007 2018-03-24 09:52 2
Its is under the MIT license, to encourage reuse by cut-and-paste.
d2b67cec sago007 2018-03-24 09:52 3
d2b67cec sago007 2018-03-24 09:52 4
The original files are hosted here: https://github.com/sago007/PlatformFolders
d2b67cec sago007 2018-03-24 09:52 5
d2b67cec sago007 2018-03-24 09:52 6
Copyright (c) 2015-2016 Poul Sander
d2b67cec sago007 2018-03-24 09:52 7
d2b67cec sago007 2018-03-24 09:52 8
Permission is hereby granted, free of charge, to any person
d2b67cec sago007 2018-03-24 09:52 9
obtaining a copy of this software and associated documentation files
d2b67cec sago007 2018-03-24 09:52 10
(the "Software"), to deal in the Software without restriction,
d2b67cec sago007 2018-03-24 09:52 11
including without limitation the rights to use, copy, modify, merge,
d2b67cec sago007 2018-03-24 09:52 12
publish, distribute, sublicense, and/or sell copies of the Software,
d2b67cec sago007 2018-03-24 09:52 13
and to permit persons to whom the Software is furnished to do so,
d2b67cec sago007 2018-03-24 09:52 14
subject to the following conditions:
d2b67cec sago007 2018-03-24 09:52 15
d2b67cec sago007 2018-03-24 09:52 16
The above copyright notice and this permission notice shall be
d2b67cec sago007 2018-03-24 09:52 17
included in all copies or substantial portions of the Software.
d2b67cec sago007 2018-03-24 09:52 18
d2b67cec sago007 2018-03-24 09:52 19
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
d2b67cec sago007 2018-03-24 09:52 20
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
d2b67cec sago007 2018-03-24 09:52 21
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
d2b67cec sago007 2018-03-24 09:52 22
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
d2b67cec sago007 2018-03-24 09:52 23
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
d2b67cec sago007 2018-03-24 09:52 24
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
d2b67cec sago007 2018-03-24 09:52 25
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
d2b67cec sago007 2018-03-24 09:52 26
SOFTWARE.
1213f3f3 sago007 2015-09-19 19:24 27
*/
1213f3f3 sago007 2015-09-19 19:24 28
1213f3f3 sago007 2015-09-19 19:24 29
#include "platform_folders.h"
1213f3f3 sago007 2015-09-19 19:24 30
#include <iostream>
1213f3f3 sago007 2015-09-19 19:24 31
#include <stdexcept>
da123945 sum01 2018-04-16 20:58 32
#include <cstdio>
1213f3f3 sago007 2015-09-19 19:24 33
#include <cstdlib>
1213f3f3 sago007 2015-09-19 19:24 34
5612f69b sago007 2018-06-16 11:22 35
#ifndef _WIN32
5612f69b sago007 2018-06-16 11:22 36
5612f69b sago007 2018-06-16 11:22 37
#include <pwd.h>
5612f69b sago007 2018-06-16 11:22 38
#include <unistd.h>
5612f69b sago007 2018-06-16 11:22 39
5612f69b sago007 2018-06-16 11:22 40
/**
5612f69b sago007 2018-06-16 11:22 41
 * Retrives the effective user's home dir.
5612f69b sago007 2018-06-16 11:22 42
 * If the user is running as root we ignore the HOME environment. It works badly with sudo.
5612f69b sago007 2018-06-16 11:22 43
 * Writing to $HOME as root implies security concerns that a multiplatform program cannot be assumed to handle.
5612f69b sago007 2018-06-16 11:22 44
 * @return The home directory. HOME environment is respected for non-root users if it exists.
5612f69b sago007 2018-06-16 11:22 45
 */
5612f69b sago007 2018-06-16 11:22 46
static std::string getHome() {
5612f69b sago007 2018-06-16 11:22 47
	std::string res;
5612f69b sago007 2018-06-16 11:22 48
	int uid = getuid();
5612f69b sago007 2018-06-16 11:22 49
	const char* homeEnv = std::getenv("HOME");
5612f69b sago007 2018-06-16 11:22 50
	if ( uid != 0 && homeEnv) {
5612f69b sago007 2018-06-16 11:22 51
		//We only acknowlegde HOME if not root.
5612f69b sago007 2018-06-16 11:22 52
		res = homeEnv;
5612f69b sago007 2018-06-16 11:22 53
		return res;
5612f69b sago007 2018-06-16 11:22 54
	}
0267c838 sago007 2019-03-03 13:35 55
	struct passwd* pw = nullptr;
0267c838 sago007 2019-03-03 13:35 56
	struct passwd pwd;
0267c838 sago007 2019-03-03 13:35 57
	long bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
0267c838 sago007 2019-03-03 13:35 58
	if (bufsize < 0) {
0267c838 sago007 2019-03-03 13:35 59
		bufsize = 16384;
0267c838 sago007 2019-03-03 13:35 60
	}
0267c838 sago007 2019-03-03 13:35 61
	std::vector<char> buffer;
0267c838 sago007 2019-03-03 13:35 62
	buffer.resize(bufsize);
0267c838 sago007 2019-03-03 13:35 63
	int error_code = getpwuid_r(uid, &pwd, buffer.data(), buffer.size(), &pw);
0267c838 sago007 2019-03-03 13:35 64
	if (error_code) {
5612f69b sago007 2018-06-16 11:22 65
		throw std::runtime_error("Unable to get passwd struct.");
5612f69b sago007 2018-06-16 11:22 66
	}
5612f69b sago007 2018-06-16 11:22 67
	const char* tempRes = pw->pw_dir;
5612f69b sago007 2018-06-16 11:22 68
	if (!tempRes) {
5612f69b sago007 2018-06-16 11:22 69
		throw std::runtime_error("User has no home directory");
5612f69b sago007 2018-06-16 11:22 70
	}
5612f69b sago007 2018-06-16 11:22 71
	res = tempRes;
5612f69b sago007 2018-06-16 11:22 72
	return res;
5612f69b sago007 2018-06-16 11:22 73
}
5612f69b sago007 2018-06-16 11:22 74
c7c13350 sum01 2019-03-06 18:59 75
#endif
5612f69b sago007 2018-06-16 11:22 76
e5876621 sum01 2018-04-21 23:00 77
#ifdef _WIN32
fff3cc86 sum01 2018-04-22 02:15 78
// Make sure we don't bring in all the extra junk with windows.h
fff3cc86 sum01 2018-04-22 02:15 79
#ifndef WIN32_LEAN_AND_MEAN
fff3cc86 sum01 2018-04-22 02:15 80
#define WIN32_LEAN_AND_MEAN
fff3cc86 sum01 2018-04-22 02:15 81
#endif
fff3cc86 sum01 2018-04-22 02:15 82
// stringapiset.h depends on this
1213f3f3 sago007 2015-09-19 19:24 83
#include <windows.h>
fff3cc86 sum01 2018-04-22 02:15 84
// For SUCCEEDED macro
fff3cc86 sum01 2018-04-22 02:15 85
#include <winerror.h>
fff3cc86 sum01 2018-04-22 02:15 86
// For WideCharToMultiByte
fff3cc86 sum01 2018-04-22 02:15 87
#include <stringapiset.h>
fff3cc86 sum01 2018-04-22 02:15 88
// For SHGetFolderPathW and various CSIDL "magic numbers"
1213f3f3 sago007 2015-09-19 19:24 89
#include <shlobj.h>
1213f3f3 sago007 2015-09-19 19:24 90
ed746269 sago007 2019-03-03 13:08 91
namespace sago {
ed746269 sago007 2019-03-03 13:08 92
namespace internal {
ed746269 sago007 2019-03-03 13:08 93
335988d2 sum01 2019-03-06 18:58 94
std::string win32_utf16_to_utf8(const wchar_t* wstr) {
b81b63ae sago007 2016-10-03 20:30 95
	std::string res;
b81b63ae sago007 2016-10-03 20:30 96
	// If the 6th parameter is 0 then WideCharToMultiByte returns the number of bytes needed to store the result.
3e5ce8ff sum01 2018-04-16 21:01 97
	int actualSize = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, nullptr, 0, nullptr, nullptr);
b81b63ae sago007 2016-10-03 20:30 98
	if (actualSize > 0) {
b81b63ae sago007 2016-10-03 20:30 99
		//If the converted UTF-8 string could not be in the initial buffer. Allocate one that can hold it.
b81b63ae sago007 2016-10-03 20:30 100
		std::vector<char> buffer(actualSize);
db2a9b62 Domen Mori 2019-05-14 19:24 101
		actualSize = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, &buffer[0], static_cast<int>(buffer.size()), nullptr, nullptr);
b81b63ae sago007 2016-10-03 20:30 102
		res = buffer.data();
b81b63ae sago007 2016-10-03 20:30 103
	}
b81b63ae sago007 2016-10-03 20:30 104
	if (actualSize == 0) {
b81b63ae sago007 2016-10-03 20:30 105
		// WideCharToMultiByte return 0 for errors.
7c06d04d Firas Assaad 2020-09-29 21:59 106
		throw std::runtime_error("UTF16 to UTF8 failed with error code: " + std::to_string(GetLastError()));
b81b63ae sago007 2016-10-03 20:30 107
	}
b81b63ae sago007 2016-10-03 20:30 108
	return res;
b81b63ae sago007 2016-10-03 20:30 109
}
b81b63ae sago007 2016-10-03 20:30 110
ed746269 sago007 2019-03-03 13:08 111
}  // namesapce internal
ed746269 sago007 2019-03-03 13:08 112
}  // namespace sago
ed746269 sago007 2019-03-03 13:08 113
513c0ebf Poul Sander 2018-06-06 17:05 114
class FreeCoTaskMemory {
513c0ebf Poul Sander 2018-06-06 17:05 115
	LPWSTR pointer = NULL;
513c0ebf Poul Sander 2018-06-06 17:05 116
public:
0b976f51 Poul Sander 2018-06-06 17:31 117
	explicit FreeCoTaskMemory(LPWSTR pointer) : pointer(pointer) {};
513c0ebf Poul Sander 2018-06-06 17:05 118
	~FreeCoTaskMemory() {
513c0ebf Poul Sander 2018-06-06 17:05 119
		CoTaskMemFree(pointer);
513c0ebf Poul Sander 2018-06-06 17:05 120
	}
513c0ebf Poul Sander 2018-06-06 17:05 121
};
513c0ebf Poul Sander 2018-06-06 17:05 122
513c0ebf Poul Sander 2018-06-06 17:05 123
static std::string GetKnownWindowsFolder(REFKNOWNFOLDERID folderId, const char* errorMsg) {
513c0ebf Poul Sander 2018-06-06 17:05 124
	LPWSTR wszPath = NULL;
513c0ebf Poul Sander 2018-06-06 17:05 125
	HRESULT hr;
513c0ebf Poul Sander 2018-06-06 17:05 126
	hr = SHGetKnownFolderPath(folderId, KF_FLAG_CREATE, NULL, &wszPath);
513c0ebf Poul Sander 2018-06-06 17:05 127
	FreeCoTaskMemory scopeBoundMemory(wszPath);
513c0ebf Poul Sander 2018-06-06 17:05 128
513c0ebf Poul Sander 2018-06-06 17:05 129
	if (!SUCCEEDED(hr)) {
1213f3f3 sago007 2015-09-19 19:24 130
		throw std::runtime_error(errorMsg);
1213f3f3 sago007 2015-09-19 19:24 131
	}
ed746269 sago007 2019-03-03 13:08 132
	return sago::internal::win32_utf16_to_utf8(wszPath);
1213f3f3 sago007 2015-09-19 19:24 133
}
1213f3f3 sago007 2015-09-19 19:24 134
1213f3f3 sago007 2015-09-19 19:24 135
static std::string GetAppData() {
513c0ebf Poul Sander 2018-06-06 17:05 136
	return GetKnownWindowsFolder(FOLDERID_RoamingAppData, "RoamingAppData could not be found");
1213f3f3 sago007 2015-09-19 19:24 137
}
1213f3f3 sago007 2015-09-19 19:24 138
1213f3f3 sago007 2015-09-19 19:24 139
static std::string GetAppDataCommon() {
513c0ebf Poul Sander 2018-06-06 17:05 140
	return GetKnownWindowsFolder(FOLDERID_ProgramData, "ProgramData could not be found");
1213f3f3 sago007 2015-09-19 19:24 141
}
1213f3f3 sago007 2015-09-19 19:24 142
1213f3f3 sago007 2015-09-19 19:24 143
static std::string GetAppDataLocal() {
513c0ebf Poul Sander 2018-06-06 17:05 144
	return GetKnownWindowsFolder(FOLDERID_LocalAppData, "LocalAppData could not be found");
1213f3f3 sago007 2015-09-19 19:24 145
}
1eaaab72 sago007 2015-10-26 17:23 146
#elif defined(__APPLE__)
1213f3f3 sago007 2015-09-19 19:24 147
#else
1213f3f3 sago007 2015-09-19 19:24 148
#include <map>
1213f3f3 sago007 2015-09-19 19:24 149
#include <fstream>
dcb0fec2 sago007 2015-09-21 17:50 150
#include <sys/types.h>
a3a13ec8 sum01 2018-04-16 21:07 151
// For strlen and strtok
68df0922 sum01 2018-04-16 21:07 152
#include <cstring>
2a74df65 Poul Sander 2019-02-13 16:51 153
#include <sstream>
1213f3f3 sago007 2015-09-19 19:24 154
//Typically Linux. For easy reading the comments will just say Linux but should work with most *nixes
1213f3f3 sago007 2015-09-19 19:24 155
1213f3f3 sago007 2015-09-19 19:24 156
static void throwOnRelative(const char* envName, const char* envValue) {
1213f3f3 sago007 2015-09-19 19:24 157
	if (envValue[0] != '/') {
1213f3f3 sago007 2015-09-19 19:24 158
		char buffer[200];
da123945 sum01 2018-04-16 20:58 159
		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);
1213f3f3 sago007 2015-09-19 19:24 160
		throw std::runtime_error(buffer);
1213f3f3 sago007 2015-09-19 19:24 161
	}
1213f3f3 sago007 2015-09-19 19:24 162
}
1213f3f3 sago007 2015-09-19 19:24 163
5612f69b sago007 2018-06-16 11:22 164
1213f3f3 sago007 2015-09-19 19:24 165
1213f3f3 sago007 2015-09-19 19:24 166
static std::string getLinuxFolderDefault(const char* envName, const char* defaultRelativePath) {
1213f3f3 sago007 2015-09-19 19:24 167
	std::string res;
68df0922 sum01 2018-04-16 21:07 168
	const char* tempRes = std::getenv(envName);
1213f3f3 sago007 2015-09-19 19:24 169
	if (tempRes) {
1213f3f3 sago007 2015-09-19 19:24 170
		throwOnRelative(envName, tempRes);
1213f3f3 sago007 2015-09-19 19:24 171
		res = tempRes;
1213f3f3 sago007 2015-09-19 19:24 172
		return res;
1213f3f3 sago007 2015-09-19 19:24 173
	}
1213f3f3 sago007 2015-09-19 19:24 174
	res = getHome() + "/" + defaultRelativePath;
1213f3f3 sago007 2015-09-19 19:24 175
	return res;
1213f3f3 sago007 2015-09-19 19:24 176
}
1213f3f3 sago007 2015-09-19 19:24 177
1213f3f3 sago007 2015-09-19 19:24 178
static void appendExtraFolders(const char* envName, const char* defaultValue, std::vector<std::string>& folders) {
68df0922 sum01 2018-04-16 21:07 179
	const char* envValue = std::getenv(envName);
1213f3f3 sago007 2015-09-19 19:24 180
	if (!envValue) {
1213f3f3 sago007 2015-09-19 19:24 181
		envValue = defaultValue;
1213f3f3 sago007 2015-09-19 19:24 182
	}
df5fd138 Poul Sander 2019-02-13 16:35 183
	sago::internal::appendExtraFoldersTokenizer(envName, envValue, folders);
1213f3f3 sago007 2015-09-19 19:24 184
}
1213f3f3 sago007 2015-09-19 19:24 185
1213f3f3 sago007 2015-09-19 19:24 186
#endif
1213f3f3 sago007 2015-09-19 19:24 187
1213f3f3 sago007 2015-09-19 19:24 188
1213f3f3 sago007 2015-09-19 19:24 189
namespace sago {
1213f3f3 sago007 2015-09-19 19:24 190
df5fd138 Poul Sander 2019-02-13 16:35 191
#if !defined(_WIN32) && !defined(__APPLE__)
df5fd138 Poul Sander 2019-02-13 16:35 192
namespace internal {
c7c13350 sum01 2019-03-06 18:59 193
void appendExtraFoldersTokenizer(const char* envName, const char* envValue, std::vector<std::string>& folders) {
c7c13350 sum01 2019-03-06 18:59 194
	std::stringstream ss(envValue);
c7c13350 sum01 2019-03-06 18:59 195
	std::string value;
c7c13350 sum01 2019-03-06 18:59 196
	while (std::getline(ss, value, ':')) {
c7c13350 sum01 2019-03-06 18:59 197
		if (value[0] == '/') {
c7c13350 sum01 2019-03-06 18:59 198
			folders.push_back(value);
c7c13350 sum01 2019-03-06 18:59 199
		}
c7c13350 sum01 2019-03-06 18:59 200
		else {
c7c13350 sum01 2019-03-06 18:59 201
			//Unless the system is wrongly configured this should never happen... But of course some systems will be incorectly configured.
c7c13350 sum01 2019-03-06 18:59 202
			//The XDG documentation indicates that the folder should be ignored but that the program should continue.
c7c13350 sum01 2019-03-06 18:59 203
			std::cerr << "Skipping path \"" << value << "\" in \"" << envName << "\" because it does not start with a \"/\"\n";
df5fd138 Poul Sander 2019-02-13 16:35 204
		}
df5fd138 Poul Sander 2019-02-13 16:35 205
	}
df5fd138 Poul Sander 2019-02-13 16:35 206
}
c7c13350 sum01 2019-03-06 18:59 207
}
df5fd138 Poul Sander 2019-02-13 16:35 208
#endif
df5fd138 Poul Sander 2019-02-13 16:35 209
1213f3f3 sago007 2015-09-19 19:24 210
std::string getDataHome() {
e5876621 sum01 2018-04-21 23:00 211
#ifdef _WIN32
1213f3f3 sago007 2015-09-19 19:24 212
	return GetAppData();
1eaaab72 sago007 2015-10-26 17:23 213
#elif defined(__APPLE__)
51c64065 Poul Sander 2018-06-24 10:58 214
	return getHome()+"/Library/Application Support";
1213f3f3 sago007 2015-09-19 19:24 215
#else
1213f3f3 sago007 2015-09-19 19:24 216
	return getLinuxFolderDefault("XDG_DATA_HOME", ".local/share");
1213f3f3 sago007 2015-09-19 19:24 217
#endif
1213f3f3 sago007 2015-09-19 19:24 218
}
1213f3f3 sago007 2015-09-19 19:24 219
1213f3f3 sago007 2015-09-19 19:24 220
std::string getConfigHome() {
e5876621 sum01 2018-04-21 23:00 221
#ifdef _WIN32
1213f3f3 sago007 2015-09-19 19:24 222
	return GetAppData();
1eaaab72 sago007 2015-10-26 17:23 223
#elif defined(__APPLE__)
51c64065 Poul Sander 2018-06-24 10:58 224
	return getHome()+"/Library/Application Support";
1213f3f3 sago007 2015-09-19 19:24 225
#else
1213f3f3 sago007 2015-09-19 19:24 226
	return getLinuxFolderDefault("XDG_CONFIG_HOME", ".config");
1213f3f3 sago007 2015-09-19 19:24 227
#endif
1213f3f3 sago007 2015-09-19 19:24 228
}
1213f3f3 sago007 2015-09-19 19:24 229
1213f3f3 sago007 2015-09-19 19:24 230
std::string getCacheDir() {
e5876621 sum01 2018-04-21 23:00 231
#ifdef _WIN32
1213f3f3 sago007 2015-09-19 19:24 232
	return GetAppDataLocal();
1eaaab72 sago007 2015-10-26 17:23 233
#elif defined(__APPLE__)
51c64065 Poul Sander 2018-06-24 10:58 234
	return getHome()+"/Library/Caches";
1213f3f3 sago007 2015-09-19 19:24 235
#else
cc1fdd3e Kogia-sima 2018-05-25 04:26 236
	return getLinuxFolderDefault("XDG_CACHE_HOME", ".cache");
1213f3f3 sago007 2015-09-19 19:24 237
#endif
1213f3f3 sago007 2015-09-19 19:24 238
}
1213f3f3 sago007 2015-09-19 19:24 239
1213f3f3 sago007 2015-09-19 19:24 240
void appendAdditionalDataDirectories(std::vector<std::string>& homes) {
e5876621 sum01 2018-04-21 23:00 241
#ifdef _WIN32
1213f3f3 sago007 2015-09-19 19:24 242
	homes.push_back(GetAppDataCommon());
b3e5a881 sum01 2018-04-21 22:53 243
#elif !defined(__APPLE__)
1213f3f3 sago007 2015-09-19 19:24 244
	appendExtraFolders("XDG_DATA_DIRS", "/usr/local/share/:/usr/share/", homes);
1213f3f3 sago007 2015-09-19 19:24 245
#endif
1213f3f3 sago007 2015-09-19 19:24 246
}
1213f3f3 sago007 2015-09-19 19:24 247
1213f3f3 sago007 2015-09-19 19:24 248
void appendAdditionalConfigDirectories(std::vector<std::string>& homes) {
e5876621 sum01 2018-04-21 23:00 249
#ifdef _WIN32
1213f3f3 sago007 2015-09-19 19:24 250
	homes.push_back(GetAppDataCommon());
b3e5a881 sum01 2018-04-21 22:53 251
#elif !defined(__APPLE__)
1213f3f3 sago007 2015-09-19 19:24 252
	appendExtraFolders("XDG_CONFIG_DIRS", "/etc/xdg", homes);
1213f3f3 sago007 2015-09-19 19:24 253
#endif
1213f3f3 sago007 2015-09-19 19:24 254
}
1213f3f3 sago007 2015-09-19 19:24 255
b3e5a881 sum01 2018-04-21 22:53 256
#if !defined(_WIN32) && !defined(__APPLE__)
1213f3f3 sago007 2015-09-19 19:24 257
struct PlatformFolders::PlatformFoldersData {
1213f3f3 sago007 2015-09-19 19:24 258
	std::map<std::string, std::string> folders;
1213f3f3 sago007 2015-09-19 19:24 259
};
1213f3f3 sago007 2015-09-19 19:24 260
1213f3f3 sago007 2015-09-19 19:24 261
static void PlatformFoldersAddFromFile(const std::string& filename, std::map<std::string, std::string>& folders) {
1213f3f3 sago007 2015-09-19 19:24 262
	std::ifstream infile(filename.c_str());
1213f3f3 sago007 2015-09-19 19:24 263
	std::string line;
1213f3f3 sago007 2015-09-19 19:24 264
	while (std::getline(infile, line)) {
0823b4a5 sago007 2018-06-11 16:33 265
		if (line.length() == 0 || line.at(0) == '#' || line.substr(0, 4) != "XDG_" || line.find("_DIR") == std::string::npos) {
0823b4a5 sago007 2018-06-11 16:33 266
			continue;
0823b4a5 sago007 2018-06-11 16:33 267
		}
0823b4a5 sago007 2018-06-11 16:33 268
		try {
0823b4a5 sago007 2018-06-11 16:33 269
			std::size_t splitPos = line.find('=');
0823b4a5 sago007 2018-06-11 16:33 270
			std::string key = line.substr(0, splitPos);
0823b4a5 sago007 2018-06-11 16:33 271
			std::size_t valueStart = line.find('"', splitPos);
0823b4a5 sago007 2018-06-11 16:33 272
			std::size_t valueEnd = line.find('"', valueStart+1);
0823b4a5 sago007 2018-06-11 16:33 273
			std::string value = line.substr(valueStart+1, valueEnd - valueStart - 1);
0823b4a5 sago007 2018-06-11 16:33 274
			folders[key] = value;
c7c13350 sum01 2019-03-06 18:59 275
		}
c7c13350 sum01 2019-03-06 18:59 276
		catch (std::exception&  e) {
0823b4a5 sago007 2018-06-11 16:33 277
			std::cerr << "WARNING: Failed to process \"" << line << "\" from \"" << filename << "\". Error: "<< e.what() << "\n";
1213f3f3 sago007 2015-09-19 19:24 278
			continue;
1213f3f3 sago007 2015-09-19 19:24 279
		}
1213f3f3 sago007 2015-09-19 19:24 280
	}
1213f3f3 sago007 2015-09-19 19:24 281
}
1213f3f3 sago007 2015-09-19 19:24 282
1213f3f3 sago007 2015-09-19 19:24 283
static void PlatformFoldersFillData(std::map<std::string, std::string>& folders) {
1213f3f3 sago007 2015-09-19 19:24 284
	folders["XDG_DOCUMENTS_DIR"] = "$HOME/Documents";
1213f3f3 sago007 2015-09-19 19:24 285
	folders["XDG_DESKTOP_DIR"] = "$HOME/Desktop";
1213f3f3 sago007 2015-09-19 19:24 286
	folders["XDG_DOWNLOAD_DIR"] = "$HOME/Downloads";
1213f3f3 sago007 2015-09-19 19:24 287
	folders["XDG_MUSIC_DIR"] = "$HOME/Music";
1213f3f3 sago007 2015-09-19 19:24 288
	folders["XDG_PICTURES_DIR"] = "$HOME/Pictures";
1213f3f3 sago007 2015-09-19 19:24 289
	folders["XDG_PUBLICSHARE_DIR"] = "$HOME/Public";
1213f3f3 sago007 2015-09-19 19:24 290
	folders["XDG_TEMPLATES_DIR"] = "$HOME/.Templates";
1213f3f3 sago007 2015-09-19 19:24 291
	folders["XDG_VIDEOS_DIR"] = "$HOME/Videos";
1213f3f3 sago007 2015-09-19 19:24 292
	PlatformFoldersAddFromFile( getConfigHome()+"/user-dirs.dirs", folders);
1ff6644e sago007 2018-02-18 16:28 293
	for (std::map<std::string, std::string>::iterator itr = folders.begin() ; itr != folders.end() ; ++itr ) {
1213f3f3 sago007 2015-09-19 19:24 294
		std::string& value = itr->second;
1213f3f3 sago007 2015-09-19 19:24 295
		if (value.compare(0, 5, "$HOME") == 0) {
1213f3f3 sago007 2015-09-19 19:24 296
			value = getHome() + value.substr(5, std::string::npos);
1213f3f3 sago007 2015-09-19 19:24 297
		}
1213f3f3 sago007 2015-09-19 19:24 298
	}
1213f3f3 sago007 2015-09-19 19:24 299
}
1213f3f3 sago007 2015-09-19 19:24 300
#endif
1213f3f3 sago007 2015-09-19 19:24 301
1213f3f3 sago007 2015-09-19 19:24 302
PlatformFolders::PlatformFolders() {
b3e5a881 sum01 2018-04-21 22:53 303
#if !defined(_WIN32) && !defined(__APPLE__)
1213f3f3 sago007 2015-09-19 19:24 304
	this->data = new PlatformFolders::PlatformFoldersData();
1213f3f3 sago007 2015-09-19 19:24 305
	try {
1213f3f3 sago007 2015-09-19 19:24 306
		PlatformFoldersFillData(data->folders);
0fd23ef9 sago007 2018-05-22 17:49 307
	}
0fd23ef9 sago007 2018-05-22 17:49 308
	catch (...) {
1213f3f3 sago007 2015-09-19 19:24 309
		delete this->data;
1213f3f3 sago007 2015-09-19 19:24 310
		throw;
1213f3f3 sago007 2015-09-19 19:24 311
	}
1213f3f3 sago007 2015-09-19 19:24 312
#endif
1213f3f3 sago007 2015-09-19 19:24 313
}
1213f3f3 sago007 2015-09-19 19:24 314
1213f3f3 sago007 2015-09-19 19:24 315
PlatformFolders::~PlatformFolders() {
b3e5a881 sum01 2018-04-21 22:53 316
#if !defined(_WIN32) && !defined(__APPLE__)
1213f3f3 sago007 2015-09-19 19:24 317
	delete this->data;
1213f3f3 sago007 2015-09-19 19:24 318
#endif
1213f3f3 sago007 2015-09-19 19:24 319
}
1213f3f3 sago007 2015-09-19 19:24 320
1213f3f3 sago007 2015-09-19 19:24 321
std::string PlatformFolders::getDocumentsFolder() const {
e5876621 sum01 2018-04-21 23:00 322
#ifdef _WIN32
513c0ebf Poul Sander 2018-06-06 17:05 323
	return GetKnownWindowsFolder(FOLDERID_Documents, "Failed to find My Documents folder");
1eaaab72 sago007 2015-10-26 17:23 324
#elif defined(__APPLE__)
51c64065 Poul Sander 2018-06-24 10:58 325
	return getHome()+"/Documents";
1213f3f3 sago007 2015-09-19 19:24 326
#else
1213f3f3 sago007 2015-09-19 19:24 327
	return data->folders["XDG_DOCUMENTS_DIR"];
1213f3f3 sago007 2015-09-19 19:24 328
#endif
1213f3f3 sago007 2015-09-19 19:24 329
}
1213f3f3 sago007 2015-09-19 19:24 330
1213f3f3 sago007 2015-09-19 19:24 331
std::string PlatformFolders::getDesktopFolder() const {
e5876621 sum01 2018-04-21 23:00 332
#ifdef _WIN32
513c0ebf Poul Sander 2018-06-06 17:05 333
	return GetKnownWindowsFolder(FOLDERID_Desktop, "Failed to find Desktop folder");
1eaaab72 sago007 2015-10-26 17:23 334
#elif defined(__APPLE__)
51c64065 Poul Sander 2018-06-24 10:58 335
	return getHome()+"/Desktop";
1213f3f3 sago007 2015-09-19 19:24 336
#else
1213f3f3 sago007 2015-09-19 19:24 337
	return data->folders["XDG_DESKTOP_DIR"];
1213f3f3 sago007 2015-09-19 19:24 338
#endif
1213f3f3 sago007 2015-09-19 19:24 339
}
1213f3f3 sago007 2015-09-19 19:24 340
1213f3f3 sago007 2015-09-19 19:24 341
std::string PlatformFolders::getPicturesFolder() const {
e5876621 sum01 2018-04-21 23:00 342
#ifdef _WIN32
513c0ebf Poul Sander 2018-06-06 17:05 343
	return GetKnownWindowsFolder(FOLDERID_Pictures, "Failed to find My Pictures folder");
1eaaab72 sago007 2015-10-26 17:23 344
#elif defined(__APPLE__)
51c64065 Poul Sander 2018-06-24 10:58 345
	return getHome()+"/Pictures";
1213f3f3 sago007 2015-09-19 19:24 346
#else
1213f3f3 sago007 2015-09-19 19:24 347
	return data->folders["XDG_PICTURES_DIR"];
1213f3f3 sago007 2015-09-19 19:24 348
#endif
1213f3f3 sago007 2015-09-19 19:24 349
}
1213f3f3 sago007 2015-09-19 19:24 350
5612f69b sago007 2018-06-16 11:22 351
std::string PlatformFolders::getPublicFolder() const {
5612f69b sago007 2018-06-16 11:22 352
#ifdef _WIN32
5612f69b sago007 2018-06-16 11:22 353
	return GetKnownWindowsFolder(FOLDERID_Public, "Failed to find the Public folder");
5612f69b sago007 2018-06-16 11:22 354
#elif defined(__APPLE__)
5612f69b sago007 2018-06-16 11:22 355
	return getHome()+"/Public";
5612f69b sago007 2018-06-16 11:22 356
#else
5612f69b sago007 2018-06-16 11:22 357
	return data->folders["XDG_PUBLICSHARE_DIR"];
5612f69b sago007 2018-06-16 11:22 358
#endif
5612f69b sago007 2018-06-16 11:22 359
}
5612f69b sago007 2018-06-16 11:22 360
1213f3f3 sago007 2015-09-19 19:24 361
std::string PlatformFolders::getDownloadFolder1() const {
e5876621 sum01 2018-04-21 23:00 362
#ifdef _WIN32
513c0ebf Poul Sander 2018-06-06 17:05 363
	return GetKnownWindowsFolder(FOLDERID_Downloads, "Failed to find My Downloads folder");
1eaaab72 sago007 2015-10-26 17:23 364
#elif defined(__APPLE__)
51c64065 Poul Sander 2018-06-24 10:58 365
	return getHome()+"/Downloads";
1213f3f3 sago007 2015-09-19 19:24 366
#else
1213f3f3 sago007 2015-09-19 19:24 367
	return data->folders["XDG_DOWNLOAD_DIR"];
1213f3f3 sago007 2015-09-19 19:24 368
#endif
1213f3f3 sago007 2015-09-19 19:24 369
}
1213f3f3 sago007 2015-09-19 19:24 370
1213f3f3 sago007 2015-09-19 19:24 371
std::string PlatformFolders::getMusicFolder() const {
e5876621 sum01 2018-04-21 23:00 372
#ifdef _WIN32
513c0ebf Poul Sander 2018-06-06 17:05 373
	return GetKnownWindowsFolder(FOLDERID_Music, "Failed to find My Music folder");
1eaaab72 sago007 2015-10-26 17:23 374
#elif defined(__APPLE__)
51c64065 Poul Sander 2018-06-24 10:58 375
	return getHome()+"/Music";
1213f3f3 sago007 2015-09-19 19:24 376
#else
1213f3f3 sago007 2015-09-19 19:24 377
	return data->folders["XDG_MUSIC_DIR"];
1213f3f3 sago007 2015-09-19 19:24 378
#endif
1213f3f3 sago007 2015-09-19 19:24 379
}
1213f3f3 sago007 2015-09-19 19:24 380
1213f3f3 sago007 2015-09-19 19:24 381
std::string PlatformFolders::getVideoFolder() const {
e5876621 sum01 2018-04-21 23:00 382
#ifdef _WIN32
513c0ebf Poul Sander 2018-06-06 17:05 383
	return GetKnownWindowsFolder(FOLDERID_Videos, "Failed to find My Video folder");
1eaaab72 sago007 2015-10-26 17:23 384
#elif defined(__APPLE__)
51c64065 Poul Sander 2018-06-24 10:58 385
	return getHome()+"/Movies";
1213f3f3 sago007 2015-09-19 19:24 386
#else
1213f3f3 sago007 2015-09-19 19:24 387
	return data->folders["XDG_VIDEOS_DIR"];
1213f3f3 sago007 2015-09-19 19:24 388
#endif
1213f3f3 sago007 2015-09-19 19:24 389
}
1213f3f3 sago007 2015-09-19 19:24 390
1213f3f3 sago007 2015-09-19 19:24 391
std::string PlatformFolders::getSaveGamesFolder1() const {
e5876621 sum01 2018-04-21 23:00 392
#ifdef _WIN32
1213f3f3 sago007 2015-09-19 19:24 393
	//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".
1213f3f3 sago007 2015-09-19 19:24 394
	//Data that should not be user accessible should be placed under GetDataHome() instead
513c0ebf Poul Sander 2018-06-06 17:05 395
	return GetKnownWindowsFolder(FOLDERID_Documents, "Failed to find My Documents folder")+"\\My Games";
1eaaab72 sago007 2015-10-26 17:23 396
#elif defined(__APPLE__)
51c64065 Poul Sander 2018-06-24 10:58 397
	return getHome()+"/Library/Application Support";
1213f3f3 sago007 2015-09-19 19:24 398
#else
1213f3f3 sago007 2015-09-19 19:24 399
	return getDataHome();
1213f3f3 sago007 2015-09-19 19:24 400
#endif
1213f3f3 sago007 2015-09-19 19:24 401
}
1213f3f3 sago007 2015-09-19 19:24 402
0fd23ef9 sago007 2018-05-22 17:49 403
std::string getDesktopFolder() {
0fd23ef9 sago007 2018-05-22 17:49 404
	return PlatformFolders().getDesktopFolder();
0fd23ef9 sago007 2018-05-22 17:49 405
}
0fd23ef9 sago007 2018-05-22 17:49 406
0fd23ef9 sago007 2018-05-22 17:49 407
std::string getDocumentsFolder() {
0fd23ef9 sago007 2018-05-22 17:49 408
	return PlatformFolders().getDocumentsFolder();
0fd23ef9 sago007 2018-05-22 17:49 409
}
0fd23ef9 sago007 2018-05-22 17:49 410
513c0ebf Poul Sander 2018-06-06 17:05 411
std::string getDownloadFolder() {
0fd23ef9 sago007 2018-05-22 17:49 412
	return PlatformFolders().getDownloadFolder1();
0fd23ef9 sago007 2018-05-22 17:49 413
}
0fd23ef9 sago007 2018-05-22 17:49 414
513c0ebf Poul Sander 2018-06-06 17:05 415
std::string getDownloadFolder1() {
513c0ebf Poul Sander 2018-06-06 17:05 416
	return getDownloadFolder();
513c0ebf Poul Sander 2018-06-06 17:05 417
}
513c0ebf Poul Sander 2018-06-06 17:05 418
0fd23ef9 sago007 2018-05-22 17:49 419
std::string getPicturesFolder() {
0fd23ef9 sago007 2018-05-22 17:49 420
	return PlatformFolders().getPicturesFolder();
0fd23ef9 sago007 2018-05-22 17:49 421
}
0fd23ef9 sago007 2018-05-22 17:49 422
5612f69b sago007 2018-06-16 11:22 423
std::string getPublicFolder() {
5612f69b sago007 2018-06-16 11:22 424
	return PlatformFolders().getPublicFolder();
5612f69b sago007 2018-06-16 11:22 425
}
5612f69b sago007 2018-06-16 11:22 426
0fd23ef9 sago007 2018-05-22 17:49 427
std::string getMusicFolder() {
0fd23ef9 sago007 2018-05-22 17:49 428
	return PlatformFolders().getMusicFolder();
0fd23ef9 sago007 2018-05-22 17:49 429
}
0fd23ef9 sago007 2018-05-22 17:49 430
0fd23ef9 sago007 2018-05-22 17:49 431
std::string getVideoFolder() {
0fd23ef9 sago007 2018-05-22 17:49 432
	return PlatformFolders().getVideoFolder();
0fd23ef9 sago007 2018-05-22 17:49 433
}
0fd23ef9 sago007 2018-05-22 17:49 434
0fd23ef9 sago007 2018-05-22 17:49 435
std::string getSaveGamesFolder1() {
0fd23ef9 sago007 2018-05-22 17:49 436
	return PlatformFolders().getSaveGamesFolder1();
0fd23ef9 sago007 2018-05-22 17:49 437
}
1213f3f3 sago007 2015-09-19 19:24 438
513c0ebf Poul Sander 2018-06-06 17:05 439
std::string getSaveGamesFolder2() {
513c0ebf Poul Sander 2018-06-06 17:05 440
#ifdef _WIN32
513c0ebf Poul Sander 2018-06-06 17:05 441
	return GetKnownWindowsFolder(FOLDERID_SavedGames, "Failed to find Saved Games folder");
513c0ebf Poul Sander 2018-06-06 17:05 442
#else
513c0ebf Poul Sander 2018-06-06 17:05 443
	return PlatformFolders().getSaveGamesFolder1();
513c0ebf Poul Sander 2018-06-06 17:05 444
#endif
513c0ebf Poul Sander 2018-06-06 17:05 445
}
1213f3f3 sago007 2015-09-19 19:24 446
1213f3f3 sago007 2015-09-19 19:24 447
}  //namespace sago
1970-01-01 00:00 448