*** empty log message ***
[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     b->buffer[0] = '\0';
38 }
39
40 void cleanup_buffer(struct safe_buffer *b)
41 {
42   if (b && b->buffer)
43     free(b->buffer);
44 }
45
46 void init_buffer(struct safe_buffer *b)
47 {
48   if (b && b->buffer == NULL) {
49     b->buffer = (char *)malloc(INITIAL_BUF_SIZE);
50     if (b->buffer) {
51       b->buffer[0] = '\0';
52       b->bufsize = INITIAL_BUF_SIZE;
53       if (b->cleanup_func && atexit(b->cleanup_func) != 0) {
54         fprintf(stderr, _("Could not register buffer cleanup function"));
55         fprintf(stderr, "\n");
56       }
57     }
58     else {
59       fprintf(stderr, _("Could not allocate memory at %s, %d"),
60               __FILE__, __LINE__);
61       fprintf(stderr, "\n");
62     }
63   }
64 }
65
66 int safe_buf_vappend(struct safe_buffer *b, const char *s, va_list ap)
67 {
68   int res = 0;
69   int len;
70   init_buffer(b);
71   if (b && b->buffer) {
72     len = strlen(b->buffer);
73     while (1) {
74       char *buf_ptr = b->buffer + len;
75       int rest_size = b->bufsize - len;
76       
77 #if HAVE_VSNPRINTF
78       res = vsnprintf(buf_ptr, rest_size, s, ap);
79       
80       if (res > -1 && res < rest_size) {
81         break;
82       }
83       else  {
84         b->bufsize *= 2;
85         b->buffer = realloc(b->buffer, b->bufsize);
86       }
87 #else /* not HAVE_VSNPRINTF */
88 #  if HAVE_VSPRINTF
89 #     warning "Using VSPRINTF. Buffer overflow could happen!"
90       vsprintf(buf_ptr, s, ap);
91       break;
92 #  else /* not HAVE_VPRINTF */
93 #     error "Your standard library has neither vsnprintf nor vsprintf defined. One of them is required!"
94 #  endif
95 #endif
96     }
97   }
98   return res;
99 }
100
101 int safe_buf_append(struct safe_buffer *b, const char *s, ...)
102 {
103   int res;
104   va_list ap;
105   
106   va_start(ap, s);
107   res = safe_buf_vappend(b, s, ap);
108   va_end(ap);
109   
110   return res;
111 }
112
113 char* get_buf_string(struct safe_buffer *b)
114 {
115   return b->buffer;
116 }