git repos / blockattack-game

blame: source/code/sago/platform_folders.cpp

normal view · raw

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>
0494de59 sago007 2018-06-03 19:32 32
#include <cstdio>
77531130 Poul Sander 2016-02-17 18:42 33
#include <cstdlib>
77531130 Poul Sander 2016-02-17 18:42 34
6b427a55 Poul Sander 2019-02-13 17:49 35
#ifndef _WIN32
6b427a55 Poul Sander 2019-02-13 17:49 36
6b427a55 Poul Sander 2019-02-13 17:49 37
#include <pwd.h>
6b427a55 Poul Sander 2019-02-13 17:49 38
#include <unistd.h>
6b427a55 Poul Sander 2019-02-13 17:49 39
6b427a55 Poul Sander 2019-02-13 17:49 40
/**
6b427a55 Poul Sander 2019-02-13 17:49 41
 * Retrives the effective user's home dir.
6b427a55 Poul Sander 2019-02-13 17:49 42
 * If the user is running as root we ignore the HOME environment. It works badly with sudo.
6b427a55 Poul Sander 2019-02-13 17:49 43
 * Writing to $HOME as root implies security concerns that a multiplatform program cannot be assumed to handle.
6b427a55 Poul Sander 2019-02-13 17:49 44
 * @return The home directory. HOME environment is respected for non-root users if it exists.
6b427a55 Poul Sander 2019-02-13 17:49 45
 */
