Remove a hack for MinGW
[openal-soft.git] / Alc / alcConfig.c
blob012c677d029606d721458ec993c7e4580e180678
1 /**
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
21 #ifdef _WIN32
22 #ifdef __MINGW64__
23 #define _WIN32_IE 0x501
24 #else
25 #define _WIN32_IE 0x400
26 #endif
27 #endif
29 #include "config.h"
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <ctype.h>
34 #include <string.h>
36 #include "alMain.h"
38 #ifdef _WIN32_IE
39 #include <shlobj.h>
40 #endif
42 typedef struct ConfigEntry {
43 char *key;
44 char *value;
45 } ConfigEntry;
47 typedef struct ConfigBlock {
48 char *name;
49 ConfigEntry *entries;
50 unsigned int entryCount;
51 } ConfigBlock;
53 static ConfigBlock *cfgBlocks;
54 static unsigned int cfgCount;
56 static char buffer[1024];
58 static void LoadConfigFromFile(FILE *f)
60 ConfigBlock *curBlock = cfgBlocks;
61 ConfigEntry *ent;
63 while(fgets(buffer, sizeof(buffer), f))
65 int i = 0;
67 while(isspace(buffer[i]))
68 i++;
69 if(!buffer[i] || buffer[i] == '#')
70 continue;
72 memmove(buffer, buffer+i, strlen(buffer+i)+1);
74 if(buffer[0] == '[')
76 ConfigBlock *nextBlock;
77 unsigned int i;
79 i = 1;
80 while(buffer[i] && buffer[i] != ']')
81 i++;
83 if(!buffer[i])
85 ERR("config parse error: bad line \"%s\"\n", buffer);
86 continue;
88 buffer[i] = 0;
90 do {
91 i++;
92 if(buffer[i] && !isspace(buffer[i]))
94 if(buffer[i] != '#')
95 WARN("config warning: extra data after block: \"%s\"\n", buffer+i);
96 break;
98 } while(buffer[i]);
100 nextBlock = NULL;
101 for(i = 0;i < cfgCount;i++)
103 if(strcasecmp(cfgBlocks[i].name, buffer+1) == 0)
105 nextBlock = cfgBlocks+i;
106 TRACE("found block '%s'\n", nextBlock->name);
107 break;
111 if(!nextBlock)
113 nextBlock = realloc(cfgBlocks, (cfgCount+1)*sizeof(ConfigBlock));
114 if(!nextBlock)
116 ERR("config parse error: error reallocating config blocks\n");
117 continue;
119 cfgBlocks = nextBlock;
120 nextBlock = cfgBlocks+cfgCount;
121 cfgCount++;
123 nextBlock->name = strdup(buffer+1);
124 nextBlock->entries = NULL;
125 nextBlock->entryCount = 0;
127 TRACE("found new block '%s'\n", nextBlock->name);
129 curBlock = nextBlock;
130 continue;
133 /* Look for the option name */
134 i = 0;
135 while(buffer[i] && buffer[i] != '#' && buffer[i] != '=' &&
136 !isspace(buffer[i]))
137 i++;
139 if(!buffer[i] || buffer[i] == '#' || i == 0)
141 ERR("config parse error: malformed option line: \"%s\"\n", buffer);
142 continue;
145 /* Seperate the option */
146 if(buffer[i] != '=')
148 buffer[i++] = 0;
150 while(isspace(buffer[i]))
151 i++;
152 if(buffer[i] != '=')
154 ERR("config parse error: option without a value: \"%s\"\n", buffer);
155 continue;
158 /* Find the start of the value */
159 buffer[i++] = 0;
160 while(isspace(buffer[i]))
161 i++;
163 /* Check if we already have this option set */
164 ent = curBlock->entries;
165 while((unsigned int)(ent-curBlock->entries) < curBlock->entryCount)
167 if(strcasecmp(ent->key, buffer) == 0)
168 break;
169 ent++;
172 if((unsigned int)(ent-curBlock->entries) >= curBlock->entryCount)
174 /* Allocate a new option entry */
175 ent = realloc(curBlock->entries, (curBlock->entryCount+1)*sizeof(ConfigEntry));
176 if(!ent)
178 ERR("config parse error: error reallocating config entries\n");
179 continue;
181 curBlock->entries = ent;
182 ent = curBlock->entries + curBlock->entryCount;
183 curBlock->entryCount++;
185 ent->key = strdup(buffer);
186 ent->value = NULL;
189 /* Look for the end of the line (Null term, new-line, or #-symbol) and
190 eat up the trailing whitespace */
191 memmove(buffer, buffer+i, strlen(buffer+i)+1);
193 i = 0;
194 while(buffer[i] && buffer[i] != '#' && buffer[i] != '\n')
195 i++;
196 do {
197 i--;
198 } while(i >= 0 && isspace(buffer[i]));
199 buffer[++i] = 0;
201 free(ent->value);
202 ent->value = strdup(buffer);
204 TRACE("found '%s' = '%s'\n", ent->key, ent->value);
208 void ReadALConfig(void)
210 const char *str;
211 FILE *f;
213 cfgBlocks = calloc(1, sizeof(ConfigBlock));
214 cfgBlocks->name = strdup("general");
215 cfgCount = 1;
217 #ifdef _WIN32
218 if(SHGetSpecialFolderPathA(NULL, buffer, CSIDL_APPDATA, FALSE) != FALSE)
220 size_t p = strlen(buffer);
221 snprintf(buffer+p, sizeof(buffer)-p, "\\alsoft.ini");
222 f = fopen(buffer, "rt");
223 if(f)
225 LoadConfigFromFile(f);
226 fclose(f);
229 #else
230 f = fopen("/etc/openal/alsoft.conf", "r");
231 if(f)
233 LoadConfigFromFile(f);
234 fclose(f);
236 if((str=getenv("HOME")) != NULL && *str)
238 snprintf(buffer, sizeof(buffer), "%s/.alsoftrc", str);
239 f = fopen(buffer, "r");
240 if(f)
242 LoadConfigFromFile(f);
243 fclose(f);
246 #endif
247 if((str=getenv("ALSOFT_CONF")) != NULL && *str)
249 f = fopen(str, "r");
250 if(f)
252 LoadConfigFromFile(f);
253 fclose(f);
258 void FreeALConfig(void)
260 unsigned int i;
262 for(i = 0;i < cfgCount;i++)
264 unsigned int j;
265 for(j = 0;j < cfgBlocks[i].entryCount;j++)
267 free(cfgBlocks[i].entries[j].key);
268 free(cfgBlocks[i].entries[j].value);
270 free(cfgBlocks[i].entries);
271 free(cfgBlocks[i].name);
273 free(cfgBlocks);
274 cfgBlocks = NULL;
275 cfgCount = 0;
278 const char *GetConfigValue(const char *blockName, const char *keyName, const char *def)
280 unsigned int i, j;
282 if(!keyName)
283 return def;
285 if(!blockName)
286 blockName = "general";
288 for(i = 0;i < cfgCount;i++)
290 if(strcasecmp(cfgBlocks[i].name, blockName) != 0)
291 continue;
293 for(j = 0;j < cfgBlocks[i].entryCount;j++)
295 if(strcasecmp(cfgBlocks[i].entries[j].key, keyName) == 0)
297 TRACE("Found %s:%s = \"%s\"\n", blockName, keyName,
298 cfgBlocks[i].entries[j].value);
299 if(cfgBlocks[i].entries[j].value[0])
300 return cfgBlocks[i].entries[j].value;
301 return def;
306 TRACE("Key %s:%s not found\n", blockName, keyName);
307 return def;
310 int ConfigValueExists(const char *blockName, const char *keyName)
312 const char *val = GetConfigValue(blockName, keyName, "");
313 return !!val[0];
316 int ConfigValueStr(const char *blockName, const char *keyName, const char **ret)
318 const char *val = GetConfigValue(blockName, keyName, "");
319 if(!val[0]) return 0;
321 *ret = val;
322 return 1;
325 int ConfigValueInt(const char *blockName, const char *keyName, int *ret)
327 const char *val = GetConfigValue(blockName, keyName, "");
328 if(!val[0]) return 0;
330 *ret = strtol(val, NULL, 0);
331 return 1;
334 int ConfigValueUInt(const char *blockName, const char *keyName, unsigned int *ret)
336 const char *val = GetConfigValue(blockName, keyName, "");
337 if(!val[0]) return 0;
339 *ret = strtoul(val, NULL, 0);
340 return 1;
343 int ConfigValueFloat(const char *blockName, const char *keyName, float *ret)
345 const char *val = GetConfigValue(blockName, keyName, "");
346 if(!val[0]) return 0;
348 #ifdef HAVE_STRTOF
349 *ret = strtof(val, NULL);
350 #else
351 *ret = (float)strtod(val, NULL);
352 #endif
353 return 1;
356 int GetConfigValueBool(const char *blockName, const char *keyName, int def)
358 const char *val = GetConfigValue(blockName, keyName, "");
360 if(!val[0]) return !!def;
361 return (strcasecmp(val, "true") == 0 || strcasecmp(val, "yes") == 0 ||
362 strcasecmp(val, "on") == 0 || atoi(val) != 0);