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
|
|
|
|
*/
|
|
|
|
|
2010-03-18 16:13:44 -04:00
|
|
|
#ifdef _WIN32
|
2012-09-11 01:39:05 -07:00
|
|
|
#ifdef __MINGW32__
|
2010-03-18 16:13:44 -04:00
|
|
|
#define _WIN32_IE 0x501
|
|
|
|
#else
|
2009-06-07 22:11:46 -04:00
|
|
|
#define _WIN32_IE 0x400
|
|
|
|
#endif
|
2010-03-18 16:13:44 -04:00
|
|
|
#endif
|
2009-06-07 22:11:46 -04:00
|
|
|
|
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>
|
|
|
|
|
2007-11-30 00:28:34 -08:00
|
|
|
#include "alMain.h"
|
|
|
|
|
2009-06-07 22:11:46 -04:00
|
|
|
#ifdef _WIN32_IE
|
2008-06-04 18:09:21 -07:00
|
|
|
#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;
|
2011-09-20 11:40:56 -07:00
|
|
|
unsigned int entryCount;
|
2007-11-13 18:02:18 -08:00
|
|
|
} ConfigBlock;
|
|
|
|
|
|
|
|
static ConfigBlock *cfgBlocks;
|
2011-09-20 11:40:56 -07:00
|
|
|
static unsigned int cfgCount;
|
2007-11-13 18:02:18 -08:00
|
|
|
|
|
|
|
static char buffer[1024];
|
|
|
|
|
2013-06-16 18:42:27 -07:00
|
|
|
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))
|
|
|
|
{
|
2013-06-16 18:42:27 -07:00
|
|
|
char *line, *comment;
|
|
|
|
char key[256] = "";
|
|
|
|
char value[256] = "";
|
2007-11-13 18:02:18 -08:00
|
|
|
|
2013-06-16 18:42:27 -07:00
|
|
|
comment = strchr(buffer, '#');
|
|
|
|
if(comment)
|
|
|
|
{
|
|
|
|
*(comment++) = 0;
|
|
|
|
comment = rstrip(lstrip(comment));
|
|
|
|
}
|
2007-11-13 18:02:18 -08:00
|
|
|
|
2013-06-16 18:42:27 -07:00
|
|
|
line = rstrip(lstrip(buffer));
|
|
|
|
if(!line[0])
|
|
|
|
continue;
|
2007-11-13 18:02:18 -08:00
|
|
|
|
2013-06-16 18:42:27 -07:00
|
|
|
if(line[0] == '[')
|
2007-11-13 18:02:18 -08:00
|
|
|
{
|
|
|
|
ConfigBlock *nextBlock;
|
2013-06-16 18:42:27 -07:00
|
|
|
char *section = line+1;
|
|
|
|
char *endsection;
|
2011-09-20 11:40:56 -07:00
|
|
|
unsigned int i;
|
2007-11-13 18:02:18 -08:00
|
|
|
|
2013-06-16 18:42:27 -07:00
|
|
|
endsection = strchr(section, ']');
|
|
|
|
if(!endsection || section == endsection || endsection[1] != 0)
|
2007-11-13 18:02:18 -08:00
|
|
|
{
|
2013-06-16 18:42:27 -07:00
|
|
|
ERR("config parse error: bad line \"%s\"\n", line);
|
2007-11-13 18:02:18 -08:00
|
|
|
continue;
|
|
|
|
}
|
2013-06-16 18:42:27 -07:00
|
|
|
*endsection = 0;
|
2007-11-13 18:02:18 -08:00
|
|
|
|
|
|
|
nextBlock = NULL;
|
|
|
|
for(i = 0;i < cfgCount;i++)
|
|
|
|
{
|
2013-06-16 18:42:27 -07:00
|
|
|
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++;
|
|
|
|
|
2013-06-16 18:42:27 -07:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-06-16 18:42:27 -07:00
|
|
|
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
|
|
|
{
|
2013-06-16 18:42:27 -07: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;
|
2011-09-20 11:40:56 -07:00
|
|
|
while((unsigned int)(ent-curBlock->entries) < curBlock->entryCount)
|
2007-11-13 18:02:18 -08:00
|
|
|
{
|
2013-06-16 18:42:27 -07:00
|
|
|
if(strcasecmp(ent->key, key) == 0)
|
2007-11-13 18:02:18 -08:00
|
|
|
break;
|
|
|
|
ent++;
|
|
|
|
}
|
|
|
|
|
2011-09-20 11:40:56 -07:00
|
|
|
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++;
|
|
|
|
|
2013-06-16 18:42:27 -07:00
|
|
|
ent->key = strdup(key);
|
2007-11-13 18:02:18 -08:00
|
|
|
ent->value = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(ent->value);
|
2013-06-16 18:42:27 -07:00
|
|
|
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)
|
|
|
|
{
|
2011-05-17 10:15:53 -07:00
|
|
|
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
|
2008-06-04 18:09:21 -07:00
|
|
|
if(SHGetSpecialFolderPathA(NULL, buffer, CSIDL_APPDATA, FALSE) != FALSE)
|
|
|
|
{
|
2010-03-18 14:21:06 -07:00
|
|
|
size_t p = strlen(buffer);
|
2008-06-04 18:09:21 -07:00
|
|
|
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);
|
|
|
|
}
|
2011-05-17 10:15:53 -07:00
|
|
|
if((str=getenv("HOME")) != NULL && *str)
|
2007-11-13 18:02:18 -08:00
|
|
|
{
|
2011-05-17 10:15:53 -07: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
|
2011-05-17 10:15:53 -07:00
|
|
|
if((str=getenv("ALSOFT_CONF")) != NULL && *str)
|
2008-11-13 07:58:39 -08:00
|
|
|
{
|
2011-05-17 10:15:53 -07:00
|
|
|
f = fopen(str, "r");
|
2008-11-13 07:58:39 -08:00
|
|
|
if(f)
|
|
|
|
{
|
|
|
|
LoadConfigFromFile(f);
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
}
|
2007-11-13 18:02:18 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void FreeALConfig(void)
|
|
|
|
{
|
2011-09-20 11:40:56 -07:00
|
|
|
unsigned int i;
|
2007-11-13 18:02:18 -08:00
|
|
|
|
|
|
|
for(i = 0;i < cfgCount;i++)
|
|
|
|
{
|
2011-09-20 11:40:56 -07:00
|
|
|
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)
|
|
|
|
{
|
2011-09-20 11:40:56 -07:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2009-12-28 13:08:15 -08:00
|
|
|
int ConfigValueExists(const char *blockName, const char *keyName)
|
|
|
|
{
|
|
|
|
const char *val = GetConfigValue(blockName, keyName, "");
|
|
|
|
return !!val[0];
|
|
|
|
}
|
|
|
|
|
2011-09-19 11:29:18 -07:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-09-18 16:16:55 -07:00
|
|
|
int ConfigValueInt(const char *blockName, const char *keyName, int *ret)
|
2007-11-13 18:02:18 -08:00
|
|
|
{
|
|
|
|
const char *val = GetConfigValue(blockName, keyName, "");
|
2011-09-18 16:16:55 -07:00
|
|
|
if(!val[0]) return 0;
|
2007-11-13 18:02:18 -08:00
|
|
|
|
2011-09-18 16:16:55 -07:00
|
|
|
*ret = strtol(val, NULL, 0);
|
|
|
|
return 1;
|
2007-11-13 18:02:18 -08:00
|
|
|
}
|
|
|
|
|
2011-09-18 16:16:55 -07: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, "");
|
2011-09-18 16:16:55 -07:00
|
|
|
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
|
2011-09-18 16:16:55 -07:00
|
|
|
*ret = strtof(val, NULL);
|
2007-11-13 18:02:18 -08:00
|
|
|
#else
|
2011-09-18 16:16:55 -07:00
|
|
|
*ret = (float)strtod(val, NULL);
|
2007-11-13 18:02:18 -08:00
|
|
|
#endif
|
2011-09-18 16:16:55 -07:00
|
|
|
return 1;
|
2007-11-13 18:02:18 -08:00
|
|
|
}
|
2009-09-15 22:18:13 -07:00
|
|
|
|
2009-12-26 07:42:57 -08:00
|
|
|
int GetConfigValueBool(const char *blockName, const char *keyName, int def)
|
2009-09-15 22:18:13 -07:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|