~ubuntu-branches/ubuntu/wily/mupen64plus/wily

« back to all changes in this revision

Viewing changes to .pc/path_max.patch/main/config.c

  • Committer: Bazaar Package Importer
  • Author(s): Sven Eckelmann
  • Date: 2011-07-24 14:23:26 UTC
  • mfrom: (10.1.2 experimental)
  • Revision ID: james.westby@ubuntu.com-20110724142326-x9z5qu8j9jecrmod
Tags: 1.99.4+2
* Upload to unstable
* Remove overrides for lintian warning about change to native package
* Update Vcs-* fields to new anonscm.debian.org URLs in debian/control
* Fix spelling of "Flexible" in debian/control (Closes: #633693)
* Mark all targets in debian/rules as phony
* Add some information about the mupen64plus 2.0 vision in debian/NEWS

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2
 
 *   Mupen64plus - config.c                                                *
3
 
 *   Mupen64Plus homepage: http://code.google.com/p/mupen64plus/           *
4
 
 *   Copyright (C) 2002 Blight                                             *
5
 
 *                                                                         *
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.                                   *
10
 
 *                                                                         *
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.                          *
15
 
 *                                                                         *
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
 
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
21
 
 
22
 
#include <stdio.h>
23
 
#include <stdlib.h>
24
 
#include <string.h>
25
 
#include <ctype.h>
26
 
#include <errno.h>
27
 
#include <limits.h>
28
 
 
29
 
#include "config.h"
30
 
#include "main.h"
31
 
#include "util.h"
32
 
#include "translate.h"
33
 
 
34
 
typedef struct _SConfigValue
35
 
{
36
 
    char *key;      // key - string
37
 
    char *cValue;   // value - string
38
 
    int   iValue;   // value - integer
39
 
    int   bValue;   // value - bool
40
 
} SConfigValue;
41
 
 
42
 
typedef struct _SConfigSection
43
 
{
44
 
    char   *name;
45
 
    list_t  values;
46
 
} SConfigSection;
47
 
 
48
 
list_t m_config = NULL;         // list of config sections
49
 
SConfigSection *m_configSection = 0;    // currently selected section
50
 
 
51
 
/** helper funcs **/
52
 
 
53
 
static SConfigValue * config_findValue( const char *key )
54
 
{
55
 
    list_node_t *node;
56
 
    SConfigValue *val;
57
 
 
58
 
    if (!m_configSection)
59
 
        config_set_section("Default");
60
 
 
61
 
    list_foreach(m_configSection->values, node)
62
 
    {
63
 
        val = (SConfigValue *)node->data;
64
 
 
65
 
        if (strcasecmp(key, val->key) == 0)
66
 
            return val;
67
 
    }
68
 
 
69
 
    return NULL;
70
 
}
71
 
 
72
 
static SConfigSection * config_findSection( const char *section )
73
 
{
74
 
    list_node_t *node;
75
 
    SConfigSection *sec;
76
 
 
77
 
    list_foreach(m_config, node)
78
 
    {
79
 
        sec = (SConfigSection *)node->data;
80
 
 
81
 
        if (strcasecmp( section, sec->name ) == 0)
82
 
            return sec;
83
 
    }
84
 
 
85
 
    return NULL;
86
 
}
87
 
 
88
 
 
89
 
/** config API **/
90
 
 
91
 
int config_set_section( const char *section )
92
 
{
93
 
    SConfigSection *sec = config_findSection( section );
94
 
 
95
 
    if (!sec)
96
 
    {
97
 
        sec = malloc(sizeof(SConfigSection));
98
 
        if(!sec)
99
 
            return -1;
100
 
 
101
 
        sec->name = strdup( section );
102
 
        sec->values = NULL;
103
 
        list_append(&m_config, sec);
104
 
    }
105
 
    m_configSection = sec;
106
 
 
107
 
    return 0;
108
 
}
109
 
 
110
 
const char * config_get_string( const char *key, const char *def )
111
 
{
112
 
    SConfigValue *val = config_findValue( key );
113
 
    if (!val)
114
 
    {
115
 
        if (def != NULL)
116
 
            config_put_string(key, def);
117
 
        return def;
118
 
    }
119
 
 
120
 
    return val->cValue;
121
 
}
122
 
 
123
 
int config_get_number( const char *key, int def )
124
 
{
125
 
    SConfigValue *val = config_findValue( key );
126
 
    if (!val)
127
 
    {
128
 
        config_put_number(key, def);
129
 
        return def;
130
 
    }
131
 
 
132
 
    return val->iValue;
133
 
}
134
 
 
135
 
int config_get_bool( const char *key, int def )
136
 
{
137
 
    SConfigValue *val = config_findValue( key );
138
 
    if (!val)
139
 
    {
140
 
        config_put_bool(key, def);
141
 
        return def;
142
 
    }
143
 
 
144
 
    return val->bValue;
145
 
}
146
 
 
147
 
 
148
 
void config_put_string( const char *key, const char *value )
149
 
{
150
 
    SConfigValue *val = config_findValue( key );
151
 
    if (!val)
152
 
    {
153
 
        val = malloc(sizeof(SConfigValue));
154
 
        if(!val) return;
155
 
        memset(val, 0, sizeof(SConfigValue));
156
 
        val->key = strdup(key);
157
 
        list_append(&(m_configSection->values), val);
158
 
    }
159
 
 
160
 
    if (val->cValue)
161
 
        free( val->cValue );
162
 
    val->cValue = strdup( value );
163
 
    val->iValue = atoi(val->cValue);
164
 
    val->bValue = val->iValue;
165
 
    if (strcasecmp(val->cValue, "yes") == 0)
166
 
        val->bValue = 1;
167
 
    else if (strcasecmp(val->cValue, "true") == 0)
168
 
        val->bValue = 1;
169
 
}
170
 
 
171
 
void config_put_number( const char *key, int value )
172
 
{
173
 
    char buf[50];
174
 
    snprintf( buf, 50, "%d", value );
175
 
    config_put_string( key, buf );
176
 
}
177
 
 
178
 
void config_put_bool( const char *key, int value )
179
 
{
180
 
    config_put_string( key, (value != 0) ? ("true") : ("false") );
181
 
}
182
 
 
183
 
void config_read( void )
184
 
{
185
 
    FILE *f;
186
 
    char filename[PATH_MAX];
187
 
    char line[2048];
188
 
    char *p;
189
 
    int linelen;
190
 
 
191
 
    config_set_section( "Default" );
192
 
 
193
 
    snprintf( filename, PATH_MAX, "%smupen64plus.conf", get_configpath() );
194
 
    f = fopen( filename, "r" );
195
 
    if( f == NULL )
196
 
    {
197
 
        printf( "%s: %s\n", filename, strerror( errno ) );
198
 
        return;
199
 
    }
200
 
 
201
 
    while( !feof( f ) )
202
 
    {
203
 
        if( !fgets( line, 2048, f ) )
204
 
            break;
205
 
 
206
 
        trim( line );
207
 
        linelen = strlen( line );
208
 
        if (line[0] == '#')     // comment
209
 
            continue;
210
 
 
211
 
        if (line[0] == '[' && line[linelen-1] == ']')
212
 
        {
213
 
            line[linelen-1] = '\0';
214
 
            config_set_section( line+1 );
215
 
            continue;
216
 
        }
217
 
 
218
 
        p = strchr( line, '=' );
219
 
        if( !p )
220
 
            continue;
221
 
 
222
 
        *(p++) = '\0';
223
 
        trim( line );
224
 
        trim( p );
225
 
        config_put_string( line, p );
226
 
    }
227
 
 
228
 
    fclose( f );
229
 
}
230
 
 
231
 
void config_write( void )
232
 
{
233
 
    FILE *f;
234
 
    char filename[PATH_MAX];
235
 
    list_node_t *secNode,
236
 
            *valNode;
237
 
    SConfigSection *sec;
238
 
    SConfigValue *val;
239
 
 
240
 
    snprintf( filename, PATH_MAX, "%smupen64plus.conf", get_configpath() );
241
 
    f = fopen( filename, "w" );
242
 
    if( !f )
243
 
        return;
244
 
 
245
 
    // for each config section
246
 
    list_foreach(m_config, secNode)
247
 
    {
248
 
        sec = (SConfigSection *)secNode->data;
249
 
 
250
 
        fprintf(f, "[%s]\n", sec->name);
251
 
 
252
 
        // for each config value
253
 
        list_foreach(sec->values, valNode)
254
 
        {
255
 
            val = (SConfigValue *)valNode->data;
256
 
            fprintf(f, "%s = %s\n", val->key, val->cValue);
257
 
        }
258
 
        fprintf( f, "\n" );
259
 
    }
260
 
 
261
 
    fclose( f );
262
 
}
263
 
 
264
 
void config_delete(void)
265
 
{
266
 
    list_node_t *secNode,
267
 
            *valNode;
268
 
    SConfigSection *sec;
269
 
    SConfigValue *val;
270
 
 
271
 
    m_configSection = NULL;
272
 
 
273
 
    // for each config section
274
 
    list_foreach(m_config, secNode)
275
 
    {
276
 
        sec = (SConfigSection *)secNode->data;
277
 
 
278
 
        // for each config value
279
 
        list_foreach(sec->values, valNode)
280
 
        {
281
 
            val = (SConfigValue *)valNode->data;
282
 
 
283
 
            free(val->key);
284
 
            free(val->cValue);
285
 
            free(val);
286
 
        }
287
 
        free(sec->name);
288
 
        list_delete(&(sec->values));
289
 
        free(sec);
290
 
    }
291
 
    list_delete(&m_config);
292
 
}
293