aboutsummaryrefslogtreecommitdiffstats
path: root/Alc/alcConfig.c
diff options
context:
space:
mode:
Diffstat (limited to 'Alc/alcConfig.c')
-rw-r--r--Alc/alcConfig.c121
1 files changed, 86 insertions, 35 deletions
diff --git a/Alc/alcConfig.c b/Alc/alcConfig.c
index 2c9aef41..6fc9db33 100644
--- a/Alc/alcConfig.c
+++ b/Alc/alcConfig.c
@@ -13,8 +13,8 @@
*
* 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.
+ * Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
* Or go to http://www.gnu.org/copyleft/lgpl.html
*/
@@ -33,11 +33,13 @@
#include <ctype.h>
#include <string.h>
#ifdef _WIN32_IE
+#include <windows.h>
#include <shlobj.h>
#endif
#include "alMain.h"
#include "compat.h"
+#include "bool.h"
typedef struct ConfigEntry {
@@ -137,13 +139,21 @@ static char *expdup(const char *str)
}
else
{
+ bool hasbraces;
char envname[1024];
size_t k = 0;
+ hasbraces = (*str == '{');
+ if(hasbraces) str++;
+
while((isalnum(*str) || *str == '_') && k < sizeof(envname)-1)
envname[k++] = *(str++);
envname[k++] = '\0';
+ if(hasbraces && *str != '}')
+ continue;
+
+ if(hasbraces) str++;
if((addstr=getenv(envname)) == NULL)
continue;
addstrlen = strlen(addstr);
@@ -192,12 +202,8 @@ static void LoadConfigFromFile(FILE *f)
char key[256] = "";
char value[256] = "";
- comment = strchr(buffer, '#');
- if(comment) *(comment++) = 0;
-
line = rstrip(lstrip(buffer));
- if(!line[0])
- continue;
+ if(!line[0]) continue;
if(line[0] == '[')
{
@@ -205,10 +211,21 @@ static void LoadConfigFromFile(FILE *f)
char *endsection;
endsection = strchr(section, ']');
- if(!endsection || section == endsection || endsection[1] != 0)
+ if(!endsection || section == endsection)
{
- ERR("config parse error: bad line \"%s\"\n", line);
- continue;
+ ERR("config parse error: bad line \"%s\"\n", line);
+ continue;
+ }
+ if(endsection[1] != 0)
+ {
+ char *end = endsection+1;
+ while(isspace(*end))
+ ++end;
+ if(*end != 0 && *end != '#')
+ {
+ ERR("config parse error: bad line \"%s\"\n", line);
+ continue;
+ }
}
*endsection = 0;
@@ -223,6 +240,10 @@ static void LoadConfigFromFile(FILE *f)
continue;
}
+ comment = strchr(line, '#');
+ if(comment) *(comment++) = 0;
+ if(!line[0]) continue;
+
if(sscanf(line, "%255[^=] = \"%255[^\"]\"", key, value) == 2 ||
sscanf(line, "%255[^=] = '%255[^\']'", key, value) == 2 ||
sscanf(line, "%255[^=] = %255[^\n]", key, value) == 2)
@@ -296,27 +317,33 @@ void ReadALConfig(void)
if(SHGetSpecialFolderPathW(NULL, buffer, CSIDL_APPDATA, FALSE) != FALSE)
{
- size_t p = lstrlenW(buffer);
- _snwprintf(buffer+p, PATH_MAX-p, L"\\alsoft.ini");
+ al_string filepath = AL_STRING_INIT_STATIC();
+ al_string_copy_wcstr(&filepath, buffer);
+ al_string_append_cstr(&filepath, "\\alsoft.ini");
- TRACE("Loading config %ls...\n", buffer);
- f = _wfopen(buffer, L"rt");
+ TRACE("Loading config %s...\n", al_string_get_cstr(filepath));
+ f = al_fopen(al_string_get_cstr(filepath), "rt");
if(f)
{
LoadConfigFromFile(f);
fclose(f);
}
+ al_string_deinit(&filepath);
}
if((str=_wgetenv(L"ALSOFT_CONF")) != NULL && *str)
{
- TRACE("Loading config %ls...\n", str);
- f = _wfopen(str, L"rt");
+ al_string filepath = AL_STRING_INIT_STATIC();
+ al_string_copy_wcstr(&filepath, str);
+
+ TRACE("Loading config %s...\n", al_string_get_cstr(filepath));
+ f = al_fopen(al_string_get_cstr(filepath), "rt");
if(f)
{
LoadConfigFromFile(f);
fclose(f);
}
+ al_string_deinit(&filepath);
}
}
#else
@@ -428,7 +455,7 @@ void FreeALConfig(void)
free(cfgBlock.entries);
}
-const char *GetConfigValue(const char *blockName, const char *keyName, const char *def)
+const char *GetConfigValue(const char *devName, const char *blockName, const char *keyName, const char *def)
{
unsigned int i;
char key[256];
@@ -437,16 +464,26 @@ const char *GetConfigValue(const char *blockName, const char *keyName, const cha
return def;
if(blockName && strcasecmp(blockName, "general") != 0)
- snprintf(key, sizeof(key), "%s/%s", blockName, keyName);
+ {
+ if(devName)
+ snprintf(key, sizeof(key), "%s/%s/%s", blockName, devName, keyName);
+ else
+ snprintf(key, sizeof(key), "%s/%s", blockName, keyName);
+ }
else
{
- strncpy(key, keyName, sizeof(key)-1);
- key[sizeof(key)-1] = 0;
+ if(devName)
+ snprintf(key, sizeof(key), "%s/%s", devName, keyName);
+ else
+ {
+ strncpy(key, keyName, sizeof(key)-1);
+ key[sizeof(key)-1] = 0;
+ }
}
for(i = 0;i < cfgBlock.entryCount;i++)
{
- if(strcasecmp(cfgBlock.entries[i].key, key) == 0)
+ if(strcmp(cfgBlock.entries[i].key, key) == 0)
{
TRACE("Found %s = \"%s\"\n", key, cfgBlock.entries[i].value);
if(cfgBlock.entries[i].value[0])
@@ -455,46 +492,50 @@ const char *GetConfigValue(const char *blockName, const char *keyName, const cha
}
}
- TRACE("Key %s not found\n", key);
- return def;
+ if(!devName)
+ {
+ TRACE("Key %s not found\n", key);
+ return def;
+ }
+ return GetConfigValue(NULL, blockName, keyName, def);
}
-int ConfigValueExists(const char *blockName, const char *keyName)
+int ConfigValueExists(const char *devName, const char *blockName, const char *keyName)
{
- const char *val = GetConfigValue(blockName, keyName, "");
+ const char *val = GetConfigValue(devName, blockName, keyName, "");
return !!val[0];
}
-int ConfigValueStr(const char *blockName, const char *keyName, const char **ret)
+int ConfigValueStr(const char *devName, const char *blockName, const char *keyName, const char **ret)
{
- const char *val = GetConfigValue(blockName, keyName, "");
+ const char *val = GetConfigValue(devName, blockName, keyName, "");
if(!val[0]) return 0;
*ret = val;
return 1;
}
-int ConfigValueInt(const char *blockName, const char *keyName, int *ret)
+int ConfigValueInt(const char *devName, const char *blockName, const char *keyName, int *ret)
{
- const char *val = GetConfigValue(blockName, keyName, "");
+ const char *val = GetConfigValue(devName, blockName, keyName, "");
if(!val[0]) return 0;
*ret = strtol(val, NULL, 0);
return 1;
}
-int ConfigValueUInt(const char *blockName, const char *keyName, unsigned int *ret)
+int ConfigValueUInt(const char *devName, const char *blockName, const char *keyName, unsigned int *ret)
{
- const char *val = GetConfigValue(blockName, keyName, "");
+ const char *val = GetConfigValue(devName, blockName, keyName, "");
if(!val[0]) return 0;
*ret = strtoul(val, NULL, 0);
return 1;
}
-int ConfigValueFloat(const char *blockName, const char *keyName, float *ret)
+int ConfigValueFloat(const char *devName, const char *blockName, const char *keyName, float *ret)
{
- const char *val = GetConfigValue(blockName, keyName, "");
+ const char *val = GetConfigValue(devName, blockName, keyName, "");
if(!val[0]) return 0;
#ifdef HAVE_STRTOF
@@ -505,9 +546,19 @@ int ConfigValueFloat(const char *blockName, const char *keyName, float *ret)
return 1;
}
-int GetConfigValueBool(const char *blockName, const char *keyName, int def)
+int ConfigValueBool(const char *devName, const char *blockName, const char *keyName, int *ret)
+{
+ const char *val = GetConfigValue(devName, blockName, keyName, "");
+ if(!val[0]) return 0;
+
+ *ret = (strcasecmp(val, "true") == 0 || strcasecmp(val, "yes") == 0 ||
+ strcasecmp(val, "on") == 0 || atoi(val) != 0);
+ return 1;
+}
+
+int GetConfigValueBool(const char *devName, const char *blockName, const char *keyName, int def)
{
- const char *val = GetConfigValue(blockName, keyName, "");
+ const char *val = GetConfigValue(devName, blockName, keyName, "");
if(!val[0]) return !!def;
return (strcasecmp(val, "true") == 0 || strcasecmp(val, "yes") == 0 ||