Add missing config.h includes
[openal-soft.git] / Alc / alcConfig.c
blobb6a28a6e785cdb858889d615482f9916522d8bde
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 #include "config.h"
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <ctype.h>
26 #include <string.h>
28 #include "alMain.h"
30 typedef struct ConfigEntry {
31 char *key;
32 char *value;
33 } ConfigEntry;
35 typedef struct ConfigBlock {
36 char *name;
37 ConfigEntry *entries;
38 size_t entryCount;
39 } ConfigBlock;
41 static ConfigBlock *cfgBlocks;
42 static size_t cfgCount;
44 static char buffer[1024];
46 static void LoadConfigFromFile(FILE *f)
48 ConfigBlock *curBlock = cfgBlocks;
49 ConfigEntry *ent;
51 while(fgets(buffer, sizeof(buffer), f))
53 size_t i = 0;
55 while(isspace(buffer[i]))
56 i++;
57 if(!buffer[i] || buffer[i] == '#')
58 continue;
60 memmove(buffer, buffer+i, strlen(buffer+i)+1);
62 if(buffer[0] == '[')
64 ConfigBlock *nextBlock;
66 i = 1;
67 while(buffer[i] && buffer[i] != ']')
68 i++;
70 if(!buffer[i])
72 AL_PRINT("config parse error: bad line \"%s\"\n", buffer);
73 continue;
75 buffer[i] = 0;
77 do {
78 i++;
79 if(buffer[i] && !isspace(buffer[i]))
81 if(buffer[i] != '#')
82 AL_PRINT("config warning: extra data after block: \"%s\"\n", buffer+i);
83 break;
85 } while(buffer[i]);
87 nextBlock = NULL;
88 for(i = 0;i < cfgCount;i++)
90 if(strcasecmp(cfgBlocks[i].name, buffer+1) == 0)
92 nextBlock = cfgBlocks+i;
93 // AL_PRINT("found block '%s'\n", nextBlock->name);
94 break;
98 if(!nextBlock)
100 nextBlock = realloc(cfgBlocks, (cfgCount+1)*sizeof(ConfigBlock));
101 if(!nextBlock)
103 AL_PRINT("config parse error: error reallocating config blocks\n");
104 continue;
106 cfgBlocks = nextBlock;
107 nextBlock = cfgBlocks+cfgCount;
108 cfgCount++;
110 nextBlock->name = strdup(buffer+1);
111 nextBlock->entries = NULL;
112 nextBlock->entryCount = 0;
114 // AL_PRINT("found new block '%s'\n", nextBlock->name);
116 curBlock = nextBlock;
117 continue;
120 /* Look for the option name */
121 i = 0;
122 while(buffer[i] && buffer[i] != '#' && buffer[i] != '=' &&
123 !isspace(buffer[i]))
124 i++;
126 if(!buffer[i] || buffer[i] == '#' || i == 0)
128 AL_PRINT("config parse error: malformed option line: \"%s\"\n", buffer);
129 continue;
132 /* Seperate the option */
133 if(buffer[i] != '=')
135 buffer[i++] = 0;
137 while(isspace(buffer[i]))
138 i++;
139 if(buffer[i] != '=')
141 AL_PRINT("config parse error: option without a value: \"%s\"\n", buffer);
142 continue;
145 /* Find the start of the value */
146 buffer[i++] = 0;
147 while(isspace(buffer[i]))
148 i++;
150 /* Check if we already have this option set */
151 ent = curBlock->entries;
152 while((size_t)(ent-curBlock->entries) < curBlock->entryCount)
154 if(strcasecmp(ent->key, buffer) == 0)
155 break;
156 ent++;
159 if((size_t)(ent-curBlock->entries) >= curBlock->entryCount)
161 /* Allocate a new option entry */
162 ent = realloc(curBlock->entries, (curBlock->entryCount+1)*sizeof(ConfigEntry));
163 if(!ent)
165 AL_PRINT("config parse error: error reallocating config entries\n");
166 continue;
168 curBlock->entries = ent;
169 ent = curBlock->entries + curBlock->entryCount;
170 curBlock->entryCount++;
172 ent->key = strdup(buffer);
173 ent->value = NULL;
176 /* Look for the end of the line (Null term, new-line, or #-symbol) and
177 eat up the trailing whitespace */
178 memmove(buffer, buffer+i, strlen(buffer+i)+1);
180 i = 0;
181 while(buffer[i] && buffer[i] != '#' && buffer[i] != '\n')
182 i++;
183 do {
184 i--;
185 } while(isspace(buffer[i]));
186 buffer[++i] = 0;
188 free(ent->value);
189 ent->value = strdup(buffer);
191 // AL_PRINT("found '%s' = '%s'\n", ent->key, ent->value);
195 void ReadALConfig(void)
197 FILE *f;
199 cfgBlocks = calloc(1, sizeof(ConfigBlock));
200 cfgBlocks->name = strdup("general");
201 cfgCount = 1;
203 #ifdef _WIN32
204 #else
205 f = fopen("/etc/openal/config", "r");
206 if(f)
208 LoadConfigFromFile(f);
209 fclose(f);
211 if(getenv("HOME") && *(getenv("HOME")))
213 snprintf(buffer, sizeof(buffer), "%s/.openalrc", getenv("HOME"));
214 f = fopen(buffer, "r");
215 if(f)
217 LoadConfigFromFile(f);
218 fclose(f);
221 #endif
224 void FreeALConfig(void)
226 size_t i;
228 for(i = 0;i < cfgCount;i++)
230 size_t j;
231 for(j = 0;j < cfgBlocks[i].entryCount;j++)
233 free(cfgBlocks[i].entries[j].key);
234 free(cfgBlocks[i].entries[j].value);
236 free(cfgBlocks[i].entries);
237 free(cfgBlocks[i].name);
239 free(cfgBlocks);
240 cfgBlocks = NULL;
241 cfgCount = 0;
244 const char *GetConfigValue(const char *blockName, const char *keyName, const char *def)
246 size_t i, j;
248 if(keyName)
250 if(!blockName)
251 blockName = "general";
253 for(i = 0;i < cfgCount;i++)
255 if(strcasecmp(cfgBlocks[i].name, blockName) != 0)
256 continue;
258 for(j = 0;j < cfgBlocks[i].entryCount;j++)
260 if(strcasecmp(cfgBlocks[i].entries[j].key, keyName) == 0)
261 return cfgBlocks[i].entries[j].value;
266 return def;
269 int GetConfigValueInt(const char *blockName, const char *keyName, int def)
271 const char *val = GetConfigValue(blockName, keyName, "");
273 if(!val[0]) return def;
274 return strtol(val, NULL, 0);
277 float GetConfigValueFloat(const char *blockName, const char *keyName, float def)
279 const char *val = GetConfigValue(blockName, keyName, "");
281 if(!val[0]) return def;
282 #ifdef HAVE_STRTOF
283 return strtof(val, NULL);
284 #else
285 return (float)strtod(val, NULL);
286 #endif