2 * UCW Library -- Reading of configuration files
4 * (c) 2006 Robert Spalek <robert@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
14 CT_END, // end of list
15 CT_INT, CT_U64, CT_DOUBLE, // number types
16 CT_STRING, // string type
17 CT_PARSER, // arbitrary parser function
18 CT_SECTION, // section appears exactly once
19 CT_LIST // list with 0..many nodes
23 typedef byte *cf_hook(void *ptr);
24 /* An init- or commit-hook gets a pointer to the section or NULL if this
25 * is the global section. It returns an error message or NULL if everything
27 typedef byte *cf_parser(uns number, byte **pars, void *ptr);
28 /* A parser function an array of strings and stores the parsed value in any
29 * way it likes into *ptr. It returns an error message or NULL if everything
35 int number; // number of values: k>=0 means exactly k, k<0 means at most -k
36 void *ptr; // pointer to a global variable or an offset in a section
38 struct cf_section *sub; // declaration of a section or a list
39 cf_parser *par; // parser function
44 uns size; // 0 for a global block, sizeof(struct) for a section
45 cf_hook *init; // fills in default values
46 cf_hook *commit; // verifies parsed data and checks ranges (optional)
47 struct cf_item *cfg; // CT_END-terminated array of items
50 #define ARRAY_ALLOC(type,len,val...) (type[]) { (type)len, ##val } + 1
51 // creates an array with an allocated space in the front for the (Pascal-like) length
52 #define ARRAY_LEN(a) *(uns*)(a-1)
53 // length of the array
55 /* Declaration of cf_items */
56 #define CF_TYPE(s) .size = sizeof(s),
57 #define CF_INIT(f) .init = (cf_hook*) f,
58 #define CF_COMMIT(f) .commit = (cf_hook*) f,
59 #define CF_ITEMS(i) .cfg = ( struct cf_item[] ) { i { .type = CT_END } },
60 /* Configuration items for single variables */
61 #define CF_INT(n,p) { .type = CT_INT, .name = n, .number = 1, .ptr = CHECK_PTR_TYPE(p,int*) },
62 #define CF_U64(n,p) { .type = CT_U64, .name = n, .number = 1, .ptr = CHECK_PTR_TYPE(p,u64*) },
63 #define CF_DOUBLE(n,p) { .type = CT_DOUBLE, .name = n, .number = 1, .ptr = CHECK_PTR_TYPE(p,double*) },
64 #define CF_STRING(n,p) { .type = CT_STRING, .name = n, .number = 1, .ptr = CHECK_PTR_TYPE(p,byte**) },
65 /* Configuration items for arrays of variables */
66 #define CF_INT_ARY(n,p,c) { .type = CT_INT, .name = n, .number = c, .ptr = CHECK_PTR_TYPE(p,int**) },
67 #define CF_U64_ARY(n,p,c) { .type = CT_U64, .name = n, .number = c, .ptr = CHECK_PTR_TYPE(p,u64**) },
68 #define CF_DOUBLE_ARY(n,p,c) { .type = CT_DOUBLE, .name = n, .number = c, .ptr = CHECK_PTR_TYPE(p,double**) },
69 #define CF_STRING_ARY(n,p,c) { .type = CT_STRING, .name = n, .number = c, .ptr = CHECK_PTR_TYPE(p,byte***) },
70 /* Configuration items for sections, lists, and parsed items */
72 #define CF_PARSER(n,p,f,c) { .type = CT_PARSER, .name = n, .number = c, .ptr = p, .ptr2.par = (cf_parser*) f },
73 #define CF_SECTION(n,p,s) { .type = CT_SECTION, .name = n, .number = 1, .ptr = p, .ptr2.sub = s },
74 #define CF_LIST(n,p,s) { .type = CT_LIST, .name = n, .number = 1, .ptr = CHECK_PTR_TYPE(p,struct clist*), .ptr2.sub = s },