1 /* 2 * $Id: scconf.h 620 2011-05-25 14:49:04Z felfert $ 3 * 4 * Copyright (C) 2002 5 * Antti Tapaninen <aet@cc.hut.fi> 6 * 7 * Originally based on source by Timo Sirainen <tss@iki.fi> 8 * 9 * This library is free software; you can redistribute it and/or 10 * modify it under the terms of the GNU Lesser General Public 11 * License as published by the Free Software Foundation; either 12 * version 2.1 of the License, or (at your option) any later version. 13 * 14 * This library is distributed in the hope that it will be useful, 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 * Lesser General Public License for more details. 18 * 19 * You should have received a copy of the GNU Lesser General Public 20 * License along with this library; if not, write to the Free Software 21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 22 */ 23 24 #ifndef _SC_CONF_H 25 #define _SC_CONF_H 26 27 #ifdef __cplusplus 28 extern "C" { 29 #endif 30 31 typedef struct _scconf_entry { 32 const char *name; 33 unsigned int type; 34 unsigned int flags; 35 void *parm; 36 void *arg; 37 } scconf_entry; 38 39 /* Entry flags */ 40 #define SCCONF_PRESENT 0x00000001 41 #define SCCONF_MANDATORY 0x00000002 42 #define SCCONF_ALLOC 0x00000004 43 #define SCCONF_ALL_BLOCKS 0x00000008 44 #define SCCONF_VERBOSE 0x00000010 /* For debugging purposes only */ 45 46 /* Entry types */ 47 #define SCCONF_CALLBACK 1 48 #define SCCONF_BLOCK 2 49 #define SCCONF_LIST 3 50 51 #define SCCONF_BOOLEAN 11 52 #define SCCONF_INTEGER 12 53 #define SCCONF_STRING 13 54 55 typedef struct _scconf_block scconf_block; 56 57 typedef struct _scconf_list { 58 struct _scconf_list *next; 59 char *data; 60 } scconf_list; 61 62 #define SCCONF_ITEM_TYPE_COMMENT 0 /* key = NULL, comment */ 63 #define SCCONF_ITEM_TYPE_BLOCK 1 /* key = key, block */ 64 #define SCCONF_ITEM_TYPE_VALUE 2 /* key = key, list */ 65 66 typedef struct _scconf_item { 67 struct _scconf_item *next; 68 int type; 69 char *key; 70 union { 71 char *comment; 72 scconf_block *block; 73 scconf_list *list; 74 } value; 75 } scconf_item; 76 77 struct _scconf_block { 78 scconf_block *parent; 79 scconf_list *name; 80 scconf_item *items; 81 }; 82 83 typedef struct { 84 char *filename; 85 int debug; 86 scconf_block *root; 87 char *errmsg; 88 } scconf_context; 89 90 /* Allocate scconf_context 91 * The filename can be NULL 92 */ 93 extern scconf_context *scconf_new(const char *filename); 94 95 /* Free scconf_context 96 */ 97 extern void scconf_free(scconf_context * config); 98 99 /* Parse configuration 100 * Returns 1 = ok, 0 = error, -1 = error opening config file 101 */ 102 extern int scconf_parse(scconf_context * config); 103 104 /* Parse a static configuration string 105 * Returns 1 = ok, 0 = error 106 */ 107 extern int scconf_parse_string(scconf_context * config, const char *string); 108 109 /* Parse entries 110 */ 111 extern int scconf_parse_entries(const scconf_context * config, const scconf_block * block, scconf_entry * entry); 112 113 /* Write config to a file 114 * If the filename is NULL, use the config->filename 115 * Returns 0 = ok, else = errno 116 */ 117 extern int scconf_write(scconf_context * config, const char *filename); 118 119 /* Write configuration entries to block 120 */ 121 extern int scconf_write_entries(scconf_context * config, scconf_block * block, scconf_entry * entry); 122 123 /* Find a block by the item_name 124 * If the block is NULL, the root block is used 125 */ 126 extern const scconf_block *scconf_find_block(const scconf_context * config, const scconf_block * block, const char *item_name); 127 128 /* Find blocks by the item_name 129 * If the block is NULL, the root block is used 130 * The key can be used to specify what the blocks first name should be 131 */ 132 extern scconf_block **scconf_find_blocks(const scconf_context * config, const scconf_block * block, const char *item_name, const char *key); 133 134 /* Get a list of values for option 135 */ 136 extern const scconf_list *scconf_find_list(const scconf_block * block, const char *option); 137 138 /* Return the first string of the option 139 * If no option found, return def 140 */ 141 extern const char *scconf_get_str(const scconf_block * block, const char *option, const char *def); 142 143 /* Return the first value of the option as integer 144 * If no option found, return def 145 */ 146 extern int scconf_get_int(const scconf_block * block, const char *option, int def); 147 148 /* Return the first value of the option as boolean 149 * If no option found, return def 150 */ 151 extern int scconf_get_bool(const scconf_block * block, const char *option, int def); 152 153 /* Write value to a block as a string 154 */ 155 extern const char *scconf_put_str(scconf_block * block, const char *option, const char *value); 156 157 /* Write value to a block as an integer 158 */ 159 extern int scconf_put_int(scconf_block * block, const char *option, int value); 160 161 /* Write value to a block as a boolean 162 */ 163 extern int scconf_put_bool(scconf_block * block, const char *option, int value); 164 165 /* Add block structure 166 * If the block is NULL, the root block is used 167 */ 168 extern scconf_block *scconf_block_add(scconf_context * config, scconf_block * block, const char *key, const scconf_list *name); 169 170 /* Copy block structure (recursive) 171 */ 172 extern scconf_block *scconf_block_copy(const scconf_block * src, scconf_block ** dst); 173 174 /* Free block structure (recursive) 175 */ 176 extern void scconf_block_destroy(scconf_block * block); 177 178 /* Add item to block structure 179 * If the block is NULL, the root block is used 180 */ 181 extern scconf_item *scconf_item_add(scconf_context * config, scconf_block * block, scconf_item * item, int type, const char *key, const void *data); 182 183 /* Copy item structure (recursive) 184 */ 185 extern scconf_item *scconf_item_copy(const scconf_item * src, scconf_item ** dst); 186 187 /* Free item structure (recursive) 188 */ 189 extern void scconf_item_destroy(scconf_item * item); 190 191 /* Add a new value to the list 192 */ 193 extern scconf_list *scconf_list_add(scconf_list ** list, const char *value); 194 195 /* Copy list structure 196 */ 197 extern scconf_list *scconf_list_copy(const scconf_list * src, scconf_list ** dst); 198 199 /* Free list structure 200 */ 201 extern void scconf_list_destroy(scconf_list * list); 202 203 /* Return the length of an list array 204 */ 205 extern int scconf_list_array_length(const scconf_list * list); 206 207 /* Return the combined length of the strings on all arrays 208 */ 209 extern int scconf_list_strings_length(const scconf_list * list); 210 211 /* Return an allocated string that contains all 212 * the strings in a list separated by the filler 213 * The filler can be NULL 214 */ 215 extern char *scconf_list_strdup(const scconf_list * list, const char *filler); 216 217 /* Returns an allocated array of const char *pointers to 218 * list elements. 219 * Last pointer is NULL 220 * Array must be freed, but pointers to strings belong to scconf_list 221 */ 222 extern const char **scconf_list_toarray(const scconf_list * list); 223 224 #ifdef __cplusplus 225 } 226 #endif 227 #endif 228