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