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>
1213f3f3 sago007 2015-09-19 19:24 140
//Typically Linux. For easy reading the comments will just say Linux but should work with most *nixes
1213f3f3 sago007 2015-09-19 19:24 141
1213f3f3 sago007 2015-09-19 19:24 142
static void throwOnRelative(const char* envName, const char* envValue) {
1213f3f3 sago007 2015-09-19 19:24 143
	if (envValue[0] != '/') {
1213f3f3 sago007 2015-09-19 19:24 144
		char buffer[200];
da123945 sum01 2018-04-16 20:58 145
		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 146
		throw std::runtime_error(buffer);
1213f3f3 sago007 2015-09-19 19:24 147
	}
1213f3f3 sago007 2015-09-19 19:24 148
}
1213f3f3 sago007 2015-09-19 19:24 149
5612f69b sago007 2018-06-16 11:22 150
1213f3f3 sago007 2015-09-19 19:24 151
1213f3f3 sago007 2015-09-19 19:24 152
static std::string getLinuxFolderDefault(const char* envName, const char* defaultRelativePath) {
1213f3f3 sago007 2015-09-19 19:24 153
	std::string res;
68df0922 sum01 2018-04-16 21:07 154
	const char* tempRes = std::getenv(envName);
1213f3f3 sago007 2015-09-19 19:24 155
	if (tempRes) {
1213f3f3 sago007 2015-09-19 19:24 156
		throwOnRelative(envName, tempRes);
1213f3f3 sago007 2015-09-19 19:24 157
		res = tempRes;
1213f3f3 sago007 2015-09-19 19:24 158
		return res;
1213f3f3 sago007 2015-09-19 19:24 159
	}
1213f3f3 sago007 2015-09-19 19:24 160
	res = getHome() + "/" + defaultRelativePath;
1213f3f3 sago007 2015-09-19 19:24 161
	return res;
1213f3f3 sago007 2015-09-19 19:24 162
}
1213f3f3 sago007 2015-09-19 19:24 163
1213f3f3 sago007 2015-09-19 19:24 164
static void appendExtraFoldersTokenizer(const char* envName, const char* envValue, std::vector<std::string>& folders) {
68df0922 sum01 2018-04-16 21:07 165
	std::vector<char> buffer(envValue, envValue + std::strlen(envValue) + 1);
a3a13ec8 sum01 2018-04-16 21:07 166
	char* p = std::strtok ( &buffer[0], ":");
3e5ce8ff sum01 2018-04-16 21:01 167
	while (p != nullptr) {
1213f3f3 sago007 2015-09-19 19:24 168
		if (p[0] == '/') {
1213f3f3 sago007 2015-09-19 19:24 169
			folders.push_back(p);
1213f3f3 sago007 2015-09-19 19:24 170
		}
1213f3f3 sago007 2015-09-19 19:24 171
		else {
1213f3f3 sago007 2015-09-19 19:24 172
			//Unless the system is wrongly configured this should never happen... But of course some systems will be incorectly configured.
1213f3f3 sago007 2015-09-19 19:24 173
			//The XDG documentation indicates that the folder should be ignored but that the program should continue.
b2ba7c7d sago007 2016-09-10 17:08 174
			std::cerr << "Skipping path \"" << p << "\" in \"" << envName << "\" because it does not start with a \"/\"\n";
1213f3f3 sago007 2015-09-19 19:24 175
		}
3e5ce8ff sum01 2018-04-16 21:01 176
		p = std::strtok (nullptr, ":");
1213f3f3 sago007 2015-09-19 19:24 177
	}
1213f3f3 sago007 2015-09-19 19:24 178
}
1213f3f3 sago007 2015-09-19 19:24 179
1213f3f3 sago007 2015-09-19 19:24 180
static void appendExtraFolders(const char* envName, const char* defaultValue, std::vector<std::string>& folders) {
68df0922 sum01 2018-04-16 21:07 181
	const char* envValue = std::getenv(envName);
1213f3f3 sago007 2015-09-19 19:24 182
	if (!envValue) {
1213f3f3 sago007 2015-09-19 19:24 183
		envValue = defaultValue;
1213f3f3 sago007 2015-09-19 19:24 184
	}
1213f3f3 sago007 2015-09-19 19:24 185
	appendExtraFoldersTokenizer(envName, envValue, folders);
1213f3f3 sago007 2015-09-19 19:24 186
}
1213f3f3 sago007 2015-09-19 19:24 187
1213f3f3 sago007 2015-09-19 19:24 188
#endif
1213f3f3 sago007 2015-09-19 19:24 189
1213f3f3 sago007 2015-09-19 19:24 190
1213f3f3 sago007 2015-09-19 19:24 191
namespace sago {
1213f3f3 sago007 2015-09-19 19:24 192
1213f3f3 sago007 2015-09-19 19:24 193
std::string getDataHome() {
e5876621 sum01 2018-04-21 23:00 194
#ifdef _WIN32
1213f3f3 sago007 2015-09-19 19:24 195
	return GetAppData();
1eaaab72 sago007 2015-10-26 17:23 196
#elif defined(__APPLE__)
51c64065 Poul Sander 2018-06-24 10:58 197
	return getHome()+"/Library/Application Support";
1213f3f3 sago007 2015-09-19 19:24 198
#else
1213f3f3 sago007 2015-09-19 19:24 199
	return getLinuxFolderDefault("XDG_DATA_HOME", ".local/share");
1213f3f3 sago007 2015-09-19 19:24 200
#endif
1213f3f3 sago007 2015-09-19 19:24 201
}
1213f3f3 sago007 2015-09-19 19:24 202
1213f3f3 sago007 2015-09-19 19:24 203
std::string getConfigHome() {
e5876621 sum01 2018-04-21 23:00 204
#ifdef _WIN32
1213f3f3 sago007 2015-09-19 19:24 205
	return GetAppData();
1eaaab72 sago007 2015-10-26 17:23 206
#elif defined(__APPLE__)
51c64065 Poul Sander 2018-06-24 10:58 207
	return getHome()+"/Library/Application Support";
1213f3f3 sago007 2015-09-19 19:24 208
#else
1213f3f3 sago007 2015-09-19 19:24 209
	return getLinuxFolderDefault("XDG_CONFIG_HOME", ".config");
1213f3f3 sago007 2015-09-19 19:24 210
#endif
1213f3f3 sago007 2015-09-19 19:24 211
}
1213f3f3 sago007 2015-09-19 19:24 212
1213f3f3 sago007 2015-09-19 19:24 213
std::string getCacheDir() {
e5876621 sum01 2018-04-21 23:00 214
#ifdef _WIN32
1213f3f3 sago007 2015-09-19 19:24 215
	return GetAppDataLocal();
1eaaab72 sago007 2015-10-26 17:23 216
#elif defined(__APPLE__)
51c64065 Poul Sander 2018-06-24 10:58 217
	return getHome()+"/Library/Caches";
1213f3f3 sago007 2015-09-19 19:24 218
#else
cc1fdd3e Kogia-sima 2018-05-25 04:26 219
	return getLinuxFolderDefault("XDG_CACHE_HOME", ".cache");
1213f3f3 sago007 2015-09-19 19:24 220
#endif
1213f3f3 sago007 2015-09-19 19:24 221
}
1213f3f3 sago007 2015-09-19 19:24 222
1213f3f3 sago007 2015-09-19 19:24 223
void appendAdditionalDataDirectories(std::vector<std::string>& homes) {
e5876621 sum01 2018-04-21 23:00 224
#ifdef _WIN32
1213f3f3 sago007 2015-09-19 19:24 225
	homes.push_back(GetAppDataCommon());
b3e5a881 sum01 2018-04-21 22:53 226
#elif !defined(__APPLE__)
1213f3f3 sago007 2015-09-19 19:24 227
	appendExtraFolders("XDG_DATA_DIRS", "/usr/local/share/:/usr/share/", homes);
1213f3f3 sago007 2015-09-19 19:24 228
#endif
1213f3f3 sago007 2015-09-19 19:24 229
}
1213f3f3 sago007 2015-09-19 19:24 230
1213f3f3 sago007 2015-09-19 19:24 231
void appendAdditionalConfigDirectories(std::vector<std::string>& homes) {
e5876621 sum01 2018-04-21 23:00 232
#ifdef _WIN32
1213f3f3 sago007 2015-09-19 19:24 233
	homes.push_back(GetAppDataCommon());
b3e5a881 sum01 2018-04-21 22:53 234
#elif !defined(__APPLE__)
1213f3f3 sago007 2015-09-19 19:24 235
	appendExtraFolders("XDG_CONFIG_DIRS", "/etc/xdg", homes);
1213f3f3 sago007 2015-09-19 19:24 236
#endif
1213f3f3 sago007 2015-09-19 19:24 237
}
1213f3f3 sago007 2015-09-19 19:24 238
b3e5a881 sum01 2018-04-21 22:53 239
#if !defined(_WIN32) && !defined(__APPLE__)
1213f3f3 sago007 2015-09-19 19:24 240
struct PlatformFolders::PlatformFoldersData {
1213f3f3 sago007 2015-09-19 19:24 241
	std::map<std::string, std::string> folders;
1213f3f3 sago007 2015-09-19 19:24 242
};
1213f3f3 sago007 2015-09-19 19:24 243
1213f3f3 sago007 2015-09-19 19:24 244
static void PlatformFoldersAddFromFile(const std::string& filename, std::map<std::string, std::string>& folders) {
1213f3f3 sago007 2015-09-19 19:24 245
	std::ifstream infile(filename.c_str());
1213f3f3 sago007 2015-09-19 19:24 246
	std::string line;
1213f3f3 sago007 2015-09-19 19:24 247
	while (std::getline(infile, line)) {
0823b4a5 sago007 2018-06-11 16:33 248
		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 249
			continue;
0823b4a5 sago007 2018-06-11 16:33 250
		}
0823b4a5 sago007 2018-06-11 16:33 251
		try {
0823b4a5 sago007 2018-06-11 16:33 252
			std::size_t splitPos = line.find('=');
0823b4a5 sago007 2018-06-11 16:33 253
			std::string key = line.substr(0, splitPos);
0823b4a5 sago007 2018-06-11 16:33 254
			std::size_t valueStart = line.find('"', splitPos);
0823b4a5 sago007 2018-06-11 16:33 255
			std::size_t valueEnd = line.find('"', valueStart+1);
0823b4a5 sago007 2018-06-11 16:33 256
			std::string value = line.substr(valueStart+1, valueEnd - valueStart - 1);
0823b4a5 sago007 2018-06-11 16:33 257
			folders[key] = value;
0823b4a5 sago007 2018-06-11 16:33 258
		} catch (std::exception&  e) {
0823b4a5 sago007 2018-06-11 16:33 259
			std::cerr << "WARNING: Failed to process \"" << line << "\" from \"" << filename << "\". Error: "<< e.what() << "\n";
1213f3f3 sago007 2015-09-19 19:24 260
			continue;
1213f3f3 sago007 2015-09-19 19:24 261
		}
1213f3f3 sago007 2015-09-19 19:24 262
	}
1213f3f3 sago007 2015-09-19 19:24 263
}
1213f3f3 sago007 2015-09-19 19:24 264
1213f3f3 sago007 2015-09-19 19:24 265
static void PlatformFoldersFillData(std::map<std::string, std::string>& folders) {
1213f3f3 sago007 2015-09-19 19:24 266
	folders["XDG_DOCUMENTS_DIR"] = "$HOME/Documents";
1213f3f3 sago007 2015-09-19 19:24 267
	folders["XDG_DESKTOP_DIR"] = "$HOME/Desktop";
1213f3f3 sago007 2015-09-19 19:24 268
	folders["XDG_DOWNLOAD_DIR"] = "$HOME/Downloads";
1213f3f3 sago007 2015-09-19 19:24 269
	folders["XDG_MUSIC_DIR"] = "$HOME/Music";
1213f3f3 sago007 2015-09-19 19:24 270
	folders["XDG_PICTURES_DIR"] = "$HOME/Pictures";
1213f3f3 sago007 2015-09-19 19:24 271
	folders["XDG_PUBLICSHARE_DIR"] = "$HOME/Public";
1213f3f3 sago007 2015-09-19 19:24 272
	folders["XDG_TEMPLATES_DIR"] = "$HOME/.Templates";
1213f3f3 sago007 2015-09-19 19:24 273
	folders["XDG_VIDEOS_DIR"] = "$HOME/Videos";
1213f3f3 sago007 2015-09-19 19:24 274
	PlatformFoldersAddFromFile( getConfigHome()+"/user-dirs.dirs", folders);
1ff6644e sago007 2018-02-18 16:28 275
	for (std::map<std::string, std::string>::iterator itr = folders.begin() ; itr != folders.end() ; ++itr ) {
1213f3f3 sago007 2015-09-19 19:24 276
		std::string& value = itr->second;
1213f3f3 sago007 2015-09-19 19:24 277
		if (value.compare(0, 5, "$HOME") == 0) {
1213f3f3 sago007 2015-09-19 19:24 278
			value = getHome() + value.substr(5, std::string::npos);
1213f3f3 sago007 2015-09-19 19:24 279
		}
1213f3f3 sago007 2015-09-19 19:24 280
	}
1213f3f3 sago007 2015-09-19 19:24 281
}
1213f3f3 sago007 2015-09-19 19:24 282
#endif
1213f3f3 sago007 2015-09-19 19:24 283
1213f3f3 sago007 2015-09-19 19:24 284
PlatformFolders::PlatformFolders() {
b3e5a881 sum01 2018-04-21 22:53 285
#if !defined(_WIN32) && !defined(__APPLE__)
1213f3f3 sago007 2015-09-19 19:24 286
	this->data = new PlatformFolders::PlatformFoldersData();
1213f3f3 sago007 2015-09-19 19:24 287
	try {
1213f3f3 sago007 2015-09-19 19:24 288
		PlatformFoldersFillData(data->folders);
0fd23ef9 sago007 2018-05-22 17:49 289
	}
0fd23ef9 sago007 2018-05-22 17:49 290
	catch (...) {
1213f3f3 sago007 2015-09-19 19:24 291
		delete this->data;
1213f3f3 sago007 2015-09-19 19:24 292
		throw;
1213f3f3 sago007 2015-09-19 19:24 293
	}
1213f3f3 sago007 2015-09-19 19:24 294
#endif
1213f3f3 sago007 2015-09-19 19:24 295
}
1213f3f3 sago007 2015-09-19 19:24 296
1213f3f3 sago007 2015-09-19 19:24 297
PlatformFolders::~PlatformFolders() {
b3e5a881 sum01 2018-04-21 22:53 298
#if !defined(_WIN32) && !defined(__APPLE__)
1213f3f3 sago007 2015-09-19 19:24 299
	delete this->data;
1213f3f3 sago007 2015-09-19 19:24 300
#endif
1213f3f3 sago007 2015-09-19 19:24 301
}
1213f3f3 sago007 2015-09-19 19:24 302
1213f3f3 sago007 2015-09-19 19:24 303
std::string PlatformFolders::getDocumentsFolder() const {
e5876621 sum01 2018-04-21 23:00 304
#ifdef _WIN32
513c0ebf Poul Sander 2018-06-06 17:05 305
	return GetKnownWindowsFolder(FOLDERID_Documents, "Failed to find My Documents folder");
1eaaab72 sago007 2015-10-26 17:23 306
#elif defined(__APPLE__)
51c64065 Poul Sander 2018-06-24 10:58 307
	return getHome()+"/Documents";
1213f3f3 sago007 2015-09-19 19:24 308
#else
1213f3f3 sago007 2015-09-19 19:24 309
	return data->folders["XDG_DOCUMENTS_DIR"];
1213f3f3 sago007 2015-09-19 19:24 310
#endif
1213f3f3 sago007 2015-09-19 19:24 311
}
1213f3f3 sago007 2015-09-19 19:24 312
1213f3f3 sago007 2015-09-19 19:24 313
std::string PlatformFolders::getDesktopFolder() const {
e5876621 sum01 2018-04-21 23:00 314
#ifdef _WIN32
513c0ebf Poul Sander 2018-06-06 17:05 315
	return GetKnownWindowsFolder(FOLDERID_Desktop, "Failed to find Desktop folder");
1eaaab72 sago007 2015-10-26 17:23 316
#elif defined(__APPLE__)
51c64065 Poul Sander 2018-06-24 10:58 317
	return getHome()+"/Desktop";
1213f3f3 sago007 2015-09-19 19:24 318
#else
1213f3f3 sago007 2015-09-19 19:24 319
	return data->folders["XDG_DESKTOP_DIR"];
1213f3f3 sago007 2015-09-19 19:24 320
#endif
1213f3f3 sago007 2015-09-19 19:24 321
}
1213f3f3 sago007 2015-09-19 19:24 322
1213f3f3 sago007 2015-09-19 19:24 323
std::string PlatformFolders::getPicturesFolder() const {
e5876621 sum01 2018-04-21 23:00 324
#ifdef _WIN32
513c0ebf Poul Sander 2018-06-06 17:05 325
	return GetKnownWindowsFolder(FOLDERID_Pictures, "Failed to find My Pictures folder");
1eaaab72 sago007 2015-10-26 17:23 326
#elif defined(__APPLE__)
51c64065 Poul Sander 2018-06-24 10:58 327
	return getHome()+"/Pictures";
1213f3f3 sago007 2015-09-19 19:24 328
#else
1213f3f3 sago007 2015-09-19 19:24 329
	return data->folders["XDG_PICTURES_DIR"];
1213f3f3 sago007 2015-09-19 19:24 330
#endif
1213f3f3 sago007 2015-09-19 19:24 331
}
1213f3f3 sago007 2015-09-19 19:24 332
5612f69b sago007 2018-06-16 11:22 333
std::string PlatformFolders::getPublicFolder() const {
5612f69b sago007 2018-06-16 11:22 334
#ifdef _WIN32
5612f69b sago007 2018-06-16 11:22 335
	return GetKnownWindowsFolder(FOLDERID_Public, "Failed to find the Public folder");
5612f69b sago007 2018-06-16 11:22 336
#elif defined(__APPLE__)
5612f69b sago007 2018-06-16 11:22 337
	return getHome()+"/Public";
5612f69b sago007 2018-06-16 11:22 338
#else
5612f69b sago007 2018-06-16 11:22 339
	return data->folders["XDG_PUBLICSHARE_DIR"];
5612f69b sago007 2018-06-16 11:22 340
#endif
5612f69b sago007 2018-06-16 11:22 341
}
5612f69b sago007 2018-06-16 11:22 342
1213f3f3 sago007 2015-09-19 19:24 343
std::string PlatformFolders::getDownloadFolder1() const {
e5876621 sum01 2018-04-21 23:00 344
#ifdef _WIN32
513c0ebf Poul Sander 2018-06-06 17:05 345
	return GetKnownWindowsFolder(FOLDERID_Downloads, "Failed to find My Downloads folder");
1eaaab72 sago007 2015-10-26 17:23 346
#elif defined(__APPLE__)
51c64065 Poul Sander 2018-06-24 10:58 347
	return getHome()+"/Downloads";
1213f3f3 sago007 2015-09-19 19:24 348
#else
1213f3f3 sago007 2015-09-19 19:24 349
	return data->folders["XDG_DOWNLOAD_DIR"];
1213f3f3 sago007 2015-09-19 19:24 350
#endif
1213f3f3 sago007 2015-09-19 19:24 351
}
1213f3f3 sago007 2015-09-19 19:24 352
1213f3f3 sago007 2015-09-19 19:24 353
std::string PlatformFolders::getMusicFolder() const {
e5876621 sum01 2018-04-21 23:00 354
#ifdef _WIN32
513c0ebf Poul Sander 2018-06-06 17:05 355
	return GetKnownWindowsFolder(FOLDERID_Music, "Failed to find My Music folder");
1eaaab72 sago007 2015-10-26 17:23 356
#elif defined(__APPLE__)
51c64065 Poul Sander 2018-06-24 10:58 357
	return getHome()+"/Music";
1213f3f3 sago007 2015-09-19 19:24 358
#else
1213f3f3 sago007 2015-09-19 19:24 359
	return data->folders["XDG_MUSIC_DIR"];
1213f3f3 sago007 2015-09-19 19:24 360
#endif
1213f3f3 sago007 2015-09-19 19:24 361
}
1213f3f3 sago007 2015-09-19 19:24 362
1213f3f3 sago007 2015-09-19 19:24 363
std::string PlatformFolders::getVideoFolder() const {
e5876621 sum01 2018-04-21 23:00 364
#ifdef _WIN32
513c0ebf Poul Sander 2018-06-06 17:05 365
	return GetKnownWindowsFolder(FOLDERID_Videos, "Failed to find My Video folder");
1eaaab72 sago007 2015-10-26 17:23 366
#elif defined(__APPLE__)
51c64065 Poul Sander 2018-06-24 10:58 367
	return getHome()+"/Movies";
1213f3f3 sago007 2015-09-19 19:24 368
#else
1213f3f3 sago007 2015-09-19 19:24 369
	return data->folders["XDG_VIDEOS_DIR"];
1213f3f3 sago007 2015-09-19 19:24 370
#endif
1213f3f3 sago007 2015-09-19 19:24 371
}
1213f3f3 sago007 2015-09-19 19:24 372
1213f3f3 sago007 2015-09-19 19:24 373
std::string PlatformFolders::getSaveGamesFolder1() const {
e5876621 sum01 2018-04-21 23:00 374
#ifdef _WIN32
1213f3f3 sago007 2015-09-19 19:24 375
	//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 376
	//Data that should not be user accessible should be placed under GetDataHome() instead
513c0ebf Poul Sander 2018-06-06 17:05 377
	return GetKnownWindowsFolder(FOLDERID_Documents, "Failed to find My Documents folder")+"\\My Games";
1eaaab72 sago007 2015-10-26 17:23 378
#elif defined(__APPLE__)
51c64065 Poul Sander 2018-06-24 10:58 379
	return getHome()+"/Library/Application Support";
1213f3f3 sago007 2015-09-19 19:24 380
#else
1213f3f3 sago007 2015-09-19 19:24 381
	return getDataHome();
1213f3f3 sago007 2015-09-19 19:24 382
#endif
1213f3f3 sago007 2015-09-19 19:24 383
}
1213f3f3 sago007 2015-09-19 19:24 384
0fd23ef9 sago007 2018-05-22 17:49 385
std::string getDesktopFolder() {
0fd23ef9 sago007 2018-05-22 17:49 386
	return PlatformFolders().getDesktopFolder();
0fd23ef9 sago007 2018-05-22 17:49 387
}
0fd23ef9 sago007 2018-05-22 17:49 388
0fd23ef9 sago007 2018-05-22 17:49 389
std::string getDocumentsFolder() {
0fd23ef9 sago007 2018-05-22 17:49 390
	return PlatformFolders().getDocumentsFolder();
0fd23ef9 sago007 2018-05-22 17:49 391
}
0fd23ef9 sago007 2018-05-22 17:49 392
513c0ebf Poul Sander 2018-06-06 17:05 393
std::string getDownloadFolder() {
0fd23ef9 sago007 2018-05-22 17:49 394
	return PlatformFolders().getDownloadFolder1();
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 getDownloadFolder1() {
513c0ebf Poul Sander 2018-06-06 17:05 398
	return getDownloadFolder();
513c0ebf Poul Sander 2018-06-06 17:05 399
}
513c0ebf Poul Sander 2018-06-06 17:05 400
0fd23ef9 sago007 2018-05-22 17:49 401
std::string getPicturesFolder() {
0fd23ef9 sago007 2018-05-22 17:49 402
	return PlatformFolders().getPicturesFolder();
0fd23ef9 sago007 2018-05-22 17:49 403
}
0fd23ef9 sago007 2018-05-22 17:49 404
5612f69b sago007 2018-06-16 11:22 405
std::string getPublicFolder() {
5612f69b sago007 2018-06-16 11:22 406
	return PlatformFolders().getPublicFolder();
5612f69b sago007 2018-06-16 11:22 407
}
5612f69b sago007 2018-06-16 11:22 408
0fd23ef9 sago007 2018-05-22 17:49 409
std::string getMusicFolder() {
0fd23ef9 sago007 2018-05-22 17:49 410
	return PlatformFolders().getMusicFolder();
0fd23ef9 sago007 2018-05-22 17:49 411
}
0fd23ef9 sago007 2018-05-22 17:49 412
0fd23ef9 sago007 2018-05-22 17:49 413
std::string getVideoFolder() {
0fd23ef9 sago007 2018-05-22 17:49 414
	return PlatformFolders().getVideoFolder();
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 getSaveGamesFolder1() {
0fd23ef9 sago007 2018-05-22 17:49 418
	return PlatformFolders().getSaveGamesFolder1();
0fd23ef9 sago007 2018-05-22 17:49 419
}
1213f3f3 sago007 2015-09-19 19:24 420
513c0ebf Poul Sander 2018-06-06 17:05 421
std::string getSaveGamesFolder2() {
513c0ebf Poul Sander 2018-06-06 17:05 422
#ifdef _WIN32
513c0ebf Poul Sander 2018-06-06 17:05 423
	return GetKnownWindowsFolder(FOLDERID_SavedGames, "Failed to find Saved Games folder");
513c0ebf Poul Sander 2018-06-06 17:05 424
#else
513c0ebf Poul Sander 2018-06-06 17:05 425
	return PlatformFolders().getSaveGamesFolder1();
513c0ebf Poul Sander 2018-06-06 17:05 426
#endif
513c0ebf Poul Sander 2018-06-06 17:05 427
}
1213f3f3 sago007 2015-09-19 19:24 428
1213f3f3 sago007 2015-09-19 19:24 429
}  //namespace sago
1970-01-01 00:00 430