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 <ucx/list.h> 36 #include <ucx/map.h> 37 #include <ucx/mempool.h> 38 #include <ucx/string.h> 39 40 #include "../util/object.h" 41 42 #ifdef __cplusplus 43 extern "C" { 44 #endif 45 46 // mempool malloc macro 47 #define OBJ_NEW(p, type) (type*)(p)->malloc((p)->pool, sizeof(type)) 48 #define OBJ_NEW_N(p, type) (type*)(p)->calloc((p)->pool, 1, sizeof(type)) 49 50 // line types 51 #define LINE_OTHER 0 52 #define LINE_DIRECTIVE 1 53 #define LINE_BEGIN_TAG 2 54 #define LINE_END_TAG 3 55 #define LINE_MULTI 4 56 #define LINE_NOCONTENT 5 // only comment or space 57 #define LINE_ERROR 6 // parse error on this line 58 59 // tag types 60 #define TAG_OBJECT 0 61 #define TAG_IF 1 62 #define TAG_ELSEIF 2 63 #define TAG_ELSE 3 64 #define TAG_CLIENT 4 65 66 67 #define INIT_DIRECTIVE 16 68 69 typedef struct _cfg_line { 70 sstr_t line; // raw line string 71 void *object; // pointer to data struct 72 int type; // type, see line types 73 } ConfigLine; 74 75 typedef int (*cfg_parse_f)(void *, ConfigLine *, ConfigLine *, sstr_t); 76 77 typedef struct _cfg_param { 78 sstr_t name; 79 sstr_t value; 80 } ConfigParam; 81 82 typedef struct _cfg_parser { 83 UcxAllocator *mp; 84 UcxList *lines; 85 cfg_parse_f parse; 86 } ConfigParser; 87 88 89 typedef struct _conf_tag ConfigTag; 90 struct _conf_tag { 91 ConfigLine *begin; 92 ConfigLine *end; 93 94 sstr_t name; 95 UcxList *param; 96 sstr_t param_str; 97 ConfigTag *parent; 98 ConfigTag *iftag; // only used by <ElseIf> and <Else> 99 int type_num; 100 }; 101 102 typedef struct _conf_directive { 103 ConfigLine *begin; 104 ConfigLine *end; 105 106 sstr_t directive_type; 107 sstr_t value; 108 //UcxList *param; 109 ConfigTag *condition; 110 int type_num; 111 } ConfigDirective; 112 113 114 int cfg_parse_basic_file(ConfigParser *parser, FILE *in); 115 116 sstr_t cfg_readln(FILE *file); 117 118 sstr_t cfg_trim_comment(sstr_t line); 119 120 sstr_t cfg_param(sstr_t params, sstr_t *name, sstr_t *value); 121 122 sstr_t cfg_param_get(UcxList *list, sstr_t name); 123 124 ConfigDirective* cfg_parse_directive(sstr_t line, UcxAllocator *mp); 125 126 UcxList* cfg_param_list(sstr_t param_str, UcxAllocator *mp); 127 128 int cfg_get_directive_type_num(sstr_t type); 129 130 int cfg_get_basic_type(sstr_t line); 131 132 int cfg_get_line_type(sstr_t line); 133 134 int cfg_get_tag_type(sstr_t tag); 135 136 sstr_t cfg_get_end_tag_name(sstr_t line); 137 138 ConfigTag* cfg_parse_begin_tag(sstr_t line, UcxAllocator *mp); 139 140 ConfigDirective* cfg_directivelist_get(UcxList *dirs, sstr_t name); 141 142 sstr_t cfg_directivelist_get_str(UcxList *dirs, sstr_t name); 143 144 sstr_t cfg_directive_pstr1(ConfigDirective *dir); 145 146 void cfg_map_destr(UcxMempool *mp, UcxMap *map); 147 148 #ifdef __cplusplus 149 } 150 #endif 151 152 #endif /* CFG_CONF_H */ 153 154