UNIXworkcode

1 /* 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 * 4 * Copyright 2013 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 CFG_CONF_H 30 #define CFG_CONF_H 31 32 #include <stdio.h> 33 #include <stdlib.h> 34 35 #include <cx/linked_list.h> 36 #include <cx/hash_map.h> 37 #include <cx/mempool.h> 38 #include <cx/string.h> 39 #include <cx/utils.h> 40 #include <cx/compare.h> 41 42 #include "../util/object.h" 43 44 #ifdef __cplusplus 45 extern "C" { 46 #endif 47 48 // mempool malloc macro 49 #define OBJ_NEW(p, type) (type*)cxMalloc(p, sizeof(type)) 50 #define OBJ_NEW_N(p, type) (type*)cxCalloc(p, 1, sizeof(type)) 51 52 // line types 53 #define LINE_OTHER 0 54 #define LINE_DIRECTIVE 1 55 #define LINE_BEGIN_TAG 2 56 #define LINE_END_TAG 3 57 #define LINE_MULTI 4 58 #define LINE_NOCONTENT 5 // only comment or space 59 #define LINE_ERROR 6 // parse error on this line 60 61 // tag types 62 #define TAG_OBJECT 0 63 #define TAG_IF 1 64 #define TAG_ELSEIF 2 65 #define TAG_ELSE 3 66 #define TAG_CLIENT 4 67 68 69 #define INIT_DIRECTIVE 16 70 71 #define CFG_LINE_ADD(list_begin, list_end, elm) \ 72 cx_linked_list_add((void**)list_begin, (void**)list_end, offsetof(ConfigLineList, prev), offsetof(ConfigLineList, next), elm) 73 74 #define CFG_PARAM_ADD(list_begin, list_end, elm) \ 75 cx_linked_list_add((void**)list_begin, (void**)list_end, -1, offsetof(ConfigParam, next), elm) 76 77 #define CFG_DIRECTIVES_ADD(list, dir) \ 78 cx_linked_list_add((void**)list, NULL, -1, offsetof(ConfigDirectiveList, next), dir) 79 80 #define CFG_NUM_PARAMS(param) cx_linked_list_size(param, offsetof(ConfigParam, next)) 81 82 typedef struct _cfg_line { 83 cxmutstr line; // raw line string 84 void *object; // pointer to data struct 85 int type; // type, see line types 86 } ConfigLine; 87 88 typedef int (*cfg_parse_f)(void *, ConfigLine *, ConfigLine *, cxmutstr); 89 90 typedef struct _cfg_param ConfigParam; 91 struct _cfg_param { 92 cxmutstr name; 93 cxmutstr value; 94 ConfigParam *next; 95 }; 96 97 typedef struct ConfigLineList ConfigLineList; 98 struct ConfigLineList { 99 ConfigLine *line; 100 ConfigLineList *prev; 101 ConfigLineList *next; 102 }; 103 104 typedef struct _cfg_parser { 105 CxAllocator *mp; 106 ConfigLineList *lines_begin; 107 ConfigLineList *lines_end; 108 cfg_parse_f parse; 109 } ConfigParser; 110 111 112 typedef struct _conf_tag ConfigTag; 113 struct _conf_tag { 114 ConfigLine *begin; 115 ConfigLine *end; 116 117 cxmutstr name; 118 ConfigParam *param; 119 cxmutstr param_str; 120 ConfigTag *parent; 121 ConfigTag *iftag; // only used by <ElseIf> and <Else> 122 int type_num; 123 }; 124 125 typedef struct _conf_directive { 126 ConfigLine *begin; 127 ConfigLine *end; 128 129 cxmutstr directive_type; 130 cxmutstr value; 131 //UcxList *param; 132 ConfigTag *condition; 133 int type_num; 134 } ConfigDirective; 135 136 typedef struct ConfigDirectiveList ConfigDirectiveList; 137 struct ConfigDirectiveList { 138 ConfigDirective *directive; 139 ConfigDirectiveList *next; 140 }; 141 142 int cfg_parse_basic_file(ConfigParser *parser, FILE *in); 143 144 cxmutstr cfg_readln(FILE *file); 145 146 cxmutstr cfg_trim_comment(cxmutstr line); 147 148 cxmutstr cfg_param(cxmutstr params, cxmutstr *name, cxmutstr *value); 149 150 cxmutstr cfg_param_get(ConfigParam *list, cxstring name); 151 152 ConfigDirective* cfg_parse_directive(cxmutstr line, CxAllocator *mp); 153 154 ConfigParam* cfg_param_list(cxmutstr param_str, CxAllocator *mp); 155 156 int cfg_get_directive_type_num(cxstring type); 157 158 int cfg_get_basic_type(cxmutstr line); 159 160 int cfg_get_line_type(cxmutstr line); 161 162 int cfg_get_tag_type(cxstring tag); 163 164 cxmutstr cfg_get_end_tag_name(cxmutstr line); 165 166 ConfigTag* cfg_parse_begin_tag(cxmutstr line, CxAllocator *mp); 167 168 //ConfigDirective* cfg_directivelist_get(UcxList *dirs, cxmutstr name); 169 170 //cxmutstr cfg_directivelist_get_str(UcxList *dirs, cxmutstr name); 171 172 cxmutstr cfg_directive_pstr1(ConfigDirective *dir); 173 174 175 176 #ifdef __cplusplus 177 } 178 #endif 179 180 #endif /* CFG_CONF_H */ 181 182