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
e5876621 sum01 2018-04-21 23:00 35
#ifdef _WIN32
fff3cc86 sum01 2018-04-22 02:15 36
// Make sure we don't bring in all the extra junk with windows.h
fff3cc86 sum01 2018-04-22 02:15 37
#ifndef WIN32_LEAN_AND_MEAN
fff3cc86 sum01 2018-04-22 02:15 38
#define WIN32_LEAN_AND_MEAN
fff3cc86 sum01 2018-04-22 02:15 39
#endif
fff3cc86 sum01 2018-04-22 02:15 40
// stringapiset.h depends on this
1213f3f3 sago007 2015-09-19 19:24 41
#include <windows.h>
fff3cc86 sum01 2018-04-22 02:15 42
// For SUCCEEDED macro
fff3cc86 sum01 2018-04-22 02:15 43
#include <winerror.h>
fff3cc86 sum01 2018-04-22 02:15 44
// For WideCharToMultiByte
fff3cc86 sum01 2018-04-22 02:15 45
#include <stringapiset.h>
fff3cc86 sum01 2018-04-22 02:15 46
// For SHGetFolderPathW and various CSIDL "magic numbers"
1213f3f3 sago007 2015-09-19 19:24 47
#include <shlobj.h>
1213f3f3 sago007 2015-09-19 19:24 48
ba428e10 sum01 2018-04-27 05:42 49
static std::string win32_utf16_to_utf8(const wchar_t* wstr) {
b81b63ae sago007 2016-10-03 20:30 50
	std::string res;
b81b63ae sago007 2016-10-03 20:30 51
	// 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 52
	int actualSize = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, nullptr, 0, nullptr, nullptr);
b81b63ae sago007 2016-10-03 20:30 53
	if (actualSize > 0) {
b81b63ae sago007 2016-10-03 20:30 54
		//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 55
		std::vector<char> buffer(actualSize);
3e5ce8ff sum01 2018-04-16 21:01 56
		actualSize = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, &buffer[0], buffer.size(), nullptr, nullptr);
b81b63ae sago007 2016-10-03 20:30 57
		res = buffer.data();
b81b63ae sago007 2016-10-03 20:30 58
	}
b81b63ae sago007 2016-10-03 20:30 59
	if (actualSize == 0) {
b81b63ae sago007 2016-10-03 20:30 60
		// WideCharToMultiByte return 0 for errors.
a3a13ec8 sum01 2018-04-16 21:07 61
		const std::string errorMsg = "UTF16 to UTF8 failed with error code: " + GetLastError();
b81b63ae sago007 2016-10-03 20:30 62
		throw std::runtime_error(errorMsg.c_str());
b81b63ae sago007 2016-10-03 20:30 63
	}
b81b63ae sago007 2016-10-03 20:30 64
	return res;
