annotate Alc/alcConfig.c @ 26:56f83c170251

adding hard-to-compile artifacts for other systems
author Robert McIntyre <rlm@mit.edu>
date Sat, 19 Nov 2011 19:17:37 -0700
parents f9476ff7637e
children
rev   line source
rlm@0 1 /**
rlm@0 2 * OpenAL cross platform audio library
rlm@0 3 * Copyright (C) 1999-2007 by authors.
rlm@0 4 * This library is free software; you can redistribute it and/or
rlm@0 5 * modify it under the terms of the GNU Library General Public
rlm@0 6 * License as published by the Free Software Foundation; either
rlm@0 7 * version 2 of the License, or (at your option) any later version.
rlm@0 8 *
rlm@0 9 * This library is distributed in the hope that it will be useful,
rlm@0 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
rlm@0 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
rlm@0 12 * Library General Public License for more details.
rlm@0 13 *
rlm@0 14 * You should have received a copy of the GNU Library General Public
rlm@0 15 * License along with this library; if not, write to the
rlm@0 16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
rlm@0 17 * Boston, MA 02111-1307, USA.
rlm@0 18 * Or go to http://www.gnu.org/copyleft/lgpl.html
rlm@0 19 */
rlm@0 20
rlm@0 21 #ifdef _WIN32
rlm@0 22 #ifdef __MINGW64__
rlm@0 23 #define _WIN32_IE 0x501
rlm@0 24 #else
rlm@0 25 #define _WIN32_IE 0x400
rlm@0 26 #endif
rlm@0 27 #endif
rlm@0 28
rlm@0 29 #include "config.h"
rlm@0 30
rlm@0 31 #include <stdlib.h>
rlm@0 32 #include <stdio.h>
rlm@0 33 #include <ctype.h>
rlm@0 34 #include <string.h>
rlm@0 35
rlm@0 36 #include "alMain.h"
rlm@0 37
rlm@0 38 #ifdef _WIN32_IE
rlm@0 39 #include <shlobj.h>
rlm@0 40 #endif
rlm@0 41
rlm@0 42 typedef struct ConfigEntry {
rlm@0 43 char *key;
rlm@0 44 char *value;
rlm@0 45 } ConfigEntry;
rlm@0 46
rlm@0 47 typedef struct ConfigBlock {
rlm@0 48 char *name;
rlm@0 49 ConfigEntry *entries;
rlm@0 50 size_t entryCount;
rlm@0 51 } ConfigBlock;
rlm@0 52
rlm@0 53 static ConfigBlock *cfgBlocks;
rlm@0 54 static size_t cfgCount;
rlm@0 55
rlm@0 56 static char buffer[1024];
rlm@0 57
rlm@0 58 static void LoadConfigFromFile(FILE *f)
rlm@0 59 {
rlm@0 60 ConfigBlock *curBlock = cfgBlocks;
rlm@0 61 ConfigEntry *ent;
rlm@0 62
rlm@0 63 while(fgets(buffer, sizeof(buffer), f))
rlm@0 64 {
rlm@0 65 size_t i = 0;
rlm@0 66
rlm@0 67 while(isspace(buffer[i]))
rlm@0 68 i++;
rlm@0 69 if(!buffer[i] || buffer[i] == '#')
rlm@0 70 continue;
rlm@0 71
rlm@0 72 memmove(buffer, buffer+i, strlen(buffer+i)+1);
rlm@0 73
rlm@0 74 if(buffer[0] == '[')
rlm@0 75 {
rlm@0 76 ConfigBlock *nextBlock;
rlm@0 77
rlm@0 78 i = 1;
rlm@0 79 while(buffer[i] && buffer[i] != ']')
rlm@0 80 i++;
rlm@0 81
rlm@0 82 if(!buffer[i])
rlm@0 83 {
rlm@0 84 ERR("config parse error: bad line \"%s\"\n", buffer);
rlm@0 85 continue;
rlm@0 86 }
rlm@0 87 buffer[i] = 0;
rlm@0 88
rlm@0 89 do {
rlm@0 90 i++;
rlm@0 91 if(buffer[i] && !isspace(buffer[i]))
rlm@0 92 {
rlm@0 93 if(buffer[i] != '#')
rlm@0 94 WARN("config warning: extra data after block: \"%s\"\n", buffer+i);
rlm@0 95 break;
rlm@0 96 }
rlm@0 97 } while(buffer[i]);
rlm@0 98
rlm@0 99 nextBlock = NULL;
rlm@0 100 for(i = 0;i < cfgCount;i++)
rlm@0 101 {
rlm@0 102 if(strcasecmp(cfgBlocks[i].name, buffer+1) == 0)
rlm@0 103 {
rlm@0 104 nextBlock = cfgBlocks+i;
rlm@0 105 TRACE("found block '%s'\n", nextBlock->name);
rlm@0 106 break;
rlm@0 107 }
rlm@0 108 }
rlm@0 109
rlm@0 110 if(!nextBlock)
rlm@0 111 {
rlm@0 112 nextBlock = realloc(cfgBlocks, (cfgCount+1)*sizeof(ConfigBlock));
rlm@0 113 if(!nextBlock)
rlm@0 114 {
rlm@0 115 ERR("config parse error: error reallocating config blocks\n");
rlm@0 116 continue;
rlm@0 117 }
rlm@0 118 cfgBlocks = nextBlock;
rlm@0 119 nextBlock = cfgBlocks+cfgCount;
rlm@0 120 cfgCount++;
rlm@0 121
rlm@0 122 nextBlock->name = strdup(buffer+1);
rlm@0 123 nextBlock->entries = NULL;
rlm@0 124 nextBlock->entryCount = 0;
rlm@0 125
rlm@0 126 TRACE("found new block '%s'\n", nextBlock->name);
rlm@0 127 }
rlm@0 128 curBlock = nextBlock;
rlm@0 129 continue;
rlm@0 130 }
rlm@0 131
rlm@0 132 /* Look for the option name */
rlm@0 133 i = 0;
rlm@0 134 while(buffer[i] && buffer[i] != '#' && buffer[i] != '=' &&
rlm@0 135 !isspace(buffer[i]))
rlm@0 136 i++;
rlm@0 137
rlm@0 138 if(!buffer[i] || buffer[i] == '#' || i == 0)
rlm@0 139 {
rlm@0 140 ERR("config parse error: malformed option line: \"%s\"\n", buffer);
rlm@0 141 continue;
rlm@0 142 }
rlm@0 143
rlm@0 144 /* Seperate the option */
rlm@0 145 if(buffer[i] != '=')
rlm@0 146 {
rlm@0 147 buffer[i++] = 0;
rlm@0 148
rlm@0 149 while(isspace(buffer[i]))
rlm@0 150 i++;
rlm@0 151 if(buffer[i] != '=')
rlm@0 152 {
rlm@0 153 ERR("config parse error: option without a value: \"%s\"\n", buffer);
rlm@0 154 continue;
rlm@0 155 }
rlm@0 156 }
rlm@0 157 /* Find the start of the value */
rlm@0 158 buffer[i++] = 0;
rlm@0 159 while(isspace(buffer[i]))
rlm@0 160 i++;
rlm@0 161
rlm@0 162 /* Check if we already have this option set */
rlm@0 163 ent = curBlock->entries;
rlm@0 164 while((size_t)(ent-curBlock->entries) < curBlock->entryCount)
rlm@0 165 {
rlm@0 166 if(strcasecmp(ent->key, buffer) == 0)
rlm@0 167 break;
rlm@0 168 ent++;
rlm@0 169 }
rlm@0 170
rlm@0 171 if((size_t)(ent-curBlock->entries) >= curBlock->entryCount)
rlm@0 172 {
rlm@0 173 /* Allocate a new option entry */
rlm@0 174 ent = realloc(curBlock->entries, (curBlock->entryCount+1)*sizeof(ConfigEntry));
rlm@0 175 if(!ent)
rlm@0 176 {
rlm@0 177 ERR("config parse error: error reallocating config entries\n");
rlm@0 178 continue;
rlm@0 179 }
rlm@0 180 curBlock->entries = ent;
rlm@0 181 ent = curBlock->entries + curBlock->entryCount;
rlm@0 182 curBlock->entryCount++;
rlm@0 183
rlm@0 184 ent->key = strdup(buffer);
rlm@0 185 ent->value = NULL;
rlm@0 186 }
rlm@0 187
rlm@0 188 /* Look for the end of the line (Null term, new-line, or #-symbol) and
rlm@0 189 eat up the trailing whitespace */
rlm@0 190 memmove(buffer, buffer+i, strlen(buffer+i)+1);
rlm@0 191
rlm@0 192 i = 0;
rlm@0 193 while(buffer[i] && buffer[i] != '#' && buffer[i] != '\n')
rlm@0 194 i++;
rlm@0 195 do {
rlm@0 196 i--;
rlm@0 197 } while(isspace(buffer[i]));
rlm@0 198 buffer[++i] = 0;
rlm@0 199
rlm@0 200 free(ent->value);
rlm@0 201 ent->value = strdup(buffer);
rlm@0 202
rlm@0 203 TRACE("found '%s' = '%s'\n", ent->key, ent->value);
rlm@0 204 }
rlm@0 205 }
rlm@0 206
rlm@0 207 void ReadALConfig(void)
rlm@0 208 {
rlm@0 209 const char *str;
rlm@0 210 FILE *f;
rlm@0 211
rlm@0 212 cfgBlocks = calloc(1, sizeof(ConfigBlock));
rlm@0 213 cfgBlocks->name = strdup("general");
rlm@0 214 cfgCount = 1;
rlm@0 215
rlm@0 216 #ifdef _WIN32
rlm@0 217 if(SHGetSpecialFolderPathA(NULL, buffer, CSIDL_APPDATA, FALSE) != FALSE)
rlm@0 218 {
rlm@0 219 size_t p = strlen(buffer);
rlm@0 220 snprintf(buffer+p, sizeof(buffer)-p, "\\alsoft.ini");
rlm@0 221 f = fopen(buffer, "rt");
rlm@0 222 if(f)
rlm@0 223 {
rlm@0 224 LoadConfigFromFile(f);
rlm@0 225 fclose(f);
rlm@0 226 }
rlm@0 227 }
rlm@0 228 #else
rlm@0 229 f = fopen("/etc/openal/alsoft.conf", "r");
rlm@0 230 if(f)
rlm@0 231 {
rlm@0 232 LoadConfigFromFile(f);
rlm@0 233 fclose(f);
rlm@0 234 }
rlm@0 235 if((str=getenv("HOME")) != NULL && *str)
rlm@0 236 {
rlm@0 237 snprintf(buffer, sizeof(buffer), "%s/.alsoftrc", str);
rlm@0 238 f = fopen(buffer, "r");
rlm@0 239 if(f)
rlm@0 240 {
rlm@0 241 LoadConfigFromFile(f);
rlm@0 242 fclose(f);
rlm@0 243 }
rlm@0 244 }
rlm@0 245 #endif
rlm@0 246 if((str=getenv("ALSOFT_CONF")) != NULL && *str)
rlm@0 247 {
rlm@0 248 f = fopen(str, "r");
rlm@0 249 if(f)
rlm@0 250 {
rlm@0 251 LoadConfigFromFile(f);
rlm@0 252 fclose(f);
rlm@0 253 }
rlm@0 254 }
rlm@0 255 }
rlm@0 256
rlm@0 257 void FreeALConfig(void)
rlm@0 258 {
rlm@0 259 size_t i;
rlm@0 260
rlm@0 261 for(i = 0;i < cfgCount;i++)
rlm@0 262 {
rlm@0 263 size_t j;
rlm@0 264 for(j = 0;j < cfgBlocks[i].entryCount;j++)
rlm@0 265 {
rlm@0 266 free(cfgBlocks[i].entries[j].key);
rlm@0 267 free(cfgBlocks[i].entries[j].value);
rlm@0 268 }
rlm@0 269 free(cfgBlocks[i].entries);
rlm@0 270 free(cfgBlocks[i].name);
rlm@0 271 }
rlm@0 272 free(cfgBlocks);
rlm@0 273 cfgBlocks = NULL;
rlm@0 274 cfgCount = 0;
rlm@0 275 }
rlm@0 276
rlm@0 277 const char *GetConfigValue(const char *blockName, const char *keyName, const char *def)
rlm@0 278 {
rlm@0 279 size_t i, j;
rlm@0 280
rlm@0 281 if(!keyName)
rlm@0 282 return def;
rlm@0 283
rlm@0 284 if(!blockName)
rlm@0 285 blockName = "general";
rlm@0 286
rlm@0 287 for(i = 0;i < cfgCount;i++)
rlm@0 288 {
rlm@0 289 if(strcasecmp(cfgBlocks[i].name, blockName) != 0)
rlm@0 290 continue;
rlm@0 291
rlm@0 292 for(j = 0;j < cfgBlocks[i].entryCount;j++)
rlm@0 293 {
rlm@0 294 if(strcasecmp(cfgBlocks[i].entries[j].key, keyName) == 0)
rlm@0 295 {
rlm@0 296 TRACE("Found %s:%s = \"%s\"\n", blockName, keyName,
rlm@0 297 cfgBlocks[i].entries[j].value);
rlm@0 298 if(cfgBlocks[i].entries[j].value[0])
rlm@0 299 return cfgBlocks[i].entries[j].value;
rlm@0 300 return def;
rlm@0 301 }
rlm@0 302 }
rlm@0 303 }
rlm@0 304
rlm@0 305 TRACE("Key %s:%s not found\n", blockName, keyName);
rlm@0 306 return def;
rlm@0 307 }
rlm@0 308
rlm@0 309 int ConfigValueExists(const char *blockName, const char *keyName)
rlm@0 310 {
rlm@0 311 const char *val = GetConfigValue(blockName, keyName, "");
rlm@0 312 return !!val[0];
rlm@0 313 }
rlm@0 314
rlm@0 315 int GetConfigValueInt(const char *blockName, const char *keyName, int def)
rlm@0 316 {
rlm@0 317 const char *val = GetConfigValue(blockName, keyName, "");
rlm@0 318
rlm@0 319 if(!val[0]) return def;
rlm@0 320 return strtol(val, NULL, 0);
rlm@0 321 }
rlm@0 322
rlm@0 323 float GetConfigValueFloat(const char *blockName, const char *keyName, float def)
rlm@0 324 {
rlm@0 325 const char *val = GetConfigValue(blockName, keyName, "");
rlm@0 326
rlm@0 327 if(!val[0]) return def;
rlm@0 328 #ifdef HAVE_STRTOF
rlm@0 329 return strtof(val, NULL);
rlm@0 330 #else
rlm@0 331 return (float)strtod(val, NULL);
rlm@0 332 #endif
rlm@0 333 }
rlm@0 334
rlm@0 335 int GetConfigValueBool(const char *blockName, const char *keyName, int def)
rlm@0 336 {
rlm@0 337 const char *val = GetConfigValue(blockName, keyName, "");
rlm@0 338
rlm@0 339 if(!val[0]) return !!def;
rlm@0 340 return (strcasecmp(val, "true") == 0 || strcasecmp(val, "yes") == 0 ||
rlm@0 341 strcasecmp(val, "on") == 0 || atoi(val) != 0);
rlm@0 342 }