Rework HRTF decision logic
[openal-soft.git] / Alc / alcConfig.c
blobc8928c3cf4acfb5349a1b9454151d3cafa624ec8
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 <shlobj.h>
37 #endif
39 #include "alMain.h"
40 #include "compat.h"
41 #include "bool.h"
44 typedef struct ConfigEntry {
45 char *key;
46 char *value;
47 } ConfigEntry;
49 typedef struct ConfigBlock {
50 ConfigEntry *entries;
51 unsigned int entryCount;
52 } ConfigBlock;
53 static ConfigBlock cfgBlock;
56 static char *lstrip(char *line)
58 while(isspace(line[0]))
59 line++;
60 return line;
63 static char *rstrip(char *line)
65 size_t len = strlen(line);
66 while(len > 0 && isspace(line[len-1]))
67 len--;
68 line[len] = 0;
69 return line;
72 static int readline(FILE *f, char **output, size_t *maxlen)
74 size_t len = 0;
75 int c;
77 while((c=fgetc(f)) != EOF && (c == '\r' || c == '\n'))
79 if(c == EOF)
80 return 0;
82 do {
83 if(len+1 >= *maxlen)
85 void *temp = NULL;
86 size_t newmax;
88 newmax = (*maxlen ? (*maxlen)<<1 : 32);
89 if(newmax > *maxlen)
90 temp = realloc(*output, newmax);
91 if(!temp)
93 ERR("Failed to realloc "SZFMT" bytes from "SZFMT"!\n", newmax, *maxlen);
94 return 0;
97 *output = temp;
98 *maxlen = newmax;
100 (*output)[len++] = c;
101 (*output)[len] = '\0';
102 } while((c=fgetc(f)) != EOF && c != '\r' && c != '\n');
104 return 1;
108 static char *expdup(const char *str)
110 char *output = NULL;
111 size_t maxlen = 0;
112 size_t len = 0;
114 while(*str != '\0')
116 const char *addstr;
117 size_t addstrlen;
118 size_t i;
120 if(str[0] != '$')
122 const char *next = strchr(str, '$');
123 addstr = str;
124 addstrlen = next ? (size_t)(next-str) : strlen(str);
126 str += addstrlen;
128 else
130 str++;
131 if(*str == '$')
133 const char *next = strchr(str+1, '$');
134 addstr = str;
135 addstrlen = next ? (size_t)(next-str) : strlen(str);
137 str += addstrlen;
139 else
141 bool hasbraces;
142 char envname[1024];
143 size_t k = 0;
145 hasbraces = (*str == '{');
146 if(hasbraces) str++;
148 while((isalnum(*str) || *str == '_') && k < sizeof(envname)-1)
149 envname[k++] = *(str++);
150 envname[k++] = '\0';
152 if(hasbraces && *str != '}')
153 continue;
155 if(hasbraces) str++;
156 if((addstr=getenv(envname)) == NULL)
157 continue;
158 addstrlen = strlen(addstr);
161 if(addstrlen == 0)
162 continue;
164 if(addstrlen >= maxlen-len)
166 void *temp = NULL;
167 size_t newmax;
169 newmax = len+addstrlen+1;
170 if(newmax > maxlen)
171 temp = realloc(output, newmax);
172 if(!temp)
174 ERR("Failed to realloc "SZFMT" bytes from "SZFMT"!\n", newmax, maxlen);
175 return output;
178 output = temp;
179 maxlen = newmax;
182 for(i = 0;i < addstrlen;i++)
183 output[len++] = addstr[i];
184 output[len] = '\0';
187 return output ? output : calloc(1, 1);
191 static void LoadConfigFromFile(FILE *f)
193 char curSection[128] = "";
194 char *buffer = NULL;
195 size_t maxlen = 0;
196 ConfigEntry *ent;
198 while(readline(f, &buffer, &maxlen))
200 char *line, *comment;
201 char key[256] = "";
202 char value[256] = "";
204 comment = strchr(buffer, '#');
205 if(comment) *(comment++) = 0;
207 line = rstrip(lstrip(buffer));
208 if(!line[0])
209 continue;
211 if(line[0] == '[')
213 char *section = line+1;
214 char *endsection;
216 endsection = strchr(section, ']');
217 if(!endsection || section == endsection || endsection[1] != 0)
219 ERR("config parse error: bad line \"%s\"\n", line);
220 continue;
222 *endsection = 0;
224 if(strcasecmp(section, "general") == 0)
225 curSection[0] = 0;
226 else
228 strncpy(curSection, section, sizeof(curSection)-1);
229 curSection[sizeof(curSection)-1] = 0;
232 continue;
235 if(sscanf(line, "%255[^=] = \"%255[^\"]\"", key, value) == 2 ||
236 sscanf(line, "%255[^=] = '%255[^\']'", key, value) == 2 ||
237 sscanf(line, "%255[^=] = %255[^\n]", key, value) == 2)
239 /* sscanf doesn't handle '' or "" as empty values, so clip it
240 * manually. */
241 if(strcmp(value, "\"\"") == 0 || strcmp(value, "''") == 0)
242 value[0] = 0;
244 else if(sscanf(line, "%255[^=] %255[=]", key, value) == 2)
246 /* Special case for 'key =' */
247 value[0] = 0;
249 else
251 ERR("config parse error: malformed option line: \"%s\"\n\n", line);
252 continue;
254 rstrip(key);
256 if(curSection[0] != 0)
258 size_t len = strlen(curSection);
259 memmove(&key[len+1], key, sizeof(key)-1-len);
260 key[len] = '/';
261 memcpy(key, curSection, len);
264 /* Check if we already have this option set */
265 ent = cfgBlock.entries;
266 while((unsigned int)(ent-cfgBlock.entries) < cfgBlock.entryCount)
268 if(strcasecmp(ent->key, key) == 0)
269 break;
270 ent++;
273 if((unsigned int)(ent-cfgBlock.entries) >= cfgBlock.entryCount)
275 /* Allocate a new option entry */
276 ent = realloc(cfgBlock.entries, (cfgBlock.entryCount+1)*sizeof(ConfigEntry));
277 if(!ent)
279 ERR("config parse error: error reallocating config entries\n");
280 continue;
282 cfgBlock.entries = ent;
283 ent = cfgBlock.entries + cfgBlock.entryCount;
284 cfgBlock.entryCount++;
286 ent->key = strdup(key);
287 ent->value = NULL;
290 free(ent->value);
291 ent->value = expdup(value);
293 TRACE("found '%s' = '%s'\n", ent->key, ent->value);
296 free(buffer);
299 #ifdef _WIN32
300 void ReadALConfig(void)
302 WCHAR buffer[PATH_MAX];
303 const WCHAR *str;
304 FILE *f;
306 if(SHGetSpecialFolderPathW(NULL, buffer, CSIDL_APPDATA, FALSE) != FALSE)
308 size_t p = lstrlenW(buffer);
309 _snwprintf(buffer+p, PATH_MAX-p, L"\\alsoft.ini");
311 TRACE("Loading config %ls...\n", buffer);
312 f = _wfopen(buffer, L"rt");
313 if(f)
315 LoadConfigFromFile(f);
316 fclose(f);
320 if((str=_wgetenv(L"ALSOFT_CONF")) != NULL && *str)
322 TRACE("Loading config %ls...\n", str);
323 f = _wfopen(str, L"rt");
324 if(f)
326 LoadConfigFromFile(f);
327 fclose(f);
331 #else
332 void ReadALConfig(void)
334 char buffer[PATH_MAX];
335 const char *str;
336 FILE *f;
338 str = "/etc/openal/alsoft.conf";
340 TRACE("Loading config %s...\n", str);
341 f = al_fopen(str, "r");
342 if(f)
344 LoadConfigFromFile(f);
345 fclose(f);
348 if(!(str=getenv("XDG_CONFIG_DIRS")) || str[0] == 0)
349 str = "/etc/xdg";
350 strncpy(buffer, str, sizeof(buffer)-1);
351 buffer[sizeof(buffer)-1] = 0;
352 /* Go through the list in reverse, since "the order of base directories
353 * denotes their importance; the first directory listed is the most
354 * important". Ergo, we need to load the settings from the later dirs
355 * first so that the settings in the earlier dirs override them.
357 while(1)
359 char *next = strrchr(buffer, ':');
360 if(next) *(next++) = 0;
361 else next = buffer;
363 if(next[0] != '/')
364 WARN("Ignoring XDG config dir: %s\n", next);
365 else
367 size_t len = strlen(next);
368 strncpy(next+len, "/alsoft.conf", buffer+sizeof(buffer)-next-len);
369 buffer[sizeof(buffer)-1] = 0;
371 TRACE("Loading config %s...\n", next);
372 f = al_fopen(next, "r");
373 if(f)
375 LoadConfigFromFile(f);
376 fclose(f);
379 if(next == buffer)
380 break;
383 if((str=getenv("HOME")) != NULL && *str)
385 snprintf(buffer, sizeof(buffer), "%s/.alsoftrc", str);
387 TRACE("Loading config %s...\n", buffer);
388 f = al_fopen(buffer, "r");
389 if(f)
391 LoadConfigFromFile(f);
392 fclose(f);
396 if((str=getenv("XDG_CONFIG_HOME")) != NULL && str[0] != 0)
397 snprintf(buffer, sizeof(buffer), "%s/%s", str, "alsoft.conf");
398 else
400 buffer[0] = 0;
401 if((str=getenv("HOME")) != NULL && str[0] != 0)
402 snprintf(buffer, sizeof(buffer), "%s/.config/%s", str, "alsoft.conf");
404 if(buffer[0] != 0)
406 TRACE("Loading config %s...\n", buffer);
407 f = al_fopen(buffer, "r");
408 if(f)
410 LoadConfigFromFile(f);
411 fclose(f);
415 if((str=getenv("ALSOFT_CONF")) != NULL && *str)
417 TRACE("Loading config %s...\n", str);
418 f = al_fopen(str, "r");
419 if(f)
421 LoadConfigFromFile(f);
422 fclose(f);
426 #endif
428 void FreeALConfig(void)
430 unsigned int i;
432 for(i = 0;i < cfgBlock.entryCount;i++)
434 free(cfgBlock.entries[i].key);
435 free(cfgBlock.entries[i].value);
437 free(cfgBlock.entries);
440 const char *GetConfigValue(const char *blockName, const char *keyName, const char *def)
442 unsigned int i;
443 char key[256];
445 if(!keyName)
446 return def;
448 if(blockName && strcasecmp(blockName, "general") != 0)
449 snprintf(key, sizeof(key), "%s/%s", blockName, keyName);
450 else
452 strncpy(key, keyName, sizeof(key)-1);
453 key[sizeof(key)-1] = 0;
456 for(i = 0;i < cfgBlock.entryCount;i++)
458 if(strcasecmp(cfgBlock.entries[i].key, key) == 0)
460 TRACE("Found %s = \"%s\"\n", key, cfgBlock.entries[i].value);
461 if(cfgBlock.entries[i].value[0])
462 return cfgBlock.entries[i].value;
463 return def;
467 TRACE("Key %s not found\n", key);
468 return def;
471 int ConfigValueExists(const char *blockName, const char *keyName)
473 const char *val = GetConfigValue(blockName, keyName, "");
474 return !!val[0];
477 int ConfigValueStr(const char *blockName, const char *keyName, const char **ret)
479 const char *val = GetConfigValue(blockName, keyName, "");
480 if(!val[0]) return 0;
482 *ret = val;
483 return 1;
486 int ConfigValueInt(const char *blockName, const char *keyName, int *ret)
488 const char *val = GetConfigValue(blockName, keyName, "");
489 if(!val[0]) return 0;
491 *ret = strtol(val, NULL, 0);
492 return 1;
495 int ConfigValueUInt(const char *blockName, const char *keyName, unsigned int *ret)
497 const char *val = GetConfigValue(blockName, keyName, "");
498 if(!val[0]) return 0;
500 *ret = strtoul(val, NULL, 0);
501 return 1;
504 int ConfigValueFloat(const char *blockName, const char *keyName, float *ret)
506 const char *val = GetConfigValue(blockName, keyName, "");
507 if(!val[0]) return 0;
509 #ifdef HAVE_STRTOF
510 *ret = strtof(val, NULL);
511 #else
512 *ret = (float)strtod(val, NULL);
513 #endif
514 return 1;
517 int ConfigValueBool(const char *blockName, const char *keyName, int *ret)
519 const char *val = GetConfigValue(blockName, keyName, "");
520 if(!val[0]) return 0;
522 *ret = (strcasecmp(val, "true") == 0 || strcasecmp(val, "yes") == 0 ||
523 strcasecmp(val, "on") == 0 || atoi(val) != 0);
524 return 1;
527 int GetConfigValueBool(const char *blockName, const char *keyName, int def)
529 const char *val = GetConfigValue(blockName, keyName, "");
531 if(!val[0]) return !!def;
532 return (strcasecmp(val, "true") == 0 || strcasecmp(val, "yes") == 0 ||
533 strcasecmp(val, "on") == 0 || atoi(val) != 0);