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