Move atomic method definitions to a separate common source
[openal-soft.git] / Alc / alcConfig.c
bloba058029e2ad0f8c24474e30250ab63e1c549037b
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)
198 *(comment++) = 0;
199 comment = rstrip(lstrip(comment));
202 line = rstrip(lstrip(buffer));
203 if(!line[0])
204 continue;
206 if(line[0] == '[')
208 char *section = line+1;
209 char *endsection;
211 endsection = strchr(section, ']');
212 if(!endsection || section == endsection || endsection[1] != 0)
214 ERR("config parse error: bad line \"%s\"\n", line);
215 continue;
217 *endsection = 0;
219 if(strcasecmp(section, "general") == 0)
220 curSection[0] = 0;
221 else
223 strncpy(curSection, section, sizeof(curSection)-1);
224 curSection[sizeof(curSection)-1] = 0;
227 continue;
230 if(sscanf(line, "%255[^=] = \"%255[^\"]\"", key, value) == 2 ||
231 sscanf(line, "%255[^=] = '%255[^\']'", key, value) == 2 ||
232 sscanf(line, "%255[^=] = %255[^\n]", key, value) == 2)
234 /* sscanf doesn't handle '' or "" as empty values, so clip it
235 * manually. */
236 if(strcmp(value, "\"\"") == 0 || strcmp(value, "''") == 0)
237 value[0] = 0;
239 else if(sscanf(line, "%255[^=] %255[=]", key, value) == 2)
241 /* Special case for 'key =' */
242 value[0] = 0;
244 else
246 ERR("config parse error: malformed option line: \"%s\"\n\n", line);
247 continue;
249 rstrip(key);
251 if(curSection[0] != 0)
253 size_t len = strlen(curSection);
254 memmove(&key[len+1], key, sizeof(key)-1-len);
255 key[len] = '/';
256 memcpy(key, curSection, len);
259 /* Check if we already have this option set */
260 ent = cfgBlock.entries;
261 while((unsigned int)(ent-cfgBlock.entries) < cfgBlock.entryCount)
263 if(strcasecmp(ent->key, key) == 0)
264 break;
265 ent++;
268 if((unsigned int)(ent-cfgBlock.entries) >= cfgBlock.entryCount)
270 /* Allocate a new option entry */
271 ent = realloc(cfgBlock.entries, (cfgBlock.entryCount+1)*sizeof(ConfigEntry));
272 if(!ent)
274 ERR("config parse error: error reallocating config entries\n");
275 continue;
277 cfgBlock.entries = ent;
278 ent = cfgBlock.entries + cfgBlock.entryCount;
279 cfgBlock.entryCount++;
281 ent->key = strdup(key);
282 ent->value = NULL;
285 free(ent->value);
286 ent->value = expdup(value);
288 TRACE("found '%s' = '%s'\n", ent->key, ent->value);
291 free(buffer);
294 #ifdef _WIN32
295 void ReadALConfig(void)
297 WCHAR buffer[PATH_MAX];
298 const WCHAR *str;
299 FILE *f;
301 if(SHGetSpecialFolderPathW(NULL, buffer, CSIDL_APPDATA, FALSE) != FALSE)
303 size_t p = lstrlenW(buffer);
304 _snwprintf(buffer+p, PATH_MAX-p, L"\\alsoft.ini");
306 TRACE("Loading config %ls...\n", buffer);
307 f = _wfopen(buffer, L"rt");
308 if(f)
310 LoadConfigFromFile(f);
311 fclose(f);
315 if((str=_wgetenv(L"ALSOFT_CONF")) != NULL && *str)
317 TRACE("Loading config %ls...\n", str);
318 f = _wfopen(str, L"rt");
319 if(f)
321 LoadConfigFromFile(f);
322 fclose(f);
326 #else
327 void ReadALConfig(void)
329 char buffer[PATH_MAX];
330 const char *str;
331 FILE *f;
333 str = "/etc/openal/alsoft.conf";
335 TRACE("Loading config %s...\n", str);
336 f = al_fopen(str, "r");
337 if(f)
339 LoadConfigFromFile(f);
340 fclose(f);
343 if(!(str=getenv("XDG_CONFIG_DIRS")) || str[0] == 0)
344 str = "/etc/xdg";
345 strncpy(buffer, str, sizeof(buffer)-1);
346 buffer[sizeof(buffer)-1] = 0;
347 /* Go through the list in reverse, since "the order of base directories
348 * denotes their importance; the first directory listed is the most
349 * important". Ergo, we need to load the settings from the later dirs
350 * first so that the settings in the earlier dirs override them.
352 while(1)
354 char *next = strrchr(buffer, ':');
355 if(next) *(next++) = 0;
356 else next = buffer;
358 if(next[0] != '/')
359 WARN("Ignoring XDG config dir: %s\n", next);
360 else
362 size_t len = strlen(next);
363 strncpy(next+len, "/alsoft.conf", buffer+sizeof(buffer)-next-len);
364 buffer[sizeof(buffer)-1] = 0;
366 TRACE("Loading config %s...\n", next);
367 f = al_fopen(next, "r");
368 if(f)
370 LoadConfigFromFile(f);
371 fclose(f);
374 if(next == buffer)
375 break;
378 if((str=getenv("HOME")) != NULL && *str)
380 snprintf(buffer, sizeof(buffer), "%s/.alsoftrc", str);
382 TRACE("Loading config %s...\n", buffer);
383 f = al_fopen(buffer, "r");
384 if(f)
386 LoadConfigFromFile(f);
387 fclose(f);
391 if((str=getenv("XDG_CONFIG_HOME")) != NULL && str[0] != 0)
392 snprintf(buffer, sizeof(buffer), "%s/%s", str, "alsoft.conf");
393 else
395 buffer[0] = 0;
396 if((str=getenv("HOME")) != NULL && str[0] != 0)
397 snprintf(buffer, sizeof(buffer), "%s/.config/%s", str, "alsoft.conf");
399 if(buffer[0] != 0)
401 TRACE("Loading config %s...\n", buffer);
402 f = al_fopen(buffer, "r");
403 if(f)
405 LoadConfigFromFile(f);
406 fclose(f);
410 if((str=getenv("ALSOFT_CONF")) != NULL && *str)
412 TRACE("Loading config %s...\n", str);
413 f = al_fopen(str, "r");
414 if(f)
416 LoadConfigFromFile(f);
417 fclose(f);
421 #endif
423 void FreeALConfig(void)
425 unsigned int i;
427 for(i = 0;i < cfgBlock.entryCount;i++)
429 free(cfgBlock.entries[i].key);
430 free(cfgBlock.entries[i].value);
432 free(cfgBlock.entries);
435 const char *GetConfigValue(const char *blockName, const char *keyName, const char *def)
437 unsigned int i;
438 char key[256];
440 if(!keyName)
441 return def;
443 if(blockName && strcasecmp(blockName, "general") != 0)
444 snprintf(key, sizeof(key), "%s/%s", blockName, keyName);
445 else
447 strncpy(key, keyName, sizeof(key)-1);
448 key[sizeof(key)-1] = 0;
451 for(i = 0;i < cfgBlock.entryCount;i++)
453 if(strcasecmp(cfgBlock.entries[i].key, key) == 0)
455 TRACE("Found %s = \"%s\"\n", key, cfgBlock.entries[i].value);
456 if(cfgBlock.entries[i].value[0])
457 return cfgBlock.entries[i].value;
458 return def;
462 TRACE("Key %s not found\n", key);
463 return def;
466 int ConfigValueExists(const char *blockName, const char *keyName)
468 const char *val = GetConfigValue(blockName, keyName, "");
469 return !!val[0];
472 int ConfigValueStr(const char *blockName, const char *keyName, const char **ret)
474 const char *val = GetConfigValue(blockName, keyName, "");
475 if(!val[0]) return 0;
477 *ret = val;
478 return 1;
481 int ConfigValueInt(const char *blockName, const char *keyName, int *ret)
483 const char *val = GetConfigValue(blockName, keyName, "");
484 if(!val[0]) return 0;
486 *ret = strtol(val, NULL, 0);
487 return 1;
490 int ConfigValueUInt(const char *blockName, const char *keyName, unsigned int *ret)
492 const char *val = GetConfigValue(blockName, keyName, "");
493 if(!val[0]) return 0;
495 *ret = strtoul(val, NULL, 0);
496 return 1;
499 int ConfigValueFloat(const char *blockName, const char *keyName, float *ret)
501 const char *val = GetConfigValue(blockName, keyName, "");
502 if(!val[0]) return 0;
504 #ifdef HAVE_STRTOF
505 *ret = strtof(val, NULL);
506 #else
507 *ret = (float)strtod(val, NULL);
508 #endif
509 return 1;
512 int GetConfigValueBool(const char *blockName, const char *keyName, int def)
514 const char *val = GetConfigValue(blockName, keyName, "");
516 if(!val[0]) return !!def;
517 return (strcasecmp(val, "true") == 0 || strcasecmp(val, "yes") == 0 ||
518 strcasecmp(val, "on") == 0 || atoi(val) != 0);