Move an inline definition to a more appropriate source
[openal-soft.git] / Alc / alcConfig.c
blob2c9aef41bcba51e599c276363b969179808f8d76
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., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, 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 <shlobj.h>
37 #endif
39 #include "alMain.h"
40 #include "compat.h"
43 typedef struct ConfigEntry {
44 char *key;
45 char *value;
46 } ConfigEntry;
48 typedef struct ConfigBlock {
49 ConfigEntry *entries;
50 unsigned int entryCount;
51 } ConfigBlock;
52 static ConfigBlock cfgBlock;
55 static char *lstrip(char *line)
57 while(isspace(line[0]))
58 line++;
59 return line;
62 static char *rstrip(char *line)
64 size_t len = strlen(line);
65 while(len > 0 && isspace(line[len-1]))
66 len--;
67 line[len] = 0;
68 return line;
71 static int readline(FILE *f, char **output, size_t *maxlen)
73 size_t len = 0;
74 int c;
76 while((c=fgetc(f)) != EOF && (c == '\r' || c == '\n'))
78 if(c == EOF)
79 return 0;
81 do {
82 if(len+1 >= *maxlen)
84 void *temp = NULL;
85 size_t newmax;
87 newmax = (*maxlen ? (*maxlen)<<1 : 32);
88 if(newmax > *maxlen)
89 temp = realloc(*output, newmax);
90 if(!temp)
92 ERR("Failed to realloc "SZFMT" bytes from "SZFMT"!\n", newmax, *maxlen);
93 return 0;
96 *output = temp;
97 *maxlen = newmax;
99 (*output)[len++] = c;
100 (*output)[len] = '\0';
101 } while((c=fgetc(f)) != EOF && c != '\r' && c != '\n');
103 return 1;
107 static char *expdup(const char *str)
109 char *output = NULL;
110 size_t maxlen = 0;
111 size_t len = 0;
113 while(*str != '\0')
115 const char *addstr;
116 size_t addstrlen;
117 size_t i;
119 if(str[0] != '$')
121 const char *next = strchr(str, '$');
122 addstr = str;
123 addstrlen = next ? (size_t)(next-str) : strlen(str);
125 str += addstrlen;
127 else
129 str++;
130 if(*str == '$')
132 const char *next = strchr(str+1, '$');
133 addstr = str;
134 addstrlen = next ? (size_t)(next-str) : strlen(str);
136 str += addstrlen;
138 else
140 char envname[1024];
141 size_t k = 0;
143 while((isalnum(*str) || *str == '_') && k < sizeof(envname)-1)
144 envname[k++] = *(str++);
145 envname[k++] = '\0';
147 if((addstr=getenv(envname)) == NULL)
148 continue;
149 addstrlen = strlen(addstr);
152 if(addstrlen == 0)
153 continue;
155 if(addstrlen >= maxlen-len)
157 void *temp = NULL;
158 size_t newmax;
160 newmax = len+addstrlen+1;
161 if(newmax > maxlen)
162 temp = realloc(output, newmax);
163 if(!temp)
165 ERR("Failed to realloc "SZFMT" bytes from "SZFMT"!\n", newmax, maxlen);
166 return output;
169 output = temp;
170 maxlen = newmax;
173 for(i = 0;i < addstrlen;i++)
174 output[len++] = addstr[i];
175 output[len] = '\0';
178 return output ? output : calloc(1, 1);
182 static void LoadConfigFromFile(FILE *f)
184 char curSection[128] = "";
185 char *buffer = NULL;
186 size_t maxlen = 0;
187 ConfigEntry *ent;
189 while(readline(f, &buffer, &maxlen))
191 char *line, *comment;
192 char key[256] = "";
193 char value[256] = "";
195 comment = strchr(buffer, '#');
196 if(comment) *(comment++) = 0;
198 line = rstrip(lstrip(buffer));
199 if(!line[0])
200 continue;
202 if(line[0] == '[')
204 char *section = line+1;
205 char *endsection;
207 endsection = strchr(section, ']');
208 if(!endsection || section == endsection || endsection[1] != 0)
210 ERR("config parse error: bad line \"%s\"\n", line);
211 continue;
213 *endsection = 0;
215 if(strcasecmp(section, "general") == 0)
216 curSection[0] = 0;
217 else
219 strncpy(curSection, section, sizeof(curSection)-1);
220 curSection[sizeof(curSection)-1] = 0;
223 continue;
226 if(sscanf(line, "%255[^=] = \"%255[^\"]\"", key, value) == 2 ||
227 sscanf(line, "%255[^=] = '%255[^\']'", key, value) == 2 ||
228 sscanf(line, "%255[^=] = %255[^\n]", key, value) == 2)
230 /* sscanf doesn't handle '' or "" as empty values, so clip it
231 * manually. */
232 if(strcmp(value, "\"\"") == 0 || strcmp(value, "''") == 0)
233 value[0] = 0;
235 else if(sscanf(line, "%255[^=] %255[=]", key, value) == 2)
237 /* Special case for 'key =' */
238 value[0] = 0;
240 else
242 ERR("config parse error: malformed option line: \"%s\"\n\n", line);
243 continue;
245 rstrip(key);
247 if(curSection[0] != 0)
249 size_t len = strlen(curSection);
250 memmove(&key[len+1], key, sizeof(key)-1-len);
251 key[len] = '/';
252 memcpy(key, curSection, len);
255 /* Check if we already have this option set */
256 ent = cfgBlock.entries;
257 while((unsigned int)(ent-cfgBlock.entries) < cfgBlock.entryCount)
259 if(strcasecmp(ent->key, key) == 0)
260 break;
261 ent++;
264 if((unsigned int)(ent-cfgBlock.entries) >= cfgBlock.entryCount)
266 /* Allocate a new option entry */
267 ent = realloc(cfgBlock.entries, (cfgBlock.entryCount+1)*sizeof(ConfigEntry));
268 if(!ent)
270 ERR("config parse error: error reallocating config entries\n");
271 continue;
273 cfgBlock.entries = ent;
274 ent = cfgBlock.entries + cfgBlock.entryCount;
275 cfgBlock.entryCount++;
277 ent->key = strdup(key);
278 ent->value = NULL;
281 free(ent->value);
282 ent->value = expdup(value);
284 TRACE("found '%s' = '%s'\n", ent->key, ent->value);
287 free(buffer);
290 #ifdef _WIN32
291 void ReadALConfig(void)
293 WCHAR buffer[PATH_MAX];
294 const WCHAR *str;
295 FILE *f;
297 if(SHGetSpecialFolderPathW(NULL, buffer, CSIDL_APPDATA, FALSE) != FALSE)
299 size_t p = lstrlenW(buffer);
300 _snwprintf(buffer+p, PATH_MAX-p, L"\\alsoft.ini");
302 TRACE("Loading config %ls...\n", buffer);
303 f = _wfopen(buffer, L"rt");
304 if(f)
306 LoadConfigFromFile(f);
307 fclose(f);
311 if((str=_wgetenv(L"ALSOFT_CONF")) != NULL && *str)
313 TRACE("Loading config %ls...\n", str);
314 f = _wfopen(str, L"rt");
315 if(f)
317 LoadConfigFromFile(f);
318 fclose(f);
322 #else
323 void ReadALConfig(void)
325 char buffer[PATH_MAX];
326 const char *str;
327 FILE *f;
329 str = "/etc/openal/alsoft.conf";
331 TRACE("Loading config %s...\n", str);
332 f = al_fopen(str, "r");
333 if(f)
335 LoadConfigFromFile(f);
336 fclose(f);
339 if(!(str=getenv("XDG_CONFIG_DIRS")) || str[0] == 0)
340 str = "/etc/xdg";
341 strncpy(buffer, str, sizeof(buffer)-1);
342 buffer[sizeof(buffer)-1] = 0;
343 /* Go through the list in reverse, since "the order of base directories
344 * denotes their importance; the first directory listed is the most
345 * important". Ergo, we need to load the settings from the later dirs
346 * first so that the settings in the earlier dirs override them.
348 while(1)
350 char *next = strrchr(buffer, ':');
351 if(next) *(next++) = 0;
352 else next = buffer;
354 if(next[0] != '/')
355 WARN("Ignoring XDG config dir: %s\n", next);
356 else
358 size_t len = strlen(next);
359 strncpy(next+len, "/alsoft.conf", buffer+sizeof(buffer)-next-len);
360 buffer[sizeof(buffer)-1] = 0;
362 TRACE("Loading config %s...\n", next);
363 f = al_fopen(next, "r");
364 if(f)
366 LoadConfigFromFile(f);
367 fclose(f);
370 if(next == buffer)
371 break;
374 if((str=getenv("HOME")) != NULL && *str)
376 snprintf(buffer, sizeof(buffer), "%s/.alsoftrc", str);
378 TRACE("Loading config %s...\n", buffer);
379 f = al_fopen(buffer, "r");
380 if(f)
382 LoadConfigFromFile(f);
383 fclose(f);
387 if((str=getenv("XDG_CONFIG_HOME")) != NULL && str[0] != 0)
388 snprintf(buffer, sizeof(buffer), "%s/%s", str, "alsoft.conf");
389 else
391 buffer[0] = 0;
392 if((str=getenv("HOME")) != NULL && str[0] != 0)
393 snprintf(buffer, sizeof(buffer), "%s/.config/%s", str, "alsoft.conf");
395 if(buffer[0] != 0)
397 TRACE("Loading config %s...\n", buffer);
398 f = al_fopen(buffer, "r");
399 if(f)
401 LoadConfigFromFile(f);
402 fclose(f);
406 if((str=getenv("ALSOFT_CONF")) != NULL && *str)
408 TRACE("Loading config %s...\n", str);
409 f = al_fopen(str, "r");
410 if(f)
412 LoadConfigFromFile(f);
413 fclose(f);
417 #endif
419 void FreeALConfig(void)
421 unsigned int i;
423 for(i = 0;i < cfgBlock.entryCount;i++)
425 free(cfgBlock.entries[i].key);
426 free(cfgBlock.entries[i].value);
428 free(cfgBlock.entries);
431 const char *GetConfigValue(const char *blockName, const char *keyName, const char *def)
433 unsigned int i;
434 char key[256];
436 if(!keyName)
437 return def;
439 if(blockName && strcasecmp(blockName, "general") != 0)
440 snprintf(key, sizeof(key), "%s/%s", blockName, keyName);
441 else
443 strncpy(key, keyName, sizeof(key)-1);
444 key[sizeof(key)-1] = 0;
447 for(i = 0;i < cfgBlock.entryCount;i++)
449 if(strcasecmp(cfgBlock.entries[i].key, key) == 0)
451 TRACE("Found %s = \"%s\"\n", key, cfgBlock.entries[i].value);
452 if(cfgBlock.entries[i].value[0])
453 return cfgBlock.entries[i].value;
454 return def;
458 TRACE("Key %s not found\n", key);
459 return def;
462 int ConfigValueExists(const char *blockName, const char *keyName)
464 const char *val = GetConfigValue(blockName, keyName, "");
465 return !!val[0];
468 int ConfigValueStr(const char *blockName, const char *keyName, const char **ret)
470 const char *val = GetConfigValue(blockName, keyName, "");
471 if(!val[0]) return 0;
473 *ret = val;
474 return 1;
477 int ConfigValueInt(const char *blockName, const char *keyName, int *ret)
479 const char *val = GetConfigValue(blockName, keyName, "");
480 if(!val[0]) return 0;
482 *ret = strtol(val, NULL, 0);
483 return 1;
486 int ConfigValueUInt(const char *blockName, const char *keyName, unsigned int *ret)
488 const char *val = GetConfigValue(blockName, keyName, "");
489 if(!val[0]) return 0;
491 *ret = strtoul(val, NULL, 0);
492 return 1;
495 int ConfigValueFloat(const char *blockName, const char *keyName, float *ret)
497 const char *val = GetConfigValue(blockName, keyName, "");
498 if(!val[0]) return 0;
500 #ifdef HAVE_STRTOF
501 *ret = strtof(val, NULL);
502 #else
503 *ret = (float)strtod(val, NULL);
504 #endif
505 return 1;
508 int GetConfigValueBool(const char *blockName, const char *keyName, int def)
510 const char *val = GetConfigValue(blockName, keyName, "");
512 if(!val[0]) return !!def;
513 return (strcasecmp(val, "true") == 0 || strcasecmp(val, "yes") == 0 ||
514 strcasecmp(val, "on") == 0 || atoi(val) != 0);