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