git repos / blockattack-game

blame: source/code/sago/platform_folders.cpp

normal view · raw

77531130 Poul Sander 2016-02-17 18:42 1
/*
47fef8e8 sago007 2016-11-24 21:37 2
Its is under the MIT license, to encourage reuse by cut-and-paste.
47fef8e8 sago007 2016-11-24 21:37 3
47fef8e8 sago007 2016-11-24 21:37 4
The original files are hosted here: https://github.com/sago007/PlatformFolders
47fef8e8 sago007 2016-11-24 21:37 5
47fef8e8 sago007 2016-11-24 21:37 6
Copyright (c) 2015-2016 Poul Sander
47fef8e8 sago007 2016-11-24 21:37 7
47fef8e8 sago007 2016-11-24 21:37 8
Permission is hereby granted, free of charge, to any person
47fef8e8 sago007 2016-11-24 21:37 9
obtaining a copy of this software and associated documentation files
47fef8e8 sago007 2016-11-24 21:37 10
(the "Software"), to deal in the Software without restriction,
47fef8e8 sago007 2016-11-24 21:37 11
including without limitation the rights to use, copy, modify, merge,
47fef8e8 sago007 2016-11-24 21:37 12
publish, distribute, sublicense, and/or sell copies of the Software,
47fef8e8 sago007 2016-11-24 21:37 13
and to permit persons to whom the Software is furnished to do so,
47fef8e8 sago007 2016-11-24 21:37 14
subject to the following conditions:
47fef8e8 sago007 2016-11-24 21:37 15
47fef8e8 sago007 2016-11-24 21:37 16
The above copyright notice and this permission notice shall be
47fef8e8 sago007 2016-11-24 21:37 17
included in all copies or substantial portions of the Software.
47fef8e8 sago007 2016-11-24 21:37 18
47fef8e8 sago007 2016-11-24 21:37 19
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
47fef8e8 sago007 2016-11-24 21:37 20
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
47fef8e8 sago007 2016-11-24 21:37 21
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
47fef8e8 sago007 2016-11-24 21:37 22
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
47fef8e8 sago007 2016-11-24 21:37 23
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
47fef8e8 sago007 2016-11-24 21:37 24
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
47fef8e8 sago007 2016-11-24 21:37 25
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
47fef8e8 sago007 2016-11-24 21:37 26
SOFTWARE.
77531130 Poul Sander 2016-02-17 18:42 27
*/
77531130 Poul Sander 2016-02-17 18:42 28
77531130 Poul Sander 2016-02-17 18:42 29
#include "platform_folders.h"
77531130 Poul Sander 2016-02-17 18:42 30
#include <iostream>
77531130 Poul Sander 2016-02-17 18:42 31
#include <stdexcept>
77531130 Poul Sander 2016-02-17 18:42 32
#include <string.h>
77531130 Poul Sander 2016-02-17 18:42 33
#include <stdio.h>
77531130 Poul Sander 2016-02-17 18:42 34
#include <cstdlib>
77531130 Poul Sander 2016-02-17 18:42 35
77531130 Poul Sander 2016-02-17 18:42 36
#if defined(_WIN32)
77531130 Poul Sander 2016-02-17 18:42 37
#include <windows.h>
77531130 Poul Sander 2016-02-17 18:42 38
#include <shlobj.h>
77531130 Poul Sander 2016-02-17 18:42 39
77531130 Poul Sander 2016-02-17 18:42 40
#define strtok_r strtok_s
77531130 Poul Sander 2016-02-17 18:42 41
f7573b59 sago007 2016-10-08 17:11 42
static std::string win32_utf16_to_utf8(const wchar_t* wstr)
f7573b59 sago007 2016-10-08 17:11 43
{
f7573b59 sago007 2016-10-08 17:11 44
	std::string res;
f7573b59 sago007 2016-10-08 17:11 45
	// If the 6th parameter is 0 then WideCharToMultiByte returns the number of bytes needed to store the result.
f7573b59 sago007 2016-10-08 17:11 46
	int actualSize = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);
f7573b59 sago007 2016-10-08 17:11 47
	if (actualSize > 0) {
f7573b59 sago007 2016-10-08 17:11 48
		//If the converted UTF-8 string could not be in the initial buffer. Allocate one that can hold it.
f7573b59 sago007 2016-10-08 17:11 49
		std::vector<char> buffer(actualSize);
f7573b59 sago007 2016-10-08 17:11 50
		actualSize = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, &buffer[0], buffer.size(), NULL, NULL);
