1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
|
/*
* TilEm II
*
* Copyright (c) 2011-2012 Benjamin Moody
*
* This program is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <glib.h>
#include <glib/gstdio.h>
#ifdef G_OS_WIN32
# include <shlobj.h>
#endif
#include "files.h"
static char *program_dir;
/* Set the name used to invoke this program */
void set_program_path(const char *path)
{
if (path && strchr(path, G_DIR_SEPARATOR))
program_dir = g_path_get_dirname(path);
}
/* Build a filename out of varargs */
static char *build_filenamev(const char *start, va_list rest)
{
char *args[10];
int i;
args[0] = (char*) start;
for (i = 1; i < 10; i++) {
args[i] = (char*) va_arg(rest, const char *);
if (!args[i])
break;
}
g_assert(i < 10);
return g_build_filenamev(args);
}
#ifdef G_OS_WIN32
static char * get_special_folder(int csidl)
{
char lpath[MAX_PATH+1];
wchar_t wpath[MAX_PATH+1];
LPITEMIDLIST pidl = NULL;
gchar *s = NULL;
if (SHGetSpecialFolderLocation(NULL, csidl, &pidl))
return NULL;
if (G_WIN32_HAVE_WIDECHAR_API()) {
if (SHGetPathFromIDListW(pidl, wpath))
s = g_utf16_to_utf8(wpath, -1, NULL, NULL, NULL);
}
else {
if (SHGetPathFromIDListA(pidl, lpath))
s = g_locale_to_utf8(lpath, -1, NULL, NULL, NULL);
}
CoTaskMemFree(pidl);
return s;
}
#endif
/* Get the default configuration directory.
On Unix, this is $XDG_CONFIG_HOME/tilem2 (where $XDG_CONFIG_HOME
defaults to $HOME/.config/ if not set.)
On Windows, this is $CSIDL_LOCAL_APPDATA\tilem2 (where
$CSIDL_LOCAL_APPDATA is typically "Local Settings\Application Data"
in the user's profile.)
Result is cached and should not be freed. */
static char * get_default_config_dir()
{
static char *result;
if (!result) {
#ifdef G_OS_WIN32
/* Do not use g_get_user_config_dir() on Windows,
because the behavior of that function is not
consistent across versions of GLib. */
char *s = get_special_folder(CSIDL_LOCAL_APPDATA);
if (s)
result = g_build_filename(s, "tilem2", NULL);
g_free(s);
#else
result = g_build_filename(g_get_user_config_dir(),
"tilem2", NULL);
#endif
}
return result;
}
/* Search for an existing file.
The default package configuration directory (defined above) is
searched first; if the file is not found there, try to find the
file that was installed along with the package, or (in case the
package hasn't yet been installed) the copy included in the source
package. */
static char * find_filev(GFileTest test, const char *name, va_list rest)
{
char *fullname, *dname, *path;
const char *userdir;
const char * const *sysdirs;
fullname = build_filenamev(name, rest);
dname = get_default_config_dir();
path = g_build_filename(dname, fullname, NULL);
if (g_file_test(path, test)) {
g_free(fullname);
return path;
}
g_free(path);
#ifdef G_OS_WIN32
if ((dname = g_win32_get_package_installation_directory(NULL, NULL))) {
path = g_build_filename(dname, "share", "tilem2", fullname, NULL);
g_free(dname);
if (g_file_test(path, test)) {
g_free(fullname);
return path;
}
g_free(path);
}
#endif
#ifdef UNINSTALLED_SHARE_DIR
if (program_dir) {
path = g_build_filename(program_dir, UNINSTALLED_SHARE_DIR,
fullname, NULL);
if (g_file_test(path, test)) {
g_free(fullname);
return path;
}
g_free(path);
}
#endif
#ifdef SHARE_DIR
path = g_build_filename(SHARE_DIR, fullname, NULL);
if (g_file_test(path, test)) {
g_free(fullname);
return path;
}
g_free(path);
#endif
userdir = g_get_user_data_dir();
if (userdir) {
path = g_build_filename(userdir, "tilem2", fullname, NULL);
if (g_file_test(path, test)) {
g_free(fullname);
return path;
}
}
sysdirs = g_get_system_data_dirs();
while (sysdirs && sysdirs[0]) {
path = g_build_filename(sysdirs[0], "tilem2", fullname, NULL);
if (g_file_test(path, test)) {
g_free(fullname);
return path;
}
sysdirs++;
}
g_free(fullname);
return NULL;
}
/* Locate an existing configuration or data file */
char * get_shared_file_path(const char *name, ...)
{
va_list ap;
char *path;
va_start(ap, name);
path = find_filev(G_FILE_TEST_IS_REGULAR, name, ap);
va_end(ap);
return path;
}
/* Locate an existing configuration or data directory */
char * get_shared_dir_path(const char *name, ...)
{
va_list ap;
char *path;
va_start(ap, name);
path = find_filev(G_FILE_TEST_IS_DIR, name, ap);
va_end(ap);
return path;
}
/* Return the path to the user's configuration directory, where any
new or modified config files should be written. Result is cached
and should not be freed. */
static char * get_config_dir()
{
static char *result;
char *fname;
FILE *f;
if (result)
return result;
/* If config.ini already exists, in any of the standard
locations, and is writable, use the directory containing
it. This will allow building the package as a relocatable
bundle. */
fname = get_shared_file_path("config.ini", NULL);
if (fname) {
f = g_fopen(fname, "r+");
if (f) {
result = g_path_get_dirname(fname);
fclose(f);
}
g_free(fname);
}
/* Otherwise use default config directory */
if (!result)
result = g_strdup(get_default_config_dir());
return result;
}
/* Get path for writing a new or modified configuration file */
char * get_config_file_path(const char *name, ...)
{
va_list ap;
const char *cfgdir;
char *fullname, *path;
cfgdir = get_config_dir();
g_mkdir_with_parents(cfgdir, 0775);
va_start(ap, name);
fullname = build_filenamev(name, ap);
va_end(ap);
path = g_build_filename(cfgdir, fullname, NULL);
g_free(fullname);
return path;
}
|