git repos / PlatformFolders

blame: sago/platform_folders.cpp

normal view · raw

1213f3f3 sago007 2015-09-19 19:24 1
/*
d2b67cec sago007 2018-03-24 09:52 2
Its is under the MIT license, to encourage reuse by cut-and-paste.
d2b67cec sago007 2018-03-24 09:52 3
d2b67cec sago007 2018-03-24 09:52 4
The original files are hosted here: https://github.com/sago007/PlatformFolders
d2b67cec sago007 2018-03-24 09:52 5
d2b67cec sago007 2018-03-24 09:52 6
Copyright (c) 2015-2016 Poul Sander
d2b67cec sago007 2018-03-24 09:52 7
d2b67cec sago007 2018-03-24 09:52 8
Permission is hereby granted, free of charge, to any person
d2b67cec sago007 2018-03-24 09:52 9
obtaining a copy of this software and associated documentation files
d2b67cec sago007 2018-03-24 09:52 10
(the "Software"), to deal in the Software without restriction,
d2b67cec sago007 2018-03-24 09:52 11
including without limitation the rights to use, copy, modify, merge,
d2b67cec sago007 2018-03-24 09:52 12
publish, distribute, sublicense, and/or sell copies of the Software,
d2b67cec sago007 2018-03-24 09:52 13
and to permit persons to whom the Software is furnished to do so,
d2b67cec sago007 2018-03-24 09:52 14
subject to the following conditions:
d2b67cec sago007 2018-03-24 09:52 15
d2b67cec sago007 2018-03-24 09:52 16
The above copyright notice and this permission notice shall be
d2b67cec sago007 2018-03-24 09:52 17
included in all copies or substantial portions of the Software.
d2b67cec sago007 2018-03-24 09:52 18
d2b67cec sago007 2018-03-24 09:52 19
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
d2b67cec sago007 2018-03-24 09:52 20
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
d2b67cec sago007 2018-03-24 09:52 21
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
d2b67cec sago007 2018-03-24 09:52 22
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
d2b67cec sago007 2018-03-24 09:52 23
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
d2b67cec sago007 2018-03-24 09:52 24
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
d2b67cec sago007 2018-03-24 09:52 25
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
d2b67cec sago007 2018-03-24 09:52 26
SOFTWARE.
1213f3f3 sago007 2015-09-19 19:24 27
*/
1213f3f3 sago007 2015-09-19 19:24 28
1213f3f3 sago007 2015-09-19 19:24 29
#include "platform_folders.h"
1213f3f3 sago007 2015-09-19 19:24 30
#include <iostream>
1213f3f3 sago007 2015-09-19 19:24 31
#include <stdexcept>
da123945 sum01 2018-04-16 20:58 32
#include <cstdio>
1213f3f3 sago007 2015-09-19 19:24 33
#include <cstdlib>
1213f3f3 sago007 2015-09-19 19:24 34
5612f69b sago007 2018-06-16 11:22 35
#ifndef _WIN32
5612f69b sago007 2018-06-16 11:22 36
5612f69b sago007 2018-06-16 11:22 37
#include <pwd.h>
5612f69b sago007 2018-06-16 11:22 38
#include <unistd.h>
5612f69b sago007 2018-06-16 11:22 39
5612f69b sago007 2018-06-16 11:22 40
/**
5612f69b sago007 2018-06-16 11:22 41
 * Retrives the effective user's home dir.
5612f69b sago007 2018-06-16 11:22 42
 * If the user is running as root we ignore the HOME environment. It works badly with sudo.
5612f69b sago007 2018-06-16 11:22 43
 * Writing to $HOME as root implies security concerns that a multiplatform program cannot be assumed to handle.
5612f69b sago007 2018-06-16 11:22 44
 * @return The home directory. HOME environment is respected for non-root users if it exists.
5612f69b sago007 2018-06-16 11:22 45
 */
