Store double formats as float
[openal-soft.git] / Alc / alcConfig.c
blob847e5d13f65c0cfa7174d112945f93f079ab121a
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 size_t entryCount;
51 } ConfigBlock;
53 static ConfigBlock *cfgBlocks;
54 static size_t 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 size_t 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;
78 i = 1;
79 while(buffer[i] && buffer[i] != ']')
80 i++;
82 if(!buffer[i])
84 AL_PRINT("config parse error: bad line \"%s\"\n", buffer);
85 continue;
87 buffer[i] = 0;
89 do {
90 i++;
91 if(buffer[i] && !isspace(buffer[i]))
93 if(buffer[i] != '#')
94 AL_PRINT("config warning: extra data after block: \"%s\"\n", buffer+i);
95 break;
97 } while(buffer[i]);
99 nextBlock = NULL;
100 for(i = 0;i < cfgCount;i++)
102 if(strcasecmp(cfgBlocks[i].name, buffer+1) == 0)
104 nextBlock = cfgBlocks+i;
105 // AL_PRINT("found block '%s'\n", nextBlock->name);
106 break;
110 if(!nextBlock)
112 nextBlock = realloc(cfgBlocks, (cfgCount+1)*sizeof(ConfigBlock));
113 if(!nextBlock)
115 AL_PRINT("config parse error: error reallocating config blocks\n");
116 continue;
118 cfgBlocks = nextBlock;
119 nextBlock = cfgBlocks+cfgCount;
120 cfgCount++;
122 nextBlock->name = strdup(buffer+1);
123 nextBlock->entries = NULL;
124 nextBlock->entryCount = 0;
126 // AL_PRINT("found new block '%s'\n", nextBlock->name);
128 curBlock = nextBlock;
129 continue;
132 /* Look for the option name */
133 i = 0;
134 while(buffer[i] && buffer[i] != '#' && buffer[i] != '=' &&
135 !isspace(buffer[i]))
136 i++;
138 if(!buffer[i] || buffer[i] == '#' || i == 0)
140 AL_PRINT("config parse error: malformed option line: \"%s\"\n", buffer);
141 continue;
144 /* Seperate the option */
145 if(buffer[i] != '=')
147 buffer[i++] = 0;
149 while(isspace(buffer[i]))
150 i++;
151 if(buffer[i] != '=')
153 AL_PRINT("config parse error: option without a value: \"%s\"\n", buffer);
154 continue;
157 /* Find the start of the value */
158 buffer[i++] = 0;
159 while(isspace(buffer[i]))
160 i++;
162 /* Check if we already have this option set */
163 ent = curBlock->entries;
164 while((size_t)(ent-curBlock->entries) < curBlock->entryCount)
166 if(strcasecmp(ent->key, buffer) == 0)
167 break;
168 ent++;
171 if((size_t)(ent-curBlock->entries) >= curBlock->entryCount)
173 /* Allocate a new option entry */
174 ent = realloc(curBlock->entries, (curBlock->entryCount+1)*sizeof(ConfigEntry));
175 if(!ent)
177 AL_PRINT("config parse error: error reallocating config entries\n");
178 continue;
180 curBlock->entries = ent;
181 ent = curBlock->entries + curBlock->entryCount;
182 curBlock->entryCount++;
184 ent->key = strdup(buffer);
185 ent->value = NULL;
188 /* Look for the end of the line (Null term, new-line, or #-symbol) and
189 eat up the trailing whitespace */
190 memmove(buffer, buffer+i, strlen(buffer+i)+1);
192 i = 0;
193 while(buffer[i] && buffer[i] != '#' && buffer[i] != '\n')
194 i++;
195 do {
196 i--;
197 } while(isspace(buffer[i]));
198 buffer[++i] = 0;
200 free(ent->value);
201 ent->value = strdup(buffer);
203 // AL_PRINT("found '%s' = '%s'\n", ent->key, ent->value);
207 void ReadALConfig(void)
209 FILE *f;
211 cfgBlocks = calloc(1, sizeof(ConfigBlock));
212 cfgBlocks->name = strdup("general");
213 cfgCount = 1;
215 #ifdef _WIN32
216 if(SHGetSpecialFolderPathA(NULL, buffer, CSIDL_APPDATA, FALSE) != FALSE)
218 size_t p = strlen(buffer);
219 snprintf(buffer+p, sizeof(buffer)-p, "\\alsoft.ini");
220 f = fopen(buffer, "rt");
221 if(f)
223 LoadConfigFromFile(f);
224 fclose(f);
227 #else
228 f = fopen("/etc/openal/alsoft.conf", "r");
229 if(f)
231 LoadConfigFromFile(f);
232 fclose(f);
234 if(getenv("HOME") && *(getenv("HOME")))
236 snprintf(buffer, sizeof(buffer), "%s/.alsoftrc", getenv("HOME"));
237 f = fopen(buffer, "r");
238 if(f)
240 LoadConfigFromFile(f);
241 fclose(f);
244 #endif
245 if(getenv("ALSOFT_CONF"))
247 f = fopen(getenv("ALSOFT_CONF"), "r");
248 if(f)
250 LoadConfigFromFile(f);
251 fclose(f);
256 void FreeALConfig(void)
258 size_t i;
260 for(i = 0;i < cfgCount;i++)
262 size_t j;
263 for(j = 0;j < cfgBlocks[i].entryCount;j++)
265 free(cfgBlocks[i].entries[j].key);
266 free(cfgBlocks[i].entries[j].value);
268 free(cfgBlocks[i].entries);
269 free(cfgBlocks[i].name);
271 free(cfgBlocks);
272 cfgBlocks = NULL;
273 cfgCount = 0;
276 const char *GetConfigValue(const char *blockName, const char *keyName, const char *def)
278 size_t i, j;
280 if(!keyName)
281 return def;
283 if(!blockName)
284 blockName = "general";
286 for(i = 0;i < cfgCount;i++)
288 if(strcasecmp(cfgBlocks[i].name, blockName) != 0)
289 continue;
291 for(j = 0;j < cfgBlocks[i].entryCount;j++)
293 if(strcasecmp(cfgBlocks[i].entries[j].key, keyName) == 0)
295 if(cfgBlocks[i].entries[j].value[0])
296 return cfgBlocks[i].entries[j].value;
297 return def;
302 return def;
305 int ConfigValueExists(const char *blockName, const char *keyName)
307 const char *val = GetConfigValue(blockName, keyName, "");
308 return !!val[0];
311 int GetConfigValueInt(const char *blockName, const char *keyName, int def)
313 const char *val = GetConfigValue(blockName, keyName, "");
315 if(!val[0]) return def;
316 return strtol(val, NULL, 0);
319 float GetConfigValueFloat(const char *blockName, const char *keyName, float def)
321 const char *val = GetConfigValue(blockName, keyName, "");
323 if(!val[0]) return def;
324 #ifdef HAVE_STRTOF
325 return strtof(val, NULL);
326 #else
327 return (float)strtod(val, NULL);
328 #endif
331 int GetConfigValueBool(const char *blockName, const char *keyName, int def)
333 const char *val = GetConfigValue(blockName, keyName, "");
335 if(!val[0]) return !!def;
336 return (strcasecmp(val, "true") == 0 || strcasecmp(val, "yes") == 0 ||
337 strcasecmp(val, "on") == 0 || atoi(val) != 0);