1
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2
* Mupen64plus - config.c *
3
* Mupen64Plus homepage: http://code.google.com/p/mupen64plus/ *
4
* Copyright (C) 2002 Blight *
6
* This program is free software; you can redistribute it and/or modify *
7
* it under the terms of the GNU General Public License as published by *
8
* the Free Software Foundation; either version 2 of the License, or *
9
* (at your option) any later version. *
11
* This program is distributed in the hope that it will be useful, *
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14
* GNU General Public License for more details. *
16
* You should have received a copy of the GNU General Public License *
17
* along with this program; if not, write to the *
18
* Free Software Foundation, Inc., *
19
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
20
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
32
#include "translate.h"
34
typedef struct _SConfigValue
36
char *key; // key - string
37
char *cValue; // value - string
38
int iValue; // value - integer
39
int bValue; // value - bool
42
typedef struct _SConfigSection
48
list_t m_config = NULL; // list of config sections
49
SConfigSection *m_configSection = 0; // currently selected section
53
static SConfigValue * config_findValue( const char *key )
59
config_set_section("Default");
61
list_foreach(m_configSection->values, node)
63
val = (SConfigValue *)node->data;
65
if (strcasecmp(key, val->key) == 0)
72
static SConfigSection * config_findSection( const char *section )
77
list_foreach(m_config, node)
79
sec = (SConfigSection *)node->data;
81
if (strcasecmp( section, sec->name ) == 0)
91
int config_set_section( const char *section )
93
SConfigSection *sec = config_findSection( section );
97
sec = malloc(sizeof(SConfigSection));
101
sec->name = strdup( section );
103
list_append(&m_config, sec);
105
m_configSection = sec;
110
const char * config_get_string( const char *key, const char *def )
112
SConfigValue *val = config_findValue( key );
116
config_put_string(key, def);
123
int config_get_number( const char *key, int def )
125
SConfigValue *val = config_findValue( key );
128
config_put_number(key, def);
135
int config_get_bool( const char *key, int def )
137
SConfigValue *val = config_findValue( key );
140
config_put_bool(key, def);
148
void config_put_string( const char *key, const char *value )
150
SConfigValue *val = config_findValue( key );
153
val = malloc(sizeof(SConfigValue));
155
memset(val, 0, sizeof(SConfigValue));
156
val->key = strdup(key);
157
list_append(&(m_configSection->values), val);
162
val->cValue = strdup( value );
163
val->iValue = atoi(val->cValue);
164
val->bValue = val->iValue;
165
if (strcasecmp(val->cValue, "yes") == 0)
167
else if (strcasecmp(val->cValue, "true") == 0)
171
void config_put_number( const char *key, int value )
174
snprintf( buf, 50, "%d", value );
175
config_put_string( key, buf );
178
void config_put_bool( const char *key, int value )
180
config_put_string( key, (value != 0) ? ("true") : ("false") );
183
void config_read( void )
186
char filename[PATH_MAX];
191
config_set_section( "Default" );
193
snprintf( filename, PATH_MAX, "%smupen64plus.conf", get_configpath() );
194
f = fopen( filename, "r" );
197
printf( "%s: %s\n", filename, strerror( errno ) );
203
if( !fgets( line, 2048, f ) )
207
linelen = strlen( line );
208
if (line[0] == '#') // comment
211
if (line[0] == '[' && line[linelen-1] == ']')
213
line[linelen-1] = '\0';
214
config_set_section( line+1 );
218
p = strchr( line, '=' );
225
config_put_string( line, p );
231
void config_write( void )
234
char filename[PATH_MAX];
235
list_node_t *secNode,
240
snprintf( filename, PATH_MAX, "%smupen64plus.conf", get_configpath() );
241
f = fopen( filename, "w" );
245
// for each config section
246
list_foreach(m_config, secNode)
248
sec = (SConfigSection *)secNode->data;
250
fprintf(f, "[%s]\n", sec->name);
252
// for each config value
253
list_foreach(sec->values, valNode)
255
val = (SConfigValue *)valNode->data;
256
fprintf(f, "%s = %s\n", val->key, val->cValue);
264
void config_delete(void)
266
list_node_t *secNode,
271
m_configSection = NULL;
273
// for each config section
274
list_foreach(m_config, secNode)
276
sec = (SConfigSection *)secNode->data;
278
// for each config value
279
list_foreach(sec->values, valNode)
281
val = (SConfigValue *)valNode->data;
288
list_delete(&(sec->values));
291
list_delete(&m_config);