f7573b59 sago007 2016-10-08 17:11 51
		res = buffer.data();
f7573b59 sago007 2016-10-08 17:11 52
	}
f7573b59 sago007 2016-10-08 17:11 53
	if (actualSize == 0) {
f7573b59 sago007 2016-10-08 17:11 54
		// WideCharToMultiByte return 0 for errors.
f7573b59 sago007 2016-10-08 17:11 55
		std::string errorMsg = "UTF16 to UTF8 failed with error code: " + GetLastError();
f7573b59 sago007 2016-10-08 17:11 56
		throw std::runtime_error(errorMsg.c_str());
f7573b59 sago007 2016-10-08 17:11 57
	}
f7573b59 sago007 2016-10-08 17:11 58
	return res;
f7573b59 sago007 2016-10-08 17:11 59
}
f7573b59 sago007 2016-10-08 17:11 60
77531130 Poul Sander 2016-02-17 18:42 61
static std::string GetWindowsFolder(int folderId, const char* errorMsg) {
f7573b59 sago007 2016-10-08 17:11 62
	wchar_t szPath[MAX_PATH];
77531130 Poul Sander 2016-02-17 18:42 63
	szPath[0] = 0;
f7573b59 sago007 2016-10-08 17:11 64
	if ( !SUCCEEDED( SHGetFolderPathW( NULL, folderId, NULL, 0, szPath ) ) )
77531130 Poul Sander 2016-02-17 18:42 65
	{
77531130 Poul Sander 2016-02-17 18:42 66
		throw std::runtime_error(errorMsg);
77531130 Poul Sander 2016-02-17 18:42 67
	}
f7573b59 sago007 2016-10-08 17:11 68
	return win32_utf16_to_utf8(szPath);
77531130 Poul Sander 2016-02-17 18:42 69
}
77531130 Poul Sander 2016-02-17 18:42 70
77531130 Poul Sander 2016-02-17 18:42 71
static std::string GetAppData() {
77531130 Poul Sander 2016-02-17 18:42 72
	return GetWindowsFolder(CSIDL_APPDATA, "RoamingAppData could not be found");
77531130 Poul Sander 2016-02-17 18:42 73
}
77531130 Poul Sander 2016-02-17 18:42 74
77531130 Poul Sander 2016-02-17 18:42 75
static std::string GetAppDataCommon() {
77531130 Poul Sander 2016-02-17 18:42 76
	return GetWindowsFolder(CSIDL_COMMON_APPDATA, "Common appdata could not be found");
77531130 Poul Sander 2016-02-17 18:42 77
}
77531130 Poul Sander 2016-02-17 18:42 78
77531130 Poul Sander 2016-02-17 18:42 79
static std::string GetAppDataLocal() {
77531130 Poul Sander 2016-02-17 18:42 80
	return GetWindowsFolder(CSIDL_LOCAL_APPDATA, "LocalAppData could not be found");
77531130 Poul Sander 2016-02-17 18:42 81
}
77531130 Poul Sander 2016-02-17 18:42 82
#elif defined(__APPLE__)
77531130 Poul Sander 2016-02-17 18:42 83
#include <CoreServices/CoreServices.h>
77531130 Poul Sander 2016-02-17 18:42 84
77531130 Poul Sander 2016-02-17 18:42 85
static std::string GetMacFolder(OSType folderType, const char* errorMsg) {
77531130 Poul Sander 2016-02-17 18:42 86
	std::string ret;
77531130 Poul Sander 2016-02-17 18:42 87
	FSRef ref;
47fef8e8 sago007 2016-11-24 21:37 88
	char path[PATH_MAX];
47fef8e8 sago007 2016-11-24 21:37 89
	OSStatus err = FSFindFolder( kUserDomain, folderType, kCreateFolder, &ref );
77531130 Poul Sander 2016-02-17 18:42 90
	if (err != noErr) {
77531130 Poul Sander 2016-02-17 18:42 91
		throw std::runtime_error(errorMsg);
77531130 Poul Sander 2016-02-17 18:42 92
	}
47fef8e8 sago007 2016-11-24 21:37 93
	FSRefMakePath( &ref, (UInt8*)&path, PATH_MAX );
77531130 Poul Sander 2016-02-17 18:42 94
	ret = path;
77531130 Poul Sander 2016-02-17 18:42 95
	return ret;
77531130 Poul Sander 2016-02-17 18:42 96
}
77531130 Poul Sander 2016-02-17 18:42 97
77531130 Poul Sander 2016-02-17 18:42 98
#else
77531130 Poul Sander 2016-02-17 18:42 99
#include <map>
77531130 Poul Sander 2016-02-17 18:42 100
#include <fstream>
77531130 Poul Sander 2016-02-17 18:42 101
#include <pwd.h>
77531130 Poul Sander 2016-02-17 18:42 102
#include <unistd.h>
77531130 Poul Sander 2016-02-17 18:42 103
#include <sys/types.h>
77531130 Poul Sander 2016-02-17 18:42 104
//Typically Linux. For easy reading the comments will just say Linux but should work with most *nixes
77531130 Poul Sander 2016-02-17 18:42 105
77531130 Poul Sander 2016-02-17 18:42 106
static void throwOnRelative(const char* envName, const char* envValue) {
77531130 Poul Sander 2016-02-17 18:42 107
	if (envValue[0] != '/') {
77531130 Poul Sander 2016-02-17 18:42 108
		char buffer[200];
77531130 Poul Sander 2016-02-17 18:42 109
		snprintf(buffer, sizeof(buffer), "Environment \"%s\" does not start with an '/'. XDG specifies that the value must be absolute. The current value is: \"%s\"", envName, envValue);
77531130 Poul Sander 2016-02-17 18:42 110
		throw std::runtime_error(buffer);
77531130 Poul Sander 2016-02-17 18:42 111
	}
77531130 Poul Sander 2016-02-17 18:42 112
}
77531130 Poul Sander 2016-02-17 18:42 113
77531130 Poul Sander 2016-02-17 18:42 114
/**
77531130 Poul Sander 2016-02-17 18:42 115
 * Retrives the effective user's home dir. 
77531130 Poul Sander 2016-02-17 18:42 116
 * If the user is running as root we ignore the HOME environment. It works badly with sudo. 
77531130 Poul Sander 2016-02-17 18:42 117
 * Writing to $HOME as root implies security concerns that a multiplatform program cannot be assumed to handle.
77531130 Poul Sander 2016-02-17 18:42 118
 * @return The home directory. HOME environment is respected for non-root users if it exists. 
77531130 Poul Sander 2016-02-17 18:42 119
 */