5612f69b sago007 2018-06-16 11:22 46
static std::string getHome() {
5612f69b sago007 2018-06-16 11:22 47
	std::string res;
5612f69b sago007 2018-06-16 11:22 48
	int uid = getuid();
5612f69b sago007 2018-06-16 11:22 49
	const char* homeEnv = std::getenv("HOME");
5612f69b sago007 2018-06-16 11:22 50
	if ( uid != 0 && homeEnv) {
5612f69b sago007 2018-06-16 11:22 51
		//We only acknowlegde HOME if not root.
5612f69b sago007 2018-06-16 11:22 52
		res = homeEnv;
5612f69b sago007 2018-06-16 11:22 53
		return res;
5612f69b sago007 2018-06-16 11:22 54
	}
5612f69b sago007 2018-06-16 11:22 55
	struct passwd* pw = getpwuid(uid);
5612f69b sago007 2018-06-16 11:22 56
	if (!pw) {
5612f69b sago007 2018-06-16 11:22 57
		throw std::runtime_error("Unable to get passwd struct.");
5612f69b sago007 2018-06-16 11:22 58
	}
5612f69b sago007 2018-06-16 11:22 59
	const char* tempRes = pw->pw_dir;
5612f69b sago007 2018-06-16 11:22 60
	if (!tempRes) {
5612f69b sago007 2018-06-16 11:22 61
		throw std::runtime_error("User has no home directory");
5612f69b sago007 2018-06-16 11:22 62
	}
5612f69b sago007 2018-06-16 11:22 63
	res = tempRes;
5612f69b sago007 2018-06-16 11:22 64
	return res;
5612f69b sago007 2018-06-16 11:22 65
}
5612f69b sago007 2018-06-16 11:22 66
5612f69b sago007 2018-06-16 11:22 67
#endif 
5612f69b sago007 2018-06-16 11:22 68
e5876621 sum01 2018-04-21 23:00 69
#ifdef _WIN32
fff3cc86 sum01 2018-04-22 02:15 70
// Make sure we don't bring in all the extra junk with windows.h
fff3cc86 sum01 2018-04-22 02:15 71
#ifndef WIN32_LEAN_AND_MEAN
fff3cc86 sum01 2018-04-22 02:15 72
#define WIN32_LEAN_AND_MEAN
fff3cc86 sum01 2018-04-22 02:15 73
#endif
fff3cc86 sum01 2018-04-22 02:15 74
// stringapiset.h depends on this
1213f3f3 sago007 2015-09-19 19:24 75
#include <windows.h>
fff3cc86 sum01 2018-04-22 02:15 76
// For SUCCEEDED macro
fff3cc86 sum01 2018-04-22 02:15 77
#include <winerror.h>
fff3cc86 sum01 2018-04-22 02:15 78
// For WideCharToMultiByte
fff3cc86 sum01 2018-04-22 02:15 79
#include <stringapiset.h>
fff3cc86 sum01 2018-04-22 02:15 80
// For SHGetFolderPathW and various CSIDL "magic numbers"
1213f3f3 sago007 2015-09-19 19:24 81
#include <shlobj.h>
1213f3f3 sago007 2015-09-19 19:24 82
ba428e10 sum01 2018-04-27 05:42 83
static std::string win32_utf16_to_utf8(const wchar_t* wstr) {
b81b63ae sago007 2016-10-03 20:30 84
	std::string res;
b81b63ae sago007 2016-10-03 20:30 85
	// If the 6th parameter is 0 then WideCharToMultiByte returns the number of bytes needed to store the result.
3e5ce8ff sum01 2018-04-16 21:01 86
	int actualSize = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, nullptr, 0, nullptr, nullptr);
b81b63ae sago007 2016-10-03 20:30 87
	if (actualSize > 0) {
b81b63ae sago007 2016-10-03 20:30 88
		//If the converted UTF-8 string could not be in the initial buffer. Allocate one that can hold it.
b81b63ae sago007 2016-10-03 20:30 89
		std::vector<char> buffer(actualSize);
3e5ce8ff sum01 2018-04-16 21:01 90
		actualSize = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, &buffer[0], buffer.size(), nullptr, nullptr);
b81b63ae sago007 2016-10-03 20:30 91
		res = buffer.data();
b81b63ae sago007 2016-10-03 20:30 92
	}
b81b63ae sago007 2016-10-03 20:30 93
	if (actualSize == 0) {
b81b63ae sago007 2016-10-03 20:30 94
		// WideCharToMultiByte return 0 for errors.
a3a13ec8 sum01 2018-04-16 21:07 95
		const std::string errorMsg = "UTF16 to UTF8 failed with error code: " + GetLastError();
b81b63ae sago007 2016-10-03 20:30 96
		throw std::runtime_error(errorMsg.c_str());
b81b63ae sago007 2016-10-03 20:30 97
	}
b81b63ae sago007 2016-10-03 20:30 98
	return res;
