Mercurial > fife-parpg
comparison ext/openal-soft/Alc/alcConfig.c @ 0:4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
author | mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222 |
---|---|
date | Sun, 29 Jun 2008 18:44:17 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4a0efb7baf70 |
---|---|
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. | |
8 * | |
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. | |
13 * | |
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 | |
19 */ | |
20 | |
21 #include "config.h" | |
22 | |
23 #include <stdlib.h> | |
24 #include <stdio.h> | |
25 #include <ctype.h> | |
26 #include <string.h> | |
27 | |
28 #include "alMain.h" | |
29 | |
30 typedef struct ConfigEntry { | |
31 char *key; | |
32 char *value; | |
33 } ConfigEntry; | |
34 | |
35 typedef struct ConfigBlock { | |
36 char *name; | |
37 ConfigEntry *entries; | |
38 size_t entryCount; | |
39 } ConfigBlock; | |
40 | |
41 static ConfigBlock *cfgBlocks; | |
42 static size_t cfgCount; | |
43 | |
44 static char buffer[1024]; | |
45 | |
46 static void LoadConfigFromFile(FILE *f) | |
47 { | |
48 ConfigBlock *curBlock = cfgBlocks; | |
49 ConfigEntry *ent; | |
50 | |
51 while(fgets(buffer, sizeof(buffer), f)) | |
52 { | |
53 size_t i = 0; | |
54 | |
55 while(isspace(buffer[i])) | |
56 i++; | |
57 if(!buffer[i] || buffer[i] == '#') | |
58 continue; | |
59 | |
60 memmove(buffer, buffer+i, strlen(buffer+i)+1); | |
61 | |
62 if(buffer[0] == '[') | |
63 { | |
64 ConfigBlock *nextBlock; | |
65 | |
66 i = 1; | |
67 while(buffer[i] && buffer[i] != ']') | |
68 i++; | |
69 | |
70 if(!buffer[i]) | |
71 { | |
72 AL_PRINT("config parse error: bad line \"%s\"\n", buffer); | |
73 continue; | |
74 } | |
75 buffer[i] = 0; | |
76 | |
77 do { | |
78 i++; | |
79 if(buffer[i] && !isspace(buffer[i])) | |
80 { | |
81 if(buffer[i] != '#') | |
82 AL_PRINT("config warning: extra data after block: \"%s\"\n", buffer+i); | |
83 break; | |
84 } | |
85 } while(buffer[i]); | |
86 | |
87 nextBlock = NULL; | |
88 for(i = 0;i < cfgCount;i++) | |
89 { | |
90 if(strcasecmp(cfgBlocks[i].name, buffer+1) == 0) | |
91 { | |
92 nextBlock = cfgBlocks+i; | |
93 // AL_PRINT("found block '%s'\n", nextBlock->name); | |
94 break; | |
95 } | |
96 } | |
97 | |
98 if(!nextBlock) | |
99 { | |
100 nextBlock = realloc(cfgBlocks, (cfgCount+1)*sizeof(ConfigBlock)); | |
101 if(!nextBlock) | |
102 { | |
103 AL_PRINT("config parse error: error reallocating config blocks\n"); | |
104 continue; | |
105 } | |
106 cfgBlocks = nextBlock; | |
107 nextBlock = cfgBlocks+cfgCount; | |
108 cfgCount++; | |
109 | |
110 nextBlock->name = strdup(buffer+1); | |
111 nextBlock->entries = NULL; | |
112 nextBlock->entryCount = 0; | |
113 | |
114 // AL_PRINT("found new block '%s'\n", nextBlock->name); | |
115 } | |
116 curBlock = nextBlock; | |
117 continue; | |
118 } | |
119 | |
120 /* Look for the option name */ | |
121 i = 0; | |
122 while(buffer[i] && buffer[i] != '#' && buffer[i] != '=' && | |
123 !isspace(buffer[i])) | |
124 i++; | |
125 | |
126 if(!buffer[i] || buffer[i] == '#' || i == 0) | |
127 { | |
128 AL_PRINT("config parse error: malformed option line: \"%s\"\n", buffer); | |
129 continue; | |
130 } | |
131 | |
132 /* Seperate the option */ | |
133 if(buffer[i] != '=') | |
134 { | |
135 buffer[i++] = 0; | |
136 | |
137 while(isspace(buffer[i])) | |
138 i++; | |
139 if(buffer[i] != '=') | |
140 { | |
141 AL_PRINT("config parse error: option without a value: \"%s\"\n", buffer); | |
142 continue; | |
143 } | |
144 } | |
145 /* Find the start of the value */ | |
146 buffer[i++] = 0; | |
147 while(isspace(buffer[i])) | |
148 i++; | |
149 | |
150 /* Check if we already have this option set */ | |
151 ent = curBlock->entries; | |
152 while((size_t)(ent-curBlock->entries) < curBlock->entryCount) | |
153 { | |
154 if(strcasecmp(ent->key, buffer) == 0) | |
155 break; | |
156 ent++; | |
157 } | |
158 | |
159 if((size_t)(ent-curBlock->entries) >= curBlock->entryCount) | |
160 { | |
161 /* Allocate a new option entry */ | |
162 ent = realloc(curBlock->entries, (curBlock->entryCount+1)*sizeof(ConfigEntry)); | |
163 if(!ent) | |
164 { | |
165 AL_PRINT("config parse error: error reallocating config entries\n"); | |
166 continue; | |
167 } | |
168 curBlock->entries = ent; | |
169 ent = curBlock->entries + curBlock->entryCount; | |
170 curBlock->entryCount++; | |
171 | |
172 ent->key = strdup(buffer); | |
173 ent->value = NULL; | |
174 } | |
175 | |
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); | |
179 | |
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; | |
187 | |
188 free(ent->value); | |
189 ent->value = strdup(buffer); | |
190 | |
191 // AL_PRINT("found '%s' = '%s'\n", ent->key, ent->value); | |
192 } | |
193 } | |
194 | |
195 void ReadALConfig(void) | |
196 { | |
197 FILE *f; | |
198 | |
199 cfgBlocks = calloc(1, sizeof(ConfigBlock)); | |
200 cfgBlocks->name = strdup("general"); | |
201 cfgCount = 1; | |
202 | |
203 #ifdef _WIN32 | |
204 #else | |
205 f = fopen("/etc/openal/alsoft.conf", "r"); | |
206 if(!f) | |
207 { | |
208 f = fopen("/etc/openal/config", "r"); | |
209 if(f) | |
210 AL_PRINT("Reading /etc/openal/config; this file is deprecated\n" | |
211 "\tPlease rename it to /etc/openal/alsoft.conf\n"); | |
212 } | |
213 if(f) | |
214 { | |
215 LoadConfigFromFile(f); | |
216 fclose(f); | |
217 } | |
218 if(getenv("HOME") && *(getenv("HOME"))) | |
219 { | |
220 snprintf(buffer, sizeof(buffer), "%s/.alsoftrc", getenv("HOME")); | |
221 f = fopen(buffer, "r"); | |
222 if(!f) | |
223 { | |
224 snprintf(buffer, sizeof(buffer), "%s/.openalrc", getenv("HOME")); | |
225 f = fopen(buffer, "r"); | |
226 if(f) | |
227 AL_PRINT("Reading ~/.openalrc; this file is deprecated\n" | |
228 "\tPlease rename it to ~/.alsoftrc\n"); | |
229 } | |
230 if(f) | |
231 { | |
232 LoadConfigFromFile(f); | |
233 fclose(f); | |
234 } | |
235 } | |
236 #endif | |
237 } | |
238 | |
239 void FreeALConfig(void) | |
240 { | |
241 size_t i; | |
242 | |
243 for(i = 0;i < cfgCount;i++) | |
244 { | |
245 size_t j; | |
246 for(j = 0;j < cfgBlocks[i].entryCount;j++) | |
247 { | |
248 free(cfgBlocks[i].entries[j].key); | |
249 free(cfgBlocks[i].entries[j].value); | |
250 } | |
251 free(cfgBlocks[i].entries); | |
252 free(cfgBlocks[i].name); | |
253 } | |
254 free(cfgBlocks); | |
255 cfgBlocks = NULL; | |
256 cfgCount = 0; | |
257 } | |
258 | |
259 const char *GetConfigValue(const char *blockName, const char *keyName, const char *def) | |
260 { | |
261 size_t i, j; | |
262 | |
263 if(keyName) | |
264 { | |
265 if(!blockName) | |
266 blockName = "general"; | |
267 | |
268 for(i = 0;i < cfgCount;i++) | |
269 { | |
270 if(strcasecmp(cfgBlocks[i].name, blockName) != 0) | |
271 continue; | |
272 | |
273 for(j = 0;j < cfgBlocks[i].entryCount;j++) | |
274 { | |
275 if(strcasecmp(cfgBlocks[i].entries[j].key, keyName) == 0) | |
276 return cfgBlocks[i].entries[j].value; | |
277 } | |
278 } | |
279 } | |
280 | |
281 return def; | |
282 } | |
283 | |
284 int GetConfigValueInt(const char *blockName, const char *keyName, int def) | |
285 { | |
286 const char *val = GetConfigValue(blockName, keyName, ""); | |
287 | |
288 if(!val[0]) return def; | |
289 return strtol(val, NULL, 0); | |
290 } | |
291 | |
292 float GetConfigValueFloat(const char *blockName, const char *keyName, float def) | |
293 { | |
294 const char *val = GetConfigValue(blockName, keyName, ""); | |
295 | |
296 if(!val[0]) return def; | |
297 #ifdef HAVE_STRTOF | |
298 return strtof(val, NULL); | |
299 #else | |
300 return (float)strtod(val, NULL); | |
301 #endif | |
302 } |