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.11 CFG_0_9_4:1.10 FSL_1_5_0:1.9 FSL_1_5a3:1.9 CFG_0_9_3:1.9 FSL_1_5a2:1.9 FSL_1_5a1:1.9 FSL_1_4_0:1.9 FSL_1_4b1:1.9 CFG_0_9_2:1.9 CFG_0_9_1:1.9 FSL_1_4a1:1.9 FSL_1_3_0:1.9 FSL_1_3b1:1.9 FSL_1_2_1:1.9 FSL_1_2_0:1.9 FSL_1_1_0:1.9 FSL_1_1b1:1.9 FSL_1_0_8:1.9 FSL_1_0_7:1.9 FSL_1_0_6:1.5 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.5 FSL_1_0_0:1.5 FSL_0_9_0:1.5 CFG_0_9_0:1.5 FSL_0_1_12:1.5 FSL_0_1_11:1.5 FSL_0_1_10:1.5 FSL_0_1_9:1.5 FSL_0_1_8:1.5 FSL_0_1_7:1.5 FSL_0_1_6:1.5 FSL_0_1_5:1.5 FSL_0_1_1:1.5; locks; strict; comment @ * @; 1.16 date 2006.08.10.19.35.56; 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.11.29.07.09.33; author rse; state Exp; branches; next 1.13; 1.13 date 2004.11.28.17.05.44; author rse; state Exp; branches; next 1.12; 1.12 date 2004.11.28.12.58.25; author rse; state Exp; branches; next 1.11; 1.11 date 2004.11.20.11.34.52; author rse; state Exp; branches; next 1.10; 1.10 date 2004.07.17.07.37.55; author rse; state Exp; branches; next 1.9; 1.9 date 2003.01.06.11.17.43; author rse; state Exp; branches; next 1.8; 1.8 date 2002.11.18.10.23.36; author rse; state Exp; branches; next 1.7; 1.7 date 2002.11.18.09.51.29; author rse; state Exp; branches; next 1.6; 1.6 date 2002.11.10.12.12.23; author rse; state Exp; branches; next 1.5; 1.5 date 2002.07.17.15.04.08; author rse; state Exp; branches; next 1.4; 1.4 date 2002.07.17.14.54.59; author rse; state Exp; branches; next 1.3; 1.3 date 2002.07.05.17.15.14; author rse; state Exp; branches; next 1.2; 1.2 date 2002.07.04.14.51.21; author rse; state Exp; branches; next 1.1; 1.1 date 2002.07.04.12.18.47; 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_buf.c: auto-resizing buffer */ #include #include #include #include "cfg.h" #include "cfg_buf.h" #include "cfg_fmt.h" #include "cfg_global.h" /* opaque buffer data structure */ struct cfg_buf_st { char *buf_ptr; /* pointer to buffer start */ size_t buf_size; /* total size of buffer in bytes */ size_t buf_len; /* character length of string in buffer */ /* (not counting NUL-termination) */ }; /* create a new buffer object */ cfg_rc_t cfg_buf_create(cfg_buf_t **buf) { if (buf == NULL) return CFG_ERR_ARG; if ((*buf = (cfg_buf_t *)malloc(sizeof(cfg_buf_t))) == NULL) return CFG_ERR_SYS; (*buf)->buf_ptr = NULL; (*buf)->buf_len = 0; (*buf)->buf_size = 0; return CFG_OK; } /* resize (destroy, expand or shrink) the buffer */ cfg_rc_t cfg_buf_resize(cfg_buf_t *buf, signed int n) { char *cp; if (buf == NULL) return CFG_ERR_ARG; if (n == 0) { /* special case: destroy buffer */ if (buf->buf_ptr != NULL) free(buf->buf_ptr); buf->buf_ptr = NULL; buf->buf_len = 0; buf->buf_size = 0; } else { /* shrink or grow buffer */ if (buf->buf_ptr == NULL) { if ((buf->buf_ptr = malloc(n+1)) == NULL) return CFG_ERR_SYS; buf->buf_size = n+1; buf->buf_len = 0; *buf->buf_ptr = '\0'; } else { if ((cp = realloc(buf->buf_ptr, buf->buf_size+n)) == NULL) return CFG_ERR_SYS; buf->buf_ptr = cp; buf->buf_size += n; if (buf->buf_len >= buf->buf_size) { buf->buf_len = buf->buf_size-1; *(buf->buf_ptr + buf->buf_len) = '\0'; } } } return CFG_OK; } /* append a string and/or single character to the buffer */ cfg_rc_t cfg_buf_append(cfg_buf_t *buf, const char *str, size_t len, char c) { cfg_rc_t rc; if (buf == NULL) return CFG_ERR_ARG; if (str != NULL) { if (len == 0) len = strlen(str); if ((rc = cfg_buf_resize(buf, len)) != CFG_OK) return rc; memcpy(buf->buf_ptr + buf->buf_len, str, len); buf->buf_len += len; } if (c != '\0') { if ((rc = cfg_buf_resize(buf, 1)) != CFG_OK) return rc; *(buf->buf_ptr + buf->buf_len) = c; buf->buf_len++; } *(buf->buf_ptr + buf->buf_len) = '\0'; return CFG_OK; } /* remove a trailing string from the buffer */ cfg_rc_t cfg_buf_remove(cfg_buf_t *buf, const char *str, size_t len) { if (buf == NULL || len == 0) return CFG_ERR_ARG; if (len > buf->buf_len) return CFG_ERR_USE; if (str != NULL) memcpy((void *)str, buf->buf_ptr + buf->buf_len - len, len + 1); buf->buf_len -= len; *(buf->buf_ptr + buf->buf_len) = '\0'; return CFG_OK; } /* append a formatted string to the buffer */ cfg_rc_t cfg_buf_format(cfg_buf_t *buf, const char *fmt, ...) { va_list ap; cfg_rc_t rc; va_start(ap, fmt); rc = cfg_buf_vformat(buf, fmt, ap); va_end(ap); return rc; } /* append a formatted string to the buffer (va_list based) */ cfg_rc_t cfg_buf_vformat(cfg_buf_t *buf, const char *fmt, va_list ap) { cfg_rc_t rc; int n; if (buf == NULL || fmt == NULL) return CFG_ERR_ARG; if ((n = cfg_fmt_vsprintf(NULL, -1, fmt, ap)) == -1) return CFG_ERR_FMT; if ((rc = cfg_buf_resize(buf, n)) != CFG_OK) return rc; if ((n = cfg_fmt_vsprintf(buf->buf_ptr + buf->buf_len, buf->buf_size - buf->buf_len, fmt, ap)) == -1) return CFG_ERR_FMT; buf->buf_len += n; return CFG_OK; } /* return the buffer string */ cfg_rc_t cfg_buf_content(cfg_buf_t *buf, char **ptr, size_t *len, size_t *size) { if (buf == NULL) return CFG_ERR_ARG; if (len != NULL) *len = buf->buf_len; if (size != NULL) *size = buf->buf_size; if (ptr != NULL) { if (buf->buf_ptr == NULL) *ptr = strdup(""); else { *ptr = buf->buf_ptr; buf->buf_ptr = NULL; buf->buf_size = 0; buf->buf_len = 0; } } return CFG_OK; } /* destroy buffer object */ cfg_rc_t cfg_buf_destroy(cfg_buf_t *buf) { if (buf == NULL) return CFG_ERR_ARG; if (buf->buf_ptr != NULL) free(buf->buf_ptr); free(buf); 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 @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.13 log @make cfg_buf_append more convenient by allowing to append a single character and reset buffer after a successful call to cfg_buf_content @ text @a35 1 #include "cfg_global.h" d38 1 @ 1.12 log @Cleanup and extend buffer handling sub-library (cfg_buf.[ch]) @ text @d99 2 a100 2 /* append a string to the buffer */ cfg_rc_t cfg_buf_append(cfg_buf_t *buf, const char *str, size_t len) d104 1 a104 1 if (buf == NULL || str == NULL || len == 0) d106 14 a119 4 if ((rc = cfg_buf_resize(buf, len)) != CFG_OK) return rc; memcpy(buf->buf_ptr + buf->buf_len, str, len); buf->buf_len += len; d175 4 d182 1 a182 1 else d184 4 a187 1 buf->buf_ptr = NULL; a188 4 if (len != NULL) *len = buf->buf_len; if (size != NULL) *size = buf->buf_size; @ 1.11 log @Return an empty string instead of NULL from internal cfg_buf_content() function to not make visible to the caller the implementation special case of an initial buffer. @ text @d40 1 d42 4 a45 3 char *buf_ptr; size_t buf_size; size_t buf_len; d48 1 d61 1 d69 1 a69 1 /* destroy buffer */ d83 1 d99 30 d140 1 d152 1 a152 1 if ((n = cfg_fmt_vsprintf(buf->buf_ptr + buf->buf_len, d160 1 d179 1 @ 1.10 log @Adjust copyright messages for new year 2004. @ text @d129 4 a132 1 *ptr = buf->buf_ptr; @ 1.9 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.8 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.7 log @add Dmalloc support @ text @d128 1 a128 1 if (ptr != NULL) d130 2 @ 1.6 log @remove trailing whitespaces @ text @d36 1 @ 1.5 log @Ops, fix a typo I cannot imagine where it comes from... @ text @d115 2 a116 2 if ((n = cfg_fmt_vsprintf(buf->buf_ptr + buf->buf_len, buf->buf_size - buf->buf_len, @ 1.4 log @cfg_buf_t is an opaque type @ text @d23 1 a23 1 ** ON ANY THEORY OF LIABILITY, WHETHER IN CONTRCFG, STRICT LIABILITY, @ 1.3 log @fix indentation in export output @ text @d39 6 @ 1.2 log @Flush today's work: the library is now able to parse a .cfg file into an internal node tree and export this again in textual .cfg file format (including indented blocks). @ text @d51 1 a51 1 cfg_rc_t cfg_buf_resize(cfg_buf_t *buf, int n) d68 1 a68 1 if ((buf->buf_ptr = malloc(n)) == NULL) d70 1 a70 1 buf->buf_size = n; d78 3 a80 3 if (buf->buf_len > buf->buf_size) { buf->buf_len = buf->buf_size; *(buf->buf_ptr + buf->buf_len - 1) = '\0'; d107 1 a107 1 if ((rc = cfg_buf_resize(buf, n+1)) != CFG_OK) @ 1.1 log @add a small object for dynamic buffer handling @ text @d89 11 a100 1 va_list ap; a102 1 va_start(ap, fmt); a113 1 va_end(ap); @