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
23 #define _WIN32_IE 0x501
25 #define _WIN32_IE 0x400
44 typedef struct ConfigEntry
{
49 typedef struct ConfigBlock
{
51 unsigned int entryCount
;
53 static ConfigBlock cfgBlock
;
56 static char *lstrip(char *line
)
58 while(isspace(line
[0]))
63 static char *rstrip(char *line
)
65 size_t len
= strlen(line
);
66 while(len
> 0 && isspace(line
[len
-1]))
72 static int readline(FILE *f
, char **output
, size_t *maxlen
)
77 while((c
=fgetc(f
)) != EOF
&& (c
== '\r' || c
== '\n'))
88 newmax
= (*maxlen
? (*maxlen
)<<1 : 32);
90 temp
= realloc(*output
, newmax
);
93 ERR("Failed to realloc "SZFMT
" bytes from "SZFMT
"!\n", newmax
, *maxlen
);
100 (*output
)[len
++] = c
;
101 (*output
)[len
] = '\0';
102 } while((c
=fgetc(f
)) != EOF
&& c
!= '\r' && c
!= '\n');
108 static char *expdup(const char *str
)
122 const char *next
= strchr(str
, '$');
124 addstrlen
= next
? (size_t)(next
-str
) : strlen(str
);
133 const char *next
= strchr(str
+1, '$');
135 addstrlen
= next
? (size_t)(next
-str
) : strlen(str
);
145 hasbraces
= (*str
== '{');
148 while((isalnum(*str
) || *str
== '_') && k
< sizeof(envname
)-1)
149 envname
[k
++] = *(str
++);
152 if(hasbraces
&& *str
!= '}')
156 if((addstr
=getenv(envname
)) == NULL
)
158 addstrlen
= strlen(addstr
);
164 if(addstrlen
>= maxlen
-len
)
169 newmax
= len
+addstrlen
+1;
171 temp
= realloc(output
, newmax
);
174 ERR("Failed to realloc "SZFMT
" bytes from "SZFMT
"!\n", newmax
, maxlen
);
182 for(i
= 0;i
< addstrlen
;i
++)
183 output
[len
++] = addstr
[i
];
187 return output
? output
: calloc(1, 1);
191 static void LoadConfigFromFile(FILE *f
)
193 char curSection
[128] = "";
198 while(readline(f
, &buffer
, &maxlen
))
200 char *line
, *comment
;
202 char value
[256] = "";
204 comment
= strchr(buffer
, '#');
205 if(comment
) *(comment
++) = 0;
207 line
= rstrip(lstrip(buffer
));
213 char *section
= line
+1;
216 endsection
= strchr(section
, ']');
217 if(!endsection
|| section
== endsection
|| endsection
[1] != 0)
219 ERR("config parse error: bad line \"%s\"\n", line
);
224 if(strcasecmp(section
, "general") == 0)
228 strncpy(curSection
, section
, sizeof(curSection
)-1);
229 curSection
[sizeof(curSection
)-1] = 0;
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
241 if(strcmp(value
, "\"\"") == 0 || strcmp(value
, "''") == 0)
244 else if(sscanf(line
, "%255[^=] %255[=]", key
, value
) == 2)
246 /* Special case for 'key =' */
251 ERR("config parse error: malformed option line: \"%s\"\n\n", line
);
256 if(curSection
[0] != 0)
258 size_t len
= strlen(curSection
);
259 memmove(&key
[len
+1], key
, sizeof(key
)-1-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)
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
));
279 ERR("config parse error: error reallocating config entries\n");
282 cfgBlock
.entries
= ent
;
283 ent
= cfgBlock
.entries
+ cfgBlock
.entryCount
;
284 cfgBlock
.entryCount
++;
286 ent
->key
= strdup(key
);
291 ent
->value
= expdup(value
);
293 TRACE("found '%s' = '%s'\n", ent
->key
, ent
->value
);
300 void ReadALConfig(void)
302 WCHAR buffer
[PATH_MAX
];
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");
315 LoadConfigFromFile(f
);
320 if((str
=_wgetenv(L
"ALSOFT_CONF")) != NULL
&& *str
)
322 TRACE("Loading config %ls...\n", str
);
323 f
= _wfopen(str
, L
"rt");
326 LoadConfigFromFile(f
);
332 void ReadALConfig(void)
334 char buffer
[PATH_MAX
];
338 str
= "/etc/openal/alsoft.conf";
340 TRACE("Loading config %s...\n", str
);
341 f
= al_fopen(str
, "r");
344 LoadConfigFromFile(f
);
348 if(!(str
=getenv("XDG_CONFIG_DIRS")) || str
[0] == 0)
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.
359 char *next
= strrchr(buffer
, ':');
360 if(next
) *(next
++) = 0;
364 WARN("Ignoring XDG config dir: %s\n", next
);
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");
375 LoadConfigFromFile(f
);
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");
391 LoadConfigFromFile(f
);
396 if((str
=getenv("XDG_CONFIG_HOME")) != NULL
&& str
[0] != 0)
397 snprintf(buffer
, sizeof(buffer
), "%s/%s", str
, "alsoft.conf");
401 if((str
=getenv("HOME")) != NULL
&& str
[0] != 0)
402 snprintf(buffer
, sizeof(buffer
), "%s/.config/%s", str
, "alsoft.conf");
406 TRACE("Loading config %s...\n", buffer
);
407 f
= al_fopen(buffer
, "r");
410 LoadConfigFromFile(f
);
415 if((str
=getenv("ALSOFT_CONF")) != NULL
&& *str
)
417 TRACE("Loading config %s...\n", str
);
418 f
= al_fopen(str
, "r");
421 LoadConfigFromFile(f
);
428 void FreeALConfig(void)
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
)
448 if(blockName
&& strcasecmp(blockName
, "general") != 0)
449 snprintf(key
, sizeof(key
), "%s/%s", blockName
, keyName
);
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
;
467 TRACE("Key %s not found\n", key
);
471 int ConfigValueExists(const char *blockName
, const char *keyName
)
473 const char *val
= GetConfigValue(blockName
, keyName
, "");
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;
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);
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);
504 int ConfigValueFloat(const char *blockName
, const char *keyName
, float *ret
)
506 const char *val
= GetConfigValue(blockName
, keyName
, "");
507 if(!val
[0]) return 0;
510 *ret
= strtof(val
, NULL
);
512 *ret
= (float)strtod(val
, NULL
);
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);
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);