Copied from old documentation. Removed all Gedcom_val details.
[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   char* new_buffer;
74   size_t old_size = b->bufsize;
75   b->bufsize *= 2;
76   new_buffer = realloc(b->buffer, b->bufsize);
77   if (new_buffer) {
78     b->buffer = new_buffer;
79     memset(b->buffer + old_size, 0, b->bufsize - old_size);
80     b->buf_end = b->buffer + b->buflen;
81   }
82   else
83     b->buffer = NULL;
84 }
85
86 int safe_buf_vappend(struct safe_buffer *b, const char *s, va_list ap)
87 {
88   int res = 0;
89   init_buffer(b);
90   if (b && b->buffer) {
91     while (1) {
92       int rest_size = b->bufsize - b->buflen;
93       
94 #if HAVE_VSNPRINTF
95       res = vsnprintf(b->buf_end, rest_size, s, ap);
96       
97       if (res > -1 && res < rest_size) {
98         b->buf_end = b->buf_end + res;
99         b->buflen  = b->buflen + res;
100         break;
101       }
102       else {
103         grow_buffer(b);
104       }
105 #else /* not HAVE_VSNPRINTF */
106 #  if HAVE_VSPRINTF
107 #     warning "Using VSPRINTF. Buffer overflow could happen!"
108       res = vsprintf(b->buf_end, s, ap);
109       b->buf_end = b->buf_end + res;
110       b->buflen  = b->buflen + res;
111       break;
112 #  else /* not HAVE_VPRINTF */
113 #     error "Your standard library has neither vsnprintf nor vsprintf defined. One of them is required!"
114 #  endif
115 #endif
116     }
117   }
118   return res;
119 }
120
121 int safe_buf_append(struct safe_buffer *b, const char *s, ...)
122 {
123   int res;
124   va_list ap;
125   
126   va_start(ap, s);
127   res = safe_buf_vappend(b, s, ap);
128   va_end(ap);
129   
130   return res;
131 }
132
133 char* get_buf_string(struct safe_buffer *b)
134 {
135   return b->buffer;
136 }