head 1.8; access; symbols FSL_1_7_0:1.8 CFG_0_9_11:1.8 FSL_1_6_1:1.7 CFG_0_9_10:1.7 FSL_1_6_0:1.7 FSL_1_6b2:1.7 CFG_0_9_9:1.7 FSL_1_6b1:1.7 CFG_0_9_8:1.7 CFG_0_9_7:1.6 CFG_0_9_6:1.6 CFG_0_9_5:1.5 CFG_0_9_4:1.4 FSL_1_5_0:1.3 FSL_1_5a3:1.3 CFG_0_9_3:1.3 FSL_1_5a2:1.3 FSL_1_5a1:1.3 FSL_1_4_0:1.3 FSL_1_4b1:1.3 CFG_0_9_2:1.3 CFG_0_9_1:1.3 FSL_1_4a1:1.3 FSL_1_3_0:1.3 FSL_1_3b1:1.3 FSL_1_2_1:1.3 FSL_1_2_0:1.3 FSL_1_1_0:1.3 FSL_1_1b1:1.3 FSL_1_0_8:1.3 FSL_1_0_7:1.3 FSL_1_0_6:1.1 FSL_1_0_5:1.1 FSL_1_0_4:1.1 FSL_1_0_3:1.1 FSL_1_0_2:1.1 FSL_1_0_1:1.1 FSL_1_0_0:1.1 FSL_0_9_0:1.1 CFG_0_9_0:1.1 FSL_0_1_12:1.1 FSL_0_1_11:1.1 FSL_0_1_10:1.1 FSL_0_1_9:1.1 FSL_0_1_8:1.1 FSL_0_1_7:1.1 FSL_0_1_6:1.1 FSL_0_1_5:1.1 FSL_0_1_1:1.1; locks; strict; comment @ * @; 1.8 date 2006.08.10.19.35.57; author rse; state Exp; branches; next 1.7; commitid Isy241gp4yykKkIr; 1.7 date 2004.12.31.19.16.25; author rse; state Exp; branches; next 1.6; 1.6 date 2004.11.29.07.09.33; author rse; state Exp; branches; next 1.5; 1.5 date 2004.11.20.12.54.07; author rse; state Exp; branches; next 1.4; 1.4 date 2004.07.17.07.37.55; author rse; state Exp; branches; next 1.3; 1.3 date 2003.01.06.11.17.43; author rse; state Exp; branches; next 1.2; 1.2 date 2002.11.18.09.51.29; author rse; state Exp; branches; next 1.1; 1.1 date 2002.07.03.13.25.34; author rse; state Exp; branches; next ; desc @@ 1.8 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_util.c: utility functions */ #include #include #include #include #include "cfg.h" #include "cfg_util.h" #include "cfg_global.h" cfg_rc_t cfg_util_readfile(const char *filename, char **buf_ptr, size_t *buf_size, size_t *buf_used) { FILE *fp = NULL; size_t n; char *cp; if (strcmp(filename, "-") == 0) { /* file is given on stdin */ (*buf_size) = 32; if ((*buf_ptr = (char *)malloc(*buf_size)) == NULL) return CFG_ERR_SYS; cp = *buf_ptr; while ((n = fread(cp, 1, (*buf_size)-(cp-(*buf_ptr)), stdin)) > 0) { cp += n; if (((*buf_ptr)+(*buf_size))-cp < (*buf_size)/8) { (*buf_size) *= 2; n = cp-(*buf_ptr); if ((cp = (char *)realloc(*buf_ptr, *buf_size)) == NULL) { free(*buf_ptr); return CFG_ERR_SYS; } *buf_ptr = cp; cp += n; } } *cp = '\0'; *buf_used = (cp - *buf_ptr); } else { /* file is given on filesystem */ if ((fp = fopen(filename, "r")) == NULL) return CFG_ERR_SYS; fseek(fp, 0, SEEK_END); if ((n = ftell(fp)) > 0) { if ((*buf_ptr = (char *)malloc(n+1)) == NULL) { fclose(fp); return CFG_ERR_SYS; } fseek(fp, 0, SEEK_SET); if ((n = fread(*buf_ptr, 1, n, fp)) == 0) { free(*buf_ptr); fclose(fp); return CFG_ERR_SYS; } (*buf_ptr)[n] = '\0'; (*buf_size) = n+1; (*buf_used) = n; } else { (*buf_ptr) = strdup(""); (*buf_size) = 1; (*buf_used) = 0; } fclose(fp); } return CFG_OK; } @ 1.7 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.6 log @Fix optional DMalloc build support. @ 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.5 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 @d37 1 a38 1 #include "cfg_util.h" @ 1.4 log @Adjust copyright messages for new year 2004. @ text @d40 1 a40 1 cfg_rc_t cfg_util_readfile(const char *filename, char **buf_ptr, size_t *buf_len) d48 2 a49 2 (*buf_len) = 32; if ((*buf_ptr = (char *)malloc(*buf_len)) == NULL) d52 1 a52 1 while ((n = fread(cp, 1, (*buf_len)-(cp-(*buf_ptr)), stdin)) > 0) { d54 2 a55 2 if (((*buf_ptr)+(*buf_len))-cp < (*buf_len)/8) { (*buf_len) *= 2; d57 1 a57 1 if ((cp = (char *)realloc(*buf_ptr, *buf_len)) == NULL) { d66 1 d85 2 a86 1 (*buf_len) = n+1; d89 3 a91 2 (*buf_ptr) = strdup(""); (*buf_len) = 1; @ 1.3 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.2 log @add Dmalloc support @ 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.1 log @Add this first cut for the forthcoming OSSP cfg library to CVS. This is work in progress and still not ready for use anywhere. @ text @d37 1 @