1 | #ifndef OSCAM_CONF_H
|
---|
2 | #define OSCAM_CONF_H
|
---|
3 |
|
---|
4 | #define MAXLINESIZE 16384
|
---|
5 |
|
---|
6 | enum opt_types {
|
---|
7 | OPT_UNKNOWN = 0,
|
---|
8 | OPT_INT8,
|
---|
9 | OPT_UINT8,
|
---|
10 | OPT_INT32,
|
---|
11 | OPT_UINT32,
|
---|
12 | OPT_STRING,
|
---|
13 | OPT_SSTRING,
|
---|
14 | OPT_HEX_ARRAY,
|
---|
15 | OPT_FUNC,
|
---|
16 | OPT_FUNC_EXTRA,
|
---|
17 | OPT_SAVE_FUNC,
|
---|
18 | OPT_FIXUP_FUNC,
|
---|
19 | };
|
---|
20 |
|
---|
21 | struct config_list {
|
---|
22 | enum opt_types opt_type;
|
---|
23 | char *config_name;
|
---|
24 | size_t var_offset;
|
---|
25 | unsigned int str_size;
|
---|
26 | union {
|
---|
27 | int8_t d_int8;
|
---|
28 | uint8_t d_uint8;
|
---|
29 | int32_t d_int32;
|
---|
30 | uint32_t d_uint32;
|
---|
31 | char *d_char;
|
---|
32 | long d_extra;
|
---|
33 | uint32_t array_size;
|
---|
34 | } def;
|
---|
35 | union {
|
---|
36 | void (*process_fn)(const char *token, char *value, void *setting, FILE *config_file);
|
---|
37 | void (*process_fn_extra)(const char *token, char *value, void *setting, long extra, FILE *config_file);
|
---|
38 | bool (*should_save_fn)(void *var);
|
---|
39 | void (*fixup_fn)(void *var);
|
---|
40 | } ops;
|
---|
41 | };
|
---|
42 |
|
---|
43 | #define DEF_OPT_INT8(__name, __var_ofs, __default) \
|
---|
44 | { \
|
---|
45 | .opt_type = OPT_INT8, \
|
---|
46 | .config_name = __name, \
|
---|
47 | .var_offset = __var_ofs, \
|
---|
48 | .def.d_int8 = __default \
|
---|
49 | }
|
---|
50 |
|
---|
51 | #define DEF_OPT_UINT8(__name, __var_ofs, __default) \
|
---|
52 | { \
|
---|
53 | .opt_type = OPT_UINT8, \
|
---|
54 | .config_name = __name, \
|
---|
55 | .var_offset = __var_ofs, \
|
---|
56 | .def.d_uint8 = __default \
|
---|
57 | }
|
---|
58 |
|
---|
59 | #define DEF_OPT_INT32(__name, __var_ofs, __default) \
|
---|
60 | { \
|
---|
61 | .opt_type = OPT_INT32, \
|
---|
62 | .config_name = __name, \
|
---|
63 | .var_offset = __var_ofs, \
|
---|
64 | .def.d_int32 = __default \
|
---|
65 | }
|
---|
66 |
|
---|
67 | #define DEF_OPT_UINT32(__name, __var_ofs, __default) \
|
---|
68 | { \
|
---|
69 | .opt_type = OPT_UINT32, \
|
---|
70 | .config_name = __name, \
|
---|
71 | .var_offset = __var_ofs, \
|
---|
72 | .def.d_uint32 = __default \
|
---|
73 | }
|
---|
74 |
|
---|
75 | #define DEF_OPT_STR(__name, __var_ofs, __default) \
|
---|
76 | { \
|
---|
77 | .opt_type = OPT_STRING, \
|
---|
78 | .config_name = __name, \
|
---|
79 | .var_offset = __var_ofs, \
|
---|
80 | .def.d_char = __default \
|
---|
81 | }
|
---|
82 |
|
---|
83 | #define DEF_OPT_SSTR(__name, __var_ofs, __default, __str_size) \
|
---|
84 | { \
|
---|
85 | .opt_type = OPT_SSTRING, \
|
---|
86 | .config_name = __name, \
|
---|
87 | .var_offset = __var_ofs, \
|
---|
88 | .str_size = __str_size, \
|
---|
89 | .def.d_char = __default \
|
---|
90 | }
|
---|
91 |
|
---|
92 | #define DEF_OPT_HEX(__name, __var_ofs, __array_size) \
|
---|
93 | { \
|
---|
94 | .opt_type = OPT_HEX_ARRAY, \
|
---|
95 | .config_name = __name, \
|
---|
96 | .var_offset = __var_ofs, \
|
---|
97 | .def.array_size = __array_size \
|
---|
98 | }
|
---|
99 |
|
---|
100 | #define DEF_OPT_FUNC(__name, __var_ofs, __process_fn) \
|
---|
101 | { \
|
---|
102 | .opt_type = OPT_FUNC, \
|
---|
103 | .config_name = __name, \
|
---|
104 | .var_offset = __var_ofs, \
|
---|
105 | .ops.process_fn = __process_fn \
|
---|
106 | }
|
---|
107 |
|
---|
108 | #define DEF_OPT_FUNC_X(__name, __var_ofs, __process_fn_extra, __extra) \
|
---|
109 | { \
|
---|
110 | .opt_type = OPT_FUNC_EXTRA, \
|
---|
111 | .config_name = __name, \
|
---|
112 | .var_offset = __var_ofs, \
|
---|
113 | .ops.process_fn_extra = __process_fn_extra, \
|
---|
114 | .def.d_extra = __extra \
|
---|
115 | }
|
---|
116 |
|
---|
117 | #define DEF_OPT_SAVE_FUNC(__fn) \
|
---|
118 | { \
|
---|
119 | .opt_type = OPT_SAVE_FUNC, \
|
---|
120 | .ops.should_save_fn = __fn \
|
---|
121 | }
|
---|
122 |
|
---|
123 | #define DEF_OPT_FIXUP_FUNC(__fn) \
|
---|
124 | { \
|
---|
125 | .opt_type = OPT_FIXUP_FUNC, \
|
---|
126 | .ops.fixup_fn = __fn \
|
---|
127 | }
|
---|
128 |
|
---|
129 | #define DEF_LAST_OPT \
|
---|
130 | { \
|
---|
131 | .opt_type = OPT_UNKNOWN \
|
---|
132 | }
|
---|
133 |
|
---|
134 | struct config_sections {
|
---|
135 | const char *section;
|
---|
136 | const struct config_list *config;
|
---|
137 | };
|
---|
138 |
|
---|
139 | int32_t strToIntVal(char *value, int32_t defaultvalue);
|
---|
140 | uint32_t strToUIntVal(char *value, uint32_t defaultvalue);
|
---|
141 |
|
---|
142 | void fprintf_conf(FILE *f, const char *varname, const char *fmt, ...) __attribute__ ((format (printf, 3, 4)));
|
---|
143 |
|
---|
144 | int config_list_parse(const struct config_list *clist, const char *token, char *value, void *config_data);
|
---|
145 | void config_list_save_ex(FILE *f, const struct config_list *clist, void *config_data, int save_all,
|
---|
146 | bool (*check_func)(const struct config_list *clist, void *config_data, const char *setting)
|
---|
147 | );
|
---|
148 | static inline void config_list_save(FILE *f, const struct config_list *clist, void *config_data, int save_all) {
|
---|
149 | config_list_save_ex(f, clist, config_data, save_all, NULL);
|
---|
150 | }
|
---|
151 | void config_list_apply_fixups(const struct config_list *clist, void *var);
|
---|
152 | bool config_list_should_be_saved(const struct config_list *clist, void *var);
|
---|
153 | void config_list_set_defaults(const struct config_list *clist, void *config_data);
|
---|
154 | void config_list_free_values(const struct config_list *clist, void *config_data);
|
---|
155 | void config_list_gc_values(const struct config_list *clist, void *config_data);
|
---|
156 |
|
---|
157 | int config_section_is_active(const struct config_sections *sec);
|
---|
158 | const struct config_sections *config_find_section(const struct config_sections *conf, char *section_name);
|
---|
159 | void config_sections_save(const struct config_sections *conf, FILE *f, void *var);
|
---|
160 | void config_sections_set_defaults(const struct config_sections *conf, void *var);
|
---|
161 | void config_sections_free(const struct config_sections *conf, void *var);
|
---|
162 |
|
---|
163 | void config_set_value(const struct config_sections *conf, char *section, const char *token, char *value, void *var);
|
---|
164 |
|
---|
165 | FILE *open_config_file(const char *conf_filename);
|
---|
166 | FILE *open_config_file_or_die(const char *conf_filename);
|
---|
167 | FILE *create_config_file(const char *conf_filename);
|
---|
168 | bool flush_config_file(FILE *f, const char *conf_filename);
|
---|
169 |
|
---|
170 | #endif
|
---|