Added wrbuf_destroy(w), equivalent to wrbuf_free(w,1)
[yaz-moved-to-github.git] / include / yaz / wrbuf.h
1 /*
2  * Copyright (c) 1995-2007, Index Data
3  * All rights reserved.
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  *     * Redistributions of source code must retain the above copyright
8  *       notice, this list of conditions and the following disclaimer.
9  *     * Redistributions in binary form must reproduce the above copyright
10  *       notice, this list of conditions and the following disclaimer in the
11  *       documentation and/or other materials provided with the distribution.
12  *     * Neither the name of Index Data nor the names of its contributors
13  *       may be used to endorse or promote products derived from this
14  *       software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 /* $Id: wrbuf.h,v 1.22 2007-01-06 15:32:46 adam Exp $ */
28
29 /**
30  * \file wrbuf.h
31  * \brief Header for WRBUF (growing buffer)
32  */
33
34 #ifndef WRBUF_H
35 #define WRBUF_H
36
37 #include <yaz/xmalloc.h>
38 #include <yaz/yaz-iconv.h>
39
40 YAZ_BEGIN_CDECL
41
42 typedef struct wrbuf
43 {
44     char *buf;
45     int pos;
46     int size;
47 } wrbuf, *WRBUF;
48
49 YAZ_EXPORT WRBUF wrbuf_alloc(void);
50 YAZ_EXPORT void wrbuf_free(WRBUF b, int free_buf);
51 YAZ_EXPORT void wrbuf_destroy(WRBUF b);
52 YAZ_EXPORT void wrbuf_rewind(WRBUF b);
53 YAZ_EXPORT int wrbuf_grow(WRBUF b, int minsize);
54 YAZ_EXPORT int wrbuf_write(WRBUF b, const char *buf, int size);
55 YAZ_EXPORT int wrbuf_xmlputs_n(WRBUF b, const char *cp, int size);
56 YAZ_EXPORT int wrbuf_puts(WRBUF b, const char *buf);
57 YAZ_EXPORT int wrbuf_puts_replace_char(WRBUF b, const char *buf, 
58                                        const char from, const char to);
59 YAZ_EXPORT int wrbuf_xmlputs(WRBUF b, const char *cp);
60 YAZ_EXPORT void wrbuf_printf(WRBUF b, const char *fmt, ...)
61 #ifdef __GNUC__
62         __attribute__ ((format (printf, 2, 3)))
63 #endif
64         ;
65
66 YAZ_EXPORT int wrbuf_iconv_write(WRBUF b, yaz_iconv_t cd, const char *buf,
67                                  int size);
68 YAZ_EXPORT int wrbuf_iconv_write_cdata(WRBUF b, yaz_iconv_t cd,
69                                        const char *buf, int size);
70 YAZ_EXPORT int wrbuf_iconv_puts(WRBUF b, yaz_iconv_t cd, const char *strz);
71
72 YAZ_EXPORT int wrbuf_iconv_putchar(WRBUF b, yaz_iconv_t cd, int ch);
73
74 YAZ_EXPORT void wrbuf_chop_right(WRBUF b);
75
76 #define wrbuf_len(b) ((b)->pos)
77 #define wrbuf_buf(b) ((b)->buf)
78
79 #define wrbuf_putc(b, c) \
80     (((b)->pos >= (b)->size ? wrbuf_grow(b, 1) : 0),  \
81     (b)->buf[(b)->pos++] = (c), 0)
82
83 YAZ_END_CDECL
84
85 #endif
86 /*
87  * Local variables:
88  * c-basic-offset: 4
89  * indent-tabs-mode: nil
90  * End:
91  * vim: shiftwidth=4 tabstop=8 expandtab
92  */
93