-
Notifications
You must be signed in to change notification settings - Fork 17
/
cfgpath.h
475 lines (452 loc) · 13.3 KB
/
cfgpath.h
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
/**
* @file cfgpath.h
* @brief Cross platform methods for obtaining paths to configuration files.
*
* Copyright (C) 2013 Adam Nielsen <[email protected]>
*
* This code is placed in the public domain. You are free to use it for any
* purpose. If you add new platform support, please contribute a patch!
*
* Example use:
*
* char cfgdir[256];
* get_user_config_file(cfgdir, sizeof(cfgdir), "myapp");
* if (cfgdir[0] == 0) {
* printf("Unable to find home directory.\n");
* return 1;
* }
* printf("Saving configuration file to %s\n", cfgdir);
*
* A number of constants are also defined:
*
* - MAX_PATH: Maximum length of a path, in characters. Used to allocate a
* char array large enough to hold the returned path.
*
* - PATH_SEPARATOR_CHAR: The separator between folders. This will be either a
* forward slash or a backslash depending on the platform. This is a
* character constant.
*
* - PATH_SEPARATOR_STRING: The same as PATH_SEPARATOR_CHAR but as a C string,
* to make it easier to append to other string constants.
*/
#ifndef CFGPATH_H_
#define CFGPATH_H_
#if defined(_MSC_VER) || defined(__MINGW32__)
#define inline __inline
#include <direct.h>
#define mkdir _mkdir
#endif
#ifdef __unix__
#include <sys/param.h>
#endif
#if defined(__linux__) || defined(BSD)
#define CFGPATH_LINUX
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#define MAX_PATH 512 /* arbitrary value */
#define PATH_SEPARATOR_CHAR '/'
#define PATH_SEPARATOR_STRING "/"
#elif defined(WIN32)
#define CFGPATH_WINDOWS
#include <shlobj.h>
/* MAX_PATH is defined by the Windows API */
#define PATH_SEPARATOR_CHAR '\\'
#define PATH_SEPARATOR_STRING "\\"
#elif defined(__APPLE__)
#define CFGPATH_MAC
#include <CoreServices/CoreServices.h>
#include <sys/stat.h>
#define MAX_PATH PATH_MAX
#define PATH_SEPARATOR_CHAR '/'
#define PATH_SEPARATOR_STRING "/"
#else
#error cfgpath.h functions have not been implemented for your platform! Please send patches.
#endif
/** Get an absolute path to a single configuration file, specific to this user.
*
* This function is useful for programs that need only a single configuration
* file. The file is unique to the user account currently logged in.
*
* Output is typically:
*
* Windows: C:\Users\jcitizen\AppData\Roaming\appname.ini
* Linux: /home/jcitizen/.config/appname.conf
* Mac: /Users/jcitizen/Library/Application Support/appname.conf
*
* @param out
* Buffer to write the path. On return will contain the path, or an empty
* string on error.
*
* @param maxlen
* Length of out. Must be >= MAX_PATH.
*
* @param appname
* Short name of the application. Avoid using spaces or version numbers, and
* use lowercase if possible.
*
* @post The file may or may not exist.
* @post The folder holding the file is created if needed.
*/
static inline void get_user_config_file(char *out, unsigned int maxlen, const char *appname)
{
#ifdef CFGPATH_LINUX
const char *out_orig = out;
char *home = getenv("XDG_CONFIG_HOME");
unsigned int config_len = 0;
if (!home) {
home = getenv("HOME");
if (!home) {
// Can't find home directory
out[0] = 0;
return;
}
config_len = strlen(".config/");
}
unsigned int home_len = strlen(home);
unsigned int appname_len = strlen(appname);
const int ext_len = strlen(".conf");
/* first +1 is "/", second is terminating null */
if (home_len + 1 + config_len + appname_len + ext_len + 1 > maxlen) {
out[0] = 0;
return;
}
memcpy(out, home, home_len);
out += home_len;
*out = '/';
out++;
if (config_len) {
memcpy(out, ".config/", config_len);
out += config_len;
/* Make the .config folder if it doesn't already exist */
*out = '\0';
mkdir(out_orig, 0755);
}
memcpy(out, appname, appname_len);
out += appname_len;
memcpy(out, ".conf", ext_len);
out += ext_len;
*out = '\0';
#elif defined(CFGPATH_WINDOWS)
if (maxlen < MAX_PATH) {
out[0] = 0;
return;
}
if (!SUCCEEDED(SHGetFolderPath(NULL, CSIDL_APPDATA, NULL, 0, out))) {
out[0] = 0;
return;
}
/* We don't try to create the AppData folder as it always exists already */
unsigned int appname_len = strlen(appname);
if (strlen(out) + 1 + appname_len + strlen(".ini") + 1 > maxlen) {
out[0] = 0;
return;
}
strcat(out, "\\");
strcat(out, appname);
strcat(out, ".ini");
#elif defined(CFGPATH_MAC)
FSRef ref;
FSFindFolder(kUserDomain, kApplicationSupportFolderType, kCreateFolder, &ref);
char home[MAX_PATH];
FSRefMakePath(&ref, (UInt8 *)&home, MAX_PATH);
/* first +1 is "/", second is terminating null */
const char *ext = ".conf";
if (strlen(home) + 1 + strlen(appname) + strlen(ext) + 1 > maxlen) {
out[0] = 0;
return;
}
strcpy(out, home);
strcat(out, PATH_SEPARATOR_STRING);
strcat(out, appname);
strcat(out, ext);
#endif
}
/** Get an absolute path to a configuration folder, specific to this user.
*
* This function is useful for programs that need to store multiple
* configuration files. The output is a folder (which may not exist and will
* need to be created) suitable for storing a number of files.
*
* The returned path will always end in a platform-specific trailing slash, so
* that a filename can simply be appended to the path.
*
* Output is typically:
*
* Windows: C:\Users\jcitizen\AppData\Roaming\appname\
* Linux: /home/jcitizen/.config/appname/
* Mac: /Users/jcitizen/Library/Application Support/appname/
*
* @param out
* Buffer to write the path. On return will contain the path, or an empty
* string on error.
*
* @param maxlen
* Length of out. Must be >= MAX_PATH.
*
* @param appname
* Short name of the application. Avoid using spaces or version numbers, and
* use lowercase if possible.
*
* @post The folder is created if needed.
*/
static inline void get_user_config_folder(char *out, unsigned int maxlen, const char *appname)
{
#ifdef CFGPATH_LINUX
const char *out_orig = out;
char *home = getenv("XDG_CONFIG_HOME");
unsigned int config_len = 0;
if (!home) {
home = getenv("HOME");
if (!home) {
// Can't find home directory
out[0] = 0;
return;
}
config_len = strlen(".config/");
}
unsigned int home_len = strlen(home);
unsigned int appname_len = strlen(appname);
/* first +1 is "/", second is trailing "/", third is terminating null */
if (home_len + 1 + config_len + appname_len + 1 + 1 > maxlen) {
out[0] = 0;
return;
}
memcpy(out, home, home_len);
out += home_len;
*out = '/';
out++;
if (config_len) {
memcpy(out, ".config/", config_len);
out += config_len;
/* Make the .config folder if it doesn't already exist */
*out = '\0';
mkdir(out_orig, 0755);
}
memcpy(out, appname, appname_len);
out += appname_len;
/* Make the .config/appname folder if it doesn't already exist */
*out = '\0';
mkdir(out_orig, 0755);
*out = '/';
out++;
*out = '\0';
#elif defined(CFGPATH_WINDOWS)
if (maxlen < MAX_PATH) {
out[0] = 0;
return;
}
if (!SUCCEEDED(SHGetFolderPath(NULL, CSIDL_APPDATA, NULL, 0, out))) {
out[0] = 0;
return;
}
/* We don't try to create the AppData folder as it always exists already */
unsigned int appname_len = strlen(appname);
if (strlen(out) + 1 + appname_len + 1 + 1 > maxlen) {
out[0] = 0;
return;
}
strcat(out, "\\");
strcat(out, appname);
/* Make the AppData\appname folder if it doesn't already exist */
mkdir(out);
strcat(out, "\\");
#elif defined(CFGPATH_MAC)
FSRef ref;
FSFindFolder(kUserDomain, kApplicationSupportFolderType, kCreateFolder, &ref);
char home[MAX_PATH];
FSRefMakePath(&ref, (UInt8 *)&home, MAX_PATH);
/* first +1 is "/", second is trailing "/", third is terminating null */
if (strlen(home) + 1 + strlen(appname) + 1 + 1 > maxlen) {
out[0] = 0;
return;
}
strcpy(out, home);
strcat(out, PATH_SEPARATOR_STRING);
strcat(out, appname);
/* Make the .config/appname folder if it doesn't already exist */
mkdir(out, 0755);
strcat(out, PATH_SEPARATOR_STRING);
#endif
}
/** Get an absolute path to a data storage folder, specific to this user.
*
* This function is useful for programs that need to store larger amounts of
* data specific to each user. The output is a folder (which may not exist and
* will need to be created) suitable for storing a number of data files.
*
* This path should be used for persistent, important data files the user would
* want to keep. Do not use this path for temporary files, cache files, or
* other files that can be recreated if they are deleted. Use
* get_user_cache_folder() for those instead.
*
* The returned path will always end in a platform-specific trailing slash, so
* that a filename can simply be appended to the path.
*
* Output is typically:
*
* Windows: C:\Users\jcitizen\AppData\Roaming\appname-data\
* Linux: /home/jcitizen/.local/share/appname/
* Mac: /Users/jcitizen/Library/Application Support/appname-data/
*
* @param out
* Buffer to write the path. On return will contain the path, or an empty
* string on error.
*
* @param maxlen
* Length of out. Must be >= MAX_PATH.
*
* @param appname
* Short name of the application. Avoid using spaces or version numbers, and
* use lowercase if possible.
*
* @post The folder is created if needed.
*/
static inline void get_user_data_folder(char *out, unsigned int maxlen, const char *appname)
{
#ifdef CFGPATH_LINUX
const char *out_orig = out;
char *home = getenv("XDG_DATA_HOME");
unsigned int config_len = 0;
if (!home) {
home = getenv("HOME");
if (!home) {
// Can't find home directory
out[0] = 0;
return;
}
config_len = strlen(".local/share/");
}
unsigned int home_len = strlen(home);
unsigned int appname_len = strlen(appname);
/* first +1 is "/", second is trailing "/", third is terminating null */
if (home_len + 1 + config_len + appname_len + 1 + 1 > maxlen) {
out[0] = 0;
return;
}
memcpy(out, home, home_len);
out += home_len;
*out = '/';
out++;
if (config_len) {
memcpy(out, ".local/share/", config_len);
out += config_len;
/* Make the .local/share folder if it doesn't already exist */
*out = '\0';
mkdir(out_orig, 0755);
}
memcpy(out, appname, appname_len);
out += appname_len;
/* Make the .local/share/appname folder if it doesn't already exist */
*out = '\0';
mkdir(out_orig, 0755);
*out = '/';
out++;
*out = '\0';
#elif defined(CFGPATH_WINDOWS) || defined(CFGPATH_MAC)
/* No distinction under Windows or OS X */
get_user_config_folder(out, maxlen, appname);
#endif
}
/** Get an absolute path to a temporary storage folder, specific to this user.
*
* This function is useful for programs that temporarily need to store larger
* amounts of data specific to each user. The output is a folder (which may
* not exist and will need to be created) suitable for storing a number of
* temporary files. The files may be lost or deleted when the program
* terminates.
*
* This path should be used for temporary, unimportant data files that can
* safely be deleted after the program terminates. Do not use this path for
* any important files the user would want to keep. Use get_user_data_folder()
* for those instead.
*
* The returned path will always end in a platform-specific trailing slash, so
* that a filename can simply be appended to the path.
*
* Output is typically:
*
* Windows: C:\Users\jcitizen\AppData\Local\appname\
* Linux: /home/jcitizen/.cache/appname/
* Mac: /Users/jcitizen/Library/Application Support/appname/
*
* @param out
* Buffer to write the path. On return will contain the path, or an empty
* string on error.
*
* @param maxlen
* Length of out. Must be >= MAX_PATH.
*
* @param appname
* Short name of the application. Avoid using spaces or version numbers, and
* use lowercase if possible.
*
* @post The folder is created if needed.
*/
static inline void get_user_cache_folder(char *out, unsigned int maxlen, const char *appname)
{
#ifdef CFGPATH_LINUX
const char *out_orig = out;
char *home = getenv("XDG_CACHE_HOME");
unsigned int config_len = 0;
if (!home) {
home = getenv("HOME");
if (!home) {
// Can't find home directory
out[0] = 0;
return;
}
config_len = strlen(".cache/");
}
unsigned int home_len = strlen(home);
unsigned int appname_len = strlen(appname);
/* first +1 is "/", second is trailing "/", third is terminating null */
if (home_len + 1 + config_len + appname_len + 1 + 1 > maxlen) {
out[0] = 0;
return;
}
memcpy(out, home, home_len);
out += home_len;
*out = '/';
out++;
if (config_len) {
memcpy(out, ".cache/", config_len);
out += config_len;
/* Make the .cache folder if it doesn't already exist */
*out = '\0';
mkdir(out_orig, 0755);
}
memcpy(out, appname, appname_len);
out += appname_len;
/* Make the .cache/appname folder if it doesn't already exist */
*out = '\0';
mkdir(out_orig, 0755);
*out = '/';
out++;
*out = '\0';
#elif defined(CFGPATH_WINDOWS)
if (maxlen < MAX_PATH) {
out[0] = 0;
return;
}
if (!SUCCEEDED(SHGetFolderPath(NULL, CSIDL_LOCAL_APPDATA, NULL, 0, out))) {
out[0] = 0;
return;
}
/* We don't try to create the AppData folder as it always exists already */
unsigned int appname_len = strlen(appname);
if (strlen(out) + 1 + appname_len + 1 + 1 > maxlen) {
out[0] = 0;
return;
}
strcat(out, "\\");
strcat(out, appname);
/* Make the AppData\appname folder if it doesn't already exist */
mkdir(out);
strcat(out, "\\");
#elif defined(CFGPATH_MAC)
/* No distinction under OS X */
get_user_config_folder(out, maxlen, appname);
#endif
}
#endif /* CFGPATH_H_ */