6b427a55 Poul Sander 2019-02-13 17:49 46
static std::string getHome() {
6b427a55 Poul Sander 2019-02-13 17:49 47
	std::string res;
6b427a55 Poul Sander 2019-02-13 17:49 48
	int uid = getuid();
6b427a55 Poul Sander 2019-02-13 17:49 49
	const char* homeEnv = std::getenv("HOME");
6b427a55 Poul Sander 2019-02-13 17:49 50
	if ( uid != 0 && homeEnv) {
6b427a55 Poul Sander 2019-02-13 17:49 51
		//We only acknowlegde HOME if not root.
6b427a55 Poul Sander 2019-02-13 17:49 52
		res = homeEnv;
6b427a55 Poul Sander 2019-02-13 17:49 53
		return res;
6b427a55 Poul Sander 2019-02-13 17:49 54
	}
6b427a55 Poul Sander 2019-02-13 17:49 55
	struct passwd* pw = getpwuid(uid);
6b427a55 Poul Sander 2019-02-13 17:49 56
	if (!pw) {
6b427a55 Poul Sander 2019-02-13 17:49 57
		throw std::runtime_error("Unable to get passwd struct.");
6b427a55 Poul Sander 2019-02-13 17:49 58
	}
6b427a55 Poul Sander 2019-02-13 17:49 59
	const char* tempRes = pw->pw_dir;
6b427a55 Poul Sander 2019-02-13 17:49 60
	if (!tempRes) {
6b427a55 Poul Sander 2019-02-13 17:49 61
		throw std::runtime_error("User has no home directory");
6b427a55 Poul Sander 2019-02-13 17:49 62
	}
6b427a55 Poul Sander 2019-02-13 17:49 63
	res = tempRes;
6b427a55 Poul Sander 2019-02-13 17:49 64
	return res;
6b427a55 Poul Sander 2019-02-13 17:49 65
}
6b427a55 Poul Sander 2019-02-13 17:49 66
6b427a55 Poul Sander 2019-02-13 17:49 67
#endif 
6b427a55 Poul Sander 2019-02-13 17:49 68
6b427a55 Poul Sander 2019-02-13 17:49 69
#ifdef _WIN32
6b427a55 Poul Sander 2019-02-13 17:49 70
// Make sure we don't bring in all the extra junk with windows.h
6b427a55 Poul Sander 2019-02-13 17:49 71
#ifndef WIN32_LEAN_AND_MEAN
6b427a55 Poul Sander 2019-02-13 17:49 72
#define WIN32_LEAN_AND_MEAN
6b427a55 Poul Sander 2019-02-13 17:49 73
#endif
6b427a55 Poul Sander 2019-02-13 17:49 74
// stringapiset.h depends on this
77531130 Poul Sander 2016-02-17 18:42 75
#include <windows.h>
6b427a55 Poul Sander 2019-02-13 17:49 76
// For SUCCEEDED macro
6b427a55 Poul Sander 2019-02-13 17:49 77
#include <winerror.h>
6b427a55 Poul Sander 2019-02-13 17:49 78
// For WideCharToMultiByte
6b427a55 Poul Sander 2019-02-13 17:49 79
#include <stringapiset.h>
6b427a55 Poul Sander 2019-02-13 17:49 80
// For SHGetFolderPathW and various CSIDL "magic numbers"
77531130 Poul Sander 2016-02-17 18:42 81
#include <shlobj.h>
77531130 Poul Sander 2016-02-17 18:42 82
d50cee1b sago007 2019-03-02 14:50 83
namespace sago {
d50cee1b sago007 2019-03-02 14:50 84
namespace internal {
d50cee1b sago007 2019-03-02 14:50 85
d50cee1b sago007 2019-03-02 14:50 86
std::string win32_utf16_to_utf8(const wchar_t* wstr) {
f7573b59 sago007 2016-10-08 17:11 87
	std::string res;
f7573b59 sago007 2016-10-08 17:11 88
	// If the 6th parameter is 0 then WideCharToMultiByte returns the number of bytes needed to store the result.
6b427a55 Poul Sander 2019-02-13 17:49 89
	int actualSize = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, nullptr, 0, nullptr, nullptr);
f7573b59 sago007 2016-10-08 17:11 90
	if (actualSize > 0) {
f7573b59 sago007 2016-10-08 17:11 91
		//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 92
		std::vector<char> buffer(actualSize);
6b427a55 Poul Sander 2019-02-13 17:49 93
		actualSize = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, &buffer[0], buffer.size(), nullptr, nullptr);
f7573b59 sago007 2016-10-08 17:11 94
		res = buffer.data();
f7573b59 sago007 2016-10-08 17:11 95
	}
f7573b59 sago007 2016-10-08 17:11 96
	if (actualSize == 0) {
f7573b59 sago007 2016-10-08 17:11 97
		// WideCharToMultiByte return 0 for errors.
0494de59 sago007 2018-06-03 19:32 98
		const std::string errorMsg = "UTF16 to UTF8 failed with error code: " + GetLastError();
f7573b59 sago007 2016-10-08 17:11 99
		throw std::runtime_error(errorMsg.c_str());
f7573b59 sago007 2016-10-08 17:11 100
	}
f7573b59 sago007 2016-10-08 17:11 101
	return res;
f7573b59 sago007 2016-10-08 17:11 102
}
f7573b59 sago007 2016-10-08 17:11 103
d50cee1b sago007 2019-03-02 14:50 104
}  // namesapce internal
d50cee1b sago007 2019-03-02 14:50 105
}  // namespace sago
d50cee1b sago007 2019-03-02 14:50 106
6b427a55 Poul Sander 2019-02-13 17:49 107
class FreeCoTaskMemory {
6b427a55 Poul Sander 2019-02-13 17:49 108
	LPWSTR pointer = NULL;
6b427a55 Poul Sander 2019-02-13 17:49 109
public:
6b427a55 Poul Sander 2019-02-13 17:49 110
	explicit FreeCoTaskMemory(LPWSTR pointer) : pointer(pointer) {};
6b427a55 Poul Sander 2019-02-13 17:49 111
	~FreeCoTaskMemory() {
6b427a55 Poul Sander 2019-02-13 17:49 112
		CoTaskMemFree(pointer);
6b427a55 Poul Sander 2019-02-13 17:49 113
	}
6b427a55 Poul Sander 2019-02-13 17:49 114
};
6b427a55 Poul Sander 2019-02-13 17:49 115
6b427a55 Poul Sander 2019-02-13 17:49 116
static std::string GetKnownWindowsFolder(REFKNOWNFOLDERID folderId, const char* errorMsg) {
6b427a55 Poul Sander 2019-02-13 17:49 117
	LPWSTR wszPath = NULL;
6b427a55 Poul Sander 2019-02-13 17:49 118
	HRESULT hr;
6b427a55 Poul Sander 2019-02-13 17:49 119
	hr = SHGetKnownFolderPath(folderId, KF_FLAG_CREATE, NULL, &wszPath);
6b427a55 Poul Sander 2019-02-13 17:49 120
	FreeCoTaskMemory scopeBoundMemory(wszPath);
6b427a55 Poul Sander 2019-02-13 17:49 121
6b427a55 Poul Sander 2019-02-13 17:49 122
	if (!SUCCEEDED(hr)) {
77531130 Poul Sander 2016-02-17 18:42 123
		throw std::runtime_error(errorMsg);
77531130 Poul Sander 2016-02-17 18:42 124
	}
d50cee1b sago007 2019-03-02 14:50 125
	return sago::internal::win32_utf16_to_utf8(wszPath);
77531130 Poul Sander 2016-02-17 18:42 126
}
77531130 Poul Sander 2016-02-17 18:42 127
77531130 Poul Sander 2016-02-17 18:42 128
static std::string GetAppData() {
6b427a55 Poul Sander 2019-02-13 17:49 129
	return GetKnownWindowsFolder(FOLDERID_RoamingAppData, "RoamingAppData could not be found");
77531130 Poul Sander 2016-02-17 18:42 130
}
77531130 Poul Sander 2016-02-17 18:42 131
77531130 Poul Sander 2016-02-17 18:42 132
static std::string GetAppDataCommon() {
6b427a55 Poul Sander 2019-02-13 17:49 133
	return GetKnownWindowsFolder(FOLDERID_ProgramData, "ProgramData could not be found");
77531130 Poul Sander 2016-02-17 18:42 134
}
77531130 Poul Sander 2016-02-17 18:42 135
77531130 Poul Sander 2016-02-17 18:42 136
static std::string GetAppDataLocal() {
6b427a55 Poul Sander 2019-02-13 17:49 137
	return GetKnownWindowsFolder(FOLDERID_LocalAppData, "LocalAppData could not be found");
77531130 Poul Sander 2016-02-17 18:42 138
}
77531130 Poul Sander 2016-02-17 18:42 139
#elif defined(__APPLE__)
77531130 Poul Sander 2016-02-17 18:42 140
#else
77531130 Poul Sander 2016-02-17 18:42 141
#include <map>
77531130 Poul Sander 2016-02-17 18:42 142
#include <fstream>
77531130 Poul Sander 2016-02-17 18:42 143
#include <sys/types.h>
0494de59 sago007 2018-06-03 19:32 144
// For strlen and strtok
0494de59 sago007 2018-06-03 19:32 145
#include <cstring>
6b427a55 Poul Sander 2019-02-13 17:49 146
#include <sstream>
77531130 Poul Sander 2016-02-17 18:42 147
//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 148
77531130 Poul Sander 2016-02-17 18:42 149
static void throwOnRelative(const char* envName, const char* envValue) {
77531130 Poul Sander 2016-02-17 18:42 150
	if (envValue[0] != '/') {
77531130 Poul Sander 2016-02-17 18:42 151
		char buffer[200];
0494de59 sago007 2018-06-03 19:32 152
		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 153
		throw std::runtime_error(buffer);
77531130 Poul Sander 2016-02-17 18:42 154
	}
77531130 Poul Sander 2016-02-17 18:42 155
}
77531130 Poul Sander 2016-02-17 18:42 156
6b427a55 Poul Sander 2019-02-13 17:49 157
77531130 Poul Sander 2016-02-17 18:42 158
77531130 Poul Sander 2016-02-17 18:42 159
static std::string getLinuxFolderDefault(const char* envName, const char* defaultRelativePath) {
77531130 Poul Sander 2016-02-17 18:42 160
	std::string res;
0494de59 sago007 2018-06-03 19:32 161
	const char* tempRes = std::getenv(envName);
77531130 Poul Sander 2016-02-17 18:42 162
	if (tempRes) {
77531130 Poul Sander 2016-02-17 18:42 163
		throwOnRelative(envName, tempRes);
77531130 Poul Sander 2016-02-17 18:42 164
		res = tempRes;
77531130 Poul Sander 2016-02-17 18:42 165
		return res;
77531130 Poul Sander 2016-02-17 18:42 166
	}
77531130 Poul Sander 2016-02-17 18:42 167
	res = getHome() + "/" + defaultRelativePath;
77531130 Poul Sander 2016-02-17 18:42 168
	return res;
77531130 Poul Sander 2016-02-17 18:42 169
}
77531130 Poul Sander 2016-02-17 18:42 170
77531130 Poul Sander 2016-02-17 18:42 171
static void appendExtraFolders(const char* envName, const char* defaultValue, std::vector<std::string>& folders) {
0494de59 sago007 2018-06-03 19:32 172
	const char* envValue = std::getenv(envName);
77531130 Poul Sander 2016-02-17 18:42 173
	if (!envValue) {
77531130 Poul Sander 2016-02-17 18:42 174
		envValue = defaultValue;
77531130 Poul Sander 2016-02-17 18:42 175
	}
6b427a55 Poul Sander 2019-02-13 17:49 176
	sago::internal::appendExtraFoldersTokenizer(envName, envValue, folders);
77531130 Poul Sander 2016-02-17 18:42 177
}
77531130 Poul Sander 2016-02-17 18:42 178
77531130 Poul Sander 2016-02-17 18:42 179
#endif
77531130 Poul Sander 2016-02-17 18:42 180
77531130 Poul Sander 2016-02-17 18:42 181
77531130 Poul Sander 2016-02-17 18:42 182
namespace sago {
77531130 Poul Sander 2016-02-17 18:42 183
6b427a55 Poul Sander 2019-02-13 17:49 184
#if !defined(_WIN32) && !defined(__APPLE__)
6b427a55 Poul Sander 2019-02-13 17:49 185
namespace internal {
6b427a55 Poul Sander 2019-02-13 17:49 186
	void appendExtraFoldersTokenizer(const char* envName, const char* envValue, std::vector<std::string>& folders) {
6b427a55 Poul Sander 2019-02-13 17:49 187
		std::stringstream ss(envValue);
6b427a55 Poul Sander 2019-02-13 17:49 188
		std::string value;
6b427a55 Poul Sander 2019-02-13 17:49 189
		while (std::getline(ss, value, ':')) {
6b427a55 Poul Sander 2019-02-13 17:49 190
			if (value[0] == '/') {
6b427a55 Poul Sander 2019-02-13 17:49 191
				folders.push_back(value);
6b427a55 Poul Sander 2019-02-13 17:49 192
			}
6b427a55 Poul Sander 2019-02-13 17:49 193
			else {
6b427a55 Poul Sander 2019-02-13 17:49 194
				//Unless the system is wrongly configured this should never happen... But of course some systems will be incorectly configured.
6b427a55 Poul Sander 2019-02-13 17:49 195
				//The XDG documentation indicates that the folder should be ignored but that the program should continue.
6b427a55 Poul Sander 2019-02-13 17:49 196
				std::cerr << "Skipping path \"" << value << "\" in \"" << envName << "\" because it does not start with a \"/\"\n";
6b427a55 Poul Sander 2019-02-13 17:49 197
			}
6b427a55 Poul Sander 2019-02-13 17:49 198
		}
6b427a55 Poul Sander 2019-02-13 17:49 199
	}
6b427a55 Poul Sander 2019-02-13 17:49 200
}
6b427a55 Poul Sander 2019-02-13 17:49 201
#endif
6b427a55 Poul Sander 2019-02-13 17:49 202
77531130 Poul Sander 2016-02-17 18:42 203
std::string getDataHome() {
6b427a55 Poul Sander 2019-02-13 17:49 204
#ifdef _WIN32
77531130 Poul Sander 2016-02-17 18:42 205
	return GetAppData();
77531130 Poul Sander 2016-02-17 18:42 206
#elif defined(__APPLE__)
6b427a55 Poul Sander 2019-02-13 17:49 207
	return getHome()+"/Library/Application Support";
77531130 Poul Sander 2016-02-17 18:42 208
#else
77531130 Poul Sander 2016-02-17 18:42 209
	return getLinuxFolderDefault("XDG_DATA_HOME", ".local/share");
77531130 Poul Sander 2016-02-17 18:42 210
#endif
77531130 Poul Sander 2016-02-17 18:42 211
}
77531130 Poul Sander 2016-02-17 18:42 212
77531130 Poul Sander 2016-02-17 18:42 213
std::string getConfigHome() {
6b427a55 Poul Sander 2019-02-13 17:49 214
#ifdef _WIN32
77531130 Poul Sander 2016-02-17 18:42 215
	return GetAppData();
77531130 Poul Sander 2016-02-17 18:42 216
#elif defined(__APPLE__)
6b427a55 Poul Sander 2019-02-13 17:49 217
	return getHome()+"/Library/Application Support";
77531130 Poul Sander 2016-02-17 18:42 218
#else
77531130 Poul Sander 2016-02-17 18:42 219
	return getLinuxFolderDefault("XDG_CONFIG_HOME", ".config");
77531130 Poul Sander 2016-02-17 18:42 220
#endif
77531130 Poul Sander 2016-02-17 18:42 221
}
77531130 Poul Sander 2016-02-17 18:42 222
77531130 Poul Sander 2016-02-17 18:42 223
std::string getCacheDir() {
6b427a55 Poul Sander 2019-02-13 17:49 224
#ifdef _WIN32
77531130 Poul Sander 2016-02-17 18:42 225
	return GetAppDataLocal();
77531130 Poul Sander 2016-02-17 18:42 226
#elif defined(__APPLE__)
6b427a55 Poul Sander 2019-02-13 17:49 227
	return getHome()+"/Library/Caches";
77531130 Poul Sander 2016-02-17 18:42 228
#else
0494de59 sago007 2018-06-03 19:32 229
	return getLinuxFolderDefault("XDG_CACHE_HOME", ".cache");
77531130 Poul Sander 2016-02-17 18:42 230
#endif
77531130 Poul Sander 2016-02-17 18:42 231
}
77531130 Poul Sander 2016-02-17 18:42 232
77531130 Poul Sander 2016-02-17 18:42 233
void appendAdditionalDataDirectories(std::vector<std::string>& homes) {
6b427a55 Poul Sander 2019-02-13 17:49 234
#ifdef _WIN32
77531130 Poul Sander 2016-02-17 18:42 235
	homes.push_back(GetAppDataCommon());
6b427a55 Poul Sander 2019-02-13 17:49 236
#elif !defined(__APPLE__)
77531130 Poul Sander 2016-02-17 18:42 237
	appendExtraFolders("XDG_DATA_DIRS", "/usr/local/share/:/usr/share/", homes);
77531130 Poul Sander 2016-02-17 18:42 238
#endif
77531130 Poul Sander 2016-02-17 18:42 239
}
77531130 Poul Sander 2016-02-17 18:42 240
77531130 Poul Sander 2016-02-17 18:42 241
void appendAdditionalConfigDirectories(std::vector<std::string>& homes) {
6b427a55 Poul Sander 2019-02-13 17:49 242
#ifdef _WIN32
77531130 Poul Sander 2016-02-17 18:42 243
	homes.push_back(GetAppDataCommon());
6b427a55 Poul Sander 2019-02-13 17:49 244
#elif !defined(__APPLE__)
77531130 Poul Sander 2016-02-17 18:42 245
	appendExtraFolders("XDG_CONFIG_DIRS", "/etc/xdg", homes);
77531130 Poul Sander 2016-02-17 18:42 246
#endif
77531130 Poul Sander 2016-02-17 18:42 247
}
77531130 Poul Sander 2016-02-17 18:42 248
6b427a55 Poul Sander 2019-02-13 17:49 249
#if !defined(_WIN32) && !defined(__APPLE__)
77531130 Poul Sander 2016-02-17 18:42 250
struct PlatformFolders::PlatformFoldersData {
77531130 Poul Sander 2016-02-17 18:42 251
	std::map<std::string, std::string> folders;
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
static void PlatformFoldersAddFromFile(const std::string& filename, std::map<std::string, std::string>& folders) {
77531130 Poul Sander 2016-02-17 18:42 255
	std::ifstream infile(filename.c_str());
77531130 Poul Sander 2016-02-17 18:42 256
	std::string line;
77531130 Poul Sander 2016-02-17 18:42 257
	while (std::getline(infile, line)) {
68f32f61 sago007 2018-06-24 16:44 258
		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 259
			continue;
68f32f61 sago007 2018-06-24 16:44 260
		}
68f32f61 sago007 2018-06-24 16:44 261
		try {
68f32f61 sago007 2018-06-24 16:44 262
			std::size_t splitPos = line.find('=');
68f32f61 sago007 2018-06-24 16:44 263
			std::string key = line.substr(0, splitPos);
68f32f61 sago007 2018-06-24 16:44 264
			std::size_t valueStart = line.find('"', splitPos);
68f32f61 sago007 2018-06-24 16:44 265
			std::size_t valueEnd = line.find('"', valueStart+1);
68f32f61 sago007 2018-06-24 16:44 266
			std::string value = line.substr(valueStart+1, valueEnd - valueStart - 1);
68f32f61 sago007 2018-06-24 16:44 267
			folders[key] = value;
68f32f61 sago007 2018-06-24 16:44 268
		} catch (std::exception&  e) {
68f32f61 sago007 2018-06-24 16:44 269
			std::cerr << "WARNING: Failed to process \"" << line << "\" from \"" << filename << "\". Error: "<< e.what() << "\n";
77531130 Poul Sander 2016-02-17 18:42 270
			continue;
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
}
77531130 Poul Sander 2016-02-17 18:42 274
77531130 Poul Sander 2016-02-17 18:42 275
static void PlatformFoldersFillData(std::map<std::string, std::string>& folders) {
77531130 Poul Sander 2016-02-17 18:42 276
	folders["XDG_DOCUMENTS_DIR"] = "$HOME/Documents";
77531130 Poul Sander 2016-02-17 18:42 277
	folders["XDG_DESKTOP_DIR"] = "$HOME/Desktop";
77531130 Poul Sander 2016-02-17 18:42 278
	folders["XDG_DOWNLOAD_DIR"] = "$HOME/Downloads";
77531130 Poul Sander 2016-02-17 18:42 279
	folders["XDG_MUSIC_DIR"] = "$HOME/Music";
77531130 Poul Sander 2016-02-17 18:42 280
	folders["XDG_PICTURES_DIR"] = "$HOME/Pictures";
77531130 Poul Sander 2016-02-17 18:42 281
	folders["XDG_PUBLICSHARE_DIR"] = "$HOME/Public";
77531130 Poul Sander 2016-02-17 18:42 282
	folders["XDG_TEMPLATES_DIR"] = "$HOME/.Templates";
77531130 Poul Sander 2016-02-17 18:42 283
	folders["XDG_VIDEOS_DIR"] = "$HOME/Videos";
77531130 Poul Sander 2016-02-17 18:42 284
	PlatformFoldersAddFromFile( getConfigHome()+"/user-dirs.dirs", folders);
55ea55dd sago007 2018-03-24 16:07 285
	for (std::map<std::string, std::string>::iterator itr = folders.begin() ; itr != folders.end() ; ++itr ) {
77531130 Poul Sander 2016-02-17 18:42 286
		std::string& value = itr->second;
77531130 Poul Sander 2016-02-17 18:42 287
		if (value.compare(0, 5, "$HOME") == 0) {
77531130 Poul Sander 2016-02-17 18:42 288
			value = getHome() + value.substr(5, std::string::npos);
77531130 Poul Sander 2016-02-17 18:42 289
		}
77531130 Poul Sander 2016-02-17 18:42 290
	}
77531130 Poul Sander 2016-02-17 18:42 291
}
77531130 Poul Sander 2016-02-17 18:42 292
#endif
77531130 Poul Sander 2016-02-17 18:42 293
77531130 Poul Sander 2016-02-17 18:42 294
PlatformFolders::PlatformFolders() {
6b427a55 Poul Sander 2019-02-13 17:49 295
#if !defined(_WIN32) && !defined(__APPLE__)
77531130 Poul Sander 2016-02-17 18:42 296
	this->data = new PlatformFolders::PlatformFoldersData();
77531130 Poul Sander 2016-02-17 18:42 297
	try {
77531130 Poul Sander 2016-02-17 18:42 298
		PlatformFoldersFillData(data->folders);
0494de59 sago007 2018-06-03 19:32 299
	}
0494de59 sago007 2018-06-03 19:32 300
	catch (...) {
77531130 Poul Sander 2016-02-17 18:42 301
		delete this->data;
77531130 Poul Sander 2016-02-17 18:42 302
		throw;
77531130 Poul Sander 2016-02-17 18:42 303
	}
77531130 Poul Sander 2016-02-17 18:42 304
#endif
77531130 Poul Sander 2016-02-17 18:42 305
}
77531130 Poul Sander 2016-02-17 18:42 306
77531130 Poul Sander 2016-02-17 18:42 307
PlatformFolders::~PlatformFolders() {
6b427a55 Poul Sander 2019-02-13 17:49 308
#if !defined(_WIN32) && !defined(__APPLE__)
77531130 Poul Sander 2016-02-17 18:42 309
	delete this->data;
77531130 Poul Sander 2016-02-17 18:42 310
#endif
77531130 Poul Sander 2016-02-17 18:42 311
}
77531130 Poul Sander 2016-02-17 18:42 312
77531130 Poul Sander 2016-02-17 18:42 313
std::string PlatformFolders::getDocumentsFolder() const {
6b427a55 Poul Sander 2019-02-13 17:49 314
#ifdef _WIN32
6b427a55 Poul Sander 2019-02-13 17:49 315
	return GetKnownWindowsFolder(FOLDERID_Documents, "Failed to find My Documents folder");
77531130 Poul Sander 2016-02-17 18:42 316
#elif defined(__APPLE__)
6b427a55 Poul Sander 2019-02-13 17:49 317
	return getHome()+"/Documents";
77531130 Poul Sander 2016-02-17 18:42 318
#else
77531130 Poul Sander 2016-02-17 18:42 319
	return data->folders["XDG_DOCUMENTS_DIR"];
77531130 Poul Sander 2016-02-17 18:42 320
#endif
77531130 Poul Sander 2016-02-17 18:42 321
}
77531130 Poul Sander 2016-02-17 18:42 322
77531130 Poul Sander 2016-02-17 18:42 323
std::string PlatformFolders::getDesktopFolder() const {
6b427a55 Poul Sander 2019-02-13 17:49 324
#ifdef _WIN32
6b427a55 Poul Sander 2019-02-13 17:49 325
	return GetKnownWindowsFolder(FOLDERID_Desktop, "Failed to find Desktop folder");
77531130 Poul Sander 2016-02-17 18:42 326
#elif defined(__APPLE__)
6b427a55 Poul Sander 2019-02-13 17:49 327
	return getHome()+"/Desktop";
77531130 Poul Sander 2016-02-17 18:42 328
#else
77531130 Poul Sander 2016-02-17 18:42 329
	return data->folders["XDG_DESKTOP_DIR"];
77531130 Poul Sander 2016-02-17 18:42 330
#endif
77531130 Poul Sander 2016-02-17 18:42 331
}
77531130 Poul Sander 2016-02-17 18:42 332
77531130 Poul Sander 2016-02-17 18:42 333
std::string PlatformFolders::getPicturesFolder() const {
6b427a55 Poul Sander 2019-02-13 17:49 334
#ifdef _WIN32
6b427a55 Poul Sander 2019-02-13 17:49 335
	return GetKnownWindowsFolder(FOLDERID_Pictures, "Failed to find My Pictures folder");
77531130 Poul Sander 2016-02-17 18:42 336
#elif defined(__APPLE__)
6b427a55 Poul Sander 2019-02-13 17:49 337
	return getHome()+"/Pictures";
77531130 Poul Sander 2016-02-17 18:42 338
#else
77531130 Poul Sander 2016-02-17 18:42 339
	return data->folders["XDG_PICTURES_DIR"];
77531130 Poul Sander 2016-02-17 18:42 340
#endif
77531130 Poul Sander 2016-02-17 18:42 341
}
77531130 Poul Sander 2016-02-17 18:42 342
6b427a55 Poul Sander 2019-02-13 17:49 343
std::string PlatformFolders::getPublicFolder() const {
6b427a55 Poul Sander 2019-02-13 17:49 344
#ifdef _WIN32
6b427a55 Poul Sander 2019-02-13 17:49 345
	return GetKnownWindowsFolder(FOLDERID_Public, "Failed to find the Public folder");
6b427a55 Poul Sander 2019-02-13 17:49 346
#elif defined(__APPLE__)
6b427a55 Poul Sander 2019-02-13 17:49 347
	return getHome()+"/Public";
6b427a55 Poul Sander 2019-02-13 17:49 348
#else
6b427a55 Poul Sander 2019-02-13 17:49 349
	return data->folders["XDG_PUBLICSHARE_DIR"];
6b427a55 Poul Sander 2019-02-13 17:49 350
#endif
6b427a55 Poul Sander 2019-02-13 17:49 351
}
6b427a55 Poul Sander 2019-02-13 17:49 352
77531130 Poul Sander 2016-02-17 18:42 353
std::string PlatformFolders::getDownloadFolder1() const {
6b427a55 Poul Sander 2019-02-13 17:49 354
#ifdef _WIN32
6b427a55 Poul Sander 2019-02-13 17:49 355
	return GetKnownWindowsFolder(FOLDERID_Downloads, "Failed to find My Downloads folder");
77531130 Poul Sander 2016-02-17 18:42 356
#elif defined(__APPLE__)
6b427a55 Poul Sander 2019-02-13 17:49 357
	return getHome()+"/Downloads";
77531130 Poul Sander 2016-02-17 18:42 358
#else
77531130 Poul Sander 2016-02-17 18:42 359
	return data->folders["XDG_DOWNLOAD_DIR"];
77531130 Poul Sander 2016-02-17 18:42 360
#endif
77531130 Poul Sander 2016-02-17 18:42 361
}
77531130 Poul Sander 2016-02-17 18:42 362
77531130 Poul Sander 2016-02-17 18:42 363
std::string PlatformFolders::getMusicFolder() const {
6b427a55 Poul Sander 2019-02-13 17:49 364
#ifdef _WIN32
6b427a55 Poul Sander 2019-02-13 17:49 365
	return GetKnownWindowsFolder(FOLDERID_Music, "Failed to find My Music folder");
77531130 Poul Sander 2016-02-17 18:42 366
#elif defined(__APPLE__)
6b427a55 Poul Sander 2019-02-13 17:49 367
	return getHome()+"/Music";
77531130 Poul Sander 2016-02-17 18:42 368
#else
77531130 Poul Sander 2016-02-17 18:42 369
	return data->folders["XDG_MUSIC_DIR"];
77531130 Poul Sander 2016-02-17 18:42 370
#endif
77531130 Poul Sander 2016-02-17 18:42 371
}
77531130 Poul Sander 2016-02-17 18:42 372
77531130 Poul Sander 2016-02-17 18:42 373
std::string PlatformFolders::getVideoFolder() const {
6b427a55 Poul Sander 2019-02-13 17:49 374
#ifdef _WIN32
6b427a55 Poul Sander 2019-02-13 17:49 375
	return GetKnownWindowsFolder(FOLDERID_Videos, "Failed to find My Video folder");
77531130 Poul Sander 2016-02-17 18:42 376
#elif defined(__APPLE__)
6b427a55 Poul Sander 2019-02-13 17:49 377
	return getHome()+"/Movies";
77531130 Poul Sander 2016-02-17 18:42 378
#else
77531130 Poul Sander 2016-02-17 18:42 379
	return data->folders["XDG_VIDEOS_DIR"];
77531130 Poul Sander 2016-02-17 18:42 380
#endif
77531130 Poul Sander 2016-02-17 18:42 381
}
77531130 Poul Sander 2016-02-17 18:42 382
77531130 Poul Sander 2016-02-17 18:42 383
std::string PlatformFolders::getSaveGamesFolder1() const {
6b427a55 Poul Sander 2019-02-13 17:49 384
#ifdef _WIN32
77531130 Poul Sander 2016-02-17 18:42 385
	//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 386
	//Data that should not be user accessible should be placed under GetDataHome() instead
6b427a55 Poul Sander 2019-02-13 17:49 387
	return GetKnownWindowsFolder(FOLDERID_Documents, "Failed to find My Documents folder")+"\\My Games";
77531130 Poul Sander 2016-02-17 18:42 388
#elif defined(__APPLE__)
6b427a55 Poul Sander 2019-02-13 17:49 389
	return getHome()+"/Library/Application Support";
77531130 Poul Sander 2016-02-17 18:42 390
#else
77531130 Poul Sander 2016-02-17 18:42 391
	return getDataHome();
77531130 Poul Sander 2016-02-17 18:42 392
#endif
77531130 Poul Sander 2016-02-17 18:42 393
}
77531130 Poul Sander 2016-02-17 18:42 394
0494de59 sago007 2018-06-03 19:32 395
std::string getDesktopFolder() {
0494de59 sago007 2018-06-03 19:32 396
	return PlatformFolders().getDesktopFolder();
0494de59 sago007 2018-06-03 19:32 397
}
0494de59 sago007 2018-06-03 19:32 398
0494de59 sago007 2018-06-03 19:32 399
std::string getDocumentsFolder() {
0494de59 sago007 2018-06-03 19:32 400
	return PlatformFolders().getDocumentsFolder();
0494de59 sago007 2018-06-03 19:32 401
}
0494de59 sago007 2018-06-03 19:32 402
6b427a55 Poul Sander 2019-02-13 17:49 403
std::string getDownloadFolder() {
0494de59 sago007 2018-06-03 19:32 404
	return PlatformFolders().getDownloadFolder1();
0494de59 sago007 2018-06-03 19:32 405
}
0494de59 sago007 2018-06-03 19:32 406
6b427a55 Poul Sander 2019-02-13 17:49 407
std::string getDownloadFolder1() {
6b427a55 Poul Sander 2019-02-13 17:49 408
	return getDownloadFolder();
6b427a55 Poul Sander 2019-02-13 17:49 409
}
6b427a55 Poul Sander 2019-02-13 17:49 410
0494de59 sago007 2018-06-03 19:32 411
std::string getPicturesFolder() {
0494de59 sago007 2018-06-03 19:32 412
	return PlatformFolders().getPicturesFolder();
0494de59 sago007 2018-06-03 19:32 413
}
0494de59 sago007 2018-06-03 19:32 414
6b427a55 Poul Sander 2019-02-13 17:49 415
std::string getPublicFolder() {
6b427a55 Poul Sander 2019-02-13 17:49 416
	return PlatformFolders().getPublicFolder();
6b427a55 Poul Sander 2019-02-13 17:49 417
}
6b427a55 Poul Sander 2019-02-13 17:49 418
0494de59 sago007 2018-06-03 19:32 419
std::string getMusicFolder() {
0494de59 sago007 2018-06-03 19:32 420
	return PlatformFolders().getMusicFolder();
0494de59 sago007 2018-06-03 19:32 421
}
0494de59 sago007 2018-06-03 19:32 422
0494de59 sago007 2018-06-03 19:32 423
std::string getVideoFolder() {
0494de59 sago007 2018-06-03 19:32 424
	return PlatformFolders().getVideoFolder();
0494de59 sago007 2018-06-03 19:32 425
}
0494de59 sago007 2018-06-03 19:32 426
0494de59 sago007 2018-06-03 19:32 427
std::string getSaveGamesFolder1() {
0494de59 sago007 2018-06-03 19:32 428
	return PlatformFolders().getSaveGamesFolder1();
0494de59 sago007 2018-06-03 19:32 429
}
77531130 Poul Sander 2016-02-17 18:42 430
6b427a55 Poul Sander 2019-02-13 17:49 431
std::string getSaveGamesFolder2() {
6b427a55 Poul Sander 2019-02-13 17:49 432
#ifdef _WIN32
6b427a55 Poul Sander 2019-02-13 17:49 433
	return GetKnownWindowsFolder(FOLDERID_SavedGames, "Failed to find Saved Games folder");
6b427a55 Poul Sander 2019-02-13 17:49 434
#else
6b427a55 Poul Sander 2019-02-13 17:49 435
	return PlatformFolders().getSaveGamesFolder1();
6b427a55 Poul Sander 2019-02-13 17:49 436
#endif
6b427a55 Poul Sander 2019-02-13 17:49 437
}
77531130 Poul Sander 2016-02-17 18:42 438
77531130 Poul Sander 2016-02-17 18:42 439
}  //namespace sago
1970-01-01 00:00 440