From: Peter Verthez Date: Sun, 24 Nov 2002 19:35:37 +0000 (+0000) Subject: Extracted from message.c for reuse. X-Git-Url: https://git.dlugolecki.net.pl/?a=commitdiff_plain;h=f908d7f1b11f6852672662bfe63e7e09f7a2b8ad;p=gedcom-parse.git Extracted from message.c for reuse. --- diff --git a/gedcom/buffer.c b/gedcom/buffer.c new file mode 100644 index 0000000..c09eaa5 --- /dev/null +++ b/gedcom/buffer.c @@ -0,0 +1,116 @@ +/* Implementation of a safe string buffer + Copyright (C) 2001 The Genes Development Team + This file is part of the Gedcom parser library. + Contributed by Peter Verthez , 2001. + + The Gedcom parser library is free software; you can redistribute it + and/or modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The Gedcom parser library is distributed in the hope that it will be + useful, but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the Gedcom parser library; if not, write to the + Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA + 02111-1307 USA. */ + +/* $Id$ */ +/* $Name$ */ + +#include "buffer.h" +#include "gedcom_internal.h" + +#if HAVE_VSNPRINTF +#define INITIAL_BUF_SIZE 256 +#else +/* Risk on overflowing buffer, so make size big */ +#define INITIAL_BUF_SIZE 65536 +#endif + +void reset_buffer(struct safe_buffer* b) +{ + if (b && b->buffer != NULL) + b->buffer[0] = '\0'; +} + +void cleanup_buffer(struct safe_buffer *b) +{ + if (b && b->buffer) + free(b->buffer); +} + +void init_buffer(struct safe_buffer *b) +{ + if (b && b->buffer == NULL) { + b->buffer = (char *)malloc(INITIAL_BUF_SIZE); + if (b->buffer) { + b->buffer[0] = '\0'; + b->bufsize = INITIAL_BUF_SIZE; + if (b->cleanup_func && atexit(b->cleanup_func) != 0) { + fprintf(stderr, _("Could not register buffer cleanup function")); + fprintf(stderr, "\n"); + } + } + else { + fprintf(stderr, _("Could not allocate memory at %s, %d"), + __FILE__, __LINE__); + fprintf(stderr, "\n"); + } + } +} + +int safe_buf_vappend(struct safe_buffer *b, const char *s, va_list ap) +{ + int res = 0; + int len; + init_buffer(b); + if (b && b->buffer) { + len = strlen(b->buffer); + while (1) { + char *buf_ptr = b->buffer + len; + int rest_size = b->bufsize - len; + +#if HAVE_VSNPRINTF + res = vsnprintf(buf_ptr, rest_size, s, ap); + + if (res > -1 && res < rest_size) { + break; + } + else { + b->bufsize *= 2; + b->buffer = realloc(b->buffer, b->bufsize); + } +#else /* not HAVE_VSNPRINTF */ +# if HAVE_VSPRINTF +# warning "Using VSPRINTF. Buffer overflow could happen!" + vsprintf(buf_ptr, s, ap); + break; +# else /* not HAVE_VPRINTF */ +# error "Your standard library has neither vsnprintf nor vsprintf defined. One of them is required!" +# endif +#endif + } + } + return res; +} + +int safe_buf_append(struct safe_buffer *b, const char *s, ...) +{ + int res; + va_list ap; + + va_start(ap, s); + res = safe_buf_vappend(b, s, ap); + va_end(ap); + + return res; +} + +char* get_buf_string(struct safe_buffer *b) +{ + return b->buffer; +} diff --git a/gedcom/buffer.h b/gedcom/buffer.h new file mode 100644 index 0000000..f51acff --- /dev/null +++ b/gedcom/buffer.h @@ -0,0 +1,44 @@ +/* Header for buffer.h + Copyright (C) 2001 The Genes Development Team + This file is part of the Gedcom parser library. + Contributed by Peter Verthez , 2001. + + The Gedcom parser library is free software; you can redistribute it + and/or modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The Gedcom parser library is distributed in the hope that it will be + useful, but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the Gedcom parser library; if not, write to the + Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA + 02111-1307 USA. */ + +/* $Id$ */ +/* $Name$ */ + +#ifndef __BUFFER_H +#define __BUFFER_H + +#include +#include + +struct safe_buffer { + char* buffer; + size_t bufsize; + void (*cleanup_func)(void); +}; + +void init_buffer(struct safe_buffer* b); +void reset_buffer(struct safe_buffer* b); +void cleanup_buffer(struct safe_buffer* b); + +int safe_buf_vappend(struct safe_buffer* b, const char* s, va_list ap); +int safe_buf_append(struct safe_buffer* b, const char* s, ...); +char* get_buf_string(struct safe_buffer* b); + +#endif /* __BUFFER_H */