]> mj.ucw.cz Git - libucw.git/blob - lib/conf2.h
significant simplifications of the interface
[libucw.git] / lib / conf2.h
1 /*
2  *      UCW Library -- Reading of configuration files
3  *
4  *      (c) 2006 Robert Spalek <robert@ucw.cz>
5  *
6  *      This software may be freely distributed and used according to the terms
7  *      of the GNU Lesser General Public License.
8  */
9
10 #ifndef _UCW_CONF2_H
11 #define _UCW_CONF2_H
12
13 enum cf_type {
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
20 };
21
22 struct cf_section;
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
26    * is all right.  */
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
30    * is all right.  */
31
32 struct cf_item {
33   enum cf_type type;
34   byte *name;
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
37   union {
38     struct cf_section *sub;             // declaration of a section or a list
39     cf_parser *par;                     // parser function
40   } ptr2;
41 };
42
43 struct cf_section {
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
48 };
49
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
54
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 */
71 struct clist;
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 },
75
76 #endif