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