b81b63ae sago007 2016-10-03 20:30 65
}
b81b63ae sago007 2016-10-03 20:30 66
513c0ebf Poul Sander 2018-06-06 17:05 67
class FreeCoTaskMemory {
513c0ebf Poul Sander 2018-06-06 17:05 68
	LPWSTR pointer = NULL;
513c0ebf Poul Sander 2018-06-06 17:05 69
public:
513c0ebf Poul Sander 2018-06-06 17:05 70
	FreeCoTaskMemory(LPWSTR pointer) : pointer(pointer) {};
513c0ebf Poul Sander 2018-06-06 17:05 71
	~FreeCoTaskMemory() {
513c0ebf Poul Sander 2018-06-06 17:05 72
		CoTaskMemFree(pointer);
513c0ebf Poul Sander 2018-06-06 17:05 73
	}
513c0ebf Poul Sander 2018-06-06 17:05 74
};
513c0ebf Poul Sander 2018-06-06 17:05 75
513c0ebf Poul Sander 2018-06-06 17:05 76
static std::string GetKnownWindowsFolder(REFKNOWNFOLDERID folderId, const char* errorMsg) {
513c0ebf Poul Sander 2018-06-06 17:05 77
	LPWSTR wszPath = NULL;
513c0ebf Poul Sander 2018-06-06 17:05 78
	HRESULT hr;
513c0ebf Poul Sander 2018-06-06 17:05 79
	hr = SHGetKnownFolderPath(folderId, KF_FLAG_CREATE, NULL, &wszPath);
513c0ebf Poul Sander 2018-06-06 17:05 80
	FreeCoTaskMemory scopeBoundMemory(wszPath);
513c0ebf Poul Sander 2018-06-06 17:05 81
513c0ebf Poul Sander 2018-06-06 17:05 82
	if (!SUCCEEDED(hr)) {
1213f3f3 sago007 2015-09-19 19:24 83
		throw std::runtime_error(errorMsg);
1213f3f3 sago007 2015-09-19 19:24 84
	}
513c0ebf Poul Sander 2018-06-06 17:05 85
	return win32_utf16_to_utf8(wszPath);
1213f3f3 sago007 2015-09-19 19:24 86
}
1213f3f3 sago007 2015-09-19 19:24 87
1213f3f3 sago007 2015-09-19 19:24 88
static std::string GetAppData() {
513c0ebf Poul Sander 2018-06-06 17:05 89
	return GetKnownWindowsFolder(FOLDERID_RoamingAppData, "RoamingAppData could not be found");
1213f3f3 sago007 2015-09-19 19:24 90
}
1213f3f3 sago007 2015-09-19 19:24 91
1213f3f3 sago007 2015-09-19 19:24 92
static std::string GetAppDataCommon() {
513c0ebf Poul Sander 2018-06-06 17:05 93
	return GetKnownWindowsFolder(FOLDERID_ProgramData, "ProgramData could not be found");
1213f3f3 sago007 2015-09-19 19:24 94
}
1213f3f3 sago007 2015-09-19 19:24 95
1213f3f3 sago007 2015-09-19 19:24 96
static std::string GetAppDataLocal() {
513c0ebf Poul Sander 2018-06-06 17:05 97
	return GetKnownWindowsFolder(FOLDERID_LocalAppData, "LocalAppData could not be found");
1213f3f3 sago007 2015-09-19 19:24 98
}
1eaaab72 sago007 2015-10-26 17:23 99
#elif defined(__APPLE__)
1eaaab72 sago007 2015-10-26 17:23 100
#include <CoreServices/CoreServices.h>
1eaaab72 sago007 2015-10-26 17:23 101
1eaaab72 sago007 2015-10-26 17:23 102
static std::string GetMacFolder(OSType folderType, const char* errorMsg) {
1eaaab72 sago007 2015-10-26 17:23 103
	std::string ret;
1eaaab72 sago007 2015-10-26 17:23 104
	FSRef ref;
d2b67cec sago007 2018-03-24 09:52 105
	char path[PATH_MAX];
d2b67cec sago007 2018-03-24 09:52 106
	OSStatus err = FSFindFolder( kUserDomain, folderType, kCreateFolder, &ref );
1eaaab72 sago007 2015-10-26 17:23 107
	if (err != noErr) {
1eaaab72 sago007 2015-10-26 17:23 108
		throw std::runtime_error(errorMsg);
1eaaab72 sago007 2015-10-26 17:23 109
	}
d2b67cec sago007 2018-03-24 09:52 110
	FSRefMakePath( &ref, (UInt8*)&path, PATH_MAX );
1eaaab72 sago007 2015-10-26 17:23 111
	ret = path;
1eaaab72 sago007 2015-10-26 17:23 112
	return ret;
1eaaab72 sago007 2015-10-26 17:23 113
}
1eaaab72 sago007 2015-10-26 17:23 114
1213f3f3 sago007 2015-09-19 19:24 115
#else
1213f3f3 sago007 2015-09-19 19:24 116
#include <map>
1213f3f3 sago007 2015-09-19 19:24 117
#include <fstream>
dcb0fec2 sago007 2015-09-21 17:50 118
#include <pwd.h>
dcb0fec2 sago007 2015-09-21 17:50 119
#include <unistd.h>
dcb0fec2 sago007 2015-09-21 17:50 120
#include <sys/types.h>
a3a13ec8 sum01 2018-04-16 21:07 121
// For strlen and strtok
68df0922 sum01 2018-04-16 21:07 122
#include <cstring>
1213f3f3 sago007 2015-09-19 19:24 123
//Typically Linux. For easy reading the comments will just say Linux but should work with most *nixes
1213f3f3 sago007 2015-09-19 19:24 124
1213f3f3 sago007 2015-09-19 19:24 125
static void throwOnRelative(const char* envName, const char* envValue) {
1213f3f3 sago007 2015-09-19 19:24 126
	if (envValue[0] != '/') {
1213f3f3 sago007 2015-09-19 19:24 127
		char buffer[200];
da123945 sum01 2018-04-16 20:58 128
		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 129
		throw std::runtime_error(buffer);
1213f3f3 sago007 2015-09-19 19:24 130
	}
1213f3f3 sago007 2015-09-19 19:24 131
}
1213f3f3 sago007 2015-09-19 19:24 132
dcb0fec2 sago007 2015-09-21 17:50 133
/**
1ff6644e sago007 2018-02-18 16:28 134
 * Retrives the effective user's home dir.
1ff6644e sago007 2018-02-18 16:28 135
 * If the user is running as root we ignore the HOME environment. It works badly with sudo.
dcb0fec2 sago007 2015-09-21 17:50 136
 * Writing to $HOME as root implies security concerns that a multiplatform program cannot be assumed to handle.
1ff6644e sago007 2018-02-18 16:28 137
 * @return The home directory. HOME environment is respected for non-root users if it exists.
dcb0fec2 sago007 2015-09-21 17:50 138
 */
