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
23 #define _WIN32_IE 0x501
25 #define _WIN32_IE 0x400
45 #define PATH_MAX MAX_PATH
51 typedef struct ConfigEntry
{
56 typedef struct ConfigBlock
{
58 unsigned int entryCount
;
60 static ConfigBlock cfgBlock
;
63 static char *lstrip(char *line
)
65 while(isspace(line
[0]))
70 static char *rstrip(char *line
)
72 size_t len
= strlen(line
);
73 while(len
> 0 && isspace(line
[len
-1]))
79 static int readline(FILE *f
, char **output
, size_t *maxlen
)
84 while((c
=fgetc(f
)) != EOF
&& (c
== '\r' || c
== '\n'))
95 newmax
= (*maxlen
? (*maxlen
)<<1 : 32);
97 temp
= realloc(*output
, newmax
);
100 ERR("Failed to realloc %lu bytes from %lu!\n", newmax
, *maxlen
);
107 (*output
)[len
++] = c
;
108 (*output
)[len
] = '\0';
109 } while((c
=fgetc(f
)) != EOF
&& c
!= '\r' && c
!= '\n');
115 static char *expdup(const char *str
)
129 const char *next
= strchr(str
, '$');
131 addstrlen
= next
? (size_t)(next
-str
) : strlen(str
);
140 const char *next
= strchr(str
+1, '$');
142 addstrlen
= next
? (size_t)(next
-str
) : strlen(str
);
151 while((isalnum(*str
) || *str
== '_') && k
< sizeof(envname
)-1)
152 envname
[k
++] = *(str
++);
155 if((addstr
=getenv(envname
)) == NULL
)
157 addstrlen
= strlen(addstr
);
163 if(addstrlen
>= maxlen
-len
)
168 newmax
= NextPowerOf2(len
+addstrlen
+1);
170 temp
= realloc(output
, newmax
);
173 ERR("Failed to realloc %lu bytes from %lu!\n", newmax
, maxlen
);
181 for(i
= 0;i
< addstrlen
;i
++)
182 output
[len
++] = addstr
[i
];
186 return output
? output
: calloc(1, 1);
190 static void LoadConfigFromFile(FILE *f
)
192 char curSection
[128] = "";
197 while(readline(f
, &buffer
, &maxlen
))
199 char *line
, *comment
;
201 char value
[256] = "";
203 comment
= strchr(buffer
, '#');
207 comment
= rstrip(lstrip(comment
));
210 line
= rstrip(lstrip(buffer
));
216 char *section
= line
+1;
219 endsection
= strchr(section
, ']');
220 if(!endsection
|| section
== endsection
|| endsection
[1] != 0)
222 ERR("config parse error: bad line \"%s\"\n", line
);
227 if(strcasecmp(section
, "general") == 0)
231 strncpy(curSection
, section
, sizeof(curSection
)-1);
232 curSection
[sizeof(curSection
)-1] = 0;
238 if(sscanf(line
, "%255[^=] = \"%255[^\"]\"", key
, value
) == 2 ||
239 sscanf(line
, "%255[^=] = '%255[^\']'", key
, value
) == 2 ||
240 sscanf(line
, "%255[^=] = %255[^\n]", key
, value
) == 2)
242 /* sscanf doesn't handle '' or "" as empty values, so clip it
244 if(strcmp(value
, "\"\"") == 0 || strcmp(value
, "''") == 0)
247 else if(sscanf(line
, "%255[^=] %255[=]", key
, value
) == 2)
249 /* Special case for 'key =' */
254 ERR("config parse error: malformed option line: \"%s\"\n\n", line
);
259 if(curSection
[0] != 0)
261 size_t len
= strlen(curSection
);
262 memmove(&key
[len
+1], key
, sizeof(key
)-1-len
);
264 memcpy(key
, curSection
, len
);
267 /* Check if we already have this option set */
268 ent
= cfgBlock
.entries
;
269 while((unsigned int)(ent
-cfgBlock
.entries
) < cfgBlock
.entryCount
)
271 if(strcasecmp(ent
->key
, key
) == 0)
276 if((unsigned int)(ent
-cfgBlock
.entries
) >= cfgBlock
.entryCount
)
278 /* Allocate a new option entry */
279 ent
= realloc(cfgBlock
.entries
, (cfgBlock
.entryCount
+1)*sizeof(ConfigEntry
));
282 ERR("config parse error: error reallocating config entries\n");
285 cfgBlock
.entries
= ent
;
286 ent
= cfgBlock
.entries
+ cfgBlock
.entryCount
;
287 cfgBlock
.entryCount
++;
289 ent
->key
= strdup(key
);
294 ent
->value
= expdup(value
);
296 TRACE("found '%s' = '%s'\n", ent
->key
, ent
->value
);
302 void ReadALConfig(void)
304 char buffer
[PATH_MAX
];
309 if(SHGetSpecialFolderPathA(NULL
, buffer
, CSIDL_APPDATA
, FALSE
) != FALSE
)
311 size_t p
= strlen(buffer
);
312 snprintf(buffer
+p
, sizeof(buffer
)-p
, "\\alsoft.ini");
314 TRACE("Loading config %s...\n", buffer
);
315 f
= fopen(buffer
, "rt");
318 LoadConfigFromFile(f
);
323 str
= "/etc/openal/alsoft.conf";
325 TRACE("Loading config %s...\n", str
);
329 LoadConfigFromFile(f
);
333 if(!(str
=getenv("XDG_CONFIG_DIRS")) || str
[0] == 0)
335 strncpy(buffer
, str
, sizeof(buffer
)-1);
336 buffer
[sizeof(buffer
)-1] = 0;
337 /* Go through the list in reverse, since "the order of base directories
338 * denotes their importance; the first directory listed is the most
339 * important". Ergo, we need to load the settings from the later dirs
340 * first so that the settings in the earlier dirs override them.
344 char *next
= strrchr(buffer
, ':');
345 if(next
) *(next
++) = 0;
349 WARN("Ignoring XDG config dir: %s\n", next
);
352 size_t len
= strlen(next
);
353 strncpy(next
+len
, "/alsoft.conf", buffer
+sizeof(buffer
)-next
-len
);
354 buffer
[sizeof(buffer
)-1] = 0;
356 TRACE("Loading config %s...\n", next
);
357 f
= fopen(next
, "r");
360 LoadConfigFromFile(f
);
368 if((str
=getenv("HOME")) != NULL
&& *str
)
370 snprintf(buffer
, sizeof(buffer
), "%s/.alsoftrc", str
);
372 TRACE("Loading config %s...\n", buffer
);
373 f
= fopen(buffer
, "r");
376 LoadConfigFromFile(f
);
381 if((str
=getenv("XDG_CONFIG_HOME")) != NULL
&& str
[0] != 0)
382 snprintf(buffer
, sizeof(buffer
), "%s/%s", str
, "alsoft.conf");
386 if((str
=getenv("HOME")) != NULL
&& str
[0] != 0)
387 snprintf(buffer
, sizeof(buffer
), "%s/.config/%s", str
, "alsoft.conf");
391 TRACE("Loading config %s...\n", buffer
);
392 f
= fopen(buffer
, "r");
395 LoadConfigFromFile(f
);
401 if((str
=getenv("ALSOFT_CONF")) != NULL
&& *str
)
403 TRACE("Loading config %s...\n", str
);
407 LoadConfigFromFile(f
);
413 void FreeALConfig(void)
417 for(i
= 0;i
< cfgBlock
.entryCount
;i
++)
419 free(cfgBlock
.entries
[i
].key
);
420 free(cfgBlock
.entries
[i
].value
);
422 free(cfgBlock
.entries
);
425 const char *GetConfigValue(const char *blockName
, const char *keyName
, const char *def
)
433 if(blockName
&& strcasecmp(blockName
, "general") != 0)
434 snprintf(key
, sizeof(key
), "%s/%s", blockName
, keyName
);
437 strncpy(key
, keyName
, sizeof(key
)-1);
438 key
[sizeof(key
)-1] = 0;
441 for(i
= 0;i
< cfgBlock
.entryCount
;i
++)
443 if(strcasecmp(cfgBlock
.entries
[i
].key
, key
) == 0)
445 TRACE("Found %s = \"%s\"\n", key
, cfgBlock
.entries
[i
].value
);
446 if(cfgBlock
.entries
[i
].value
[0])
447 return cfgBlock
.entries
[i
].value
;
452 TRACE("Key %s not found\n", key
);
456 int ConfigValueExists(const char *blockName
, const char *keyName
)
458 const char *val
= GetConfigValue(blockName
, keyName
, "");
462 int ConfigValueStr(const char *blockName
, const char *keyName
, const char **ret
)
464 const char *val
= GetConfigValue(blockName
, keyName
, "");
465 if(!val
[0]) return 0;
471 int ConfigValueInt(const char *blockName
, const char *keyName
, int *ret
)
473 const char *val
= GetConfigValue(blockName
, keyName
, "");
474 if(!val
[0]) return 0;
476 *ret
= strtol(val
, NULL
, 0);
480 int ConfigValueUInt(const char *blockName
, const char *keyName
, unsigned int *ret
)
482 const char *val
= GetConfigValue(blockName
, keyName
, "");
483 if(!val
[0]) return 0;
485 *ret
= strtoul(val
, NULL
, 0);
489 int ConfigValueFloat(const char *blockName
, const char *keyName
, float *ret
)
491 const char *val
= GetConfigValue(blockName
, keyName
, "");
492 if(!val
[0]) return 0;
495 *ret
= strtof(val
, NULL
);
497 *ret
= (float)strtod(val
, NULL
);
502 int GetConfigValueBool(const char *blockName
, const char *keyName
, int def
)
504 const char *val
= GetConfigValue(blockName
, keyName
, "");
506 if(!val
[0]) return !!def
;
507 return (strcasecmp(val
, "true") == 0 || strcasecmp(val
, "yes") == 0 ||
508 strcasecmp(val
, "on") == 0 || atoi(val
) != 0);