| 1 |
|
| 2 |
Ü |
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
#ifndef _USUAL_CFPARSER_H_ |
| 21 |
#define _USUAL_CFPARSER_H_ |
| 22 |
|
| 23 |
#include <usual/base.h> |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
typedef bool (*cf_handler_f)(void *arg, bool is_sect, const char *key, const char *val); |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
bool parse_ini_file(const char *fn, cf_handler_f user_handler, void *arg) _MUSTCHECK; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
#define CF_VAL_ABS 0 |
| 51 |
|
| 52 |
|
| 53 |
#define CF_VAL_REL (1<<1) |
| 54 |
|
| 55 |
|
| 56 |
#define CF_NO_RELOAD (1<<2) |
| 57 |
|
| 58 |
|
| 59 |
#define CF_READONLY (1<<3) |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
struct CfValue { |
| 67 |
void *value_p; |
| 68 |
const void *extra; |
| 69 |
const char *key_name; |
| 70 |
char *buf; |
| 71 |
int buflen; |
| 72 |
}; |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
struct CfOps { |
| 81 |
bool (*setter)(struct CfValue *cv, const char *value); |
| 82 |
const char *(*getter)(struct CfValue *cv); |
| 83 |
const void *op_extra; |
| 84 |
}; |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
struct CfKey { |
| 90 |
|
| 91 |
const char *key_name; |
| 92 |
|
| 93 |
struct CfOps op; |
| 94 |
|
| 95 |
int flags; |
| 96 |
|
| 97 |
uintptr_t key_ofs; |
| 98 |
|
| 99 |
const char *def_value; |
| 100 |
}; |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
struct CfSect { |
| 106 |
|
| 107 |
const char *sect_name; |
| 108 |
|
| 109 |
|
| 110 |
const struct CfKey *key_list; |
| 111 |
|
| 112 |
|
| 113 |
void *(*base_lookup)(void *top_base, const char *sect_name); |
| 114 |
|
| 115 |
|
| 116 |
bool (*set_key)(void *base, const char *key, const char *val); |
| 117 |
|
| 118 |
|
| 119 |
const char *(*get_key)(void *base, const char *key, char *buf, int buflen); |
| 120 |
|
| 121 |
|
| 122 |
bool (*section_start)(void *top_base, const char *sect_name); |
| 123 |
}; |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
struct CfContext { |
| 129 |
|
| 130 |
const struct CfSect *sect_list; |
| 131 |
|
| 132 |
void *base; |
| 133 |
|
| 134 |
bool loaded; |
| 135 |
}; |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
bool cf_set_str(struct CfValue *cv, const char *value); |
| 144 |
|
| 145 |
bool cf_set_filename(struct CfValue *cv, const char *value); |
| 146 |
|
| 147 |
bool cf_set_int(struct CfValue *cv, const char *value); |
| 148 |
|
| 149 |
bool cf_set_uint(struct CfValue *cv, const char *value); |
| 150 |
|
| 151 |
bool cf_set_time_usec(struct CfValue *cv, const char *value); |
| 152 |
|
| 153 |
bool cf_set_time_double(struct CfValue *cv, const char *value); |
| 154 |
|
| 155 |
bool cf_set_lookup(struct CfValue *cv, const char *value); |
| 156 |
|
| 157 |
|
| 158 |
const char *cf_get_str(struct CfValue *cv); |
| 159 |
|
| 160 |
const char *cf_get_int(struct CfValue *cv); |
| 161 |
|
| 162 |
const char *cf_get_uint(struct CfValue *cv); |
| 163 |
|
| 164 |
const char *cf_get_time_usec(struct CfValue *cv); |
| 165 |
|
| 166 |
const char *cf_get_time_double(struct CfValue *cv); |
| 167 |
|
| 168 |
const char *cf_get_lookup(struct CfValue *cv); |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
#define CF_STR { cf_set_str, cf_get_str } |
| 179 |
|
| 180 |
#define CF_FILE { cf_set_filename, cf_get_str } |
| 181 |
|
| 182 |
#define CF_INT { cf_set_int, cf_get_int } |
| 183 |
|
| 184 |
#define CF_UINT { cf_set_uint, cf_get_uint } |
| 185 |
|
| 186 |
#define CF_BOOL { cf_set_int, cf_get_int } |
| 187 |
|
| 188 |
#define CF_TIME_USEC { cf_set_time_usec, cf_get_time_usec } |
| 189 |
|
| 190 |
#define CF_TIME_DOUBLE { cf_set_time_double, cf_get_time_double } |
| 191 |
|
| 192 |
#define CF_LOOKUP(t) { cf_set_lookup, cf_get_lookup, t } |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
struct CfLookup { |
| 200 |
const char *name; |
| 201 |
int value; |
| 202 |
}; |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
#define CF_ABS(name, ops, var, flags, def) \ |
| 208 |
{ name, ops, flags | CF_VAL_ABS, (uintptr_t)&(var), def } |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
#define CF_REL(name, ops, var, flags, def) \ |
| 229 |
{ name, ops, flags | CF_VAL_REL, offsetof(CF_REL_BASE, var), def } |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
bool cf_load_file(const struct CfContext *cf, const char *fn) _MUSTCHECK; |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
const char *cf_get(const struct CfContext *cf, const char *sect, const char *var, char *buf, int buflen); |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
bool cf_set(const struct CfContext *cf, const char *sect, const char *var, const char *val); |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
#endif |