b81b63ae sago007 2016-10-03 20:30 99
}
b81b63ae sago007 2016-10-03 20:30 100
513c0ebf Poul Sander 2018-06-06 17:05 101
class FreeCoTaskMemory {
513c0ebf Poul Sander 2018-06-06 17:05 102
	LPWSTR pointer = NULL;
513c0ebf Poul Sander 2018-06-06 17:05 103
public:
0b976f51 Poul Sander 2018-06-06 17:31 104
	explicit FreeCoTaskMemory(LPWSTR pointer) : pointer(pointer) {};
513c0ebf Poul Sander 2018-06-06 17:05 105
	~FreeCoTaskMemory() {
513c0ebf Poul Sander 2018-06-06 17:05 106
		CoTaskMemFree(pointer);
513c0ebf Poul Sander 2018-06-06 17:05 107
	}
513c0ebf Poul Sander 2018-06-06 17:05 108
};
513c0ebf Poul Sander 2018-06-06 17:05 109
513c0ebf Poul Sander 2018-06-06 17:05 110
static std::string GetKnownWindowsFolder(REFKNOWNFOLDERID folderId, const char* errorMsg) {
513c0ebf Poul Sander 2018-06-06 17:05 111
	LPWSTR wszPath = NULL;
513c0ebf Poul Sander 2018-06-06 17:05 112
	HRESULT hr;
513c0ebf Poul Sander 2018-06-06 17:05 113
	hr = SHGetKnownFolderPath(folderId, KF_FLAG_CREATE, NULL, &wszPath);
513c0ebf Poul Sander 2018-06-06 17:05 114
	FreeCoTaskMemory scopeBoundMemory(wszPath);
513c0ebf Poul Sander 2018-06-06 17:05 115
513c0ebf Poul Sander 2018-06-06 17:05 116
	if (!SUCCEEDED(hr)) {
1213f3f3 sago007 2015-09-19 19:24 117
		throw std::runtime_error(errorMsg);
1213f3f3 sago007 2015-09-19 19:24 118
	}
513c0ebf Poul Sander 2018-06-06 17:05 119
	return win32_utf16_to_utf8(wszPath);
1213f3f3 sago007 2015-09-19 19:24 120
}
1213f3f3 sago007 2015-09-19 19:24 121
1213f3f3 sago007 2015-09-19 19:24 122
static std::string GetAppData() {
513c0ebf Poul Sander 2018-06-06 17:05 123
	return GetKnownWindowsFolder(FOLDERID_RoamingAppData, "RoamingAppData could not be found");
1213f3f3 sago007 2015-09-19 19:24 124
}
1213f3f3 sago007 2015-09-19 19:24 125
1213f3f3 sago007 2015-09-19 19:24 126
static std::string GetAppDataCommon() {
513c0ebf Poul Sander 2018-06-06 17:05 127
	return GetKnownWindowsFolder(FOLDERID_ProgramData, "ProgramData could not be found");
1213f3f3 sago007 2015-09-19 19:24 128
}
1213f3f3 sago007 2015-09-19 19:24 129
1213f3f3 sago007 2015-09-19 19:24 130
static std::string GetAppDataLocal() {
513c0ebf Poul Sander 2018-06-06 17:05 131
	return GetKnownWindowsFolder(FOLDERID_LocalAppData, "LocalAppData could not be found");
1213f3f3 sago007 2015-09-19 19:24 132
}
1eaaab72 sago007 2015-10-26 17:23 133
#elif defined(__APPLE__)
1213f3f3 sago007 2015-09-19 19:24 134
#else
1213f3f3 sago007 2015-09-19 19:24 135
#include <map>
1213f3f3 sago007 2015-09-19 19:24 136
#include <fstream>
dcb0fec2 sago007 2015-09-21 17:50 137
#include <sys/types.h>
a3a13ec8 sum01 2018-04-16 21:07 138
// For strlen and strtok
68df0922 sum01 2018-04-16 21:07 139
#include <cstring>
2a74df65 Poul Sander 2019-02-13 16:51 140
#include <sstream>
1213f3f3 sago007 2015-09-19 19:24 141
//Typically Linux. For easy reading the comments will just say Linux but should work with most *nixes
1213f3f3 sago007 2015-09-19 19:24 142
1213f3f3 sago007 2015-09-19 19:24 143
static void throwOnRelative(const char* envName, const char* envValue) {
1213f3f3 sago007 2015-09-19 19:24 144
	if (envValue[0] != '/') {
1213f3f3 sago007 2015-09-19 19:24 145
		char buffer[200];
da123945 sum01 2018-04-16 20:58 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);
1213f3f3 sago007 2015-09-19 19:24 147
		throw std::runtime_error(buffer);
1213f3f3 sago007 2015-09-19 19:24 148
	}
