Clear the buffer at init.
[gedcom-parse.git] / gedcom / buffer.c
1 /* Implementation of a safe string buffer
2    Copyright (C) 2001 The Genes Development Team
3    This file is part of the Gedcom parser library.
4    Contributed by Peter Verthez <Peter.Verthez@advalvas.be>, 2001.
5
6    The Gedcom parser library is free software; you can redistribute it
7    and/or modify it under the terms of the GNU Lesser General Public
8    License as published by the Free Software Foundation; either
9    version 2.1 of the License, or (at your option) any later version.
10
11    The Gedcom parser library is distributed in the hope that it will be
12    useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Lesser General Public License for more details.
15
16    You should have received a copy of the GNU Lesser General Public
17    License along with the Gedcom parser library; if not, write to the
18    Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19    02111-1307 USA.  */
20
21 /* $Id$ */
22 /* $Name$ */
23
24 #include "buffer.h"
25 #include "gedcom_internal.h"
26
27 #if HAVE_VSNPRINTF
28 #define INITIAL_BUF_SIZE 256
29 #else
30 /* Risk on overflowing buffer, so make size big */
31 #define INITIAL_BUF_SIZE 65536
32 #endif
33
34 void reset_buffer(struct safe_buffer* b)
35 {
36   if (b && b->buffer != NULL) {
37     memset(b->buffer, 0, b->bufsize);
38     b->buf_end = b->buffer;
39     b->buflen  = 0;
40   }
41 }
42
43 void cleanup_buffer(struct safe_buffer *b)
44 {
45   if (b && b->buffer)
46     free(b->buffer);
47 }
48
49 void init_buffer(struct safe_buffer *b)
50 {
51   if (b && b->buffer == NULL) {
52     b->buffer = (char *)malloc(INITIAL_BUF_SIZE);
53     if (b->buffer) {
54       b->bufsize = INITIAL_BUF_SIZE;
55       memset(b->buffer, 0, b->bufsize);
56       b->buf_end = b->buffer;
57       b->buflen  = 0;
58       if (b->cleanup_func && atexit(b->cleanup_func) != 0) {
59         fprintf(stderr, _("Could not register buffer cleanup function"));
60         fprintf(stderr, "\n");
61       }
62     }
63     else {
64       fprintf(stderr, _("Could not allocate memory at %s, %d"),
65               __FILE__, __LINE__);
66       fprintf(stderr, "\n");
67     }
68   }
69 }
70
71 void grow_buffer(struct safe_buffer *b)
72 {
73   b->bufsize *= 2;
74   b->buffer = realloc(b->buffer, b->bufsize);
75   b->buf_end = b->buffer + b->buflen;
76 }
77
78 int safe_buf_vappend(struct safe_buffer *b, const char *s, va_list ap)
79 {
80   int res = 0;
81   init_buffer(b);
82   if (b && b->buffer) {
83     while (1) {
84       int rest_size = b->bufsize - b->buflen;
85       
86 #if HAVE_VSNPRINTF
87       res = vsnprintf(b->buf_end, rest_size, s, ap);
88       
89       if (res > -1 && res < rest_size) {
90         b->buf_end = b->buf_end + res;
91         b->buflen  = b->buflen + res;
92         break;
93       }
94       else {
95         grow_buffer(b);
96       }
97 #else /* not HAVE_VSNPRINTF */
98 #  if HAVE_VSPRINTF
99 #     warning "Using VSPRINTF. Buffer overflow could happen!"
100       res = vsprintf(b->buf_end, s, ap);
101       b->buf_end = b->buf_end + res;
102       b->buflen  = b->buflen + res;
103       break;
104 #  else /* not HAVE_VPRINTF */
105 #     error "Your standard library has neither vsnprintf nor vsprintf defined. One of them is required!"
106 #  endif
107 #endif
108     }
109   }
110   return res;
111 }
112
113 int safe_buf_append(struct safe_buffer *b, const char *s, ...)
114 {
115   int res;
116   va_list ap;
117   
118   va_start(ap, s);
119   res = safe_buf_vappend(b, s, ap);
120   va_end(ap);
121   
122   return res;
123 }
124
125 char* get_buf_string(struct safe_buffer *b)
126 {
127   return b->buffer;
128 }