Copied from old documentation. Removed all Gedcom_val details.
[gedcom-parse.git] / gedcom / buffer.h
1 /* Header for buffer.h
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 #ifndef __BUFFER_H
25 #define __BUFFER_H
26
27 #include <stdarg.h>
28 #include <sys/types.h>
29
30 struct safe_buffer {
31   char* buffer;
32   size_t bufsize;  /* total size */
33   char* buf_end;
34   size_t buflen;   /* used size */
35   void (*cleanup_func)(void);
36 };
37
38 void init_buffer(struct safe_buffer* b);
39 void reset_buffer(struct safe_buffer* b);
40 void cleanup_buffer(struct safe_buffer* b);
41 void grow_buffer(struct safe_buffer* b);
42
43 int safe_buf_vappend(struct safe_buffer* b, const char* s, va_list ap);
44 int safe_buf_append(struct safe_buffer* b, const char* s, ...);
45 char* get_buf_string(struct safe_buffer* b);
46
47 #define SAFE_BUF_ADDCHAR(b, ch)                                               \
48   {                                                                           \
49     struct safe_buffer *buf = b;                                              \
50     char c = ch;                                                              \
51     if (buf && buf->buffer == NULL)                                           \
52       init_buffer(buf);                                                       \
53     if (buf && buf->buffer) {                                                 \
54       if (buf->buflen == buf->bufsize)                                        \
55         grow_buffer(buf);                                                     \
56       *buf->buf_end++ = c;                                                    \
57       buf->buflen++;                                                          \
58     }                                                                         \
59   }
60
61 #endif /* __BUFFER_H */