openal-soft/Alc/alcConfig.c

354 lines
8.9 KiB
C
Raw Normal View History

2007-11-13 18:02:18 -08:00
/**
* OpenAL cross platform audio library
* Copyright (C) 1999-2007 by authors.
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
* Or go to http://www.gnu.org/copyleft/lgpl.html
*/
#ifdef _WIN32
#ifdef __MINGW32__
#define _WIN32_IE 0x501
#else
#define _WIN32_IE 0x400
#endif
#endif
2008-01-16 14:09:04 -08:00
#include "config.h"
2007-11-13 18:02:18 -08:00
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include "alMain.h"
#ifdef _WIN32_IE
#include <shlobj.h>
#endif
2007-11-13 18:02:18 -08:00
typedef struct ConfigEntry {
char *key;
char *value;
} ConfigEntry;
typedef struct ConfigBlock {
char *name;
ConfigEntry *entries;
unsigned int entryCount;
2007-11-13 18:02:18 -08:00
} ConfigBlock;
static ConfigBlock *cfgBlocks;
static unsigned int cfgCount;
2007-11-13 18:02:18 -08:00
static char buffer[1024];
static char *lstrip(char *line)
{
while(isspace(line[0]))
line++;
return line;
}
static char *rstrip(char *line)
{
size_t len = strlen(line);
while(len > 0 && isspace(line[len-1]))
len--;
line[len] = 0;
return line;
}
2007-11-13 18:02:18 -08:00
static void LoadConfigFromFile(FILE *f)
{
ConfigBlock *curBlock = cfgBlocks;
ConfigEntry *ent;
while(fgets(buffer, sizeof(buffer), f))
{
char *line, *comment;
char key[256] = "";
char value[256] = "";
2007-11-13 18:02:18 -08:00
comment = strchr(buffer, '#');
if(comment)
{
*(comment++) = 0;
comment = rstrip(lstrip(comment));
}
2007-11-13 18:02:18 -08:00
line = rstrip(lstrip(buffer));
if(!line[0])
continue;
2007-11-13 18:02:18 -08:00
if(line[0] == '[')
2007-11-13 18:02:18 -08:00
{
ConfigBlock *nextBlock;
char *section = line+1;
char *endsection;
unsigned int i;
2007-11-13 18:02:18 -08:00
endsection = strchr(section, ']');
if(!endsection || section == endsection || endsection[1] != 0)
2007-11-13 18:02:18 -08:00
{
ERR("config parse error: bad line \"%s\"\n", line);
2007-11-13 18:02:18 -08:00
continue;
}
*endsection = 0;
2007-11-13 18:02:18 -08:00
nextBlock = NULL;
for(i = 0;i < cfgCount;i++)
{
if(strcasecmp(cfgBlocks[i].name, section) == 0)
2007-11-13 18:02:18 -08:00
{
nextBlock = cfgBlocks+i;
2011-07-10 21:44:42 -07:00
TRACE("found block '%s'\n", nextBlock->name);
2007-11-13 18:02:18 -08:00
break;
}
}
if(!nextBlock)
{
nextBlock = realloc(cfgBlocks, (cfgCount+1)*sizeof(ConfigBlock));
if(!nextBlock)
{
2011-07-13 01:43:00 -07:00
ERR("config parse error: error reallocating config blocks\n");
2007-11-13 18:02:18 -08:00
continue;
}
cfgBlocks = nextBlock;
nextBlock = cfgBlocks+cfgCount;
cfgCount++;
nextBlock->name = strdup(section);
2007-11-13 18:02:18 -08:00
nextBlock->entries = NULL;
nextBlock->entryCount = 0;
2011-07-10 21:44:42 -07:00
TRACE("found new block '%s'\n", nextBlock->name);
2007-11-13 18:02:18 -08:00
}
curBlock = nextBlock;
continue;
}
if(sscanf(line, "%255[^=] = \"%255[^\"]\"", key, value) == 2 ||
sscanf(line, "%255[^=] = '%255[^\']'", key, value) == 2 ||
sscanf(line, "%255[^=] = %255[^\n]", key, value) == 2)
2007-11-13 18:02:18 -08:00
{
/* sscanf doesn't handle '' or "" as empty values, so clip it
* manually. */
if(strcmp(value, "\"\"") == 0 || strcmp(value, "''") == 0)
value[0] = 0;
}
else if(sscanf(line, "%255[^=] %255[=]", key, value) == 2)
{
/* Special case for 'key =' */
value[0] = 0;
}
else
{
ERR("config parse error: malformed option line: \"%s\"\n\n", line);
continue;
}
rstrip(key);
2007-11-13 18:02:18 -08:00
/* Check if we already have this option set */
ent = curBlock->entries;
while((unsigned int)(ent-curBlock->entries) < curBlock->entryCount)
2007-11-13 18:02:18 -08:00
{
if(strcasecmp(ent->key, key) == 0)
2007-11-13 18:02:18 -08:00
break;
ent++;
}
if((unsigned int)(ent-curBlock->entries) >= curBlock->entryCount)
2007-11-13 18:02:18 -08:00
{
/* Allocate a new option entry */
ent = realloc(curBlock->entries, (curBlock->entryCount+1)*sizeof(ConfigEntry));
if(!ent)
{
2011-07-13 01:43:00 -07:00
ERR("config parse error: error reallocating config entries\n");
2007-11-13 18:02:18 -08:00
continue;
}
curBlock->entries = ent;
ent = curBlock->entries + curBlock->entryCount;
curBlock->entryCount++;
ent->key = strdup(key);
2007-11-13 18:02:18 -08:00
ent->value = NULL;
}
free(ent->value);
ent->value = strdup(value);
2007-11-13 18:02:18 -08:00
2011-07-10 21:44:42 -07:00
TRACE("found '%s' = '%s'\n", ent->key, ent->value);
2007-11-13 18:02:18 -08:00
}
}
void ReadALConfig(void)
{
const char *str;
2007-11-13 18:02:18 -08:00
FILE *f;
cfgBlocks = calloc(1, sizeof(ConfigBlock));
cfgBlocks->name = strdup("general");
cfgCount = 1;
#ifdef _WIN32
if(SHGetSpecialFolderPathA(NULL, buffer, CSIDL_APPDATA, FALSE) != FALSE)
{
size_t p = strlen(buffer);
snprintf(buffer+p, sizeof(buffer)-p, "\\alsoft.ini");
f = fopen(buffer, "rt");
if(f)
{
LoadConfigFromFile(f);
fclose(f);
}
}
2007-11-13 18:02:18 -08:00
#else
2008-01-19 18:18:14 -08:00
f = fopen("/etc/openal/alsoft.conf", "r");
2007-11-13 18:02:18 -08:00
if(f)
{
LoadConfigFromFile(f);
fclose(f);
}
if((str=getenv("HOME")) != NULL && *str)
2007-11-13 18:02:18 -08:00
{
snprintf(buffer, sizeof(buffer), "%s/.alsoftrc", str);
2007-11-13 18:02:18 -08:00
f = fopen(buffer, "r");
if(f)
{
LoadConfigFromFile(f);
fclose(f);
}
}
#endif
if((str=getenv("ALSOFT_CONF")) != NULL && *str)
{
f = fopen(str, "r");
if(f)
{
LoadConfigFromFile(f);
fclose(f);
}
}
2007-11-13 18:02:18 -08:00
}
void FreeALConfig(void)
{
unsigned int i;
2007-11-13 18:02:18 -08:00
for(i = 0;i < cfgCount;i++)
{
unsigned int j;
2007-11-13 18:02:18 -08:00
for(j = 0;j < cfgBlocks[i].entryCount;j++)
{
free(cfgBlocks[i].entries[j].key);
free(cfgBlocks[i].entries[j].value);
}
free(cfgBlocks[i].entries);
free(cfgBlocks[i].name);
}
free(cfgBlocks);
cfgBlocks = NULL;
cfgCount = 0;
}
const char *GetConfigValue(const char *blockName, const char *keyName, const char *def)
{
unsigned int i, j;
2007-11-13 18:02:18 -08:00
2010-05-12 07:30:45 -07:00
if(!keyName)
return def;
if(!blockName)
blockName = "general";
for(i = 0;i < cfgCount;i++)
2007-11-13 18:02:18 -08:00
{
2010-05-12 07:30:45 -07:00
if(strcasecmp(cfgBlocks[i].name, blockName) != 0)
continue;
2007-11-13 18:02:18 -08:00
2010-05-12 07:30:45 -07:00
for(j = 0;j < cfgBlocks[i].entryCount;j++)
2007-11-13 18:02:18 -08:00
{
2010-05-12 07:30:45 -07:00
if(strcasecmp(cfgBlocks[i].entries[j].key, keyName) == 0)
2007-11-13 18:02:18 -08:00
{
2011-07-10 21:44:42 -07:00
TRACE("Found %s:%s = \"%s\"\n", blockName, keyName,
cfgBlocks[i].entries[j].value);
2010-05-12 07:30:45 -07:00
if(cfgBlocks[i].entries[j].value[0])
return cfgBlocks[i].entries[j].value;
return def;
2007-11-13 18:02:18 -08:00
}
}
}
2011-07-10 21:44:42 -07:00
TRACE("Key %s:%s not found\n", blockName, keyName);
2007-11-13 18:02:18 -08:00
return def;
}
int ConfigValueExists(const char *blockName, const char *keyName)
{
const char *val = GetConfigValue(blockName, keyName, "");
return !!val[0];
}
int ConfigValueStr(const char *blockName, const char *keyName, const char **ret)
{
const char *val = GetConfigValue(blockName, keyName, "");
if(!val[0]) return 0;
*ret = val;
return 1;
}
int ConfigValueInt(const char *blockName, const char *keyName, int *ret)
2007-11-13 18:02:18 -08:00
{
const char *val = GetConfigValue(blockName, keyName, "");
if(!val[0]) return 0;
2007-11-13 18:02:18 -08:00
*ret = strtol(val, NULL, 0);
return 1;
2007-11-13 18:02:18 -08:00
}
int ConfigValueUInt(const char *blockName, const char *keyName, unsigned int *ret)
2007-11-13 18:02:18 -08:00
{
const char *val = GetConfigValue(blockName, keyName, "");
if(!val[0]) return 0;
*ret = strtoul(val, NULL, 0);
return 1;
}
int ConfigValueFloat(const char *blockName, const char *keyName, float *ret)
{
const char *val = GetConfigValue(blockName, keyName, "");
if(!val[0]) return 0;
2007-11-13 18:02:18 -08:00
#ifdef HAVE_STRTOF
*ret = strtof(val, NULL);
2007-11-13 18:02:18 -08:00
#else
*ret = (float)strtod(val, NULL);
2007-11-13 18:02:18 -08:00
#endif
return 1;
2007-11-13 18:02:18 -08:00
}
int GetConfigValueBool(const char *blockName, const char *keyName, int def)
{
const char *val = GetConfigValue(blockName, keyName, "");
if(!val[0]) return !!def;
return (strcasecmp(val, "true") == 0 || strcasecmp(val, "yes") == 0 ||
strcasecmp(val, "on") == 0 || atoi(val) != 0);
}