Use size_t where appropriate
[openal-soft.git] / Alc / alcConfig.c
blobb7e131efd53e7fa69a324edb7d6546e0f92d3f0d
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 #if defined(_WIN32) && (_WIN32_IE < 0x400)
22 #undef _WIN32_IE
23 #define _WIN32_IE 0x400
24 #endif
26 #include "config.h"
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <ctype.h>
31 #include <string.h>
33 #include "alMain.h"
35 #ifdef _WIN32_IE
36 #include <shlobj.h>
37 #endif
39 typedef struct ConfigEntry {
40 char *key;
41 char *value;
42 } ConfigEntry;
44 typedef struct ConfigBlock {
45 char *name;
46 ConfigEntry *entries;
47 size_t entryCount;
48 } ConfigBlock;
50 static ConfigBlock *cfgBlocks;
51 static size_t cfgCount;
53 static char buffer[1024];
55 static void LoadConfigFromFile(FILE *f)
57 ConfigBlock *curBlock = cfgBlocks;
58 ConfigEntry *ent;
60 while(fgets(buffer, sizeof(buffer), f))
62 size_t i = 0;
64 while(isspace(buffer[i]))
65 i++;
66 if(!buffer[i] || buffer[i] == '#')
67 continue;
69 memmove(buffer, buffer+i, strlen(buffer+i)+1);
71 if(buffer[0] == '[')
73 ConfigBlock *nextBlock;
75 i = 1;
76 while(buffer[i] && buffer[i] != ']')
77 i++;
79 if(!buffer[i])
81 AL_PRINT("config parse error: bad line \"%s\"\n", buffer);
82 continue;
84 buffer[i] = 0;
86 do {
87 i++;
88 if(buffer[i] && !isspace(buffer[i]))
90 if(buffer[i] != '#')
91 AL_PRINT("config warning: extra data after block: \"%s\"\n", buffer+i);
92 break;
94 } while(buffer[i]);
96 nextBlock = NULL;
97 for(i = 0;i < cfgCount;i++)
99 if(strcasecmp(cfgBlocks[i].name, buffer+1) == 0)
101 nextBlock = cfgBlocks+i;
102 // AL_PRINT("found block '%s'\n", nextBlock->name);
103 break;
107 if(!nextBlock)
109 nextBlock = realloc(cfgBlocks, (cfgCount+1)*sizeof(ConfigBlock));
110 if(!nextBlock)
112 AL_PRINT("config parse error: error reallocating config blocks\n");
113 continue;
115 cfgBlocks = nextBlock;
116 nextBlock = cfgBlocks+cfgCount;
117 cfgCount++;
119 nextBlock->name = strdup(buffer+1);
120 nextBlock->entries = NULL;
121 nextBlock->entryCount = 0;
123 // AL_PRINT("found new block '%s'\n", nextBlock->name);
125 curBlock = nextBlock;
126 continue;
129 /* Look for the option name */
130 i = 0;
131 while(buffer[i] && buffer[i] != '#' && buffer[i] != '=' &&
132 !isspace(buffer[i]))
133 i++;
135 if(!buffer[i] || buffer[i] == '#' || i == 0)
137 AL_PRINT("config parse error: malformed option line: \"%s\"\n", buffer);
138 continue;
141 /* Seperate the option */
142 if(buffer[i] != '=')
144 buffer[i++] = 0;
146 while(isspace(buffer[i]))
147 i++;
148 if(buffer[i] != '=')
150 AL_PRINT("config parse error: option without a value: \"%s\"\n", buffer);
151 continue;
154 /* Find the start of the value */
155 buffer[i++] = 0;
156 while(isspace(buffer[i]))
157 i++;
159 /* Check if we already have this option set */
160 ent = curBlock->entries;
161 while((size_t)(ent-curBlock->entries) < curBlock->entryCount)
163 if(strcasecmp(ent->key, buffer) == 0)
164 break;
165 ent++;
168 if((size_t)(ent-curBlock->entries) >= curBlock->entryCount)
170 /* Allocate a new option entry */
171 ent = realloc(curBlock->entries, (curBlock->entryCount+1)*sizeof(ConfigEntry));
172 if(!ent)
174 AL_PRINT("config parse error: error reallocating config entries\n");
175 continue;
177 curBlock->entries = ent;
178 ent = curBlock->entries + curBlock->entryCount;
179 curBlock->entryCount++;
181 ent->key = strdup(buffer);
182 ent->value = NULL;
185 /* Look for the end of the line (Null term, new-line, or #-symbol) and
186 eat up the trailing whitespace */
187 memmove(buffer, buffer+i, strlen(buffer+i)+1);
189 i = 0;
190 while(buffer[i] && buffer[i] != '#' && buffer[i] != '\n')
191 i++;
192 do {
193 i--;
194 } while(isspace(buffer[i]));
195 buffer[++i] = 0;
197 free(ent->value);
198 ent->value = strdup(buffer);
200 // AL_PRINT("found '%s' = '%s'\n", ent->key, ent->value);
204 void ReadALConfig(void)
206 FILE *f;
208 cfgBlocks = calloc(1, sizeof(ConfigBlock));
209 cfgBlocks->name = strdup("general");
210 cfgCount = 1;
212 #ifdef _WIN32
213 if(SHGetSpecialFolderPathA(NULL, buffer, CSIDL_APPDATA, FALSE) != FALSE)
215 size_t p = strlen(buffer);
216 snprintf(buffer+p, sizeof(buffer)-p, "\\alsoft.ini");
217 f = fopen(buffer, "rt");
218 if(f)
220 LoadConfigFromFile(f);
221 fclose(f);
224 #else
225 f = fopen("/etc/openal/alsoft.conf", "r");
226 if(f)
228 LoadConfigFromFile(f);
229 fclose(f);
231 if(getenv("HOME") && *(getenv("HOME")))
233 snprintf(buffer, sizeof(buffer), "%s/.alsoftrc", getenv("HOME"));
234 f = fopen(buffer, "r");
235 if(f)
237 LoadConfigFromFile(f);
238 fclose(f);
241 #endif
242 if(getenv("ALSOFT_CONF"))
244 f = fopen(getenv("ALSOFT_CONF"), "r");
245 if(f)
247 LoadConfigFromFile(f);
248 fclose(f);
253 void FreeALConfig(void)
255 size_t i;
257 for(i = 0;i < cfgCount;i++)
259 size_t j;
260 for(j = 0;j < cfgBlocks[i].entryCount;j++)
262 free(cfgBlocks[i].entries[j].key);
263 free(cfgBlocks[i].entries[j].value);
265 free(cfgBlocks[i].entries);
266 free(cfgBlocks[i].name);
268 free(cfgBlocks);
269 cfgBlocks = NULL;
270 cfgCount = 0;
273 const char *GetConfigValue(const char *blockName, const char *keyName, const char *def)
275 size_t i, j;
277 if(keyName)
279 if(!blockName)
280 blockName = "general";
282 for(i = 0;i < cfgCount;i++)
284 if(strcasecmp(cfgBlocks[i].name, blockName) != 0)
285 continue;
287 for(j = 0;j < cfgBlocks[i].entryCount;j++)
289 if(strcasecmp(cfgBlocks[i].entries[j].key, keyName) == 0)
291 if(cfgBlocks[i].entries[j].value[0])
292 return cfgBlocks[i].entries[j].value;
293 return def;
299 return def;
302 int ConfigValueExists(const char *blockName, const char *keyName)
304 const char *val = GetConfigValue(blockName, keyName, "");
305 return !!val[0];
308 int GetConfigValueInt(const char *blockName, const char *keyName, int def)
310 const char *val = GetConfigValue(blockName, keyName, "");
312 if(!val[0]) return def;
313 return strtol(val, NULL, 0);
316 float GetConfigValueFloat(const char *blockName, const char *keyName, float def)
318 const char *val = GetConfigValue(blockName, keyName, "");
320 if(!val[0]) return def;
321 #ifdef HAVE_STRTOF
322 return strtof(val, NULL);
323 #else
324 return (float)strtod(val, NULL);
325 #endif
328 int GetConfigValueBool(const char *blockName, const char *keyName, int def)
330 const char *val = GetConfigValue(blockName, keyName, "");
332 if(!val[0]) return !!def;
333 return (strcasecmp(val, "true") == 0 || strcasecmp(val, "yes") == 0 ||
334 strcasecmp(val, "on") == 0 || atoi(val) != 0);