cfgpath.h
Go to the documentation of this file.
1 
33 #ifndef CFGPATH_H_
34 #define CFGPATH_H_
35 
36 #if defined(_MSC_VER) || defined(__MINGW32__)
37 #define inline __inline
38 #include <direct.h>
39 #define mkdir _mkdir
40 #endif
41 
42 #ifdef __unix__
43 #include <sys/param.h>
44 #endif
45 
46 #if defined(__linux__) || defined(BSD)
47 #define CFGPATH_LINUX
48 #include <string.h>
49 #include <stdlib.h>
50 #include <sys/stat.h>
51 #define MAX_PATH 512 /* arbitrary value */
52 #define PATH_SEPARATOR_CHAR '/'
53 #define PATH_SEPARATOR_STRING "/"
54 #elif defined(WIN32)
55 #define CFGPATH_WINDOWS
56 #include <shlobj.h>
57 /* MAX_PATH is defined by the Windows API */
58 #define PATH_SEPARATOR_CHAR '\\'
59 #define PATH_SEPARATOR_STRING "\\"
60 #elif defined(__APPLE__)
61 #define CFGPATH_MAC
62 #include <CoreServices/CoreServices.h>
63 #include <sys/stat.h>
64 #define MAX_PATH PATH_MAX
65 #define PATH_SEPARATOR_CHAR '/'
66 #define PATH_SEPARATOR_STRING "/"
67 #else
68 #error cfgpath.h functions have not been implemented for your platform! Please send patches.
69 #endif
70 
96 static inline void get_user_config_file(char *out, unsigned int maxlen, const char *appname)
97 {
98 #ifdef CFGPATH_LINUX
99  const char *out_orig = out;
100  char *home = getenv("XDG_CONFIG_HOME");
101  unsigned int config_len = 0;
102  if (!home) {
103  home = getenv("HOME");
104  if (!home) {
105  // Can't find home directory
106  out[0] = 0;
107  return;
108  }
109  config_len = strlen(".config/");
110  }
111 
112  unsigned int home_len = strlen(home);
113  unsigned int appname_len = strlen(appname);
114  const int ext_len = strlen(".conf");
115 
116  /* first +1 is "/", second is terminating null */
117  if (home_len + 1 + config_len + appname_len + ext_len + 1 > maxlen) {
118  out[0] = 0;
119  return;
120  }
121 
122  memcpy(out, home, home_len);
123  out += home_len;
124  *out = '/';
125  out++;
126  if (config_len) {
127  memcpy(out, ".config/", config_len);
128  out += config_len;
129  /* Make the .config folder if it doesn't already exist */
130  *out = '\0';
131  mkdir(out_orig, 0755);
132  }
133  memcpy(out, appname, appname_len);
134  out += appname_len;
135  memcpy(out, ".conf", ext_len);
136  out += ext_len;
137  *out = '\0';
138 #elif defined(CFGPATH_WINDOWS)
139  if (maxlen < MAX_PATH) {
140  out[0] = 0;
141  return;
142  }
143  if (!SUCCEEDED(SHGetFolderPath(NULL, CSIDL_APPDATA, NULL, 0, out))) {
144  out[0] = 0;
145  return;
146  }
147  /* We don't try to create the AppData folder as it always exists already */
148  unsigned int appname_len = strlen(appname);
149  if (strlen(out) + 1 + appname_len + strlen(".ini") + 1 > maxlen) {
150  out[0] = 0;
151  return;
152  }
153  strcat(out, "\\");
154  strcat(out, appname);
155  strcat(out, ".ini");
156 #elif defined(CFGPATH_MAC)
157  FSRef ref;
158  FSFindFolder(kUserDomain, kApplicationSupportFolderType, kCreateFolder, &ref);
159  char home[MAX_PATH];
160  FSRefMakePath(&ref, (UInt8 *)&home, MAX_PATH);
161  /* first +1 is "/", second is terminating null */
162  const char *ext = ".conf";
163  if (strlen(home) + 1 + strlen(appname) + strlen(ext) + 1 > maxlen) {
164  out[0] = 0;
165  return;
166  }
167 
168  strcpy(out, home);
169  strcat(out, PATH_SEPARATOR_STRING);
170  strcat(out, appname);
171  strcat(out, ext);
172 #endif
173 }
174 
203 static inline void get_user_config_folder(char *out, unsigned int maxlen, const char *appname)
204 {
205 #ifdef CFGPATH_LINUX
206  const char *out_orig = out;
207  char *home = getenv("XDG_CONFIG_HOME");
208  unsigned int config_len = 0;
209  if (!home) {
210  home = getenv("HOME");
211  if (!home) {
212  // Can't find home directory
213  out[0] = 0;
214  return;
215  }
216  config_len = strlen(".config/");
217  }
218 
219  unsigned int home_len = strlen(home);
220  unsigned int appname_len = strlen(appname);
221 
222  /* first +1 is "/", second is trailing "/", third is terminating null */
223  if (home_len + 1 + config_len + appname_len + 1 + 1 > maxlen) {
224  out[0] = 0;
225  return;
226  }
227 
228  memcpy(out, home, home_len);
229  out += home_len;
230  *out = '/';
231  out++;
232  if (config_len) {
233  memcpy(out, ".config/", config_len);
234  out += config_len;
235  /* Make the .config folder if it doesn't already exist */
236  *out = '\0';
237  mkdir(out_orig, 0755);
238  }
239  memcpy(out, appname, appname_len);
240  out += appname_len;
241  /* Make the .config/appname folder if it doesn't already exist */
242  *out = '\0';
243  mkdir(out_orig, 0755);
244  *out = '/';
245  out++;
246  *out = '\0';
247 #elif defined(CFGPATH_WINDOWS)
248  if (maxlen < MAX_PATH) {
249  out[0] = 0;
250  return;
251  }
252  if (!SUCCEEDED(SHGetFolderPath(NULL, CSIDL_APPDATA, NULL, 0, out))) {
253  out[0] = 0;
254  return;
255  }
256  /* We don't try to create the AppData folder as it always exists already */
257  unsigned int appname_len = strlen(appname);
258  if (strlen(out) + 1 + appname_len + 1 + 1 > maxlen) {
259  out[0] = 0;
260  return;
261  }
262  strcat(out, "\\");
263  strcat(out, appname);
264  /* Make the AppData\appname folder if it doesn't already exist */
265  mkdir(out);
266  strcat(out, "\\");
267 #elif defined(CFGPATH_MAC)
268  FSRef ref;
269  FSFindFolder(kUserDomain, kApplicationSupportFolderType, kCreateFolder, &ref);
270  char home[MAX_PATH];
271  FSRefMakePath(&ref, (UInt8 *)&home, MAX_PATH);
272  /* first +1 is "/", second is trailing "/", third is terminating null */
273  if (strlen(home) + 1 + strlen(appname) + 1 + 1 > maxlen) {
274  out[0] = 0;
275  return;
276  }
277 
278  strcpy(out, home);
279  strcat(out, PATH_SEPARATOR_STRING);
280  strcat(out, appname);
281  /* Make the .config/appname folder if it doesn't already exist */
282  mkdir(out, 0755);
283  strcat(out, PATH_SEPARATOR_STRING);
284 #endif
285 }
286 
320 static inline void get_user_data_folder(char *out, unsigned int maxlen, const char *appname)
321 {
322 #ifdef CFGPATH_LINUX
323  const char *out_orig = out;
324  char *home = getenv("XDG_DATA_HOME");
325  unsigned int config_len = 0;
326  if (!home) {
327  home = getenv("HOME");
328  if (!home) {
329  // Can't find home directory
330  out[0] = 0;
331  return;
332  }
333  config_len = strlen(".local/share/");
334  }
335 
336  unsigned int home_len = strlen(home);
337  unsigned int appname_len = strlen(appname);
338 
339  /* first +1 is "/", second is trailing "/", third is terminating null */
340  if (home_len + 1 + config_len + appname_len + 1 + 1 > maxlen) {
341  out[0] = 0;
342  return;
343  }
344 
345  memcpy(out, home, home_len);
346  out += home_len;
347  *out = '/';
348  out++;
349  if (config_len) {
350  memcpy(out, ".local/share/", config_len);
351  out += config_len;
352  /* Make the .local/share folder if it doesn't already exist */
353  *out = '\0';
354  mkdir(out_orig, 0755);
355  }
356  memcpy(out, appname, appname_len);
357  out += appname_len;
358  /* Make the .local/share/appname folder if it doesn't already exist */
359  *out = '\0';
360  mkdir(out_orig, 0755);
361  *out = '/';
362  out++;
363  *out = '\0';
364 #elif defined(CFGPATH_WINDOWS) || defined(CFGPATH_MAC)
365  /* No distinction under Windows or OS X */
366  get_user_config_folder(out, maxlen, appname);
367 #endif
368 }
369 
405 static inline void get_user_cache_folder(char *out, unsigned int maxlen, const char *appname)
406 {
407 #ifdef CFGPATH_LINUX
408  const char *out_orig = out;
409  char *home = getenv("XDG_CACHE_HOME");
410  unsigned int config_len = 0;
411  if (!home) {
412  home = getenv("HOME");
413  if (!home) {
414  // Can't find home directory
415  out[0] = 0;
416  return;
417  }
418  config_len = strlen(".cache/");
419  }
420 
421  unsigned int home_len = strlen(home);
422  unsigned int appname_len = strlen(appname);
423 
424  /* first +1 is "/", second is trailing "/", third is terminating null */
425  if (home_len + 1 + config_len + appname_len + 1 + 1 > maxlen) {
426  out[0] = 0;
427  return;
428  }
429 
430  memcpy(out, home, home_len);
431  out += home_len;
432  *out = '/';
433  out++;
434  if (config_len) {
435  memcpy(out, ".cache/", config_len);
436  out += config_len;
437  /* Make the .cache folder if it doesn't already exist */
438  *out = '\0';
439  mkdir(out_orig, 0755);
440  }
441  memcpy(out, appname, appname_len);
442  out += appname_len;
443  /* Make the .cache/appname folder if it doesn't already exist */
444  *out = '\0';
445  mkdir(out_orig, 0755);
446  *out = '/';
447  out++;
448  *out = '\0';
449 #elif defined(CFGPATH_WINDOWS)
450  if (maxlen < MAX_PATH) {
451  out[0] = 0;
452  return;
453  }
454  if (!SUCCEEDED(SHGetFolderPath(NULL, CSIDL_LOCAL_APPDATA, NULL, 0, out))) {
455  out[0] = 0;
456  return;
457  }
458  /* We don't try to create the AppData folder as it always exists already */
459  unsigned int appname_len = strlen(appname);
460  if (strlen(out) + 1 + appname_len + 1 + 1 > maxlen) {
461  out[0] = 0;
462  return;
463  }
464  strcat(out, "\\");
465  strcat(out, appname);
466  /* Make the AppData\appname folder if it doesn't already exist */
467  mkdir(out);
468  strcat(out, "\\");
469 #elif defined(CFGPATH_MAC)
470  /* No distinction under OS X */
471  get_user_config_folder(out, maxlen, appname);
472 #endif
473 }
474 
475 #endif /* CFGPATH_H_ */
get_user_config_folder
static void get_user_config_folder(char *out, unsigned int maxlen, const char *appname)
Definition: cfgpath.h:203
get_user_cache_folder
static void get_user_cache_folder(char *out, unsigned int maxlen, const char *appname)
Definition: cfgpath.h:405
kitti-batch-convert.out
string out
Definition: kitti-batch-convert.py:7
get_user_config_file
static void get_user_config_file(char *out, unsigned int maxlen, const char *appname)
Definition: cfgpath.h:96
get_user_data_folder
static void get_user_data_folder(char *out, unsigned int maxlen, const char *appname)
Definition: cfgpath.h:320


mp2p_icp
Author(s):
autogenerated on Wed Oct 23 2024 02:45:38