summaryrefslogtreecommitdiffstats
path: root/Alc/alcConfig.c
blob: b6e4613964769e61b7f399c469c50e1616b14098 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/**
 * 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
#define _WIN32_IE 0x400
#endif

#include "config.h"

#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>

#include "alMain.h"

#ifdef _WIN32_IE
#include <shlobj.h>
#endif

typedef struct ConfigEntry {
    char *key;
    char *value;
} ConfigEntry;

typedef struct ConfigBlock {
    char *name;
    ConfigEntry *entries;
    size_t entryCount;
} ConfigBlock;

static ConfigBlock *cfgBlocks;
static size_t cfgCount;

static char buffer[1024];

static void LoadConfigFromFile(FILE *f)
{
    ConfigBlock *curBlock = cfgBlocks;
    ConfigEntry *ent;

    while(fgets(buffer, sizeof(buffer), f))
    {
        size_t i = 0;

        while(isspace(buffer[i]))
            i++;
        if(!buffer[i] || buffer[i] == '#')
            continue;

        memmove(buffer, buffer+i, strlen(buffer+i)+1);

        if(buffer[0] == '[')
        {
            ConfigBlock *nextBlock;

            i = 1;
            while(buffer[i] && buffer[i] != ']')
                i++;

            if(!buffer[i])
            {
                 AL_PRINT("config parse error: bad line \"%s\"\n", buffer);
                 continue;
            }
            buffer[i] = 0;

            do {
                i++;
                if(buffer[i] && !isspace(buffer[i]))
                {
                    if(buffer[i] != '#')
                        AL_PRINT("config warning: extra data after block: \"%s\"\n", buffer+i);
                    break;
                }
            } while(buffer[i]);

            nextBlock = NULL;
            for(i = 0;i < cfgCount;i++)
            {
                if(strcasecmp(cfgBlocks[i].name, buffer+1) == 0)
                {
                    nextBlock = cfgBlocks+i;
//                    AL_PRINT("found block '%s'\n", nextBlock->name);
                    break;
                }
            }

            if(!nextBlock)
            {
                nextBlock = realloc(cfgBlocks, (cfgCount+1)*sizeof(ConfigBlock));
                if(!nextBlock)
                {
                     AL_PRINT("config parse error: error reallocating config blocks\n");
                     continue;
                }
                cfgBlocks = nextBlock;
                nextBlock = cfgBlocks+cfgCount;
                cfgCount++;

                nextBlock->name = strdup(buffer+1);
                nextBlock->entries = NULL;
                nextBlock->entryCount = 0;

//                AL_PRINT("found new block '%s'\n", nextBlock->name);
            }
            curBlock = nextBlock;
            continue;
        }

        /* Look for the option name */
        i = 0;
        while(buffer[i] && buffer[i] != '#' && buffer[i] != '=' &&
              !isspace(buffer[i]))
            i++;

        if(!buffer[i] || buffer[i] == '#' || i == 0)
        {
            AL_PRINT("config parse error: malformed option line: \"%s\"\n", buffer);
            continue;
        }

        /* Seperate the option */
        if(buffer[i] != '=')
        {
            buffer[i++] = 0;

            while(isspace(buffer[i]))
                i++;
            if(buffer[i] != '=')
            {
                AL_PRINT("config parse error: option without a value: \"%s\"\n", buffer);
                continue;
            }
        }
        /* Find the start of the value */
        buffer[i++] = 0;
        while(isspace(buffer[i]))
            i++;

        /* Check if we already have this option set */
        ent = curBlock->entries;
        while((size_t)(ent-curBlock->entries) < curBlock->entryCount)
        {
            if(strcasecmp(ent->key, buffer) == 0)
                break;
            ent++;
        }

        if((size_t)(ent-curBlock->entries) >= curBlock->entryCount)
        {
            /* Allocate a new option entry */
            ent = realloc(curBlock->entries, (curBlock->entryCount+1)*sizeof(ConfigEntry));
            if(!ent)
            {
                 AL_PRINT("config parse error: error reallocating config entries\n");
                 continue;
            }
            curBlock->entries = ent;
            ent = curBlock->entries + curBlock->entryCount;
            curBlock->entryCount++;

            ent->key = strdup(buffer);
            ent->value = NULL;
        }

        /* Look for the end of the line (Null term, new-line, or #-symbol) and
           eat up the trailing whitespace */
        memmove(buffer, buffer+i, strlen(buffer+i)+1);

        i = 0;
        while(buffer[i] && buffer[i] != '#' && buffer[i] != '\n')
            i++;
        do {
            i--;
        } while(isspace(buffer[i]));
        buffer[++i] = 0;

        free(ent->value);
        ent->value = strdup(buffer);

//        AL_PRINT("found '%s' = '%s'\n", ent->key, ent->value);
    }
}

void ReadALConfig(void)
{
    FILE *f;

    cfgBlocks = calloc(1, sizeof(ConfigBlock));
    cfgBlocks->name = strdup("general");
    cfgCount = 1;

#ifdef _WIN32
    if(SHGetSpecialFolderPathA(NULL, buffer, CSIDL_APPDATA, FALSE) != FALSE)
    {
        int p = strlen(buffer);
        snprintf(buffer+p, sizeof(buffer)-p, "\\alsoft.ini");
        f = fopen(buffer, "rt");
        if(f)
        {
            LoadConfigFromFile(f);
            fclose(f);
        }
    }
#else
    f = fopen("/etc/openal/alsoft.conf", "r");
    if(f)
    {
        LoadConfigFromFile(f);
        fclose(f);
    }
    if(getenv("HOME") && *(getenv("HOME")))
    {
        snprintf(buffer, sizeof(buffer), "%s/.alsoftrc", getenv("HOME"));
        f = fopen(buffer, "r");
        if(f)
        {
            LoadConfigFromFile(f);
            fclose(f);
        }
    }
#endif
    if(getenv("ALSOFT_CONF"))
    {
        f = fopen(getenv("ALSOFT_CONF"), "r");
        if(f)
        {
            LoadConfigFromFile(f);
            fclose(f);
        }
    }
}

void FreeALConfig(void)
{
    size_t i;

    for(i = 0;i < cfgCount;i++)
    {
        size_t j;
        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)
{
    size_t i, j;

    if(keyName)
    {
        if(!blockName)
            blockName = "general";

        for(i = 0;i < cfgCount;i++)
        {
            if(strcasecmp(cfgBlocks[i].name, blockName) != 0)
                continue;

            for(j = 0;j < cfgBlocks[i].entryCount;j++)
            {
                if(strcasecmp(cfgBlocks[i].entries[j].key, keyName) == 0)
                    return cfgBlocks[i].entries[j].value;
            }
        }
    }

    return def;
}

int GetConfigValueInt(const char *blockName, const char *keyName, int def)
{
    const char *val = GetConfigValue(blockName, keyName, "");

    if(!val[0]) return def;
    return strtol(val, NULL, 0);
}

float GetConfigValueFloat(const char *blockName, const char *keyName, float def)
{
    const char *val = GetConfigValue(blockName, keyName, "");

    if(!val[0]) return def;
#ifdef HAVE_STRTOF
    return strtof(val, NULL);
#else
    return (float)strtod(val, NULL);
#endif
}