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 "SZFMT
" bytes from "SZFMT
"!\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
= len
+addstrlen
+1;
162 temp
= realloc(output
, newmax
);
165 ERR("Failed to realloc "SZFMT
" bytes from "SZFMT
"!\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
, '#');
196 if(comment
) *(comment
++) = 0;
198 line
= rstrip(lstrip(buffer
));
204 char *section
= line
+1;
207 endsection
= strchr(section
, ']');
208 if(!endsection
|| section
== endsection
|| endsection
[1] != 0)
210 ERR("config parse error: bad line \"%s\"\n", line
);
215 if(strcasecmp(section
, "general") == 0)
219 strncpy(curSection
, section
, sizeof(curSection
)-1);
220 curSection
[sizeof(curSection
)-1] = 0;
226 if(sscanf(line
, "%255[^=] = \"%255[^\"]\"", key
, value
) == 2 ||
227 sscanf(line
, "%255[^=] = '%255[^\']'", key
, value
) == 2 ||
228 sscanf(line
, "%255[^=] = %255[^\n]", key
, value
) == 2)
230 /* sscanf doesn't handle '' or "" as empty values, so clip it
232 if(strcmp(value
, "\"\"") == 0 || strcmp(value
, "''") == 0)
235 else if(sscanf(line
, "%255[^=] %255[=]", key
, value
) == 2)
237 /* Special case for 'key =' */
242 ERR("config parse error: malformed option line: \"%s\"\n\n", line
);
247 if(curSection
[0] != 0)
249 size_t len
= strlen(curSection
);
250 memmove(&key
[len
+1], key
, sizeof(key
)-1-len
);
252 memcpy(key
, curSection
, len
);
255 /* Check if we already have this option set */
256 ent
= cfgBlock
.entries
;
257 while((unsigned int)(ent
-cfgBlock
.entries
) < cfgBlock
.entryCount
)
259 if(strcasecmp(ent
->key
, key
) == 0)
264 if((unsigned int)(ent
-cfgBlock
.entries
) >= cfgBlock
.entryCount
)
266 /* Allocate a new option entry */
267 ent
= realloc(cfgBlock
.entries
, (cfgBlock
.entryCount
+1)*sizeof(ConfigEntry
));
270 ERR("config parse error: error reallocating config entries\n");
273 cfgBlock
.entries
= ent
;
274 ent
= cfgBlock
.entries
+ cfgBlock
.entryCount
;
275 cfgBlock
.entryCount
++;
277 ent
->key
= strdup(key
);
282 ent
->value
= expdup(value
);
284 TRACE("found '%s' = '%s'\n", ent
->key
, ent
->value
);
291 void ReadALConfig(void)
293 WCHAR buffer
[PATH_MAX
];
297 if(SHGetSpecialFolderPathW(NULL
, buffer
, CSIDL_APPDATA
, FALSE
) != FALSE
)
299 size_t p
= lstrlenW(buffer
);
300 _snwprintf(buffer
+p
, PATH_MAX
-p
, L
"\\alsoft.ini");
302 TRACE("Loading config %ls...\n", buffer
);
303 f
= _wfopen(buffer
, L
"rt");
306 LoadConfigFromFile(f
);
311 if((str
=_wgetenv(L
"ALSOFT_CONF")) != NULL
&& *str
)
313 TRACE("Loading config %ls...\n", str
);
314 f
= _wfopen(str
, L
"rt");
317 LoadConfigFromFile(f
);
323 void ReadALConfig(void)
325 char buffer
[PATH_MAX
];
329 str
= "/etc/openal/alsoft.conf";
331 TRACE("Loading config %s...\n", str
);
332 f
= al_fopen(str
, "r");
335 LoadConfigFromFile(f
);
339 if(!(str
=getenv("XDG_CONFIG_DIRS")) || str
[0] == 0)
341 strncpy(buffer
, str
, sizeof(buffer
)-1);
342 buffer
[sizeof(buffer
)-1] = 0;
343 /* Go through the list in reverse, since "the order of base directories
344 * denotes their importance; the first directory listed is the most
345 * important". Ergo, we need to load the settings from the later dirs
346 * first so that the settings in the earlier dirs override them.
350 char *next
= strrchr(buffer
, ':');
351 if(next
) *(next
++) = 0;
355 WARN("Ignoring XDG config dir: %s\n", next
);
358 size_t len
= strlen(next
);
359 strncpy(next
+len
, "/alsoft.conf", buffer
+sizeof(buffer
)-next
-len
);
360 buffer
[sizeof(buffer
)-1] = 0;
362 TRACE("Loading config %s...\n", next
);
363 f
= al_fopen(next
, "r");
366 LoadConfigFromFile(f
);
374 if((str
=getenv("HOME")) != NULL
&& *str
)
376 snprintf(buffer
, sizeof(buffer
), "%s/.alsoftrc", str
);
378 TRACE("Loading config %s...\n", buffer
);
379 f
= al_fopen(buffer
, "r");
382 LoadConfigFromFile(f
);
387 if((str
=getenv("XDG_CONFIG_HOME")) != NULL
&& str
[0] != 0)
388 snprintf(buffer
, sizeof(buffer
), "%s/%s", str
, "alsoft.conf");
392 if((str
=getenv("HOME")) != NULL
&& str
[0] != 0)
393 snprintf(buffer
, sizeof(buffer
), "%s/.config/%s", str
, "alsoft.conf");
397 TRACE("Loading config %s...\n", buffer
);
398 f
= al_fopen(buffer
, "r");
401 LoadConfigFromFile(f
);
406 if((str
=getenv("ALSOFT_CONF")) != NULL
&& *str
)
408 TRACE("Loading config %s...\n", str
);
409 f
= al_fopen(str
, "r");
412 LoadConfigFromFile(f
);
419 void FreeALConfig(void)
423 for(i
= 0;i
< cfgBlock
.entryCount
;i
++)
425 free(cfgBlock
.entries
[i
].key
);
426 free(cfgBlock
.entries
[i
].value
);
428 free(cfgBlock
.entries
);
431 const char *GetConfigValue(const char *blockName
, const char *keyName
, const char *def
)
439 if(blockName
&& strcasecmp(blockName
, "general") != 0)
440 snprintf(key
, sizeof(key
), "%s/%s", blockName
, keyName
);
443 strncpy(key
, keyName
, sizeof(key
)-1);
444 key
[sizeof(key
)-1] = 0;
447 for(i
= 0;i
< cfgBlock
.entryCount
;i
++)
449 if(strcasecmp(cfgBlock
.entries
[i
].key
, key
) == 0)
451 TRACE("Found %s = \"%s\"\n", key
, cfgBlock
.entries
[i
].value
);
452 if(cfgBlock
.entries
[i
].value
[0])
453 return cfgBlock
.entries
[i
].value
;
458 TRACE("Key %s not found\n", key
);
462 int ConfigValueExists(const char *blockName
, const char *keyName
)
464 const char *val
= GetConfigValue(blockName
, keyName
, "");
468 int ConfigValueStr(const char *blockName
, const char *keyName
, const char **ret
)
470 const char *val
= GetConfigValue(blockName
, keyName
, "");
471 if(!val
[0]) return 0;
477 int ConfigValueInt(const char *blockName
, const char *keyName
, int *ret
)
479 const char *val
= GetConfigValue(blockName
, keyName
, "");
480 if(!val
[0]) return 0;
482 *ret
= strtol(val
, NULL
, 0);
486 int ConfigValueUInt(const char *blockName
, const char *keyName
, unsigned int *ret
)
488 const char *val
= GetConfigValue(blockName
, keyName
, "");
489 if(!val
[0]) return 0;
491 *ret
= strtoul(val
, NULL
, 0);
495 int ConfigValueFloat(const char *blockName
, const char *keyName
, float *ret
)
497 const char *val
= GetConfigValue(blockName
, keyName
, "");
498 if(!val
[0]) return 0;
501 *ret
= strtof(val
, NULL
);
503 *ret
= (float)strtod(val
, NULL
);
508 int GetConfigValueBool(const char *blockName
, const char *keyName
, int def
)
510 const char *val
= GetConfigValue(blockName
, keyName
, "");
512 if(!val
[0]) return !!def
;
513 return (strcasecmp(val
, "true") == 0 || strcasecmp(val
, "yes") == 0 ||
514 strcasecmp(val
, "on") == 0 || atoi(val
) != 0);