1213f3f3 sago007 2015-09-19 19:24 139
static std::string getHome() {
1213f3f3 sago007 2015-09-19 19:24 140
	std::string res;
dcb0fec2 sago007 2015-09-21 17:50 141
	int uid = getuid();
68df0922 sum01 2018-04-16 21:07 142
	const char* homeEnv = std::getenv("HOME");
dcb0fec2 sago007 2015-09-21 17:50 143
	if ( uid != 0 && homeEnv) {
dcb0fec2 sago007 2015-09-21 17:50 144
		//We only acknowlegde HOME if not root.
dcb0fec2 sago007 2015-09-21 17:50 145
		res = homeEnv;
dcb0fec2 sago007 2015-09-21 17:50 146
		return res;
dcb0fec2 sago007 2015-09-21 17:50 147
	}
0fd23ef9 sago007 2018-05-22 17:49 148
	struct passwd* pw = getpwuid(uid);
dcb0fec2 sago007 2015-09-21 17:50 149
	if (!pw) {
dcb0fec2 sago007 2015-09-21 17:50 150
		throw std::runtime_error("Unable to get passwd struct.");
dcb0fec2 sago007 2015-09-21 17:50 151
	}
dcb0fec2 sago007 2015-09-21 17:50 152
	const char* tempRes = pw->pw_dir;
1213f3f3 sago007 2015-09-19 19:24 153
	if (!tempRes) {
dcb0fec2 sago007 2015-09-21 17:50 154
		throw std::runtime_error("User has no home directory");
1213f3f3 sago007 2015-09-19 19:24 155
	}
1213f3f3 sago007 2015-09-19 19:24 156
	res = tempRes;
1213f3f3 sago007 2015-09-19 19:24 157
	return res;
1213f3f3 sago007 2015-09-19 19:24 158
}
1213f3f3 sago007 2015-09-19 19:24 159
1213f3f3 sago007 2015-09-19 19:24 160
static std::string getLinuxFolderDefault(const char* envName, const char* defaultRelativePath) {
1213f3f3 sago007 2015-09-19 19:24 161
	std::string res;
68df0922 sum01 2018-04-16 21:07 162
	const char* tempRes = std::getenv(envName);
1213f3f3 sago007 2015-09-19 19:24 163
	if (tempRes) {
1213f3f3 sago007 2015-09-19 19:24 164
		throwOnRelative(envName, tempRes);
1213f3f3 sago007 2015-09-19 19:24 165
		res = tempRes;
1213f3f3 sago007 2015-09-19 19:24 166
		return res;
1213f3f3 sago007 2015-09-19 19:24 167
	}
1213f3f3 sago007 2015-09-19 19:24 168
	res = getHome() + "/" + defaultRelativePath;
1213f3f3 sago007 2015-09-19 19:24 169
	return res;
1213f3f3 sago007 2015-09-19 19:24 170
}
1213f3f3 sago007 2015-09-19 19:24 171
1213f3f3 sago007 2015-09-19 19:24 172
static void appendExtraFoldersTokenizer(const char* envName, const char* envValue, std::vector<std::string>& folders) {
68df0922 sum01 2018-04-16 21:07 173
	std::vector<char> buffer(envValue, envValue + std::strlen(envValue) + 1);
a3a13ec8 sum01 2018-04-16 21:07 174
	char* p = std::strtok ( &buffer[0], ":");
3e5ce8ff sum01 2018-04-16 21:01 175
	while (p != nullptr) {
1213f3f3 sago007 2015-09-19 19:24 176
		if (p[0] == '/') {
1213f3f3 sago007 2015-09-19 19:24 177
			folders.push_back(p);
1213f3f3 sago007 2015-09-19 19:24 178
		}
1213f3f3 sago007 2015-09-19 19:24 179
		else {
1213f3f3 sago007 2015-09-19 19:24 180
			//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 181
			//The XDG documentation indicates that the folder should be ignored but that the program should continue.
b2ba7c7d sago007 2016-09-10 17:08 182
			std::cerr << "Skipping path \"" << p << "\" in \"" << envName << "\" because it does not start with a \"/\"\n";
1213f3f3 sago007 2015-09-19 19:24 183
		}
3e5ce8ff sum01 2018-04-16 21:01 184
		p = std::strtok (nullptr, ":");
1213f3f3 sago007 2015-09-19 19:24 185
	}
1213f3f3 sago007 2015-09-19 19:24 186
}
1213f3f3 sago007 2015-09-19 19:24 187
1213f3f3 sago007 2015-09-19 19:24 188
static void appendExtraFolders(const char* envName, const char* defaultValue, std::vector<std::string>& folders) {
68df0922 sum01 2018-04-16 21:07 189
	const char* envValue = std::getenv(envName);
1213f3f3 sago007 2015-09-19 19:24 190
	if (!envValue) {
1213f3f3 sago007 2015-09-19 19:24 191
		envValue = defaultValue;
1213f3f3 sago007 2015-09-19 19:24 192
	}
1213f3f3 sago007 2015-09-19 19:24 193
	appendExtraFoldersTokenizer(envName, envValue, folders);
1213f3f3 sago007 2015-09-19 19:24 194
}
1213f3f3 sago007 2015-09-19 19:24 195
1213f3f3 sago007 2015-09-19 19:24 196
#endif
1213f3f3 sago007 2015-09-19 19:24 197
1213f3f3 sago007 2015-09-19 19:24 198
1213f3f3 sago007 2015-09-19 19:24 199
namespace sago {
1213f3f3 sago007 2015-09-19 19:24 200
1213f3f3 sago007 2015-09-19 19:24 201
std::string getDataHome() {
e5876621 sum01 2018-04-21 23:00 202
#ifdef _WIN32
1213f3f3 sago007 2015-09-19 19:24 203
	return GetAppData();
1eaaab72 sago007 2015-10-26 17:23 204
#elif defined(__APPLE__)
1eaaab72 sago007 2015-10-26 17:23 205
	return GetMacFolder(kApplicationSupportFolderType, "Failed to find the Application Support Folder");
1213f3f3 sago007 2015-09-19 19:24 206
#else
1213f3f3 sago007 2015-09-19 19:24 207
	return getLinuxFolderDefault("XDG_DATA_HOME", ".local/share");
1213f3f3 sago007 2015-09-19 19:24 208
#endif
1213f3f3 sago007 2015-09-19 19:24 209
}
1213f3f3 sago007 2015-09-19 19:24 210
1213f3f3 sago007 2015-09-19 19:24 211
std::string getConfigHome() {
e5876621 sum01 2018-04-21 23:00 212
#ifdef _WIN32
1213f3f3 sago007 2015-09-19 19:24 213
	return GetAppData();
1eaaab72 sago007 2015-10-26 17:23 214
#elif defined(__APPLE__)
1eaaab72 sago007 2015-10-26 17:23 215
	return GetMacFolder(kApplicationSupportFolderType, "Failed to find the Application Support Folder");
1213f3f3 sago007 2015-09-19 19:24 216
#else
1213f3f3 sago007 2015-09-19 19:24 217
	return getLinuxFolderDefault("XDG_CONFIG_HOME", ".config");
1213f3f3 sago007 2015-09-19 19:24 218
#endif
1213f3f3 sago007 2015-09-19 19:24 219
}
1213f3f3 sago007 2015-09-19 19:24 220
1213f3f3 sago007 2015-09-19 19:24 221
std::string getCacheDir() {
e5876621 sum01 2018-04-21 23:00 222
#ifdef _WIN32
1213f3f3 sago007 2015-09-19 19:24 223
	return GetAppDataLocal();
1eaaab72 sago007 2015-10-26 17:23 224
#elif defined(__APPLE__)
1eaaab72 sago007 2015-10-26 17:23 225
	return GetMacFolder(kCachedDataFolderType, "Failed to find the Application Support Folder");
1213f3f3 sago007 2015-09-19 19:24 226
#else
cc1fdd3e Kogia-sima 2018-05-25 04:26 227
	return getLinuxFolderDefault("XDG_CACHE_HOME", ".cache");
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 appendAdditionalDataDirectories(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_DATA_DIRS", "/usr/local/share/:/usr/share/", 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
1213f3f3 sago007 2015-09-19 19:24 239
void appendAdditionalConfigDirectories(std::vector<std::string>& homes) {
e5876621 sum01 2018-04-21 23:00 240
#ifdef _WIN32
1213f3f3 sago007 2015-09-19 19:24 241
	homes.push_back(GetAppDataCommon());
b3e5a881 sum01 2018-04-21 22:53 242
#elif !defined(__APPLE__)
1213f3f3 sago007 2015-09-19 19:24 243
	appendExtraFolders("XDG_CONFIG_DIRS", "/etc/xdg", homes);
1213f3f3 sago007 2015-09-19 19:24 244
#endif
1213f3f3 sago007 2015-09-19 19:24 245
}
1213f3f3 sago007 2015-09-19 19:24 246
b3e5a881 sum01 2018-04-21 22:53 247
#if !defined(_WIN32) && !defined(__APPLE__)
1213f3f3 sago007 2015-09-19 19:24 248
struct PlatformFolders::PlatformFoldersData {
1213f3f3 sago007 2015-09-19 19:24 249
	std::map<std::string, std::string> folders;
1213f3f3 sago007 2015-09-19 19:24 250
};
1213f3f3 sago007 2015-09-19 19:24 251
1213f3f3 sago007 2015-09-19 19:24 252
static void PlatformFoldersAddFromFile(const std::string& filename, std::map<std::string, std::string>& folders) {
1213f3f3 sago007 2015-09-19 19:24 253
	std::ifstream infile(filename.c_str());
1213f3f3 sago007 2015-09-19 19:24 254
	std::string line;
1213f3f3 sago007 2015-09-19 19:24 255
	while (std::getline(infile, line)) {
1213f3f3 sago007 2015-09-19 19:24 256
		if (line.length() == 0 || line.at(0) == '#') {
1213f3f3 sago007 2015-09-19 19:24 257
			continue;
1213f3f3 sago007 2015-09-19 19:24 258
		}
1213f3f3 sago007 2015-09-19 19:24 259
		std::size_t splitPos = line.find("=");
1213f3f3 sago007 2015-09-19 19:24 260
		std::string key = line.substr(0, splitPos);
1213f3f3 sago007 2015-09-19 19:24 261
		std::string value = line.substr(splitPos+2, line.length()-splitPos-3);
1213f3f3 sago007 2015-09-19 19:24 262
		folders[key] = value;
1213f3f3 sago007 2015-09-19 19:24 263
	}
1213f3f3 sago007 2015-09-19 19:24 264
}
1213f3f3 sago007 2015-09-19 19:24 265
1213f3f3 sago007 2015-09-19 19:24 266
static void PlatformFoldersFillData(std::map<std::string, std::string>& folders) {
1213f3f3 sago007 2015-09-19 19:24 267
	folders["XDG_DOCUMENTS_DIR"] = "$HOME/Documents";
1213f3f3 sago007 2015-09-19 19:24 268
	folders["XDG_DESKTOP_DIR"] = "$HOME/Desktop";
1213f3f3 sago007 2015-09-19 19:24 269
	folders["XDG_DOWNLOAD_DIR"] = "$HOME/Downloads";
1213f3f3 sago007 2015-09-19 19:24 270
	folders["XDG_MUSIC_DIR"] = "$HOME/Music";
1213f3f3 sago007 2015-09-19 19:24 271
	folders["XDG_PICTURES_DIR"] = "$HOME/Pictures";
1213f3f3 sago007 2015-09-19 19:24 272
	folders["XDG_PUBLICSHARE_DIR"] = "$HOME/Public";
1213f3f3 sago007 2015-09-19 19:24 273
	folders["XDG_TEMPLATES_DIR"] = "$HOME/.Templates";
1213f3f3 sago007 2015-09-19 19:24 274
	folders["XDG_VIDEOS_DIR"] = "$HOME/Videos";
1213f3f3 sago007 2015-09-19 19:24 275
	PlatformFoldersAddFromFile( getConfigHome()+"/user-dirs.dirs", folders);
1ff6644e sago007 2018-02-18 16:28 276
	for (std::map<std::string, std::string>::iterator itr = folders.begin() ; itr != folders.end() ; ++itr ) {
1213f3f3 sago007 2015-09-19 19:24 277
		std::string& value = itr->second;
1213f3f3 sago007 2015-09-19 19:24 278
		if (value.compare(0, 5, "$HOME") == 0) {
1213f3f3 sago007 2015-09-19 19:24 279
			value = getHome() + value.substr(5, std::string::npos);
1213f3f3 sago007 2015-09-19 19:24 280
		}
1213f3f3 sago007 2015-09-19 19:24 281
	}
1213f3f3 sago007 2015-09-19 19:24 282
}
1213f3f3 sago007 2015-09-19 19:24 283
#endif
1213f3f3 sago007 2015-09-19 19:24 284
1213f3f3 sago007 2015-09-19 19:24 285
PlatformFolders::PlatformFolders() {
b3e5a881 sum01 2018-04-21 22:53 286
#if !defined(_WIN32) && !defined(__APPLE__)
1213f3f3 sago007 2015-09-19 19:24 287
	this->data = new PlatformFolders::PlatformFoldersData();
1213f3f3 sago007 2015-09-19 19:24 288
	try {
1213f3f3 sago007 2015-09-19 19:24 289
		PlatformFoldersFillData(data->folders);
0fd23ef9 sago007 2018-05-22 17:49 290
	}
0fd23ef9 sago007 2018-05-22 17:49 291
	catch (...) {
1213f3f3 sago007 2015-09-19 19:24 292
		delete this->data;
1213f3f3 sago007 2015-09-19 19:24 293
		throw;
1213f3f3 sago007 2015-09-19 19:24 294
	}
1213f3f3 sago007 2015-09-19 19:24 295
#endif
1213f3f3 sago007 2015-09-19 19:24 296
}
1213f3f3 sago007 2015-09-19 19:24 297
1213f3f3 sago007 2015-09-19 19:24 298
PlatformFolders::~PlatformFolders() {
b3e5a881 sum01 2018-04-21 22:53 299
#if !defined(_WIN32) && !defined(__APPLE__)
1213f3f3 sago007 2015-09-19 19:24 300
	delete this->data;
1213f3f3 sago007 2015-09-19 19:24 301
#endif
1213f3f3 sago007 2015-09-19 19:24 302
}
1213f3f3 sago007 2015-09-19 19:24 303
1213f3f3 sago007 2015-09-19 19:24 304
std::string PlatformFolders::getDocumentsFolder() const {
e5876621 sum01 2018-04-21 23:00 305
#ifdef _WIN32
513c0ebf Poul Sander 2018-06-06 17:05 306
	return GetKnownWindowsFolder(FOLDERID_Documents, "Failed to find My Documents folder");
1eaaab72 sago007 2015-10-26 17:23 307
#elif defined(__APPLE__)
1eaaab72 sago007 2015-10-26 17:23 308
	return GetMacFolder(kDocumentsFolderType, "Failed to find Documents Folder");
1213f3f3 sago007 2015-09-19 19:24 309
#else
1213f3f3 sago007 2015-09-19 19:24 310
	return data->folders["XDG_DOCUMENTS_DIR"];
1213f3f3 sago007 2015-09-19 19:24 311
#endif
1213f3f3 sago007 2015-09-19 19:24 312
}
1213f3f3 sago007 2015-09-19 19:24 313
1213f3f3 sago007 2015-09-19 19:24 314
std::string PlatformFolders::getDesktopFolder() const {
e5876621 sum01 2018-04-21 23:00 315
#ifdef _WIN32
513c0ebf Poul Sander 2018-06-06 17:05 316
	return GetKnownWindowsFolder(FOLDERID_Desktop, "Failed to find Desktop folder");
1eaaab72 sago007 2015-10-26 17:23 317
#elif defined(__APPLE__)
1eaaab72 sago007 2015-10-26 17:23 318
	return GetMacFolder(kDesktopFolderType, "Failed to find Desktop folder");
1213f3f3 sago007 2015-09-19 19:24 319
#else
1213f3f3 sago007 2015-09-19 19:24 320
	return data->folders["XDG_DESKTOP_DIR"];
1213f3f3 sago007 2015-09-19 19:24 321
#endif
1213f3f3 sago007 2015-09-19 19:24 322
}
1213f3f3 sago007 2015-09-19 19:24 323
1213f3f3 sago007 2015-09-19 19:24 324
std::string PlatformFolders::getPicturesFolder() const {
e5876621 sum01 2018-04-21 23:00 325
#ifdef _WIN32
513c0ebf Poul Sander 2018-06-06 17:05 326
	return GetKnownWindowsFolder(FOLDERID_Pictures, "Failed to find My Pictures folder");
1eaaab72 sago007 2015-10-26 17:23 327
#elif defined(__APPLE__)
1eaaab72 sago007 2015-10-26 17:23 328
	return GetMacFolder(kPictureDocumentsFolderType, "Failed to find Picture folder");
1213f3f3 sago007 2015-09-19 19:24 329
#else
1213f3f3 sago007 2015-09-19 19:24 330
	return data->folders["XDG_PICTURES_DIR"];
1213f3f3 sago007 2015-09-19 19:24 331
#endif
1213f3f3 sago007 2015-09-19 19:24 332
}
1213f3f3 sago007 2015-09-19 19:24 333
1213f3f3 sago007 2015-09-19 19:24 334
std::string PlatformFolders::getDownloadFolder1() const {
e5876621 sum01 2018-04-21 23:00 335
#ifdef _WIN32
513c0ebf Poul Sander 2018-06-06 17:05 336
	return GetKnownWindowsFolder(FOLDERID_Downloads, "Failed to find My Downloads folder");
1eaaab72 sago007 2015-10-26 17:23 337
#elif defined(__APPLE__)
1eaaab72 sago007 2015-10-26 17:23 338
	return GetMacFolder(kDownloadsFolderType, "Failed to find Download folder");
1213f3f3 sago007 2015-09-19 19:24 339
#else
1213f3f3 sago007 2015-09-19 19:24 340
	return data->folders["XDG_DOWNLOAD_DIR"];
1213f3f3 sago007 2015-09-19 19:24 341
#endif
1213f3f3 sago007 2015-09-19 19:24 342
}
1213f3f3 sago007 2015-09-19 19:24 343
1213f3f3 sago007 2015-09-19 19:24 344
std::string PlatformFolders::getMusicFolder() const {
e5876621 sum01 2018-04-21 23:00 345
#ifdef _WIN32
513c0ebf Poul Sander 2018-06-06 17:05 346
	return GetKnownWindowsFolder(FOLDERID_Music, "Failed to find My Music folder");
1eaaab72 sago007 2015-10-26 17:23 347
#elif defined(__APPLE__)
1eaaab72 sago007 2015-10-26 17:23 348
	return GetMacFolder(kMusicDocumentsFolderType, "Failed to find Music folder");
1213f3f3 sago007 2015-09-19 19:24 349
#else
1213f3f3 sago007 2015-09-19 19:24 350
	return data->folders["XDG_MUSIC_DIR"];
1213f3f3 sago007 2015-09-19 19:24 351
#endif
1213f3f3 sago007 2015-09-19 19:24 352
}
1213f3f3 sago007 2015-09-19 19:24 353
1213f3f3 sago007 2015-09-19 19:24 354
std::string PlatformFolders::getVideoFolder() const {
e5876621 sum01 2018-04-21 23:00 355
#ifdef _WIN32
513c0ebf Poul Sander 2018-06-06 17:05 356
	return GetKnownWindowsFolder(FOLDERID_Videos, "Failed to find My Video folder");
1eaaab72 sago007 2015-10-26 17:23 357
#elif defined(__APPLE__)
1eaaab72 sago007 2015-10-26 17:23 358
	return GetMacFolder(kMovieDocumentsFolderType, "Failed to find Movie folder");
1213f3f3 sago007 2015-09-19 19:24 359
#else
1213f3f3 sago007 2015-09-19 19:24 360
	return data->folders["XDG_VIDEOS_DIR"];
1213f3f3 sago007 2015-09-19 19:24 361
#endif
1213f3f3 sago007 2015-09-19 19:24 362
}
1213f3f3 sago007 2015-09-19 19:24 363
1213f3f3 sago007 2015-09-19 19:24 364
std::string PlatformFolders::getSaveGamesFolder1() const {
e5876621 sum01 2018-04-21 23:00 365
#ifdef _WIN32
1213f3f3 sago007 2015-09-19 19:24 366
	//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 367
	//Data that should not be user accessible should be placed under GetDataHome() instead
513c0ebf Poul Sander 2018-06-06 17:05 368
	return GetKnownWindowsFolder(FOLDERID_Documents, "Failed to find My Documents folder")+"\\My Games";
1eaaab72 sago007 2015-10-26 17:23 369
#elif defined(__APPLE__)
1eaaab72 sago007 2015-10-26 17:23 370
	return GetMacFolder(kApplicationSupportFolderType, "Failed to find Application Support Folder");
1213f3f3 sago007 2015-09-19 19:24 371
#else
1213f3f3 sago007 2015-09-19 19:24 372
	return getDataHome();
1213f3f3 sago007 2015-09-19 19:24 373
#endif
1213f3f3 sago007 2015-09-19 19:24 374
}
1213f3f3 sago007 2015-09-19 19:24 375
0fd23ef9 sago007 2018-05-22 17:49 376
std::string getDesktopFolder() {
0fd23ef9 sago007 2018-05-22 17:49 377
	return PlatformFolders().getDesktopFolder();
0fd23ef9 sago007 2018-05-22 17:49 378
}
0fd23ef9 sago007 2018-05-22 17:49 379
0fd23ef9 sago007 2018-05-22 17:49 380
std::string getDocumentsFolder() {
0fd23ef9 sago007 2018-05-22 17:49 381
	return PlatformFolders().getDocumentsFolder();
0fd23ef9 sago007 2018-05-22 17:49 382
}
0fd23ef9 sago007 2018-05-22 17:49 383
513c0ebf Poul Sander 2018-06-06 17:05 384
std::string getDownloadFolder() {
0fd23ef9 sago007 2018-05-22 17:49 385
	return PlatformFolders().getDownloadFolder1();
0fd23ef9 sago007 2018-05-22 17:49 386
}
0fd23ef9 sago007 2018-05-22 17:49 387
513c0ebf Poul Sander 2018-06-06 17:05 388
std::string getDownloadFolder1() {
513c0ebf Poul Sander 2018-06-06 17:05 389
	return getDownloadFolder();
513c0ebf Poul Sander 2018-06-06 17:05 390
}
513c0ebf Poul Sander 2018-06-06 17:05 391
0fd23ef9 sago007 2018-05-22 17:49 392
std::string getPicturesFolder() {
0fd23ef9 sago007 2018-05-22 17:49 393
	return PlatformFolders().getPicturesFolder();
0fd23ef9 sago007 2018-05-22 17:49 394
}
0fd23ef9 sago007 2018-05-22 17:49 395
0fd23ef9 sago007 2018-05-22 17:49 396
std::string getMusicFolder() {
0fd23ef9 sago007 2018-05-22 17:49 397
	return PlatformFolders().getMusicFolder();
0fd23ef9 sago007 2018-05-22 17:49 398
}
0fd23ef9 sago007 2018-05-22 17:49 399
0fd23ef9 sago007 2018-05-22 17:49 400
std::string getVideoFolder() {
0fd23ef9 sago007 2018-05-22 17:49 401
	return PlatformFolders().getVideoFolder();
0fd23ef9 sago007 2018-05-22 17:49 402
}
0fd23ef9 sago007 2018-05-22 17:49 403
0fd23ef9 sago007 2018-05-22 17:49 404
std::string getSaveGamesFolder1() {
0fd23ef9 sago007 2018-05-22 17:49 405
	return PlatformFolders().getSaveGamesFolder1();
0fd23ef9 sago007 2018-05-22 17:49 406
}
1213f3f3 sago007 2015-09-19 19:24 407
513c0ebf Poul Sander 2018-06-06 17:05 408
std::string getSaveGamesFolder2() {
513c0ebf Poul Sander 2018-06-06 17:05 409
#ifdef _WIN32
513c0ebf Poul Sander 2018-06-06 17:05 410
	return GetKnownWindowsFolder(FOLDERID_SavedGames, "Failed to find Saved Games folder");
513c0ebf Poul Sander 2018-06-06 17:05 411
#else
513c0ebf Poul Sander 2018-06-06 17:05 412
	return PlatformFolders().getSaveGamesFolder1();
513c0ebf Poul Sander 2018-06-06 17:05 413
#endif
513c0ebf Poul Sander 2018-06-06 17:05 414
}
1213f3f3 sago007 2015-09-19 19:24 415
1213f3f3 sago007 2015-09-19 19:24 416
}  //namespace sago
1970-01-01 00:00 417