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
43 typedef struct ConfigEntry
{
48 typedef struct ConfigBlock
{
50 unsigned int entryCount
;
52 static ConfigBlock cfgBlock
;
55 static char *lstrip(char *line
)
57 while(isspace(line
[0]))
62 static char *rstrip(char *line
)
64 size_t len
= strlen(line
);
65 while(len
> 0 && isspace(line
[len
-1]))
71 static int readline(FILE *f
, char **output
, size_t *maxlen
)
76 while((c
=fgetc(f
)) != EOF
&& (c
== '\r' || c
== '\n'))
87 newmax
= (*maxlen
? (*maxlen
)<<1 : 32);
89 temp
= realloc(*output
, newmax
);
92 ERR("Failed to realloc %lu bytes from %lu!\n", newmax
, *maxlen
);
100 (*output
)[len
] = '\0';
101 } while((c
=fgetc(f
)) != EOF
&& c
!= '\r' && c
!= '\n');
107 static char *expdup(const char *str
)
121 const char *next
= strchr(str
, '$');
123 addstrlen
= next
? (size_t)(next
-str
) : strlen(str
);
132 const char *next
= strchr(str
+1, '$');
134 addstrlen
= next
? (size_t)(next
-str
) : strlen(str
);
143 while((isalnum(*str
) || *str
== '_') && k
< sizeof(envname
)-1)
144 envname
[k
++] = *(str
++);
147 if((addstr
=getenv(envname
)) == NULL
)
149 addstrlen
= strlen(addstr
);
155 if(addstrlen
>= maxlen
-len
)
160 newmax
= NextPowerOf2(len
+addstrlen
+1);
162 temp
= realloc(output
, newmax
);
165 ERR("Failed to realloc %lu bytes from %lu!\n", newmax
, maxlen
);
173 for(i
= 0;i
< addstrlen
;i
++)
174 output
[len
++] = addstr
[i
];
178 return output
? output
: calloc(1, 1);
182 static void LoadConfigFromFile(FILE *f
)
184 char curSection
[128] = "";
189 while(readline(f
, &buffer
, &maxlen
))
191 char *line
, *comment
;
193 char value
[256] = "";
195 comment
= strchr(buffer
, '#');
199 comment
= rstrip(lstrip(comment
));
202 line
= rstrip(lstrip(buffer
));
208 char *section
= line
+1;
211 endsection
= strchr(section
, ']');
212 if(!endsection
|| section
== endsection
|| endsection
[1] != 0)
214 ERR("config parse error: bad line \"%s\"\n", line
);
219 if(strcasecmp(section
, "general") == 0)
223 strncpy(curSection
, section
, sizeof(curSection
)-1);
224 curSection
[sizeof(curSection
)-1] = 0;
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
236 if(strcmp(value
, "\"\"") == 0 || strcmp(value
, "''") == 0)
239 else if(sscanf(line
, "%255[^=] %255[=]", key
, value
) == 2)
241 /* Special case for 'key =' */
246 ERR("config parse error: malformed option line: \"%s\"\n\n", line
);
251 if(curSection
[0] != 0)
253 size_t len
= strlen(curSection
);
254 memmove(&key
[len
+1], key
, sizeof(key
)-1-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)
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
));
274 ERR("config parse error: error reallocating config entries\n");
277 cfgBlock
.entries
= ent
;
278 ent
= cfgBlock
.entries
+ cfgBlock
.entryCount
;
279 cfgBlock
.entryCount
++;
281 ent
->key
= strdup(key
);
286 ent
->value
= expdup(value
);
288 TRACE("found '%s' = '%s'\n", ent
->key
, ent
->value
);
294 void ReadALConfig(void)
296 char buffer
[PATH_MAX
];
301 if(SHGetSpecialFolderPathA(NULL
, buffer
, CSIDL_APPDATA
, FALSE
) != FALSE
)
303 size_t p
= strlen(buffer
);
304 snprintf(buffer
+p
, sizeof(buffer
)-p
, "\\alsoft.ini");
306 TRACE("Loading config %s...\n", buffer
);
307 f
= fopen(buffer
, "rt");
310 LoadConfigFromFile(f
);
315 str
= "/etc/openal/alsoft.conf";
317 TRACE("Loading config %s...\n", str
);
321 LoadConfigFromFile(f
);
325 if(!(str
=getenv("XDG_CONFIG_DIRS")) || str
[0] == 0)
327 strncpy(buffer
, str
, sizeof(buffer
)-1);
328 buffer
[sizeof(buffer
)-1] = 0;
329 /* Go through the list in reverse, since "the order of base directories
330 * denotes their importance; the first directory listed is the most
331 * important". Ergo, we need to load the settings from the later dirs
332 * first so that the settings in the earlier dirs override them.
336 char *next
= strrchr(buffer
, ':');
337 if(next
) *(next
++) = 0;
341 WARN("Ignoring XDG config dir: %s\n", next
);
344 size_t len
= strlen(next
);
345 strncpy(next
+len
, "/alsoft.conf", buffer
+sizeof(buffer
)-next
-len
);
346 buffer
[sizeof(buffer
)-1] = 0;
348 TRACE("Loading config %s...\n", next
);
349 f
= fopen(next
, "r");
352 LoadConfigFromFile(f
);
360 if((str
=getenv("HOME")) != NULL
&& *str
)
362 snprintf(buffer
, sizeof(buffer
), "%s/.alsoftrc", str
);
364 TRACE("Loading config %s...\n", buffer
);
365 f
= fopen(buffer
, "r");
368 LoadConfigFromFile(f
);
373 if((str
=getenv("XDG_CONFIG_HOME")) != NULL
&& str
[0] != 0)
374 snprintf(buffer
, sizeof(buffer
), "%s/%s", str
, "alsoft.conf");
378 if((str
=getenv("HOME")) != NULL
&& str
[0] != 0)
379 snprintf(buffer
, sizeof(buffer
), "%s/.config/%s", str
, "alsoft.conf");
383 TRACE("Loading config %s...\n", buffer
);
384 f
= fopen(buffer
, "r");
387 LoadConfigFromFile(f
);
393 if((str
=getenv("ALSOFT_CONF")) != NULL
&& *str
)
395 TRACE("Loading config %s...\n", str
);
399 LoadConfigFromFile(f
);
405 void FreeALConfig(void)
409 for(i
= 0;i
< cfgBlock
.entryCount
;i
++)
411 free(cfgBlock
.entries
[i
].key
);
412 free(cfgBlock
.entries
[i
].value
);
414 free(cfgBlock
.entries
);
417 const char *GetConfigValue(const char *blockName
, const char *keyName
, const char *def
)
425 if(blockName
&& strcasecmp(blockName
, "general") != 0)
426 snprintf(key
, sizeof(key
), "%s/%s", blockName
, keyName
);
429 strncpy(key
, keyName
, sizeof(key
)-1);
430 key
[sizeof(key
)-1] = 0;
433 for(i
= 0;i
< cfgBlock
.entryCount
;i
++)
435 if(strcasecmp(cfgBlock
.entries
[i
].key
, key
) == 0)
437 TRACE("Found %s = \"%s\"\n", key
, cfgBlock
.entries
[i
].value
);
438 if(cfgBlock
.entries
[i
].value
[0])
439 return cfgBlock
.entries
[i
].value
;
444 TRACE("Key %s not found\n", key
);
448 int ConfigValueExists(const char *blockName
, const char *keyName
)
450 const char *val
= GetConfigValue(blockName
, keyName
, "");
454 int ConfigValueStr(const char *blockName
, const char *keyName
, const char **ret
)
456 const char *val
= GetConfigValue(blockName
, keyName
, "");
457 if(!val
[0]) return 0;
463 int ConfigValueInt(const char *blockName
, const char *keyName
, int *ret
)
465 const char *val
= GetConfigValue(blockName
, keyName
, "");
466 if(!val
[0]) return 0;
468 *ret
= strtol(val
, NULL
, 0);
472 int ConfigValueUInt(const char *blockName
, const char *keyName
, unsigned int *ret
)
474 const char *val
= GetConfigValue(blockName
, keyName
, "");
475 if(!val
[0]) return 0;
477 *ret
= strtoul(val
, NULL
, 0);
481 int ConfigValueFloat(const char *blockName
, const char *keyName
, float *ret
)
483 const char *val
= GetConfigValue(blockName
, keyName
, "");
484 if(!val
[0]) return 0;
487 *ret
= strtof(val
, NULL
);
489 *ret
= (float)strtod(val
, NULL
);
494 int GetConfigValueBool(const char *blockName
, const char *keyName
, int def
)
496 const char *val
= GetConfigValue(blockName
, keyName
, "");
498 if(!val
[0]) return !!def
;
499 return (strcasecmp(val
, "true") == 0 || strcasecmp(val
, "yes") == 0 ||
500 strcasecmp(val
, "on") == 0 || atoi(val
) != 0);