]> mj.ucw.cz Git - libucw.git/blob - lib/conf2.h
2a2281ff616fdb26c67907c77119d597257227b0
[libucw.git] / lib / conf2.h
1 /*
2  *      UCW Library -- Reading of configuration files
3  *
4  *      (c) 2001--2006 Robert Spalek <robert@ucw.cz>
5  *      (c) 2003--2006 Martin Mares <mj@ucw.cz>
6  *
7  *      This software may be freely distributed and used according to the terms
8  *      of the GNU Lesser General Public License.
9  */
10
11 #ifndef _UCW_CONF2_H
12 #define _UCW_CONF2_H
13
14 enum cf_class {
15   CC_END,                               // end of list
16   CC_STATIC,                            // single variable or static array
17   CC_DYNAMIC,                           // dynamically allocated array
18   CC_PARSER,                            // arbitrary parser function
19   CC_SECTION,                           // section appears exactly once
20   CC_LIST                               // list with 0..many nodes
21 };
22
23 enum cf_type {
24   CT_INT, CT_U64, CT_DOUBLE,            // number types
25   CT_STRING                             // string type
26 };
27
28 typedef byte *cf_parser(uns number, byte **pars, void *ptr);
29   /* A parser function gets an array of (strdup'ed) strings and a pointer with
30    * the customized information (most likely the target address).  It can store
31    * the parsed value anywhere in any way it likes, however it must first call
32    * cf_journal_block() on the overwritten memory block.  It returns an error
33    * message or NULL if everything is all right.  */
34 typedef byte *cf_hook(void *ptr);
35   /* An init- or commit-hook gets a pointer to the section or NULL if this
36    * is the global section.  It returns an error message or NULL if everything
37    * is all right.  The init-hook should fill in default values (needed for
38    * dynamically allocated nodes of link lists or for filling global variables
39    * that are run-time dependent).  The commit-hook should perform sanity
40    * checks and postprocess the parsed values.  Commit-hooks must call
41    * cf_journal_block() too.  */
42
43 struct cf_section;
44 struct cf_item {
45   enum cf_class cls;
46   byte *name;
47   int number;                           // length of an array or #parameters of a parser (negative means at most)
48   void *ptr;                            // pointer to a global variable or an offset in a section
49   union {
50     enum cf_type type;                  // type of a static or dynamic attribute
51     struct cf_section *sec;             // declaration of a section or a list
52     cf_parser *par;                     // parser function
53   } u;
54 };
55
56 struct cf_section {
57   uns size;                             // 0 for a global block, sizeof(struct) for a section
58   cf_hook *init;                        // fills in default values (otherwise 0's are used)
59   cf_hook *commit;                      // verifies parsed data (optional)
60   struct cf_item *cfg;                  // CC_END-terminated array of items
61 };
62
63 /* Declaration of cf_section */
64 #define CF_TYPE(s)      .size = sizeof(s)
65 #define CF_INIT(f)      .init = (cf_hook*) f
66 #define CF_COMMIT(f)    .commit = (cf_hook*) f
67 #define CF_ITEMS        .cfg = ( struct cf_item[] )
68 #define CF_END          { .cls = CC_END }
69 /* Configuration items */
70 struct clist;
71 #define CF_STATIC(n,p,T,t,c)    { .cls = CC_STATIC, .name = n, .number = c, .ptr = CHECK_PTR_TYPE(p,t*), .u.type = CT_##T }
72 #define CF_DYNAMIC(n,p,T,t,c)   { .cls = CC_DYNAMIC, .name = n, .number = c, .ptr = CHECK_PTR_TYPE(p,t**), .u.type = CT_##T }
73 #define CF_PARSER(n,p,f,c)      { .cls = CC_PARSER, .name = n, .number = c, .ptr = p, .u.par = (cf_parser*) f }
74 #define CF_SECTION(n,p,s)       { .cls = CC_SECTION, .name = n, .number = 1, .ptr = p, .u.sec = s }
75 #define CF_LIST(n,p,s)          { .cls = CC_LIST, .name = n, .number = 1, .ptr = CHECK_PTR_TYPE(p,struct clist*), .u.sec = s }
76 /* Configuration items for basic types */
77 #define CF_INT(n,p)             CF_STATIC(n,p,INT,int,1)
78 #define CF_INT_ARY(n,p,c)       CF_STATIC(n,p,INT,int,c)
79 #define CF_INT_DYN(n,p,c)       CF_DYNAMIC(n,p,INT,int,c)
80 #define CF_U64(n,p)             CF_STATIC(n,p,U64,u64,1)
81 #define CF_U64_ARY(n,p,c)       CF_STATIC(n,p,U64,u64,c)
82 #define CF_U64_DYN(n,p,c)       CF_DYNAMIC(n,p,U64,u64,c)
83 #define CF_DOUBLE(n,p)          CF_STATIC(n,p,DOUBLE,double,1)
84 #define CF_DOUBLE_ARY(n,p,c)    CF_STATIC(n,p,DOUBLE,double,c)
85 #define CF_DOUBLE_DYN(n,p,c)    CF_DYNAMIC(n,p,DOUBLE,double,c)
86 #define CF_STRING(n,p)          CF_STATIC(n,p,STRING,byte*,1)
87 #define CF_STRING_ARY(n,p,c)    CF_STATIC(n,p,STRING,byte*,c)
88 #define CF_STRING_DYN(n,p,c)    CF_DYNAMIC(n,p,STRING,byte*,c)
89
90 #define DYN_LEN(a) *(uns*)(a-1)
91   // length of a dynamic array
92 #define DYN_ALLOC(type,len,val...) (type[]) { (type)len, ##val } + 1
93   // creates a static instance of a dynamic array
94
95 /* Memory allocation */
96 struct mempool;
97 extern struct mempool *cf_pool;
98 void *cf_malloc(uns size);
99 void *cf_malloc_zero(uns size);
100 byte *cf_strdup(byte *s);
101 byte *cf_printf(char *fmt, ...);
102
103 /* Undo journal for error recovery */
104 extern uns cf_need_journal;
105 void cf_journal_block(void *ptr, uns len);
106
107 /* Declaration */
108 void cf_declare_section(byte *name, struct cf_section *sec);
109 void cf_init_section(byte *name, struct cf_section *sec, void *ptr);
110
111 /* Safe reloading and loading of configuration files */
112 extern byte *cf_def_file;
113 byte *cf_reload(byte *file);
114 byte *cf_load(byte *file);
115 byte *cf_set(byte *string);
116
117 /* Parsers for basic types */
118 byte *cf_parse_int(byte *str, int *ptr);
119 byte *cf_parse_u64(byte *str, u64 *ptr);
120 byte *cf_parse_double(byte *str, double *ptr);
121
122 #endif