77531130 Poul Sander 2016-02-17 18:42 120
static std::string getHome() {
77531130 Poul Sander 2016-02-17 18:42 121
	std::string res;
77531130 Poul Sander 2016-02-17 18:42 122
	int uid = getuid();
77531130 Poul Sander 2016-02-17 18:42 123
	const char* homeEnv = getenv("HOME");
77531130 Poul Sander 2016-02-17 18:42 124
	if ( uid != 0 && homeEnv) {
77531130 Poul Sander 2016-02-17 18:42 125
		//We only acknowlegde HOME if not root.
77531130 Poul Sander 2016-02-17 18:42 126
		res = homeEnv;
77531130 Poul Sander 2016-02-17 18:42 127
		return res;
77531130 Poul Sander 2016-02-17 18:42 128
	}
77531130 Poul Sander 2016-02-17 18:42 129
	struct passwd *pw = getpwuid(uid);
77531130 Poul Sander 2016-02-17 18:42 130
	if (!pw) {
77531130 Poul Sander 2016-02-17 18:42 131
		throw std::runtime_error("Unable to get passwd struct.");
77531130 Poul Sander 2016-02-17 18:42 132
	}
77531130 Poul Sander 2016-02-17 18:42 133
	const char* tempRes = pw->pw_dir;
77531130 Poul Sander 2016-02-17 18:42 134
	if (!tempRes) {
77531130 Poul Sander 2016-02-17 18:42 135
		throw std::runtime_error("User has no home directory");
77531130 Poul Sander 2016-02-17 18:42 136
	}
77531130 Poul Sander 2016-02-17 18:42 137
	res = tempRes;
77531130 Poul Sander 2016-02-17 18:42 138
	return res;
77531130 Poul Sander 2016-02-17 18:42 139
}
77531130 Poul Sander 2016-02-17 18:42 140
77531130 Poul Sander 2016-02-17 18:42 141
static std::string getLinuxFolderDefault(const char* envName, const char* defaultRelativePath) {
77531130 Poul Sander 2016-02-17 18:42 142
	std::string res;
77531130 Poul Sander 2016-02-17 18:42 143
	const char* tempRes = getenv(envName);
77531130 Poul Sander 2016-02-17 18:42 144
	if (tempRes) {
77531130 Poul Sander 2016-02-17 18:42 145
		throwOnRelative(envName, tempRes);
77531130 Poul Sander 2016-02-17 18:42 146
		res = tempRes;
77531130 Poul Sander 2016-02-17 18:42 147
		return res;
77531130 Poul Sander 2016-02-17 18:42 148
	}
77531130 Poul Sander 2016-02-17 18:42 149
	res = getHome() + "/" + defaultRelativePath;
77531130 Poul Sander 2016-02-17 18:42 150
	return res;
77531130 Poul Sander 2016-02-17 18:42 151
}
77531130 Poul Sander 2016-02-17 18:42 152
77531130 Poul Sander 2016-02-17 18:42 153
static void appendExtraFoldersTokenizer(const char* envName, const char* envValue, std::vector<std::string>& folders) {
f7573b59 sago007 2016-10-08 17:11 154
	std::vector<char> buffer(envValue, envValue + strlen(envValue) + 1);
77531130 Poul Sander 2016-02-17 18:42 155
	char *saveptr;
f7573b59 sago007 2016-10-08 17:11 156
	const char* p = strtok_r ( &buffer[0], ":", &saveptr);
77531130 Poul Sander 2016-02-17 18:42 157
	while (p != NULL) {
77531130 Poul Sander 2016-02-17 18:42 158
		if (p[0] == '/') {
77531130 Poul Sander 2016-02-17 18:42 159
			folders.push_back(p);
77531130 Poul Sander 2016-02-17 18:42 160
		}
77531130 Poul Sander 2016-02-17 18:42 161
		else {
77531130 Poul Sander 2016-02-17 18:42 162
			//Unless the system is wrongly configured this should never happen... But of course some systems will be incorectly configured.
77531130 Poul Sander 2016-02-17 18:42 163
			//The XDG documentation indicates that the folder should be ignored but that the program should continue.
f7573b59 sago007 2016-10-08 17:11 164
			std::cerr << "Skipping path \"" << p << "\" in \"" << envName << "\" because it does not start with a \"/\"\n";
77531130 Poul Sander 2016-02-17 18:42 165
		}
77531130 Poul Sander 2016-02-17 18:42 166
		p = strtok_r (NULL, ":", &saveptr);
77531130 Poul Sander 2016-02-17 18:42 167
	}
77531130 Poul Sander 2016-02-17 18:42 168
}
77531130 Poul Sander 2016-02-17 18:42 169
77531130 Poul Sander 2016-02-17 18:42 170
static void appendExtraFolders(const char* envName, const char* defaultValue, std::vector<std::string>& folders) {
77531130 Poul Sander 2016-02-17 18:42 171
	const char* envValue = getenv(envName);
77531130 Poul Sander 2016-02-17 18:42 172
	if (!envValue) {
77531130 Poul Sander 2016-02-17 18:42 173
		envValue = defaultValue;
77531130 Poul Sander 2016-02-17 18:42 174
	}
77531130 Poul Sander 2016-02-17 18:42 175
	appendExtraFoldersTokenizer(envName, envValue, folders);
77531130 Poul Sander 2016-02-17 18:42 176
}
77531130 Poul Sander 2016-02-17 18:42 177
77531130 Poul Sander 2016-02-17 18:42 178
#endif
77531130 Poul Sander 2016-02-17 18:42 179
77531130 Poul Sander 2016-02-17 18:42 180
77531130 Poul Sander 2016-02-17 18:42 181
namespace sago {
77531130 Poul Sander 2016-02-17 18:42 182
77531130 Poul Sander 2016-02-17 18:42 183
std::string getDataHome() {
77531130 Poul Sander 2016-02-17 18:42 184
#if defined(_WIN32)
77531130 Poul Sander 2016-02-17 18:42 185
	return GetAppData();
77531130 Poul Sander 2016-02-17 18:42 186
#elif defined(__APPLE__)
77531130 Poul Sander 2016-02-17 18:42 187
	return GetMacFolder(kApplicationSupportFolderType, "Failed to find the Application Support Folder");
77531130 Poul Sander 2016-02-17 18:42 188
#else
77531130 Poul Sander 2016-02-17 18:42 189
	return getLinuxFolderDefault("XDG_DATA_HOME", ".local/share");
77531130 Poul Sander 2016-02-17 18:42 190
#endif
77531130 Poul Sander 2016-02-17 18:42 191
}
77531130 Poul Sander 2016-02-17 18:42 192
77531130 Poul Sander 2016-02-17 18:42 193
std::string getConfigHome() {
77531130 Poul Sander 2016-02-17 18:42 194
#if defined(_WIN32)
77531130 Poul Sander 2016-02-17 18:42 195
	return GetAppData();
77531130 Poul Sander 2016-02-17 18:42 196
#elif defined(__APPLE__)
77531130 Poul Sander 2016-02-17 18:42 197
	return GetMacFolder(kApplicationSupportFolderType, "Failed to find the Application Support Folder");
77531130 Poul Sander 2016-02-17 18:42 198
#else
77531130 Poul Sander 2016-02-17 18:42 199
	return getLinuxFolderDefault("XDG_CONFIG_HOME", ".config");
77531130 Poul Sander 2016-02-17 18:42 200
#endif
77531130 Poul Sander 2016-02-17 18:42 201
}
77531130 Poul Sander 2016-02-17 18:42 202
77531130 Poul Sander 2016-02-17 18:42 203
std::string getCacheDir() {
77531130 Poul Sander 2016-02-17 18:42 204
#if defined(_WIN32)
77531130 Poul Sander 2016-02-17 18:42 205
	return GetAppDataLocal();
77531130 Poul Sander 2016-02-17 18:42 206
#elif defined(__APPLE__)
77531130 Poul Sander 2016-02-17 18:42 207
	return GetMacFolder(kCachedDataFolderType, "Failed to find the Application Support Folder");
77531130 Poul Sander 2016-02-17 18:42 208
#else
77531130 Poul Sander 2016-02-17 18:42 209
	return getLinuxFolderDefault("XDG_CONFIG_HOME", ".cache");
77531130 Poul Sander 2016-02-17 18:42 210
#endif
77531130 Poul Sander 2016-02-17 18:42 211
}
77531130 Poul Sander 2016-02-17 18:42 212
77531130 Poul Sander 2016-02-17 18:42 213
void appendAdditionalDataDirectories(std::vector<std::string>& homes) {
77531130 Poul Sander 2016-02-17 18:42 214
#if defined(_WIN32)
77531130 Poul Sander 2016-02-17 18:42 215
	homes.push_back(GetAppDataCommon());
77531130 Poul Sander 2016-02-17 18:42 216
#elif defined(__APPLE__)
77531130 Poul Sander 2016-02-17 18:42 217
#else
77531130 Poul Sander 2016-02-17 18:42 218
	appendExtraFolders("XDG_DATA_DIRS", "/usr/local/share/:/usr/share/", homes);
77531130 Poul Sander 2016-02-17 18:42 219
#endif
77531130 Poul Sander 2016-02-17 18:42 220
}
77531130 Poul Sander 2016-02-17 18:42 221
77531130 Poul Sander 2016-02-17 18:42 222
void appendAdditionalConfigDirectories(std::vector<std::string>& homes) {
77531130 Poul Sander 2016-02-17 18:42 223
#if defined(_WIN32)
77531130 Poul Sander 2016-02-17 18:42 224
	homes.push_back(GetAppDataCommon());
77531130 Poul Sander 2016-02-17 18:42 225
#elif defined(__APPLE__)
77531130 Poul Sander 2016-02-17 18:42 226
#else
77531130 Poul Sander 2016-02-17 18:42 227
	appendExtraFolders("XDG_CONFIG_DIRS", "/etc/xdg", homes);
77531130 Poul Sander 2016-02-17 18:42 228
#endif
77531130 Poul Sander 2016-02-17 18:42 229
}
77531130 Poul Sander 2016-02-17 18:42 230
77531130 Poul Sander 2016-02-17 18:42 231
#if defined(_WIN32)
77531130 Poul Sander 2016-02-17 18:42 232
#elif defined(__APPLE__)
77531130 Poul Sander 2016-02-17 18:42 233
#else
77531130 Poul Sander 2016-02-17 18:42 234
struct PlatformFolders::PlatformFoldersData {
77531130 Poul Sander 2016-02-17 18:42 235
	std::map<std::string, std::string> folders;
77531130 Poul Sander 2016-02-17 18:42 236
};
77531130 Poul Sander 2016-02-17 18:42 237
77531130 Poul Sander 2016-02-17 18:42 238
static void PlatformFoldersAddFromFile(const std::string& filename, std::map<std::string, std::string>& folders) {
77531130 Poul Sander 2016-02-17 18:42 239
	std::ifstream infile(filename.c_str());
77531130 Poul Sander 2016-02-17 18:42 240
	std::string line;
77531130 Poul Sander 2016-02-17 18:42 241
	while (std::getline(infile, line)) {
77531130 Poul Sander 2016-02-17 18:42 242
		if (line.length() == 0 || line.at(0) == '#') {
77531130 Poul Sander 2016-02-17 18:42 243
			continue;
77531130 Poul Sander 2016-02-17 18:42 244
		}
77531130 Poul Sander 2016-02-17 18:42 245
		std::size_t splitPos = line.find("=");
77531130 Poul Sander 2016-02-17 18:42 246
		std::string key = line.substr(0, splitPos);
77531130 Poul Sander 2016-02-17 18:42 247
		std::string value = line.substr(splitPos+2, line.length()-splitPos-3);
77531130 Poul Sander 2016-02-17 18:42 248
		folders[key] = value;
161a010c sago007 2016-05-06 14:00 249
		//std::cout << key << " : " << value << "\n";
77531130 Poul Sander 2016-02-17 18:42 250
	}
77531130 Poul Sander 2016-02-17 18:42 251
}
77531130 Poul Sander 2016-02-17 18:42 252
77531130 Poul Sander 2016-02-17 18:42 253
static void PlatformFoldersFillData(std::map<std::string, std::string>& folders) {
77531130 Poul Sander 2016-02-17 18:42 254
	folders["XDG_DOCUMENTS_DIR"] = "$HOME/Documents";
77531130 Poul Sander 2016-02-17 18:42 255
	folders["XDG_DESKTOP_DIR"] = "$HOME/Desktop";
77531130 Poul Sander 2016-02-17 18:42 256
	folders["XDG_DOWNLOAD_DIR"] = "$HOME/Downloads";
77531130 Poul Sander 2016-02-17 18:42 257
	folders["XDG_MUSIC_DIR"] = "$HOME/Music";
77531130 Poul Sander 2016-02-17 18:42 258
	folders["XDG_PICTURES_DIR"] = "$HOME/Pictures";
77531130 Poul Sander 2016-02-17 18:42 259
	folders["XDG_PUBLICSHARE_DIR"] = "$HOME/Public";
77531130 Poul Sander 2016-02-17 18:42 260
	folders["XDG_TEMPLATES_DIR"] = "$HOME/.Templates";
77531130 Poul Sander 2016-02-17 18:42 261
	folders["XDG_VIDEOS_DIR"] = "$HOME/Videos";
77531130 Poul Sander 2016-02-17 18:42 262
	PlatformFoldersAddFromFile( getConfigHome()+"/user-dirs.dirs", folders);
77531130 Poul Sander 2016-02-17 18:42 263
	for (std::map<std::string, std::string>::iterator itr = folders.begin() ; itr != folders.end() ; itr ++ ) {
77531130 Poul Sander 2016-02-17 18:42 264
		std::string& value = itr->second;
77531130 Poul Sander 2016-02-17 18:42 265
		if (value.compare(0, 5, "$HOME") == 0) {
77531130 Poul Sander 2016-02-17 18:42 266
			value = getHome() + value.substr(5, std::string::npos);
77531130 Poul Sander 2016-02-17 18:42 267
		}
77531130 Poul Sander 2016-02-17 18:42 268
	}
77531130 Poul Sander 2016-02-17 18:42 269
}
77531130 Poul Sander 2016-02-17 18:42 270
#endif
77531130 Poul Sander 2016-02-17 18:42 271
77531130 Poul Sander 2016-02-17 18:42 272
PlatformFolders::PlatformFolders() {
77531130 Poul Sander 2016-02-17 18:42 273
#if defined(_WIN32)
77531130 Poul Sander 2016-02-17 18:42 274
#elif defined(__APPLE__)
77531130 Poul Sander 2016-02-17 18:42 275
#else
77531130 Poul Sander 2016-02-17 18:42 276
	this->data = new PlatformFolders::PlatformFoldersData();
77531130 Poul Sander 2016-02-17 18:42 277
	try {
77531130 Poul Sander 2016-02-17 18:42 278
		PlatformFoldersFillData(data->folders);
77531130 Poul Sander 2016-02-17 18:42 279
	} catch (...) {
77531130 Poul Sander 2016-02-17 18:42 280
		delete this->data;
77531130 Poul Sander 2016-02-17 18:42 281
		throw;
77531130 Poul Sander 2016-02-17 18:42 282
	}
77531130 Poul Sander 2016-02-17 18:42 283
#endif
77531130 Poul Sander 2016-02-17 18:42 284
}
77531130 Poul Sander 2016-02-17 18:42 285
77531130 Poul Sander 2016-02-17 18:42 286
PlatformFolders::~PlatformFolders() {
77531130 Poul Sander 2016-02-17 18:42 287
#if defined(_WIN32)
77531130 Poul Sander 2016-02-17 18:42 288
#elif defined(__APPLE__)
77531130 Poul Sander 2016-02-17 18:42 289
#else
77531130 Poul Sander 2016-02-17 18:42 290
	delete this->data;
77531130 Poul Sander 2016-02-17 18:42 291
#endif
77531130 Poul Sander 2016-02-17 18:42 292
}
77531130 Poul Sander 2016-02-17 18:42 293
77531130 Poul Sander 2016-02-17 18:42 294
std::string PlatformFolders::getDocumentsFolder() const {
77531130 Poul Sander 2016-02-17 18:42 295
#if defined(_WIN32)
77531130 Poul Sander 2016-02-17 18:42 296
	return GetWindowsFolder(CSIDL_PERSONAL, "Failed to find My Documents folder");
77531130 Poul Sander 2016-02-17 18:42 297
#elif defined(__APPLE__)
77531130 Poul Sander 2016-02-17 18:42 298
	return GetMacFolder(kDocumentsFolderType, "Failed to find Documents Folder");
77531130 Poul Sander 2016-02-17 18:42 299
#else
77531130 Poul Sander 2016-02-17 18:42 300
	return data->folders["XDG_DOCUMENTS_DIR"];
77531130 Poul Sander 2016-02-17 18:42 301
#endif
77531130 Poul Sander 2016-02-17 18:42 302
}
77531130 Poul Sander 2016-02-17 18:42 303
77531130 Poul Sander 2016-02-17 18:42 304
std::string PlatformFolders::getDesktopFolder() const {
77531130 Poul Sander 2016-02-17 18:42 305
#if defined(_WIN32)
77531130 Poul Sander 2016-02-17 18:42 306
	return GetWindowsFolder(CSIDL_DESKTOP, "Failed to find Desktop folder");
77531130 Poul Sander 2016-02-17 18:42 307
#elif defined(__APPLE__)
77531130 Poul Sander 2016-02-17 18:42 308
	return GetMacFolder(kDesktopFolderType, "Failed to find Desktop folder");
77531130 Poul Sander 2016-02-17 18:42 309
#else
77531130 Poul Sander 2016-02-17 18:42 310
	return data->folders["XDG_DESKTOP_DIR"];
77531130 Poul Sander 2016-02-17 18:42 311
#endif
77531130 Poul Sander 2016-02-17 18:42 312
}
77531130 Poul Sander 2016-02-17 18:42 313
77531130 Poul Sander 2016-02-17 18:42 314
std::string PlatformFolders::getPicturesFolder() const {
77531130 Poul Sander 2016-02-17 18:42 315
#if defined(_WIN32)
77531130 Poul Sander 2016-02-17 18:42 316
	return GetWindowsFolder(CSIDL_MYPICTURES, "Failed to find My Pictures folder");
77531130 Poul Sander 2016-02-17 18:42 317
#elif defined(__APPLE__)
77531130 Poul Sander 2016-02-17 18:42 318
	return GetMacFolder(kPictureDocumentsFolderType, "Failed to find Picture folder");
77531130 Poul Sander 2016-02-17 18:42 319
#else
77531130 Poul Sander 2016-02-17 18:42 320
	return data->folders["XDG_PICTURES_DIR"];
77531130 Poul Sander 2016-02-17 18:42 321
#endif
77531130 Poul Sander 2016-02-17 18:42 322
}
77531130 Poul Sander 2016-02-17 18:42 323
77531130 Poul Sander 2016-02-17 18:42 324
std::string PlatformFolders::getDownloadFolder1() const {
77531130 Poul Sander 2016-02-17 18:42 325
#if defined(_WIN32)
77531130 Poul Sander 2016-02-17 18:42 326
	//Pre Vista. Files was downloaded to the desktop
77531130 Poul Sander 2016-02-17 18:42 327
	return GetWindowsFolder(CSIDL_DESKTOP, "Failed to find My Downloads (Desktop) folder");
77531130 Poul Sander 2016-02-17 18:42 328
#elif defined(__APPLE__)
77531130 Poul Sander 2016-02-17 18:42 329
	return GetMacFolder(kDownloadsFolderType, "Failed to find Download folder");
77531130 Poul Sander 2016-02-17 18:42 330
#else
77531130 Poul Sander 2016-02-17 18:42 331
	return data->folders["XDG_DOWNLOAD_DIR"];
77531130 Poul Sander 2016-02-17 18:42 332
#endif
77531130 Poul Sander 2016-02-17 18:42 333
}
77531130 Poul Sander 2016-02-17 18:42 334
77531130 Poul Sander 2016-02-17 18:42 335
std::string PlatformFolders::getMusicFolder() const {
77531130 Poul Sander 2016-02-17 18:42 336
#if defined(_WIN32)
77531130 Poul Sander 2016-02-17 18:42 337
	return GetWindowsFolder(CSIDL_MYMUSIC, "Failed to find My Music folder");
77531130 Poul Sander 2016-02-17 18:42 338
#elif defined(__APPLE__)
77531130 Poul Sander 2016-02-17 18:42 339
	return GetMacFolder(kMusicDocumentsFolderType, "Failed to find Music folder");
77531130 Poul Sander 2016-02-17 18:42 340
#else
77531130 Poul Sander 2016-02-17 18:42 341
	return data->folders["XDG_MUSIC_DIR"];
77531130 Poul Sander 2016-02-17 18:42 342
#endif
77531130 Poul Sander 2016-02-17 18:42 343
}
77531130 Poul Sander 2016-02-17 18:42 344
77531130 Poul Sander 2016-02-17 18:42 345
std::string PlatformFolders::getVideoFolder() const {
77531130 Poul Sander 2016-02-17 18:42 346
#if defined(_WIN32)
77531130 Poul Sander 2016-02-17 18:42 347
	return GetWindowsFolder(CSIDL_MYVIDEO, "Failed to find My Video folder");
77531130 Poul Sander 2016-02-17 18:42 348
#elif defined(__APPLE__)
77531130 Poul Sander 2016-02-17 18:42 349
	return GetMacFolder(kMovieDocumentsFolderType, "Failed to find Movie folder");
77531130 Poul Sander 2016-02-17 18:42 350
#else
77531130 Poul Sander 2016-02-17 18:42 351
	return data->folders["XDG_VIDEOS_DIR"];
77531130 Poul Sander 2016-02-17 18:42 352
#endif
77531130 Poul Sander 2016-02-17 18:42 353
}
77531130 Poul Sander 2016-02-17 18:42 354
77531130 Poul Sander 2016-02-17 18:42 355
std::string PlatformFolders::getSaveGamesFolder1() const {
77531130 Poul Sander 2016-02-17 18:42 356
#if defined(_WIN32)
77531130 Poul Sander 2016-02-17 18:42 357
	//A dedicated Save Games folder was not introduced until Vista. For XP and older save games are most often saved in a normal folder named "My Games".
77531130 Poul Sander 2016-02-17 18:42 358
	//Data that should not be user accessible should be placed under GetDataHome() instead
77531130 Poul Sander 2016-02-17 18:42 359
	return GetWindowsFolder(CSIDL_PERSONAL, "Failed to find My Documents folder")+"\\My Games";
77531130 Poul Sander 2016-02-17 18:42 360
#elif defined(__APPLE__)
77531130 Poul Sander 2016-02-17 18:42 361
	return GetMacFolder(kApplicationSupportFolderType, "Failed to find Application Support Folder");
77531130 Poul Sander 2016-02-17 18:42 362
#else
77531130 Poul Sander 2016-02-17 18:42 363
	return getDataHome();
77531130 Poul Sander 2016-02-17 18:42 364
#endif
77531130 Poul Sander 2016-02-17 18:42 365
}
77531130 Poul Sander 2016-02-17 18:42 366
77531130 Poul Sander 2016-02-17 18:42 367
77531130 Poul Sander 2016-02-17 18:42 368
77531130 Poul Sander 2016-02-17 18:42 369
}  //namespace sago
1970-01-01 00:00 370