-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathconfig.h
107 lines (86 loc) · 2.23 KB
/
config.h
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
/** \file
* Key/value parser until we have a proper config.
*
* Auto-parsed variables will be assigned when read.
* To create a configuration parameter:
* <code>
* CONFIG_INT( "name", variable, default_value );
* CONFIG_STR( "name", variable, default_value );
* </code>
*/
/*
* Copyright (C) 2009 Trammell Hudson <hudson+ml@osresearch.net>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef _config_h_
#define _config_h_
#define MAX_NAME_LEN 64
#define MAX_VALUE_LEN 60
struct config
{
struct config * next;
char name[ MAX_NAME_LEN ];
char value[ MAX_VALUE_LEN ];
};
extern struct config * global_config;
extern struct config *
config_parse(
FILE * file
);
extern char *
config_value(
struct config * config,
const char * name
);
extern int
config_int(
struct config * config,
const char * name,
int def
);
extern struct config *
config_parse_file(
const char * filename
);
extern int
config_save_file(
struct config * config,
const char * filename
);
/** Create an auto-parsed config variable */
struct config_var
{
const char * name;
int type; //!< 0 == int, 1 == char *
void * value; //!< int* if len == 0
};
#define _CONFIG_VAR( NAME, TYPE_ENUM, TYPE, VAR, VALUE ) \
static TYPE VAR = VALUE; \
struct config_var \
__attribute__((section(".config_vars"))) \
__config_##VAR = \
{ \
.name = NAME, \
.type = TYPE_ENUM, \
.value = &VAR, \
}
#define CONFIG_INT( NAME, VAR, VALUE ) \
_CONFIG_VAR( NAME, 0, unsigned, VAR, VALUE )
#define CONFIG_STR( NAME, VAR, VALUE ) \
_CONFIG_VAR( NAME, 1, char *, VAR, VALUE )
#endif