Check for snprintf in stdio.h
[openal-soft.git] / Alc / alcConfig.c
blob89e33a995379b37861deab269a9e07a46b0dbd09
1 /**
2 * OpenAL cross platform audio library
3 * Copyright (C) 1999-2007 by authors.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 * Or go to http://www.gnu.org/copyleft/lgpl.html
21 #ifdef _WIN32
22 #ifdef __MINGW32__
23 #define _WIN32_IE 0x501
24 #else
25 #define _WIN32_IE 0x400
26 #endif
27 #endif
29 #include "config.h"
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <ctype.h>
34 #include <string.h>
35 #ifdef _WIN32_IE
36 #include <windows.h>
37 #include <shlobj.h>
38 #endif
40 #include "alMain.h"
41 #include "compat.h"
42 #include "bool.h"
45 typedef struct ConfigEntry {
46 char *key;
47 char *value;
48 } ConfigEntry;
50 typedef struct ConfigBlock {
51 ConfigEntry *entries;
52 unsigned int entryCount;
53 } ConfigBlock;
54 static ConfigBlock cfgBlock;
57 static char *lstrip(char *line)
59 while(isspace(line[0]))
60 line++;
61 return line;
64 static char *rstrip(char *line)
66 size_t len = strlen(line);
67 while(len > 0 && isspace(line[len-1]))
68 len--;
69 line[len] = 0;
70 return line;
73 static int readline(FILE *f, char **output, size_t *maxlen)
75 size_t len = 0;
76 int c;
78 while((c=fgetc(f)) != EOF && (c == '\r' || c == '\n'))
80 if(c == EOF)
81 return 0;
83 do {
84 if(len+1 >= *maxlen)
86 void *temp = NULL;
87 size_t newmax;
89 newmax = (*maxlen ? (*maxlen)<<1 : 32);
90 if(newmax > *maxlen)
91 temp = realloc(*output, newmax);
92 if(!temp)
94 ERR("Failed to realloc "SZFMT" bytes from "SZFMT"!\n", newmax, *maxlen);
95 return 0;
98 *output = temp;
99 *maxlen = newmax;
101 (*output)[len++] = c;
102 (*output)[len] = '\0';
103 } while((c=fgetc(f)) != EOF && c != '\r' && c != '\n');
105 return 1;
109 static char *expdup(const char *str)
111 char *output = NULL;
112 size_t maxlen = 0;
113 size_t len = 0;
115 while(*str != '\0')
117 const char *addstr;
118 size_t addstrlen;
119 size_t i;
121 if(str[0] != '$')
123 const char *next = strchr(str, '$');
124 addstr = str;
125 addstrlen = next ? (size_t)(next-str) : strlen(str);
127 str += addstrlen;
129 else
131 str++;
132 if(*str == '$')
134 const char *next = strchr(str+1, '$');
135 addstr = str;
136 addstrlen = next ? (size_t)(next-str) : strlen(str);
138 str += addstrlen;
140 else
142 bool hasbraces;
143 char envname[1024];
144 size_t k = 0;
146 hasbraces = (*str == '{');
147 if(hasbraces) str++;
149 while((isalnum(*str) || *str == '_') && k < sizeof(envname)-1)
150 envname[k++] = *(str++);
151 envname[k++] = '\0';
153 if(hasbraces && *str != '}')
154 continue;
156 if(hasbraces) str++;
157 if((addstr=getenv(envname)) == NULL)
158 continue;
159 addstrlen = strlen(addstr);
162 if(addstrlen == 0)
163 continue;
165 if(addstrlen >= maxlen-len)
167 void *temp = NULL;
168 size_t newmax;
170 newmax = len+addstrlen+1;
171 if(newmax > maxlen)
172 temp = realloc(output, newmax);
173 if(!temp)
175 ERR("Failed to realloc "SZFMT" bytes from "SZFMT"!\n", newmax, maxlen);
176 return output;
179 output = temp;
180 maxlen = newmax;
183 for(i = 0;i < addstrlen;i++)
184 output[len++] = addstr[i];
185 output[len] = '\0';
188 return output ? output : calloc(1, 1);
192 static void LoadConfigFromFile(FILE *f)
194 char curSection[128] = "";
195 char *buffer = NULL;
196 size_t maxlen = 0;
197 ConfigEntry *ent;
199 while(readline(f, &buffer, &maxlen))
201 char *line, *comment;
202 char key[256] = "";
203 char value[256] = "";
205 comment = strchr(buffer, '#');
206 if(comment) *(comment++) = 0;
208 line = rstrip(lstrip(buffer));
209 if(!line[0])
210 continue;
212 if(line[0] == '[')
214 char *section = line+1;
215 char *endsection;
217 endsection = strchr(section, ']');
218 if(!endsection || section == endsection || endsection[1] != 0)
220 ERR("config parse error: bad line \"%s\"\n", line);
221 continue;
223 *endsection = 0;
225 if(strcasecmp(section, "general") == 0)
226 curSection[0] = 0;
227 else
229 strncpy(curSection, section, sizeof(curSection)-1);
230 curSection[sizeof(curSection)-1] = 0;
233 continue;
236 if(sscanf(line, "%255[^=] = \"%255[^\"]\"", key, value) == 2 ||
237 sscanf(line, "%255[^=] = '%255[^\']'", key, value) == 2 ||
238 sscanf(line, "%255[^=] = %255[^\n]", key, value) == 2)
240 /* sscanf doesn't handle '' or "" as empty values, so clip it
241 * manually. */
242 if(strcmp(value, "\"\"") == 0 || strcmp(value, "''") == 0)
243 value[0] = 0;
245 else if(sscanf(line, "%255[^=] %255[=]", key, value) == 2)
247 /* Special case for 'key =' */
248 value[0] = 0;
250 else
252 ERR("config parse error: malformed option line: \"%s\"\n\n", line);
253 continue;
255 rstrip(key);
257 if(curSection[0] != 0)
259 size_t len = strlen(curSection);
260 memmove(&key[len+1], key, sizeof(key)-1-len);
261 key[len] = '/';
262 memcpy(key, curSection, len);
265 /* Check if we already have this option set */
266 ent = cfgBlock.entries;
267 while((unsigned int)(ent-cfgBlock.entries) < cfgBlock.entryCount)
269 if(strcasecmp(ent->key, key) == 0)
270 break;
271 ent++;
274 if((unsigned int)(ent-cfgBlock.entries) >= cfgBlock.entryCount)
276 /* Allocate a new option entry */
277 ent = realloc(cfgBlock.entries, (cfgBlock.entryCount+1)*sizeof(ConfigEntry));
278 if(!ent)
280 ERR("config parse error: error reallocating config entries\n");
281 continue;
283 cfgBlock.entries = ent;
284 ent = cfgBlock.entries + cfgBlock.entryCount;
285 cfgBlock.entryCount++;
287 ent->key = strdup(key);
288 ent->value = NULL;
291 free(ent->value);
292 ent->value = expdup(value);
294 TRACE("found '%s' = '%s'\n", ent->key, ent->value);
297 free(buffer);
300 #ifdef _WIN32
301 void ReadALConfig(void)
303 WCHAR buffer[PATH_MAX];
304 const WCHAR *str;
305 FILE *f;
307 if(SHGetSpecialFolderPathW(NULL, buffer, CSIDL_APPDATA, FALSE) != FALSE)
309 al_string filepath = AL_STRING_INIT_STATIC();
310 al_string_copy_wcstr(&filepath, buffer);
311 al_string_append_cstr(&filepath, "\\alsoft.ini");
313 TRACE("Loading config %s...\n", al_string_get_cstr(filepath));
314 f = al_fopen(al_string_get_cstr(filepath), "rt");
315 if(f)
317 LoadConfigFromFile(f);
318 fclose(f);
320 al_string_deinit(&filepath);
323 if((str=_wgetenv(L"ALSOFT_CONF")) != NULL && *str)
325 al_string filepath = AL_STRING_INIT_STATIC();
326 al_string_copy_wcstr(&filepath, str);
328 TRACE("Loading config %s...\n", al_string_get_cstr(filepath));
329 f = al_fopen(al_string_get_cstr(filepath), "rt");
330 if(f)
332 LoadConfigFromFile(f);
333 fclose(f);
335 al_string_deinit(&filepath);
338 #else
339 void ReadALConfig(void)
341 char buffer[PATH_MAX];
342 const char *str;
343 FILE *f;
345 str = "/etc/openal/alsoft.conf";
347 TRACE("Loading config %s...\n", str);
348 f = al_fopen(str, "r");
349 if(f)
351 LoadConfigFromFile(f);
352 fclose(f);
355 if(!(str=getenv("XDG_CONFIG_DIRS")) || str[0] == 0)
356 str = "/etc/xdg";
357 strncpy(buffer, str, sizeof(buffer)-1);
358 buffer[sizeof(buffer)-1] = 0;
359 /* Go through the list in reverse, since "the order of base directories
360 * denotes their importance; the first directory listed is the most
361 * important". Ergo, we need to load the settings from the later dirs
362 * first so that the settings in the earlier dirs override them.
364 while(1)
366 char *next = strrchr(buffer, ':');
367 if(next) *(next++) = 0;
368 else next = buffer;
370 if(next[0] != '/')
371 WARN("Ignoring XDG config dir: %s\n", next);
372 else
374 size_t len = strlen(next);
375 strncpy(next+len, "/alsoft.conf", buffer+sizeof(buffer)-next-len);
376 buffer[sizeof(buffer)-1] = 0;
378 TRACE("Loading config %s...\n", next);
379 f = al_fopen(next, "r");
380 if(f)
382 LoadConfigFromFile(f);
383 fclose(f);
386 if(next == buffer)
387 break;
390 if((str=getenv("HOME")) != NULL && *str)
392 snprintf(buffer, sizeof(buffer), "%s/.alsoftrc", str);
394 TRACE("Loading config %s...\n", buffer);
395 f = al_fopen(buffer, "r");
396 if(f)
398 LoadConfigFromFile(f);
399 fclose(f);
403 if((str=getenv("XDG_CONFIG_HOME")) != NULL && str[0] != 0)
404 snprintf(buffer, sizeof(buffer), "%s/%s", str, "alsoft.conf");
405 else
407 buffer[0] = 0;
408 if((str=getenv("HOME")) != NULL && str[0] != 0)
409 snprintf(buffer, sizeof(buffer), "%s/.config/%s", str, "alsoft.conf");
411 if(buffer[0] != 0)
413 TRACE("Loading config %s...\n", buffer);
414 f = al_fopen(buffer, "r");
415 if(f)
417 LoadConfigFromFile(f);
418 fclose(f);
422 if((str=getenv("ALSOFT_CONF")) != NULL && *str)
424 TRACE("Loading config %s...\n", str);
425 f = al_fopen(str, "r");
426 if(f)
428 LoadConfigFromFile(f);
429 fclose(f);
433 #endif
435 void FreeALConfig(void)
437 unsigned int i;
439 for(i = 0;i < cfgBlock.entryCount;i++)
441 free(cfgBlock.entries[i].key);
442 free(cfgBlock.entries[i].value);
444 free(cfgBlock.entries);
447 const char *GetConfigValue(const char *blockName, const char *keyName, const char *def)
449 unsigned int i;
450 char key[256];
452 if(!keyName)
453 return def;
455 if(blockName && strcasecmp(blockName, "general") != 0)
456 snprintf(key, sizeof(key), "%s/%s", blockName, keyName);
457 else
459 strncpy(key, keyName, sizeof(key)-1);
460 key[sizeof(key)-1] = 0;
463 for(i = 0;i < cfgBlock.entryCount;i++)
465 if(strcasecmp(cfgBlock.entries[i].key, key) == 0)
467 TRACE("Found %s = \"%s\"\n", key, cfgBlock.entries[i].value);
468 if(cfgBlock.entries[i].value[0])
469 return cfgBlock.entries[i].value;
470 return def;
474 TRACE("Key %s not found\n", key);
475 return def;
478 int ConfigValueExists(const char *blockName, const char *keyName)
480 const char *val = GetConfigValue(blockName, keyName, "");
481 return !!val[0];
484 int ConfigValueStr(const char *blockName, const char *keyName, const char **ret)
486 const char *val = GetConfigValue(blockName, keyName, "");
487 if(!val[0]) return 0;
489 *ret = val;
490 return 1;
493 int ConfigValueInt(const char *blockName, const char *keyName, int *ret)
495 const char *val = GetConfigValue(blockName, keyName, "");
496 if(!val[0]) return 0;
498 *ret = strtol(val, NULL, 0);
499 return 1;
502 int ConfigValueUInt(const char *blockName, const char *keyName, unsigned int *ret)
504 const char *val = GetConfigValue(blockName, keyName, "");
505 if(!val[0]) return 0;
507 *ret = strtoul(val, NULL, 0);
508 return 1;
511 int ConfigValueFloat(const char *blockName, const char *keyName, float *ret)
513 const char *val = GetConfigValue(blockName, keyName, "");
514 if(!val[0]) return 0;
516 #ifdef HAVE_STRTOF
517 *ret = strtof(val, NULL);
518 #else
519 *ret = (float)strtod(val, NULL);
520 #endif
521 return 1;
524 int ConfigValueBool(const char *blockName, const char *keyName, int *ret)
526 const char *val = GetConfigValue(blockName, keyName, "");
527 if(!val[0]) return 0;
529 *ret = (strcasecmp(val, "true") == 0 || strcasecmp(val, "yes") == 0 ||
530 strcasecmp(val, "on") == 0 || atoi(val) != 0);
531 return 1;
534 int GetConfigValueBool(const char *blockName, const char *keyName, int def)
536 const char *val = GetConfigValue(blockName, keyName, "");
538 if(!val[0]) return !!def;
539 return (strcasecmp(val, "true") == 0 || strcasecmp(val, "yes") == 0 ||
540 strcasecmp(val, "on") == 0 || atoi(val) != 0);