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
30 typedef struct ConfigEntry
{
35 typedef struct 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
;
51 while(fgets(buffer
, sizeof(buffer
), f
))
55 while(isspace(buffer
[i
]))
57 if(!buffer
[i
] || buffer
[i
] == '#')
60 memmove(buffer
, buffer
+i
, strlen(buffer
+i
)+1);
64 ConfigBlock
*nextBlock
;
67 while(buffer
[i
] && buffer
[i
] != ']')
72 AL_PRINT("config parse error: bad line \"%s\"\n", buffer
);
79 if(buffer
[i
] && !isspace(buffer
[i
]))
82 AL_PRINT("config warning: extra data after block: \"%s\"\n", buffer
+i
);
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);
100 nextBlock
= realloc(cfgBlocks
, (cfgCount
+1)*sizeof(ConfigBlock
));
103 AL_PRINT("config parse error: error reallocating config blocks\n");
106 cfgBlocks
= nextBlock
;
107 nextBlock
= cfgBlocks
+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
;
120 /* Look for the option name */
122 while(buffer
[i
] && buffer
[i
] != '#' && buffer
[i
] != '=' &&
126 if(!buffer
[i
] || buffer
[i
] == '#' || i
== 0)
128 AL_PRINT("config parse error: malformed option line: \"%s\"\n", buffer
);
132 /* Seperate the option */
137 while(isspace(buffer
[i
]))
141 AL_PRINT("config parse error: option without a value: \"%s\"\n", buffer
);
145 /* Find the start of the value */
147 while(isspace(buffer
[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)
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
));
165 AL_PRINT("config parse error: error reallocating config entries\n");
168 curBlock
->entries
= ent
;
169 ent
= curBlock
->entries
+ curBlock
->entryCount
;
170 curBlock
->entryCount
++;
172 ent
->key
= strdup(buffer
);
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);
181 while(buffer
[i
] && buffer
[i
] != '#' && buffer
[i
] != '\n')
185 } while(isspace(buffer
[i
]));
189 ent
->value
= strdup(buffer
);
191 // AL_PRINT("found '%s' = '%s'\n", ent->key, ent->value);
195 void ReadALConfig(void)
199 cfgBlocks
= calloc(1, sizeof(ConfigBlock
));
200 cfgBlocks
->name
= strdup("general");
205 f
= fopen("/etc/openal/alsoft.conf", "r");
208 f
= fopen("/etc/openal/config", "r");
210 AL_PRINT("Reading /etc/openal/config; this file is deprecated\n"
211 "\tPlease rename it to /etc/openal/alsoft.conf\n");
215 LoadConfigFromFile(f
);
218 if(getenv("HOME") && *(getenv("HOME")))
220 snprintf(buffer
, sizeof(buffer
), "%s/.alsoftrc", getenv("HOME"));
221 f
= fopen(buffer
, "r");
224 snprintf(buffer
, sizeof(buffer
), "%s/.openalrc", getenv("HOME"));
225 f
= fopen(buffer
, "r");
227 AL_PRINT("Reading ~/.openalrc; this file is deprecated\n"
228 "\tPlease rename it to ~/.alsoftrc\n");
232 LoadConfigFromFile(f
);
239 void FreeALConfig(void)
243 for(i
= 0;i
< cfgCount
;i
++)
246 for(j
= 0;j
< cfgBlocks
[i
].entryCount
;j
++)
248 free(cfgBlocks
[i
].entries
[j
].key
);
249 free(cfgBlocks
[i
].entries
[j
].value
);
251 free(cfgBlocks
[i
].entries
);
252 free(cfgBlocks
[i
].name
);
259 const char *GetConfigValue(const char *blockName
, const char *keyName
, const char *def
)
266 blockName
= "general";
268 for(i
= 0;i
< cfgCount
;i
++)
270 if(strcasecmp(cfgBlocks
[i
].name
, blockName
) != 0)
273 for(j
= 0;j
< cfgBlocks
[i
].entryCount
;j
++)
275 if(strcasecmp(cfgBlocks
[i
].entries
[j
].key
, keyName
) == 0)
276 return cfgBlocks
[i
].entries
[j
].value
;
284 int GetConfigValueInt(const char *blockName
, const char *keyName
, int def
)
286 const char *val
= GetConfigValue(blockName
, keyName
, "");
288 if(!val
[0]) return def
;
289 return strtol(val
, NULL
, 0);
292 float GetConfigValueFloat(const char *blockName
, const char *keyName
, float def
)
294 const char *val
= GetConfigValue(blockName
, keyName
, "");
296 if(!val
[0]) return def
;
298 return strtof(val
, NULL
);
300 return (float)strtod(val
, NULL
);