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
45 typedef struct ConfigEntry
{
50 typedef struct ConfigBlock
{
52 unsigned int entryCount
;
54 static ConfigBlock cfgBlock
;
57 static char *lstrip(char *line
)
59 while(isspace(line
[0]))
64 static char *rstrip(char *line
)
66 size_t len
= strlen(line
);
67 while(len
> 0 && isspace(line
[len
-1]))
73 static int readline(FILE *f
, char **output
, size_t *maxlen
)
78 while((c
=fgetc(f
)) != EOF
&& (c
== '\r' || c
== '\n'))
89 newmax
= (*maxlen
? (*maxlen
)<<1 : 32);
91 temp
= realloc(*output
, newmax
);
94 ERR("Failed to realloc "SZFMT
" bytes from "SZFMT
"!\n", newmax
, *maxlen
);
101 (*output
)[len
++] = c
;
102 (*output
)[len
] = '\0';
103 } while((c
=fgetc(f
)) != EOF
&& c
!= '\r' && c
!= '\n');
109 static char *expdup(const char *str
)
123 const char *next
= strchr(str
, '$');
125 addstrlen
= next
? (size_t)(next
-str
) : strlen(str
);
134 const char *next
= strchr(str
+1, '$');
136 addstrlen
= next
? (size_t)(next
-str
) : strlen(str
);
146 hasbraces
= (*str
== '{');
149 while((isalnum(*str
) || *str
== '_') && k
< sizeof(envname
)-1)
150 envname
[k
++] = *(str
++);
153 if(hasbraces
&& *str
!= '}')
157 if((addstr
=getenv(envname
)) == NULL
)
159 addstrlen
= strlen(addstr
);
165 if(addstrlen
>= maxlen
-len
)
170 newmax
= len
+addstrlen
+1;
172 temp
= realloc(output
, newmax
);
175 ERR("Failed to realloc "SZFMT
" bytes from "SZFMT
"!\n", newmax
, maxlen
);
183 for(i
= 0;i
< addstrlen
;i
++)
184 output
[len
++] = addstr
[i
];
188 return output
? output
: calloc(1, 1);
192 static void LoadConfigFromFile(FILE *f
)
194 char curSection
[128] = "";
199 while(readline(f
, &buffer
, &maxlen
))
201 char *line
, *comment
;
203 char value
[256] = "";
205 line
= rstrip(lstrip(buffer
));
206 if(!line
[0]) continue;
210 char *section
= line
+1;
213 endsection
= strchr(section
, ']');
214 if(!endsection
|| section
== endsection
)
216 ERR("config parse error: bad line \"%s\"\n", line
);
219 if(endsection
[1] != 0)
221 char *end
= endsection
+1;
224 if(*end
!= 0 && *end
!= '#')
226 ERR("config parse error: bad line \"%s\"\n", line
);
232 if(strcasecmp(section
, "general") == 0)
236 strncpy(curSection
, section
, sizeof(curSection
)-1);
237 curSection
[sizeof(curSection
)-1] = 0;
243 comment
= strchr(line
, '#');
244 if(comment
) *(comment
++) = 0;
245 if(!line
[0]) continue;
247 if(sscanf(line
, "%255[^=] = \"%255[^\"]\"", key
, value
) == 2 ||
248 sscanf(line
, "%255[^=] = '%255[^\']'", key
, value
) == 2 ||
249 sscanf(line
, "%255[^=] = %255[^\n]", key
, value
) == 2)
251 /* sscanf doesn't handle '' or "" as empty values, so clip it
253 if(strcmp(value
, "\"\"") == 0 || strcmp(value
, "''") == 0)
256 else if(sscanf(line
, "%255[^=] %255[=]", key
, value
) == 2)
258 /* Special case for 'key =' */
263 ERR("config parse error: malformed option line: \"%s\"\n\n", line
);
268 if(curSection
[0] != 0)
270 size_t len
= strlen(curSection
);
271 memmove(&key
[len
+1], key
, sizeof(key
)-1-len
);
273 memcpy(key
, curSection
, len
);
276 /* Check if we already have this option set */
277 ent
= cfgBlock
.entries
;
278 while((unsigned int)(ent
-cfgBlock
.entries
) < cfgBlock
.entryCount
)
280 if(strcasecmp(ent
->key
, key
) == 0)
285 if((unsigned int)(ent
-cfgBlock
.entries
) >= cfgBlock
.entryCount
)
287 /* Allocate a new option entry */
288 ent
= realloc(cfgBlock
.entries
, (cfgBlock
.entryCount
+1)*sizeof(ConfigEntry
));
291 ERR("config parse error: error reallocating config entries\n");
294 cfgBlock
.entries
= ent
;
295 ent
= cfgBlock
.entries
+ cfgBlock
.entryCount
;
296 cfgBlock
.entryCount
++;
298 ent
->key
= strdup(key
);
303 ent
->value
= expdup(value
);
305 TRACE("found '%s' = '%s'\n", ent
->key
, ent
->value
);
312 void ReadALConfig(void)
314 WCHAR buffer
[PATH_MAX
];
318 if(SHGetSpecialFolderPathW(NULL
, buffer
, CSIDL_APPDATA
, FALSE
) != FALSE
)
320 al_string filepath
= AL_STRING_INIT_STATIC();
321 al_string_copy_wcstr(&filepath
, buffer
);
322 al_string_append_cstr(&filepath
, "\\alsoft.ini");
324 TRACE("Loading config %s...\n", al_string_get_cstr(filepath
));
325 f
= al_fopen(al_string_get_cstr(filepath
), "rt");
328 LoadConfigFromFile(f
);
331 al_string_deinit(&filepath
);
334 if((str
=_wgetenv(L
"ALSOFT_CONF")) != NULL
&& *str
)
336 al_string filepath
= AL_STRING_INIT_STATIC();
337 al_string_copy_wcstr(&filepath
, str
);
339 TRACE("Loading config %s...\n", al_string_get_cstr(filepath
));
340 f
= al_fopen(al_string_get_cstr(filepath
), "rt");
343 LoadConfigFromFile(f
);
346 al_string_deinit(&filepath
);
350 void ReadALConfig(void)
352 char buffer
[PATH_MAX
];
356 str
= "/etc/openal/alsoft.conf";
358 TRACE("Loading config %s...\n", str
);
359 f
= al_fopen(str
, "r");
362 LoadConfigFromFile(f
);
366 if(!(str
=getenv("XDG_CONFIG_DIRS")) || str
[0] == 0)
368 strncpy(buffer
, str
, sizeof(buffer
)-1);
369 buffer
[sizeof(buffer
)-1] = 0;
370 /* Go through the list in reverse, since "the order of base directories
371 * denotes their importance; the first directory listed is the most
372 * important". Ergo, we need to load the settings from the later dirs
373 * first so that the settings in the earlier dirs override them.
377 char *next
= strrchr(buffer
, ':');
378 if(next
) *(next
++) = 0;
382 WARN("Ignoring XDG config dir: %s\n", next
);
385 size_t len
= strlen(next
);
386 strncpy(next
+len
, "/alsoft.conf", buffer
+sizeof(buffer
)-next
-len
);
387 buffer
[sizeof(buffer
)-1] = 0;
389 TRACE("Loading config %s...\n", next
);
390 f
= al_fopen(next
, "r");
393 LoadConfigFromFile(f
);
401 if((str
=getenv("HOME")) != NULL
&& *str
)
403 snprintf(buffer
, sizeof(buffer
), "%s/.alsoftrc", str
);
405 TRACE("Loading config %s...\n", buffer
);
406 f
= al_fopen(buffer
, "r");
409 LoadConfigFromFile(f
);
414 if((str
=getenv("XDG_CONFIG_HOME")) != NULL
&& str
[0] != 0)
415 snprintf(buffer
, sizeof(buffer
), "%s/%s", str
, "alsoft.conf");
419 if((str
=getenv("HOME")) != NULL
&& str
[0] != 0)
420 snprintf(buffer
, sizeof(buffer
), "%s/.config/%s", str
, "alsoft.conf");
424 TRACE("Loading config %s...\n", buffer
);
425 f
= al_fopen(buffer
, "r");
428 LoadConfigFromFile(f
);
433 if((str
=getenv("ALSOFT_CONF")) != NULL
&& *str
)
435 TRACE("Loading config %s...\n", str
);
436 f
= al_fopen(str
, "r");
439 LoadConfigFromFile(f
);
446 void FreeALConfig(void)
450 for(i
= 0;i
< cfgBlock
.entryCount
;i
++)
452 free(cfgBlock
.entries
[i
].key
);
453 free(cfgBlock
.entries
[i
].value
);
455 free(cfgBlock
.entries
);
458 const char *GetConfigValue(const char *devName
, const char *blockName
, const char *keyName
, const char *def
)
466 if(blockName
&& strcasecmp(blockName
, "general") != 0)
469 snprintf(key
, sizeof(key
), "%s/%s/%s", blockName
, devName
, keyName
);
471 snprintf(key
, sizeof(key
), "%s/%s", blockName
, keyName
);
476 snprintf(key
, sizeof(key
), "%s/%s", devName
, keyName
);
479 strncpy(key
, keyName
, sizeof(key
)-1);
480 key
[sizeof(key
)-1] = 0;
484 for(i
= 0;i
< cfgBlock
.entryCount
;i
++)
486 if(strcmp(cfgBlock
.entries
[i
].key
, key
) == 0)
488 TRACE("Found %s = \"%s\"\n", key
, cfgBlock
.entries
[i
].value
);
489 if(cfgBlock
.entries
[i
].value
[0])
490 return cfgBlock
.entries
[i
].value
;
497 TRACE("Key %s not found\n", key
);
500 return GetConfigValue(NULL
, blockName
, keyName
, def
);
503 int ConfigValueExists(const char *devName
, const char *blockName
, const char *keyName
)
505 const char *val
= GetConfigValue(devName
, blockName
, keyName
, "");
509 int ConfigValueStr(const char *devName
, const char *blockName
, const char *keyName
, const char **ret
)
511 const char *val
= GetConfigValue(devName
, blockName
, keyName
, "");
512 if(!val
[0]) return 0;
518 int ConfigValueInt(const char *devName
, const char *blockName
, const char *keyName
, int *ret
)
520 const char *val
= GetConfigValue(devName
, blockName
, keyName
, "");
521 if(!val
[0]) return 0;
523 *ret
= strtol(val
, NULL
, 0);
527 int ConfigValueUInt(const char *devName
, const char *blockName
, const char *keyName
, unsigned int *ret
)
529 const char *val
= GetConfigValue(devName
, blockName
, keyName
, "");
530 if(!val
[0]) return 0;
532 *ret
= strtoul(val
, NULL
, 0);
536 int ConfigValueFloat(const char *devName
, const char *blockName
, const char *keyName
, float *ret
)
538 const char *val
= GetConfigValue(devName
, blockName
, keyName
, "");
539 if(!val
[0]) return 0;
542 *ret
= strtof(val
, NULL
);
544 *ret
= (float)strtod(val
, NULL
);
549 int ConfigValueBool(const char *devName
, const char *blockName
, const char *keyName
, int *ret
)
551 const char *val
= GetConfigValue(devName
, blockName
, keyName
, "");
552 if(!val
[0]) return 0;
554 *ret
= (strcasecmp(val
, "true") == 0 || strcasecmp(val
, "yes") == 0 ||
555 strcasecmp(val
, "on") == 0 || atoi(val
) != 0);
559 int GetConfigValueBool(const char *devName
, const char *blockName
, const char *keyName
, int def
)
561 const char *val
= GetConfigValue(devName
, blockName
, keyName
, "");
563 if(!val
[0]) return !!def
;
564 return (strcasecmp(val
, "true") == 0 || strcasecmp(val
, "yes") == 0 ||
565 strcasecmp(val
, "on") == 0 || atoi(val
) != 0);