|
1 /* |
|
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
|
3 * |
|
4 * Copyright 2011 Olaf Wintermann. All rights reserved. |
|
5 * |
|
6 * Redistribution and use in source and binary forms, with or without |
|
7 * modification, are permitted provided that the following conditions are met: |
|
8 * |
|
9 * 1. Redistributions of source code must retain the above copyright |
|
10 * notice, this list of conditions and the following disclaimer. |
|
11 * |
|
12 * 2. Redistributions in binary form must reproduce the above copyright |
|
13 * notice, this list of conditions and the following disclaimer in the |
|
14 * documentation and/or other materials provided with the distribution. |
|
15 * |
|
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
|
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
|
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
|
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
26 * POSSIBILITY OF SUCH DAMAGE. |
|
27 */ |
|
28 |
|
29 #ifndef ACONF_H |
|
30 #define ACONF_H |
|
31 |
|
32 #include <stdio.h> |
|
33 #include <stdlib.h> |
|
34 |
|
35 #include "../ucx/list.h" |
|
36 #include "../ucx/dlist.h" |
|
37 #include "../ucx/map.h" |
|
38 #include "../ucx/mempool.h" |
|
39 #include "../ucx/string.h" |
|
40 |
|
41 #ifdef __cplusplus |
|
42 extern "C" { |
|
43 #endif |
|
44 |
|
45 // mempool malloc macro |
|
46 #define OBJ_NEW(pool, type) (type*)ucx_mempool_malloc(pool, sizeof(type)) |
|
47 #define OBJ_NEW_N(pool, type) (type*)ucx_mempool_calloc(pool, 1, sizeof(type)) |
|
48 |
|
49 // line types |
|
50 #define LINE_OTHER 0 |
|
51 #define LINE_DIRECTIVE 1 |
|
52 #define LINE_BEGIN_TAG 2 |
|
53 #define LINE_END_TAG 3 |
|
54 #define LINE_MULTI 4 |
|
55 #define LINE_NOCONTENT 5 // only comment or space |
|
56 #define LINE_ERROR 6 // parse error on this line |
|
57 |
|
58 typedef struct _cfg_line { |
|
59 sstr_t line; // raw line string |
|
60 void *object; // pointer to data struct |
|
61 int type; // type, see line types |
|
62 } ConfigLine; |
|
63 |
|
64 typedef int (*cfg_parse_f)(void *, ConfigLine *, ConfigLine *, sstr_t); |
|
65 |
|
66 typedef struct _cfg_param { |
|
67 sstr_t name; |
|
68 sstr_t value; |
|
69 } ConfigParam; |
|
70 |
|
71 typedef struct _cfg_parser { |
|
72 UcxMempool *mp; |
|
73 UcxDlist *lines; |
|
74 cfg_parse_f parse; |
|
75 } ConfigParser; |
|
76 |
|
77 |
|
78 int cfg_parse_basic_file(ConfigParser *parser, FILE *in); |
|
79 |
|
80 sstr_t cfg_readln(FILE *file); |
|
81 |
|
82 int cfg_get_basic_type(sstr_t line); |
|
83 |
|
84 sstr_t cfg_trim_comment(sstr_t line); |
|
85 |
|
86 sstr_t cfg_param(sstr_t params, sstr_t *name, sstr_t *value); |
|
87 |
|
88 sstr_t cfg_param_get(UcxList *list, sstr_t name); |
|
89 |
|
90 int cfg_get_directive_type_num(sstr_t type); |
|
91 |
|
92 |
|
93 #ifdef __cplusplus |
|
94 } |
|
95 #endif |
|
96 |
|
97 #endif /* ACONF_H */ |
|
98 |