1213f3f3 sago007 2015-09-19 19:24 149
}
1213f3f3 sago007 2015-09-19 19:24 150
5612f69b sago007 2018-06-16 11:22 151
1213f3f3 sago007 2015-09-19 19:24 152
1213f3f3 sago007 2015-09-19 19:24 153
static std::string getLinuxFolderDefault(const char* envName, const char* defaultRelativePath) {
1213f3f3 sago007 2015-09-19 19:24 154
	std::string res;
68df0922 sum01 2018-04-16 21:07 155
	const char* tempRes = std::getenv(envName);
1213f3f3 sago007 2015-09-19 19:24 156
	if (tempRes) {
1213f3f3 sago007 2015-09-19 19:24 157
		throwOnRelative(envName, tempRes);
1213f3f3 sago007 2015-09-19 19:24 158
		res = tempRes;
1213f3f3 sago007 2015-09-19 19:24 159
		return res;
1213f3f3 sago007 2015-09-19 19:24 160
	}
1213f3f3 sago007 2015-09-19 19:24 161
	res = getHome() + "/" + defaultRelativePath;
1213f3f3 sago007 2015-09-19 19:24 162
	return res;
1213f3f3 sago007 2015-09-19 19:24 163
}
1213f3f3 sago007 2015-09-19 19:24 164
1213f3f3 sago007 2015-09-19 19:24 165
static void appendExtraFolders(const char* envName, const char* defaultValue, std::vector<std::string>& folders) {
68df0922 sum01 2018-04-16 21:07 166
	const char* envValue = std::getenv(envName);
1213f3f3 sago007 2015-09-19 19:24 167
	if (!envValue) {
1213f3f3 sago007 2015-09-19 19:24 168
		envValue = defaultValue;
1213f3f3 sago007 2015-09-19 19:24 169
	}
df5fd138 Poul Sander 2019-02-13 16:35 170
	sago::internal::appendExtraFoldersTokenizer(envName, envValue, folders);
1213f3f3 sago007 2015-09-19 19:24 171
}
1213f3f3 sago007 2015-09-19 19:24 172
1213f3f3 sago007 2015-09-19 19:24 173
#endif
1213f3f3 sago007 2015-09-19 19:24 174
1213f3f3 sago007 2015-09-19 19:24 175
1213f3f3 sago007 2015-09-19 19:24 176
namespace sago {
1213f3f3 sago007 2015-09-19 19:24 177
df5fd138 Poul Sander 2019-02-13 16:35 178
#if !defined(_WIN32) && !defined(__APPLE__)
df5fd138 Poul Sander 2019-02-13 16:35 179
namespace internal {
df5fd138 Poul Sander 2019-02-13 16:35 180
	void appendExtraFoldersTokenizer(const char* envName, const char* envValue, std::vector<std::string>& folders) {
0c48f2f0 Poul Sander 2019-02-13 16:55 181
		std::stringstream ss(envValue);
2a74df65 Poul Sander 2019-02-13 16:51 182
		std::string value;
2a74df65 Poul Sander 2019-02-13 16:51 183
		while (std::getline(ss, value, ':')) {
2a74df65 Poul Sander 2019-02-13 16:51 184
			if (value[0] == '/') {
2a74df65 Poul Sander 2019-02-13 16:51 185
				folders.push_back(value);
df5fd138 Poul Sander 2019-02-13 16:35 186
			}
df5fd138 Poul Sander 2019-02-13 16:35 187
			else {
df5fd138 Poul Sander 2019-02-13 16:35 188
				//Unless the system is wrongly configured this should never happen... But of course some systems will be incorectly configured.
df5fd138 Poul Sander 2019-02-13 16:35 189
				//The XDG documentation indicates that the folder should be ignored but that the program should continue.
2a74df65 Poul Sander 2019-02-13 16:51 190
				std::cerr << "Skipping path \"" << value << "\" in \"" << envName << "\" because it does not start with a \"/\"\n";
df5fd138 Poul Sander 2019-02-13 16:35 191
			}
df5fd138 Poul Sander 2019-02-13 16:35 192
		}
df5fd138 Poul Sander 2019-02-13 16:35 193
	}
df5fd138 Poul Sander 2019-02-13 16:35 194
}
df5fd138 Poul Sander 2019-02-13 16:35 195
#endif
df5fd138 Poul Sander 2019-02-13 16:35 196
1213f3f3 sago007 2015-09-19 19:24 197
std::string getDataHome() {
e5876621 sum01 2018-04-21 23:00 198
#ifdef _WIN32
1213f3f3 sago007 2015-09-19 19:24 199
	return GetAppData();
1eaaab72 sago007 2015-10-26 17:23 200
#elif defined(__APPLE__)
51c64065 Poul Sander 2018-06-24 10:58 201
	return getHome()+"/Library/Application Support";
1213f3f3 sago007 2015-09-19 19:24 202
#else
1213f3f3 sago007 2015-09-19 19:24 203
	return getLinuxFolderDefault("XDG_DATA_HOME", ".local/share");
1213f3f3 sago007 2015-09-19 19:24 204
#endif
1213f3f3 sago007 2015-09-19 19:24 205
}
1213f3f3 sago007 2015-09-19 19:24 206
1213f3f3 sago007 2015-09-19 19:24 207
std::string getConfigHome() {
e5876621 sum01 2018-04-21 23:00 208
#ifdef _WIN32
1213f3f3 sago007 2015-09-19 19:24 209
	return GetAppData();
1eaaab72 sago007 2015-10-26 17:23 210
#elif defined(__APPLE__)
51c64065 Poul Sander 2018-06-24 10:58 211
	return getHome()+"/Library/Application Support";
1213f3f3 sago007 2015-09-19 19:24 212
#else
1213f3f3 sago007 2015-09-19 19:24 213
	return getLinuxFolderDefault("XDG_CONFIG_HOME", ".config");
1213f3f3 sago007 2015-09-19 19:24 214
#endif
1213f3f3 sago007 2015-09-19 19:24 215
}
1213f3f3 sago007 2015-09-19 19:24 216
1213f3f3 sago007 2015-09-19 19:24 217
std::string getCacheDir() {
e5876621 sum01 2018-04-21 23:00 218
#ifdef _WIN32
1213f3f3 sago007 2015-09-19 19:24 219
	return GetAppDataLocal();
1eaaab72 sago007 2015-10-26 17:23 220
#elif defined(__APPLE__)
51c64065 Poul Sander 2018-06-24 10:58 221
	return getHome()+"/Library/Caches";
1213f3f3 sago007 2015-09-19 19:24 222
#else
cc1fdd3e Kogia-sima 2018-05-25 04:26 223
	return getLinuxFolderDefault("XDG_CACHE_HOME", ".cache");
1213f3f3 sago007 2015-09-19 19:24 224
#endif
1213f3f3 sago007 2015-09-19 19:24 225
}
1213f3f3 sago007 2015-09-19 19:24 226
1213f3f3 sago007 2015-09-19 19:24 227
void appendAdditionalDataDirectories(std::vector<std::string>& homes) {
e5876621 sum01 2018-04-21 23:00 228
#ifdef _WIN32
1213f3f3 sago007 2015-09-19 19:24 229
	homes.push_back(GetAppDataCommon());
b3e5a881 sum01 2018-04-21 22:53 230
#elif !defined(__APPLE__)
1213f3f3 sago007 2015-09-19 19:24 231
	appendExtraFolders("XDG_DATA_DIRS", "/usr/local/share/:/usr/share/", homes);
1213f3f3 sago007 2015-09-19 19:24 232
#endif
1213f3f3 sago007 2015-09-19 19:24 233
}
1213f3f3 sago007 2015-09-19 19:24 234
1213f3f3 sago007 2015-09-19 19:24 235
void appendAdditionalConfigDirectories(std::vector<std::string>& homes) {
e5876621 sum01 2018-04-21 23:00 236
#ifdef _WIN32
1213f3f3 sago007 2015-09-19 19:24 237
	homes.push_back(GetAppDataCommon());
b3e5a881 sum01 2018-04-21 22:53 238
#elif !defined(__APPLE__)
1213f3f3 sago007 2015-09-19 19:24 239
	appendExtraFolders("XDG_CONFIG_DIRS", "/etc/xdg", homes);
1213f3f3 sago007 2015-09-19 19:24 240
#endif
1213f3f3 sago007 2015-09-19 19:24 241
}
1213f3f3 sago007 2015-09-19 19:24 242
b3e5a881 sum01 2018-04-21 22:53 243
#if !defined(_WIN32) && !defined(__APPLE__)
1213f3f3 sago007 2015-09-19 19:24 244
struct PlatformFolders::PlatformFoldersData {
1213f3f3 sago007 2015-09-19 19:24 245
	std::map<std::string, std::string> folders;
1213f3f3 sago007 2015-09-19 19:24 246
};
1213f3f3 sago007 2015-09-19 19:24 247
1213f3f3 sago007 2015-09-19 19:24 248
static void PlatformFoldersAddFromFile(const std::string& filename, std::map<std::string, std::string>& folders) {
1213f3f3 sago007 2015-09-19 19:24 249
	std::ifstream infile(filename.c_str());
1213f3f3 sago007 2015-09-19 19:24 250
	std::string line;
1213f3f3 sago007 2015-09-19 19:24 251
	while (std::getline(infile, line)) {
0823b4a5 sago007 2018-06-11 16:33 252
		if (line.length() == 0 || line.at(0) == '#' || line.substr(0, 4) != "XDG_" || line.find("_DIR") == std::string::npos) {
0823b4a5 sago007 2018-06-11 16:33 253
			continue;
0823b4a5 sago007 2018-06-11 16:33 254
		}
0823b4a5 sago007 2018-06-11 16:33 255
		try {
0823b4a5 sago007 2018-06-11 16:33 256
			std::size_t splitPos = line.find('=');
0823b4a5 sago007 2018-06-11 16:33 257
			std::string key = line.substr(0, splitPos);
0823b4a5 sago007 2018-06-11 16:33 258
			std::size_t valueStart = line.find('"', splitPos);
0823b4a5 sago007 2018-06-11 16:33 259
			std::size_t valueEnd = line.find('"', valueStart+1);
0823b4a5 sago007 2018-06-11 16:33 260
			std::string value = line.substr(valueStart+1, valueEnd - valueStart - 1);
0823b4a5 sago007 2018-06-11 16:33 261
			folders[key] = value;
0823b4a5 sago007 2018-06-11 16:33 262
		} catch (std::exception&  e) {
0823b4a5 sago007 2018-06-11 16:33 263
			std::cerr << "WARNING: Failed to process \"" << line << "\" from \"" << filename << "\". Error: "<< e.what() << "\n";
1213f3f3 sago007 2015-09-19 19:24 264
			continue;
1213f3f3 sago007 2015-09-19 19:24 265
		}
1213f3f3 sago007 2015-09-19 19:24 266
	}
1213f3f3 sago007 2015-09-19 19:24 267
}
1213f3f3 sago007 2015-09-19 19:24 268
1213f3f3 sago007 2015-09-19 19:24 269
static void PlatformFoldersFillData(std::map<std::string, std::string>& folders) {
1213f3f3 sago007 2015-09-19 19:24 270
	folders["XDG_DOCUMENTS_DIR"] = "$HOME/Documents";
1213f3f3 sago007 2015-09-19 19:24 271
	folders["XDG_DESKTOP_DIR"] = "$HOME/Desktop";
1213f3f3 sago007 2015-09-19 19:24 272
	folders["XDG_DOWNLOAD_DIR"] = "$HOME/Downloads";
1213f3f3 sago007 2015-09-19 19:24 273
	folders["XDG_MUSIC_DIR"] = "$HOME/Music";
1213f3f3 sago007 2015-09-19 19:24 274
	folders["XDG_PICTURES_DIR"] = "$HOME/Pictures";
1213f3f3 sago007 2015-09-19 19:24 275
	folders["XDG_PUBLICSHARE_DIR"] = "$HOME/Public";
1213f3f3 sago007 2015-09-19 19:24 276
	folders["XDG_TEMPLATES_DIR"] = "$HOME/.Templates";
1213f3f3 sago007 2015-09-19 19:24 277
	folders["XDG_VIDEOS_DIR"] = "$HOME/Videos";
1213f3f3 sago007 2015-09-19 19:24 278
	PlatformFoldersAddFromFile( getConfigHome()+"/user-dirs.dirs", folders);
1ff6644e sago007 2018-02-18 16:28 279
	for (std::map<std::string, std::string>::iterator itr = folders.begin() ; itr != folders.end() ; ++itr ) {
1213f3f3 sago007 2015-09-19 19:24 280
		std::string& value = itr->second;
1213f3f3 sago007 2015-09-19 19:24 281
		if (value.compare(0, 5, "$HOME") == 0) {
1213f3f3 sago007 2015-09-19 19:24 282
			value = getHome() + value.substr(5, std::string::npos);
1213f3f3 sago007 2015-09-19 19:24 283
		}
1213f3f3 sago007 2015-09-19 19:24 284
	}
1213f3f3 sago007 2015-09-19 19:24 285
}
1213f3f3 sago007 2015-09-19 19:24 286
#endif
1213f3f3 sago007 2015-09-19 19:24 287
1213f3f3 sago007 2015-09-19 19:24 288
PlatformFolders::PlatformFolders() {
b3e5a881 sum01 2018-04-21 22:53 289
#if !defined(_WIN32) && !defined(__APPLE__)
1213f3f3 sago007 2015-09-19 19:24 290
	this->data = new PlatformFolders::PlatformFoldersData();
1213f3f3 sago007 2015-09-19 19:24 291
	try {
1213f3f3 sago007 2015-09-19 19:24 292
		PlatformFoldersFillData(data->folders);
0fd23ef9 sago007 2018-05-22 17:49 293
	}
0fd23ef9 sago007 2018-05-22 17:49 294
	catch (...) {
1213f3f3 sago007 2015-09-19 19:24 295
		delete this->data;
1213f3f3 sago007 2015-09-19 19:24 296
		throw;
1213f3f3 sago007 2015-09-19 19:24 297
	}
1213f3f3 sago007 2015-09-19 19:24 298
#endif
1213f3f3 sago007 2015-09-19 19:24 299
}
1213f3f3 sago007 2015-09-19 19:24 300
1213f3f3 sago007 2015-09-19 19:24 301
PlatformFolders::~PlatformFolders() {
b3e5a881 sum01 2018-04-21 22:53 302
#if !defined(_WIN32) && !defined(__APPLE__)
1213f3f3 sago007 2015-09-19 19:24 303
	delete this->data;
1213f3f3 sago007 2015-09-19 19:24 304
#endif
1213f3f3 sago007 2015-09-19 19:24 305
}
1213f3f3 sago007 2015-09-19 19:24 306
1213f3f3 sago007 2015-09-19 19:24 307
std::string PlatformFolders::getDocumentsFolder() const {
e5876621 sum01 2018-04-21 23:00 308
#ifdef _WIN32
513c0ebf Poul Sander 2018-06-06 17:05 309
	return GetKnownWindowsFolder(FOLDERID_Documents, "Failed to find My Documents folder");
1eaaab72 sago007 2015-10-26 17:23 310
#elif defined(__APPLE__)
51c64065 Poul Sander 2018-06-24 10:58 311
	return getHome()+"/Documents";
1213f3f3 sago007 2015-09-19 19:24 312
#else
1213f3f3 sago007 2015-09-19 19:24 313
	return data->folders["XDG_DOCUMENTS_DIR"];
1213f3f3 sago007 2015-09-19 19:24 314
#endif
1213f3f3 sago007 2015-09-19 19:24 315
}
1213f3f3 sago007 2015-09-19 19:24 316
1213f3f3 sago007 2015-09-19 19:24 317
std::string PlatformFolders::getDesktopFolder() const {
e5876621 sum01 2018-04-21 23:00 318
#ifdef _WIN32
513c0ebf Poul Sander 2018-06-06 17:05 319
	return GetKnownWindowsFolder(FOLDERID_Desktop, "Failed to find Desktop folder");
1eaaab72 sago007 2015-10-26 17:23 320
#elif defined(__APPLE__)
51c64065 Poul Sander 2018-06-24 10:58 321
	return getHome()+"/Desktop";
1213f3f3 sago007 2015-09-19 19:24 322
#else
1213f3f3 sago007 2015-09-19 19:24 323
	return data->folders["XDG_DESKTOP_DIR"];
1213f3f3 sago007 2015-09-19 19:24 324
#endif
1213f3f3 sago007 2015-09-19 19:24 325
}
1213f3f3 sago007 2015-09-19 19:24 326
1213f3f3 sago007 2015-09-19 19:24 327
std::string PlatformFolders::getPicturesFolder() const {
e5876621 sum01 2018-04-21 23:00 328
#ifdef _WIN32
513c0ebf Poul Sander 2018-06-06 17:05 329
	return GetKnownWindowsFolder(FOLDERID_Pictures, "Failed to find My Pictures folder");
1eaaab72 sago007 2015-10-26 17:23 330
#elif defined(__APPLE__)
51c64065 Poul Sander 2018-06-24 10:58 331
	return getHome()+"/Pictures";
1213f3f3 sago007 2015-09-19 19:24 332
#else
1213f3f3 sago007 2015-09-19 19:24 333
	return data->folders["XDG_PICTURES_DIR"];
1213f3f3 sago007 2015-09-19 19:24 334
#endif
1213f3f3 sago007 2015-09-19 19:24 335
}
1213f3f3 sago007 2015-09-19 19:24 336
5612f69b sago007 2018-06-16 11:22 337
std::string PlatformFolders::getPublicFolder() const {
5612f69b sago007 2018-06-16 11:22 338
#ifdef _WIN32
5612f69b sago007 2018-06-16 11:22 339
	return GetKnownWindowsFolder(FOLDERID_Public, "Failed to find the Public folder");
5612f69b sago007 2018-06-16 11:22 340
#elif defined(__APPLE__)
5612f69b sago007 2018-06-16 11:22 341
	return getHome()+"/Public";
5612f69b sago007 2018-06-16 11:22 342
#else
5612f69b sago007 2018-06-16 11:22 343
	return data->folders["XDG_PUBLICSHARE_DIR"];
5612f69b sago007 2018-06-16 11:22 344
#endif
5612f69b sago007 2018-06-16 11:22 345
}
5612f69b sago007 2018-06-16 11:22 346
1213f3f3 sago007 2015-09-19 19:24 347
std::string PlatformFolders::getDownloadFolder1() const {
e5876621 sum01 2018-04-21 23:00 348
#ifdef _WIN32
513c0ebf Poul Sander 2018-06-06 17:05 349
	return GetKnownWindowsFolder(FOLDERID_Downloads, "Failed to find My Downloads folder");
1eaaab72 sago007 2015-10-26 17:23 350
#elif defined(__APPLE__)
51c64065 Poul Sander 2018-06-24 10:58 351
	return getHome()+"/Downloads";
1213f3f3 sago007 2015-09-19 19:24 352
#else
1213f3f3 sago007 2015-09-19 19:24 353
	return data->folders["XDG_DOWNLOAD_DIR"];
1213f3f3 sago007 2015-09-19 19:24 354
#endif
1213f3f3 sago007 2015-09-19 19:24 355
}
1213f3f3 sago007 2015-09-19 19:24 356
1213f3f3 sago007 2015-09-19 19:24 357
std::string PlatformFolders::getMusicFolder() const {
e5876621 sum01 2018-04-21 23:00 358
#ifdef _WIN32
513c0ebf Poul Sander 2018-06-06 17:05 359
	return GetKnownWindowsFolder(FOLDERID_Music, "Failed to find My Music folder");
1eaaab72 sago007 2015-10-26 17:23 360
#elif defined(__APPLE__)
51c64065 Poul Sander 2018-06-24 10:58 361
	return getHome()+"/Music";
1213f3f3 sago007 2015-09-19 19:24 362
#else
1213f3f3 sago007 2015-09-19 19:24 363
	return data->folders["XDG_MUSIC_DIR"];
1213f3f3 sago007 2015-09-19 19:24 364
#endif
1213f3f3 sago007 2015-09-19 19:24 365
}
1213f3f3 sago007 2015-09-19 19:24 366
1213f3f3 sago007 2015-09-19 19:24 367
std::string PlatformFolders::getVideoFolder() const {
e5876621 sum01 2018-04-21 23:00 368
#ifdef _WIN32
513c0ebf Poul Sander 2018-06-06 17:05 369
	return GetKnownWindowsFolder(FOLDERID_Videos, "Failed to find My Video folder");
1eaaab72 sago007 2015-10-26 17:23 370
#elif defined(__APPLE__)
51c64065 Poul Sander 2018-06-24 10:58 371
	return getHome()+"/Movies";
1213f3f3 sago007 2015-09-19 19:24 372
#else
1213f3f3 sago007 2015-09-19 19:24 373
	return data->folders["XDG_VIDEOS_DIR"];
1213f3f3 sago007 2015-09-19 19:24 374
#endif
1213f3f3 sago007 2015-09-19 19:24 375
}
1213f3f3 sago007 2015-09-19 19:24 376
1213f3f3 sago007 2015-09-19 19:24 377
std::string PlatformFolders::getSaveGamesFolder1() const {
e5876621 sum01 2018-04-21 23:00 378
#ifdef _WIN32
1213f3f3 sago007 2015-09-19 19:24 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".
1213f3f3 sago007 2015-09-19 19:24 380
	//Data that should not be user accessible should be placed under GetDataHome() instead
513c0ebf Poul Sander 2018-06-06 17:05 381
	return GetKnownWindowsFolder(FOLDERID_Documents, "Failed to find My Documents folder")+"\\My Games";
1eaaab72 sago007 2015-10-26 17:23 382
#elif defined(__APPLE__)
51c64065 Poul Sander 2018-06-24 10:58 383
	return getHome()+"/Library/Application Support";
1213f3f3 sago007 2015-09-19 19:24 384
#else
1213f3f3 sago007 2015-09-19 19:24 385
	return getDataHome();
1213f3f3 sago007 2015-09-19 19:24 386
#endif
1213f3f3 sago007 2015-09-19 19:24 387
}
1213f3f3 sago007 2015-09-19 19:24 388
0fd23ef9 sago007 2018-05-22 17:49 389
std::string getDesktopFolder() {
0fd23ef9 sago007 2018-05-22 17:49 390
	return PlatformFolders().getDesktopFolder();
0fd23ef9 sago007 2018-05-22 17:49 391
}
0fd23ef9 sago007 2018-05-22 17:49 392
0fd23ef9 sago007 2018-05-22 17:49 393
std::string getDocumentsFolder() {
0fd23ef9 sago007 2018-05-22 17:49 394
	return PlatformFolders().getDocumentsFolder();
0fd23ef9 sago007 2018-05-22 17:49 395
}
0fd23ef9 sago007 2018-05-22 17:49 396
513c0ebf Poul Sander 2018-06-06 17:05 397
std::string getDownloadFolder() {
0fd23ef9 sago007 2018-05-22 17:49 398
	return PlatformFolders().getDownloadFolder1();
0fd23ef9 sago007 2018-05-22 17:49 399
}
0fd23ef9 sago007 2018-05-22 17:49 400
513c0ebf Poul Sander 2018-06-06 17:05 401
std::string getDownloadFolder1() {
513c0ebf Poul Sander 2018-06-06 17:05 402
	return getDownloadFolder();
513c0ebf Poul Sander 2018-06-06 17:05 403
}
513c0ebf Poul Sander 2018-06-06 17:05 404
0fd23ef9 sago007 2018-05-22 17:49 405
std::string getPicturesFolder() {
0fd23ef9 sago007 2018-05-22 17:49 406
	return PlatformFolders().getPicturesFolder();
0fd23ef9 sago007 2018-05-22 17:49 407
}
0fd23ef9 sago007 2018-05-22 17:49 408
5612f69b sago007 2018-06-16 11:22 409
std::string getPublicFolder() {
5612f69b sago007 2018-06-16 11:22 410
	return PlatformFolders().getPublicFolder();
5612f69b sago007 2018-06-16 11:22 411
}
5612f69b sago007 2018-06-16 11:22 412
0fd23ef9 sago007 2018-05-22 17:49 413
std::string getMusicFolder() {
0fd23ef9 sago007 2018-05-22 17:49 414
	return PlatformFolders().getMusicFolder();
0fd23ef9 sago007 2018-05-22 17:49 415
}
0fd23ef9 sago007 2018-05-22 17:49 416
0fd23ef9 sago007 2018-05-22 17:49 417
std::string getVideoFolder() {
0fd23ef9 sago007 2018-05-22 17:49 418
	return PlatformFolders().getVideoFolder();
0fd23ef9 sago007 2018-05-22 17:49 419
}
0fd23ef9 sago007 2018-05-22 17:49 420
0fd23ef9 sago007 2018-05-22 17:49 421
std::string getSaveGamesFolder1() {
0fd23ef9 sago007 2018-05-22 17:49 422
	return PlatformFolders().getSaveGamesFolder1();
0fd23ef9 sago007 2018-05-22 17:49 423
}
1213f3f3 sago007 2015-09-19 19:24 424
513c0ebf Poul Sander 2018-06-06 17:05 425
std::string getSaveGamesFolder2() {
513c0ebf Poul Sander 2018-06-06 17:05 426
#ifdef _WIN32
513c0ebf Poul Sander 2018-06-06 17:05 427
	return GetKnownWindowsFolder(FOLDERID_SavedGames, "Failed to find Saved Games folder");
513c0ebf Poul Sander 2018-06-06 17:05 428
#else
513c0ebf Poul Sander 2018-06-06 17:05 429
	return PlatformFolders().getSaveGamesFolder1();
513c0ebf Poul Sander 2018-06-06 17:05 430
#endif
513c0ebf Poul Sander 2018-06-06 17:05 431
}
1213f3f3 sago007 2015-09-19 19:24 432
1213f3f3 sago007 2015-09-19 19:24 433
}  //namespace sago
1970-01-01 00:00 434