head 1.16; access; symbols FSL_1_7_0:1.16 CFG_0_9_11:1.16 FSL_1_6_1:1.15 CFG_0_9_10:1.15 FSL_1_6_0:1.15 FSL_1_6b2:1.15 CFG_0_9_9:1.15 FSL_1_6b1:1.15 CFG_0_9_8:1.15 CFG_0_9_7:1.14 CFG_0_9_6:1.14 CFG_0_9_5:1.12 CFG_0_9_4:1.11 FSL_1_5_0:1.10 FSL_1_5a3:1.10 CFG_0_9_3:1.10 FSL_1_5a2:1.10 FSL_1_5a1:1.10 FSL_1_4_0:1.10 FSL_1_4b1:1.10 CFG_0_9_2:1.10 CFG_0_9_1:1.10 FSL_1_4a1:1.10 FSL_1_3_0:1.10 FSL_1_3b1:1.10 FSL_1_2_1:1.10 FSL_1_2_0:1.10 FSL_1_1_0:1.10 FSL_1_1b1:1.10 FSL_1_0_8:1.10 FSL_1_0_7:1.10 FSL_1_0_6:1.6 FSL_1_0_5:1.5 FSL_1_0_4:1.5 FSL_1_0_3:1.5 FSL_1_0_2:1.5 FSL_1_0_1:1.4 FSL_1_0_0:1.4 FSL_0_9_0:1.4 CFG_0_9_0:1.4 FSL_0_1_12:1.4 FSL_0_1_11:1.4 FSL_0_1_10:1.4 FSL_0_1_9:1.4 FSL_0_1_8:1.4 FSL_0_1_7:1.4 FSL_0_1_6:1.4 FSL_0_1_5:1.4 FSL_0_1_1:1.4; locks; strict; comment @ * @; 1.16 date 2006.08.10.19.35.57; author rse; state Exp; branches; next 1.15; commitid Isy241gp4yykKkIr; 1.15 date 2004.12.31.19.16.25; author rse; state Exp; branches; next 1.14; 1.14 date 2004.12.04.12.48.40; author rse; state Exp; branches; next 1.13; 1.13 date 2004.11.29.07.09.33; author rse; state Exp; branches; next 1.12; 1.12 date 2004.11.20.12.54.07; author rse; state Exp; branches; next 1.11; 1.11 date 2004.07.17.07.37.55; author rse; state Exp; branches; next 1.10; 1.10 date 2003.01.06.11.17.43; author rse; state Exp; branches; next 1.9; 1.9 date 2002.11.18.10.23.36; author rse; state Exp; branches; next 1.8; 1.8 date 2002.11.18.09.51.29; author rse; state Exp; branches; next 1.7; 1.7 date 2002.11.10.12.12.23; author rse; state Exp; branches; next 1.6; 1.6 date 2002.11.09.14.15.55; author rse; state Exp; branches; next 1.5; 1.5 date 2002.08.02.19.07.12; author rse; state Exp; branches; next 1.4; 1.4 date 2002.07.17.15.04.08; author rse; state Exp; branches; next 1.3; 1.3 date 2002.07.10.19.26.32; author rse; state Exp; branches; next 1.2; 1.2 date 2002.07.10.14.46.28; author rse; state Exp; branches; next 1.1; 1.1 date 2002.07.10.12.00.23; author rse; state Exp; branches; next ; desc @@ 1.16 log @cleanup source tree for status as of 2006 @ text @/* ** OSSP cfg - Configuration Parsing ** Copyright (c) 2002-2006 Ralf S. Engelschall ** Copyright (c) 2002-2006 The OSSP Project (http://www.ossp.org/) ** ** This file is part of OSSP cfg, a configuration parsing ** library which can be found at http://www.ossp.org/pkg/lib/cfg/. ** ** Permission to use, copy, modify, and distribute this software for ** any purpose with or without fee is hereby granted, provided that ** the above copyright notice and this permission notice appear in all ** copies. ** ** THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED ** WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF ** MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. ** IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR ** CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF ** USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ** ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, ** OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT ** OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF ** SUCH DAMAGE. ** ** cfg_main.c: main API */ #include #include #include #include #include "cfg.h" #include "cfg_fmt.h" #include "cfg_main.h" #include "cfg_data.h" #include "cfg_node.h" #include "cfg_syn.h" #include "cfg_global.h" #define _CFG_VERS_C_AS_HEADER_ #include "cfg_vers.c" #undef _CFG_VERS_C_AS_HEADER_ long cfg_version(void) { return __cfg_version.v_hex; } cfg_rc_t cfg_create(cfg_t **cfg) { cfg_rc_t rc; if (cfg == NULL) return CFG_ERR_ARG; if ((*cfg = malloc(sizeof(cfg_t))) == NULL) return CFG_ERR_SYS; (*cfg)->root = NULL; (*cfg)->szError[0] = '\0'; (*cfg)->szErrorInfo[0] = '\0'; (*cfg)->rcErrorInfo = CFG_OK; if ((rc = cfg_grid_create(&((*cfg)->grid_nodes), sizeof(cfg_node_t), 512)) != CFG_OK) { free(*cfg); return rc; } if ((rc = cfg_grid_create(&((*cfg)->grid_tokens), 8192, 1)) != CFG_OK) { free(*cfg); cfg_grid_destroy((*cfg)->grid_nodes); return rc; } return CFG_OK; } static cfg_rc_t cfg_destroy_node(cfg_t *cfg, cfg_node_t *node, void *ctx) { cfg_node_destroy(cfg, node); return CFG_OK; } cfg_rc_t cfg_destroy(cfg_t *cfg) { if (cfg == NULL) return CFG_ERR_ARG; if (cfg->root != NULL) cfg_node_apply(cfg, cfg->root, NULL, NULL, cfg_destroy_node, NULL); cfg_grid_destroy(cfg->grid_nodes); cfg_grid_destroy(cfg->grid_tokens); free(cfg); return CFG_OK; } cfg_rc_t cfg_error_info(cfg_t *cfg, cfg_rc_t rv, const char *fmt, ...) { va_list ap; /* argument sanity check */ if (cfg == NULL || rv == CFG_OK || fmt == NULL) return CFG_ERR_ARG; /* remember error information */ va_start(ap, fmt); cfg_fmt_vsprintf(cfg->szErrorInfo, sizeof(cfg->szErrorInfo), fmt, ap); cfg->rcErrorInfo = rv; va_end(ap); return CFG_OK; } cfg_rc_t cfg_error(cfg_t *cfg, cfg_rc_t rv, char **error) { char *sz; char *cpBuf; int nBuf; int n; /* argument sanity check */ if (cfg == NULL || error == NULL) return CFG_ERR_ARG; /* start at begin of buffer */ cpBuf = cfg->szError; nBuf = sizeof(cfg->szError); /* translate result value into corresponding string */ if (rv == CFG_OK) sz = "everything ok"; else if (rv == CFG_ERR_ARG) sz = "invalid argument"; else if (rv == CFG_ERR_USE) sz = "invalid use"; else if (rv == CFG_ERR_MEM) sz = "no more memory available"; else if (rv == CFG_ERR_SYS) sz = "operating system error"; else if (rv == CFG_ERR_FMT) sz = "formatting error"; else if (rv == CFG_ERR_INT) sz = "internal error"; else if (rv == CFG_ERR_SYN) sz = "syntax error"; else if (rv == CFG_ERR_NDE) sz = "node reference error"; else sz = "unknown error"; n = cfg_fmt_sprintf(cpBuf, nBuf, "%s", sz); cpBuf += n; nBuf -= n; /* optionally annotate with error information */ if (rv == cfg->rcErrorInfo && cfg->szErrorInfo[0] != '\0') { n = cfg_fmt_sprintf(cpBuf, nBuf, "; %s", cfg->szErrorInfo); cpBuf += n; nBuf -= n; } /* optionally annotate with operating system error information */ if (rv == CFG_ERR_SYS) { n = cfg_fmt_sprintf(cpBuf, nBuf, "; %s (%d)", strerror(errno), errno); cpBuf += n; nBuf -= n; } /* return pointer to internal buffer */ *error = cfg->szError; return CFG_OK; } cfg_rc_t cfg_import(cfg_t *cfg, cfg_node_t *node, cfg_fmt_t fmt, const char *in_ptr, size_t in_len) { cfg_rc_t rv; char error[1024]; cfg_node_t *root; if (cfg == NULL || in_ptr == NULL) return CFG_ERR_ARG; if (node == NULL) node = cfg->root; if (fmt == CFG_FMT_CFG) { if ((rv = cfg_syn_import(cfg, &root, in_ptr, in_len, error, sizeof(error))) != CFG_OK) { cfg_error_info(cfg, rv, "%s", error); return rv; } } /* FIXME: CFG_FMT_XML */ else return CFG_ERR_INT; /* FIXME: merge: cfg->root[node] := cfg->root[node] + root */ cfg->root = root; return CFG_OK; } cfg_rc_t cfg_export(cfg_t *cfg, cfg_node_t *node, cfg_fmt_t fmt, char **ex_ptr, size_t ex_len) { cfg_rc_t rv; char *output; size_t n; if (cfg == NULL || ex_ptr == NULL) return CFG_ERR_ARG; if (node == NULL) node = cfg->root; if (fmt == CFG_FMT_CFG) { if ((rv = cfg_syn_export(cfg, node, &output)) != CFG_OK) return rv; } /* FIXME: CFG_FMT_XML */ else return CFG_ERR_INT; /* provide output */ if (ex_len == 0) *ex_ptr = output; else { n = strlen(output); if (n >= ex_len) return CFG_ERR_MEM; memmove(*ex_ptr, output, n+1); free(output); } return CFG_OK; } @ 1.15 log @Adjust copyright messages for new year 2005. @ text @d3 2 a4 3 ** Copyright (c) 2002-2005 Ralf S. Engelschall ** Copyright (c) 2002-2005 The OSSP Project (http://www.ossp.org/) ** Copyright (c) 2002-2005 Cable & Wireless (http://www.cw.com/) @ 1.14 log @Plug remaining memory leaks by introducing the usual LOAN/GIFT/COPY argument passing semantics to cfg_node_{set,get} and using this to directly pass the allocated tokens from the scanner through the parser into the node tree. @ text @d3 3 a5 3 ** Copyright (c) 2002-2004 Ralf S. Engelschall ** Copyright (c) 2002-2004 The OSSP Project (http://www.ossp.org/) ** Copyright (c) 2002-2004 Cable & Wireless (http://www.cw.com/) @ 1.13 log @Fix optional DMalloc build support. @ text @d76 6 d86 2 a87 1 /* FIXME: on each node, free token string first */ @ 1.12 log @- Fixed cfg_test program by correctly passing the used number of bytes in the buffer instead of the size of the buffer. - Accept zero-length strings for parsing. - Correctly handle end-of-file during plain text token scanning. @ text @a36 1 #include "cfg_global.h" d42 1 @ 1.11 log @Adjust copyright messages for new year 2004. @ text @d160 1 a160 1 if (cfg == NULL || in_ptr == NULL || in_len == 0) @ 1.10 log @update copyright messages for new year @ text @d3 3 a5 3 ** Copyright (c) 2002-2003 Ralf S. Engelschall ** Copyright (c) 2002-2003 The OSSP Project (http://www.ossp.org/) ** Copyright (c) 2002-2003 Cable & Wireless Deutschland (http://www.cw.com/de/) @ 1.9 log @more memory handling cleanups @ text @d3 3 a5 3 ** Copyright (c) 1999-2002 Ralf S. Engelschall ** Copyright (c) 1999-2002 The OSSP Project (http://www.ossp.org/) ** Copyright (c) 2001-2002 Cable & Wireless Deutschland (http://www.cw.com/de/) @ 1.8 log @add Dmalloc support @ text @d80 1 @ 1.7 log @remove trailing whitespaces @ text @d37 1 @ 1.6 log @fix intermediate memory leak @ text @d169 2 a170 2 else return CFG_ERR_INT; d193 2 a194 2 else return CFG_ERR_INT; d197 1 a197 1 if (ex_len == 0) @ 1.5 log @full source tree overhauling @ text @d63 2 a64 1 if ((rc = cfg_grid_create(&((*cfg)->grid_nodes), sizeof(cfg_node_t), 512)) != CFG_OK) d66 1 d68 1 @ 1.4 log @Ops, fix a typo I cannot imagine where it comes from... @ text @d42 8 @ 1.3 log @implement cfg_node_clone @ text @d23 1 a23 1 ** ON ANY THEORY OF LIABILITY, WHETHER IN CONTRCFG, STRICT LIABILITY, @ 1.2 log @Step 2 in API overhauling and implementation filling. @ text @d161 1 a161 1 /* FIXME merge: cfg->root[node] := cfg->root[node] + root */ @ 1.1 log @Mega-change: full API overhauling. @ text @d34 1 d37 2 d40 2 d45 16 d66 22 d91 1 a91 1 cfg_rc_t cfg_error(cfg_t *cfg, cfg_rc_t rc, char *err_ptr, size_t err_len) d93 45 d143 21 d167 1 a167 1 cfg_rc_t cfg_export(cfg_t *cfg, cfg_node_t *node, cfg_fmt_t fmt, char *ex_ptr, size_t ex_len) d169 27 @