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.
a3a13ec8 sum01 2018-04-16 21:07 106
		const std::string errorMsg = "UTF16 to UTF8 failed with error code: " + GetLastError();
b81b63ae sago007 2016-10-03 20:30 107
		throw std::runtime_error(errorMsg.c_str());
b81b63ae sago007 2016-10-03 20:30 108
	}
b81b63ae sago007 2016-10-03 20:30 109
	return res;
b81b63ae sago007 2016-10-03 20:30 110
}
b81b63ae sago007 2016-10-03 20:30 111
ed746269 sago007 2019-03-03 13:08 112
}  // namesapce internal
ed746269 sago007 2019-03-03 13:08 113
}  // namespace sago
ed746269 sago007 2019-03-03 13:08 114
513c0ebf Poul Sander 2018-06-06 17:05 115
class FreeCoTaskMemory {
513c0ebf Poul Sander 2018-06-06 17:05 116
	LPWSTR pointer = NULL;
513c0ebf Poul Sander 2018-06-06 17:05 117
public:
0b976f51 Poul Sander 2018-06-06 17:31 118
	explicit FreeCoTaskMemory(LPWSTR pointer) : pointer(pointer) {};
513c0ebf Poul Sander 2018-06-06 17:05 119
	~FreeCoTaskMemory() {
513c0ebf Poul Sander 2018-06-06 17:05 120
		CoTaskMemFree(pointer);
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
513c0ebf Poul Sander 2018-06-06 17:05 124
static std::string GetKnownWindowsFolder(REFKNOWNFOLDERID folderId, const char* errorMsg) {
513c0ebf Poul Sander 2018-06-06 17:05 125
	LPWSTR wszPath = NULL;
513c0ebf Poul Sander 2018-06-06 17:05 126
	HRESULT hr;
513c0ebf Poul Sander 2018-06-06 17:05 127
	hr = SHGetKnownFolderPath(folderId, KF_FLAG_CREATE, NULL, &wszPath);
513c0ebf Poul Sander 2018-06-06 17:05 128
	FreeCoTaskMemory scopeBoundMemory(wszPath);
513c0ebf Poul Sander 2018-06-06 17:05 129
513c0ebf Poul Sander 2018-06-06 17:05 130
	if (!SUCCEEDED(hr)) {
1213f3f3 sago007 2015-09-19 19:24 131
		throw std::runtime_error(errorMsg);
1213f3f3 sago007 2015-09-19 19:24 132
	}
ed746269 sago007 2019-03-03 13:08 133
	return sago::internal::win32_utf16_to_utf8(wszPath);
1213f3f3 sago007 2015-09-19 19:24 134
}
1213f3f3 sago007 2015-09-19 19:24 135
1213f3f3 sago007 2015-09-19 19:24 136
static std::string GetAppData() {
513c0ebf Poul Sander 2018-06-06 17:05 137
	return GetKnownWindowsFolder(FOLDERID_RoamingAppData, "RoamingAppData could not be found");
1213f3f3 sago007 2015-09-19 19:24 138
}
1213f3f3 sago007 2015-09-19 19:24 139
1213f3f3 sago007 2015-09-19 19:24 140
static std::string GetAppDataCommon() {
513c0ebf Poul Sander 2018-06-06 17:05 141
	return GetKnownWindowsFolder(FOLDERID_ProgramData, "ProgramData could not be found");
1213f3f3 sago007 2015-09-19 19:24 142
}
1213f3f3 sago007 2015-09-19 19:24 143
1213f3f3 sago007 2015-09-19 19:24 144
static std::string GetAppDataLocal() {
513c0ebf Poul Sander 2018-06-06 17:05 145
	return GetKnownWindowsFolder(FOLDERID_LocalAppData, "LocalAppData could not be found");
1213f3f3 sago007 2015-09-19 19:24 146
}
1eaaab72 sago007 2015-10-26 17:23 147
#elif defined(__APPLE__)
1213f3f3 sago007 2015-09-19 19:24 148
#else
1213f3f3 sago007 2015-09-19 19:24 149
#include <map>
1213f3f3 sago007 2015-09-19 19:24 150
#include <fstream>
dcb0fec2 sago007 2015-09-21 17:50 151
#include <sys/types.h>
a3a13ec8 sum01 2018-04-16 21:07 152
// For strlen and strtok
68df0922 sum01 2018-04-16 21:07 153
#include <cstring>
2a74df65 Poul Sander 2019-02-13 16:51 154
#include <sstream>
1213f3f3 sago007 2015-09-19 19:24 155
//Typically Linux. For easy reading the comments will just say Linux but should work with most *nixes
1213f3f3 sago007 2015-09-19 19:24 156
1213f3f3 sago007 2015-09-19 19:24 157
static void throwOnRelative(const char* envName, const char* envValue) {
1213f3f3 sago007 2015-09-19 19:24 158
	if (envValue[0] != '/') {
1213f3f3 sago007 2015-09-19 19:24 159
		char buffer[200];
da123945 sum01 2018-04-16 20:58 160
		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 161
		throw std::runtime_error(buffer);
1213f3f3 sago007 2015-09-19 19:24 162
	}
1213f3f3 sago007 2015-09-19 19:24 163
}
1213f3f3 sago007 2015-09-19 19:24 164
5612f69b sago007 2018-06-16 11:22 165
1213f3f3 sago007 2015-09-19 19:24 166
1213f3f3 sago007 2015-09-19 19:24 167
static std::string getLinuxFolderDefault(const char* envName, const char* defaultRelativePath) {
1213f3f3 sago007 2015-09-19 19:24 168
	std::string res;
68df0922 sum01 2018-04-16 21:07 169
	const char* tempRes = std::getenv(envName);
1213f3f3 sago007 2015-09-19 19:24 170
	if (tempRes) {
1213f3f3 sago007 2015-09-19 19:24 171
		throwOnRelative(envName, tempRes);
1213f3f3 sago007 2015-09-19 19:24 172
		res = tempRes;
1213f3f3 sago007 2015-09-19 19:24 173
		return res;
1213f3f3 sago007 2015-09-19 19:24 174
	}
1213f3f3 sago007 2015-09-19 19:24 175
	res = getHome() + "/" + defaultRelativePath;
1213f3f3 sago007 2015-09-19 19:24 176
	return res;
1213f3f3 sago007 2015-09-19 19:24 177
}
1213f3f3 sago007 2015-09-19 19:24 178
1213f3f3 sago007 2015-09-19 19:24 179
static void appendExtraFolders(const char* envName, const char* defaultValue, std::vector<std::string>& folders) {
68df0922 sum01 2018-04-16 21:07 180
	const char* envValue = std::getenv(envName);
1213f3f3 sago007 2015-09-19 19:24 181
	if (!envValue) {
1213f3f3 sago007 2015-09-19 19:24 182
		envValue = defaultValue;
1213f3f3 sago007 2015-09-19 19:24 183
	}
df5fd138 Poul Sander 2019-02-13 16:35 184
	sago::internal::appendExtraFoldersTokenizer(envName, envValue, folders);
1213f3f3 sago007 2015-09-19 19:24 185
}
1213f3f3 sago007 2015-09-19 19:24 186
1213f3f3 sago007 2015-09-19 19:24 187
#endif
1213f3f3 sago007 2015-09-19 19:24 188
1213f3f3 sago007 2015-09-19 19:24 189
1213f3f3 sago007 2015-09-19 19:24 190
namespace sago {
1213f3f3 sago007 2015-09-19 19:24 191
df5fd138 Poul Sander 2019-02-13 16:35 192
#if !defined(_WIN32) && !defined(__APPLE__)
df5fd138 Poul Sander 2019-02-13 16:35 193
namespace internal {
c7c13350 sum01 2019-03-06 18:59 194
void appendExtraFoldersTokenizer(const char* envName, const char* envValue, std::vector<std::string>& folders) {
c7c13350 sum01 2019-03-06 18:59 195
	std::stringstream ss(envValue);
c7c13350 sum01 2019-03-06 18:59 196
	std::string value;
c7c13350 sum01 2019-03-06 18:59 197
	while (std::getline(ss, value, ':')) {
c7c13350 sum01 2019-03-06 18:59 198
		if (value[0] == '/') {
c7c13350 sum01 2019-03-06 18:59 199
			folders.push_back(value);
c7c13350 sum01 2019-03-06 18:59 200
		}
c7c13350 sum01 2019-03-06 18:59 201
		else {
c7c13350 sum01 2019-03-06 18:59 202
			//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 203
			//The XDG documentation indicates that the folder should be ignored but that the program should continue.
c7c13350 sum01 2019-03-06 18:59 204
			std::cerr << "Skipping path \"" << value << "\" in \"" << envName << "\" because it does not start with a \"/\"\n";
df5fd138 Poul Sander 2019-02-13 16:35 205
		}
df5fd138 Poul Sander 2019-02-13 16:35 206
	}
df5fd138 Poul Sander 2019-02-13 16:35 207
}
c7c13350 sum01 2019-03-06 18:59 208
}
df5fd138 Poul Sander 2019-02-13 16:35 209
#endif
df5fd138 Poul Sander 2019-02-13 16:35 210
1213f3f3 sago007 2015-09-19 19:24 211
std::string getDataHome() {
e5876621 sum01 2018-04-21 23:00 212
#ifdef _WIN32
1213f3f3 sago007 2015-09-19 19:24 213
	return GetAppData();
1eaaab72 sago007 2015-10-26 17:23 214
#elif defined(__APPLE__)
51c64065 Poul Sander 2018-06-24 10:58 215
	return getHome()+"/Library/Application Support";
1213f3f3 sago007 2015-09-19 19:24 216
#else
1213f3f3 sago007 2015-09-19 19:24 217
	return getLinuxFolderDefault("XDG_DATA_HOME", ".local/share");
1213f3f3 sago007 2015-09-19 19:24 218
#endif
1213f3f3 sago007 2015-09-19 19:24 219
}
1213f3f3 sago007 2015-09-19 19:24 220
1213f3f3 sago007 2015-09-19 19:24 221
std::string getConfigHome() {
e5876621 sum01 2018-04-21 23:00 222
#ifdef _WIN32
1213f3f3 sago007 2015-09-19 19:24 223
	return GetAppData();
1eaaab72 sago007 2015-10-26 17:23 224
#elif defined(__APPLE__)
51c64065 Poul Sander 2018-06-24 10:58 225
	return getHome()+"/Library/Application Support";
1213f3f3 sago007 2015-09-19 19:24 226
#else
1213f3f3 sago007 2015-09-19 19:24 227
	return getLinuxFolderDefault("XDG_CONFIG_HOME", ".config");
1213f3f3 sago007 2015-09-19 19:24 228
#endif
1213f3f3 sago007 2015-09-19 19:24 229
}
1213f3f3 sago007 2015-09-19 19:24 230
1213f3f3 sago007 2015-09-19 19:24 231
std::string getCacheDir() {
e5876621 sum01 2018-04-21 23:00 232
#ifdef _WIN32
1213f3f3 sago007 2015-09-19 19:24 233
	return GetAppDataLocal();
1eaaab72 sago007 2015-10-26 17:23 234
#elif defined(__APPLE__)
51c64065 Poul Sander 2018-06-24 10:58 235
	return getHome()+"/Library/Caches";
1213f3f3 sago007 2015-09-19 19:24 236
#else
cc1fdd3e Kogia-sima 2018-05-25 04:26 237
	return getLinuxFolderDefault("XDG_CACHE_HOME", ".cache");
1213f3f3 sago007 2015-09-19 19:24 238
#endif
1213f3f3 sago007 2015-09-19 19:24 239
}
1213f3f3 sago007 2015-09-19 19:24 240
1213f3f3 sago007 2015-09-19 19:24 241
void appendAdditionalDataDirectories(std::vector<std::string>& homes) {
e5876621 sum01 2018-04-21 23:00 242
#ifdef _WIN32
1213f3f3 sago007 2015-09-19 19:24 243
	homes.push_back(GetAppDataCommon());
b3e5a881 sum01 2018-04-21 22:53 244
#elif !defined(__APPLE__)
1213f3f3 sago007 2015-09-19 19:24 245
	appendExtraFolders("XDG_DATA_DIRS", "/usr/local/share/:/usr/share/", homes);
1213f3f3 sago007 2015-09-19 19:24 246
#endif
1213f3f3 sago007 2015-09-19 19:24 247
}
1213f3f3 sago007 2015-09-19 19:24 248
1213f3f3 sago007 2015-09-19 19:24 249
void appendAdditionalConfigDirectories(std::vector<std::string>& homes) {
e5876621 sum01 2018-04-21 23:00 250
#ifdef _WIN32
1213f3f3 sago007 2015-09-19 19:24 251
	homes.push_back(GetAppDataCommon());
b3e5a881 sum01 2018-04-21 22:53 252
#elif !defined(__APPLE__)
1213f3f3 sago007 2015-09-19 19:24 253
	appendExtraFolders("XDG_CONFIG_DIRS", "/etc/xdg", homes);
1213f3f3 sago007 2015-09-19 19:24 254
#endif
1213f3f3 sago007 2015-09-19 19:24 255
}
1213f3f3 sago007 2015-09-19 19:24 256
b3e5a881 sum01 2018-04-21 22:53 257
#if !defined(_WIN32) && !defined(__APPLE__)
1213f3f3 sago007 2015-09-19 19:24 258
struct PlatformFolders::PlatformFoldersData {
1213f3f3 sago007 2015-09-19 19:24 259
	std::map<std::string, std::string> folders;
1213f3f3 sago007 2015-09-19 19:24 260
};
1213f3f3 sago007 2015-09-19 19:24 261
1213f3f3 sago007 2015-09-19 19:24 262
static void PlatformFoldersAddFromFile(const std::string& filename, std::map<std::string, std::string>& folders) {
1213f3f3 sago007 2015-09-19 19:24 263
	std::ifstream infile(filename.c_str());
1213f3f3 sago007 2015-09-19 19:24 264
	std::string line;
1213f3f3 sago007 2015-09-19 19:24 265
	while (std::getline(infile, line)) {
0823b4a5 sago007 2018-06-11 16:33 266
		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 267
			continue;
0823b4a5 sago007 2018-06-11 16:33 268
		}
0823b4a5 sago007 2018-06-11 16:33 269
		try {
0823b4a5 sago007 2018-06-11 16:33 270
			std::size_t splitPos = line.find('=');
0823b4a5 sago007 2018-06-11 16:33 271
			std::string key = line.substr(0, splitPos);
0823b4a5 sago007 2018-06-11 16:33 272
			std::size_t valueStart = line.find('"', splitPos);
0823b4a5 sago007 2018-06-11 16:33 273
			std::size_t valueEnd = line.find('"', valueStart+1);
0823b4a5 sago007 2018-06-11 16:33 274
			std::string value = line.substr(valueStart+1, valueEnd - valueStart - 1);
0823b4a5 sago007 2018-06-11 16:33 275
			folders[key] = value;
c7c13350 sum01 2019-03-06 18:59 276
		}
c7c13350 sum01 2019-03-06 18:59 277
		catch (std::exception&  e) {
0823b4a5 sago007 2018-06-11 16:33 278
			std::cerr << "WARNING: Failed to process \"" << line << "\" from \"" << filename << "\". Error: "<< e.what() << "\n";
1213f3f3 sago007 2015-09-19 19:24 279
			continue;
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
1213f3f3 sago007 2015-09-19 19:24 284
static void PlatformFoldersFillData(std::map<std::string, std::string>& folders) {
1213f3f3 sago007 2015-09-19 19:24 285
	folders["XDG_DOCUMENTS_DIR"] = "$HOME/Documents";
1213f3f3 sago007 2015-09-19 19:24 286
	folders["XDG_DESKTOP_DIR"] = "$HOME/Desktop";
1213f3f3 sago007 2015-09-19 19:24 287
	folders["XDG_DOWNLOAD_DIR"] = "$HOME/Downloads";
1213f3f3 sago007 2015-09-19 19:24 288
	folders["XDG_MUSIC_DIR"] = "$HOME/Music";
1213f3f3 sago007 2015-09-19 19:24 289
	folders["XDG_PICTURES_DIR"] = "$HOME/Pictures";
1213f3f3 sago007 2015-09-19 19:24 290
	folders["XDG_PUBLICSHARE_DIR"] = "$HOME/Public";
1213f3f3 sago007 2015-09-19 19:24 291
	folders["XDG_TEMPLATES_DIR"] = "$HOME/.Templates";
1213f3f3 sago007 2015-09-19 19:24 292
	folders["XDG_VIDEOS_DIR"] = "$HOME/Videos";
1213f3f3 sago007 2015-09-19 19:24 293
	PlatformFoldersAddFromFile( getConfigHome()+"/user-dirs.dirs", folders);
1ff6644e sago007 2018-02-18 16:28 294
	for (std::map<std::string, std::string>::iterator itr = folders.begin() ; itr != folders.end() ; ++itr ) {
1213f3f3 sago007 2015-09-19 19:24 295
		std::string& value = itr->second;
1213f3f3 sago007 2015-09-19 19:24 296
		if (value.compare(0, 5, "$HOME") == 0) {
1213f3f3 sago007 2015-09-19 19:24 297
			value = getHome() + value.substr(5, std::string::npos);
1213f3f3 sago007 2015-09-19 19:24 298
		}
1213f3f3 sago007 2015-09-19 19:24 299
	}
1213f3f3 sago007 2015-09-19 19:24 300
}
1213f3f3 sago007 2015-09-19 19:24 301
#endif
1213f3f3 sago007 2015-09-19 19:24 302
1213f3f3 sago007 2015-09-19 19:24 303
PlatformFolders::PlatformFolders() {
b3e5a881 sum01 2018-04-21 22:53 304
#if !defined(_WIN32) && !defined(__APPLE__)
1213f3f3 sago007 2015-09-19 19:24 305
	this->data = new PlatformFolders::PlatformFoldersData();
1213f3f3 sago007 2015-09-19 19:24 306
	try {
1213f3f3 sago007 2015-09-19 19:24 307
		PlatformFoldersFillData(data->folders);
0fd23ef9 sago007 2018-05-22 17:49 308
	}
0fd23ef9 sago007 2018-05-22 17:49 309
	catch (...) {
1213f3f3 sago007 2015-09-19 19:24 310
		delete this->data;
1213f3f3 sago007 2015-09-19 19:24 311
		throw;
1213f3f3 sago007 2015-09-19 19:24 312
	}
1213f3f3 sago007 2015-09-19 19:24 313
#endif
1213f3f3 sago007 2015-09-19 19:24 314
}
1213f3f3 sago007 2015-09-19 19:24 315
1213f3f3 sago007 2015-09-19 19:24 316
PlatformFolders::~PlatformFolders() {
b3e5a881 sum01 2018-04-21 22:53 317
#if !defined(_WIN32) && !defined(__APPLE__)
1213f3f3 sago007 2015-09-19 19:24 318
	delete this->data;
1213f3f3 sago007 2015-09-19 19:24 319
#endif
1213f3f3 sago007 2015-09-19 19:24 320
}
1213f3f3 sago007 2015-09-19 19:24 321
1213f3f3 sago007 2015-09-19 19:24 322
std::string PlatformFolders::getDocumentsFolder() const {
e5876621 sum01 2018-04-21 23:00 323
#ifdef _WIN32
513c0ebf Poul Sander 2018-06-06 17:05 324
	return GetKnownWindowsFolder(FOLDERID_Documents, "Failed to find My Documents folder");
1eaaab72 sago007 2015-10-26 17:23 325
#elif defined(__APPLE__)
51c64065 Poul Sander 2018-06-24 10:58 326
	return getHome()+"/Documents";
1213f3f3 sago007 2015-09-19 19:24 327
#else
1213f3f3 sago007 2015-09-19 19:24 328
	return data->folders["XDG_DOCUMENTS_DIR"];
1213f3f3 sago007 2015-09-19 19:24 329
#endif
1213f3f3 sago007 2015-09-19 19:24 330
}
1213f3f3 sago007 2015-09-19 19:24 331
1213f3f3 sago007 2015-09-19 19:24 332
std::string PlatformFolders::getDesktopFolder() const {
e5876621 sum01 2018-04-21 23:00 333
#ifdef _WIN32
513c0ebf Poul Sander 2018-06-06 17:05 334
	return GetKnownWindowsFolder(FOLDERID_Desktop, "Failed to find Desktop folder");
1eaaab72 sago007 2015-10-26 17:23 335
#elif defined(__APPLE__)
51c64065 Poul Sander 2018-06-24 10:58 336
	return getHome()+"/Desktop";
1213f3f3 sago007 2015-09-19 19:24 337
#else
1213f3f3 sago007 2015-09-19 19:24 338
	return data->folders["XDG_DESKTOP_DIR"];
1213f3f3 sago007 2015-09-19 19:24 339
#endif
1213f3f3 sago007 2015-09-19 19:24 340
}
1213f3f3 sago007 2015-09-19 19:24 341
1213f3f3 sago007 2015-09-19 19:24 342
std::string PlatformFolders::getPicturesFolder() const {
e5876621 sum01 2018-04-21 23:00 343
#ifdef _WIN32
513c0ebf Poul Sander 2018-06-06 17:05 344
	return GetKnownWindowsFolder(FOLDERID_Pictures, "Failed to find My Pictures folder");
1eaaab72 sago007 2015-10-26 17:23 345
#elif defined(__APPLE__)
51c64065 Poul Sander 2018-06-24 10:58 346
	return getHome()+"/Pictures";
1213f3f3 sago007 2015-09-19 19:24 347
#else
1213f3f3 sago007 2015-09-19 19:24 348
	return data->folders["XDG_PICTURES_DIR"];
1213f3f3 sago007 2015-09-19 19:24 349
#endif
1213f3f3 sago007 2015-09-19 19:24 350
}
1213f3f3 sago007 2015-09-19 19:24 351
5612f69b sago007 2018-06-16 11:22 352
std::string PlatformFolders::getPublicFolder() const {
5612f69b sago007 2018-06-16 11:22 353
#ifdef _WIN32
5612f69b sago007 2018-06-16 11:22 354
	return GetKnownWindowsFolder(FOLDERID_Public, "Failed to find the Public folder");
5612f69b sago007 2018-06-16 11:22 355
#elif defined(__APPLE__)
5612f69b sago007 2018-06-16 11:22 356
	return getHome()+"/Public";
5612f69b sago007 2018-06-16 11:22 357
#else
5612f69b sago007 2018-06-16 11:22 358
	return data->folders["XDG_PUBLICSHARE_DIR"];
5612f69b sago007 2018-06-16 11:22 359
#endif
5612f69b sago007 2018-06-16 11:22 360
}
5612f69b sago007 2018-06-16 11:22 361
1213f3f3 sago007 2015-09-19 19:24 362
std::string PlatformFolders::getDownloadFolder1() const {
e5876621 sum01 2018-04-21 23:00 363
#ifdef _WIN32
513c0ebf Poul Sander 2018-06-06 17:05 364
	return GetKnownWindowsFolder(FOLDERID_Downloads, "Failed to find My Downloads folder");
1eaaab72 sago007 2015-10-26 17:23 365
#elif defined(__APPLE__)
51c64065 Poul Sander 2018-06-24 10:58 366
	return getHome()+"/Downloads";
1213f3f3 sago007 2015-09-19 19:24 367
#else
1213f3f3 sago007 2015-09-19 19:24 368
	return data->folders["XDG_DOWNLOAD_DIR"];
1213f3f3 sago007 2015-09-19 19:24 369
#endif
1213f3f3 sago007 2015-09-19 19:24 370
}
1213f3f3 sago007 2015-09-19 19:24 371
1213f3f3 sago007 2015-09-19 19:24 372
std::string PlatformFolders::getMusicFolder() const {
e5876621 sum01 2018-04-21 23:00 373
#ifdef _WIN32
513c0ebf Poul Sander 2018-06-06 17:05 374
	return GetKnownWindowsFolder(FOLDERID_Music, "Failed to find My Music folder");
1eaaab72 sago007 2015-10-26 17:23 375
#elif defined(__APPLE__)
51c64065 Poul Sander 2018-06-24 10:58 376
	return getHome()+"/Music";
1213f3f3 sago007 2015-09-19 19:24 377
#else
1213f3f3 sago007 2015-09-19 19:24 378
	return data->folders["XDG_MUSIC_DIR"];
1213f3f3 sago007 2015-09-19 19:24 379
#endif
1213f3f3 sago007 2015-09-19 19:24 380
}
1213f3f3 sago007 2015-09-19 19:24 381
1213f3f3 sago007 2015-09-19 19:24 382
std::string PlatformFolders::getVideoFolder() const {
e5876621 sum01 2018-04-21 23:00 383
#ifdef _WIN32
513c0ebf Poul Sander 2018-06-06 17:05 384
	return GetKnownWindowsFolder(FOLDERID_Videos, "Failed to find My Video folder");
1eaaab72 sago007 2015-10-26 17:23 385
#elif defined(__APPLE__)
51c64065 Poul Sander 2018-06-24 10:58 386
	return getHome()+"/Movies";
1213f3f3 sago007 2015-09-19 19:24 387
#else
1213f3f3 sago007 2015-09-19 19:24 388
	return data->folders["XDG_VIDEOS_DIR"];
1213f3f3 sago007 2015-09-19 19:24 389
#endif
1213f3f3 sago007 2015-09-19 19:24 390
}
1213f3f3 sago007 2015-09-19 19:24 391
1213f3f3 sago007 2015-09-19 19:24 392
std::string PlatformFolders::getSaveGamesFolder1() const {
e5876621 sum01 2018-04-21 23:00 393
#ifdef _WIN32
1213f3f3 sago007 2015-09-19 19:24 394
	//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 395
	//Data that should not be user accessible should be placed under GetDataHome() instead
513c0ebf Poul Sander 2018-06-06 17:05 396
	return GetKnownWindowsFolder(FOLDERID_Documents, "Failed to find My Documents folder")+"\\My Games";
1eaaab72 sago007 2015-10-26 17:23 397
#elif defined(__APPLE__)
51c64065 Poul Sander 2018-06-24 10:58 398
	return getHome()+"/Library/Application Support";
1213f3f3 sago007 2015-09-19 19:24 399
#else
1213f3f3 sago007 2015-09-19 19:24 400
	return getDataHome();
1213f3f3 sago007 2015-09-19 19:24 401
#endif
1213f3f3 sago007 2015-09-19 19:24 402
}
1213f3f3 sago007 2015-09-19 19:24 403
0fd23ef9 sago007 2018-05-22 17:49 404
std::string getDesktopFolder() {
0fd23ef9 sago007 2018-05-22 17:49 405
	return PlatformFolders().getDesktopFolder();
0fd23ef9 sago007 2018-05-22 17:49 406
}
0fd23ef9 sago007 2018-05-22 17:49 407
0fd23ef9 sago007 2018-05-22 17:49 408
std::string getDocumentsFolder() {
0fd23ef9 sago007 2018-05-22 17:49 409
	return PlatformFolders().getDocumentsFolder();
0fd23ef9 sago007 2018-05-22 17:49 410
}
0fd23ef9 sago007 2018-05-22 17:49 411
513c0ebf Poul Sander 2018-06-06 17:05 412
std::string getDownloadFolder() {
0fd23ef9 sago007 2018-05-22 17:49 413
	return PlatformFolders().getDownloadFolder1();
0fd23ef9 sago007 2018-05-22 17:49 414
}
0fd23ef9 sago007 2018-05-22 17:49 415
513c0ebf Poul Sander 2018-06-06 17:05 416
std::string getDownloadFolder1() {
513c0ebf Poul Sander 2018-06-06 17:05 417
	return getDownloadFolder();
513c0ebf Poul Sander 2018-06-06 17:05 418
}
513c0ebf Poul Sander 2018-06-06 17:05 419
0fd23ef9 sago007 2018-05-22 17:49 420
std::string getPicturesFolder() {
0fd23ef9 sago007 2018-05-22 17:49 421
	return PlatformFolders().getPicturesFolder();
0fd23ef9 sago007 2018-05-22 17:49 422
}
0fd23ef9 sago007 2018-05-22 17:49 423
5612f69b sago007 2018-06-16 11:22 424
std::string getPublicFolder() {
5612f69b sago007 2018-06-16 11:22 425
	return PlatformFolders().getPublicFolder();
5612f69b sago007 2018-06-16 11:22 426
}
5612f69b sago007 2018-06-16 11:22 427
0fd23ef9 sago007 2018-05-22 17:49 428
std::string getMusicFolder() {
0fd23ef9 sago007 2018-05-22 17:49 429
	return PlatformFolders().getMusicFolder();
0fd23ef9 sago007 2018-05-22 17:49 430
}
0fd23ef9 sago007 2018-05-22 17:49 431
0fd23ef9 sago007 2018-05-22 17:49 432
std::string getVideoFolder() {
0fd23ef9 sago007 2018-05-22 17:49 433
	return PlatformFolders().getVideoFolder();
0fd23ef9 sago007 2018-05-22 17:49 434
}
0fd23ef9 sago007 2018-05-22 17:49 435
0fd23ef9 sago007 2018-05-22 17:49 436
std::string getSaveGamesFolder1() {
0fd23ef9 sago007 2018-05-22 17:49 437
	return PlatformFolders().getSaveGamesFolder1();
0fd23ef9 sago007 2018-05-22 17:49 438
}
1213f3f3 sago007 2015-09-19 19:24 439
513c0ebf Poul Sander 2018-06-06 17:05 440
std::string getSaveGamesFolder2() {
513c0ebf Poul Sander 2018-06-06 17:05 441
#ifdef _WIN32
513c0ebf Poul Sander 2018-06-06 17:05 442
	return GetKnownWindowsFolder(FOLDERID_SavedGames, "Failed to find Saved Games folder");
513c0ebf Poul Sander 2018-06-06 17:05 443
#else
513c0ebf Poul Sander 2018-06-06 17:05 444
	return PlatformFolders().getSaveGamesFolder1();
513c0ebf Poul Sander 2018-06-06 17:05 445
#endif
513c0ebf Poul Sander 2018-06-06 17:05 446
}
1213f3f3 sago007 2015-09-19 19:24 447
1213f3f3 sago007 2015-09-19 19:24 448
}  //namespace sago
1970-01-01 00:00 449