Comment WRBUF. Remove wrbuf_vputs
[yaz-moved-to-github.git] / include / yaz / wrbuf.h
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2010 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
28 /**
29  * \file wrbuf.h
30  * \brief Header for WRBUF (growing buffer)
31  */
32
33 #ifndef WRBUF_H
34 #define WRBUF_H
35
36 #include <yaz/xmalloc.h>
37 #include <yaz/yaz-iconv.h>
38
39 YAZ_BEGIN_CDECL
40
41 /** \brief string buffer */
42 typedef struct wrbuf
43 {
44     char *buf;
45     size_t pos;
46     size_t size;
47 } wrbuf, *WRBUF;
48
49 /** \brief construct WRBUF
50     \returns WRBUF
51  */
52 YAZ_EXPORT WRBUF wrbuf_alloc(void);
53
54 /** \brief destroy WRBUF and its buffer
55     \param b WRBUF
56  */
57 YAZ_EXPORT void wrbuf_destroy(WRBUF b);
58
59 /** \brief empty WRBUF content (length of buffer set to 0)
60     \param b WRBUF
61  */
62 YAZ_EXPORT void wrbuf_rewind(WRBUF b);
63
64 /** \brief append constant size buffer to WRBU
65     \param b WRBUF
66     \param buf buffer
67     \param size size of buffer
68  */
69 YAZ_EXPORT void wrbuf_write(WRBUF b, const char *buf, size_t size);
70
71 /** \brief appends C-string to WRBUF
72     \param b WRBUF
73     \param buf C-string (0-terminated)
74  */
75 YAZ_EXPORT void wrbuf_puts(WRBUF b, const char *buf);
76
77 /** \brief writes buffer of certain size to WRBUF and XML encode (as CDATA)
78     \param b WRBUF
79     \param cp CDATA
80     \param size size of CDATA
81  */
82 YAZ_EXPORT void wrbuf_xmlputs_n(WRBUF b, const char *cp, size_t size);
83
84 /** \brief writes C-String to WRBUF and XML encode (as CDATA)
85     \param b WRBUF
86     \param cp CDATA buffer (0-terminated)
87  */
88 YAZ_EXPORT void wrbuf_xmlputs(WRBUF b, const char *cp);
89
90 /** \brief puts buf to WRBUF and replaces a single char
91     \param b WRBUF
92     \param buf buffer to append (C-string)
93     \param from character "from"
94     \param to charcter "to"
95 */
96 YAZ_EXPORT void wrbuf_puts_replace_char(WRBUF b, const char *buf, 
97                                         const char from, const char to);
98
99 /** \brief writes C-string to WRBUF and escape non-ASCII characters
100     \param b WRBUF
101     \param str C-string
102
103     Non-ASCII characters will be presented as \\xDD .
104  */
105 YAZ_EXPORT void wrbuf_puts_escaped(WRBUF b, const char *str);
106
107 /** \brief writes buffer to WRBUF and escape non-ASCII characters
108     \param b WRBUF
109     \param buf buffer
110     \param len size of buffer
111
112     Non-ASCII characters will be presented as \\xDD .
113  */
114 YAZ_EXPORT void wrbuf_write_escaped(WRBUF b, const char *buf, size_t len);
115
116 /** \brief writes printf result to WRBUF
117     \param b WRBUF
118     \param fmt printf-like format
119  */
120 YAZ_EXPORT void wrbuf_printf(WRBUF b, const char *fmt, ...)
121 #ifdef __GNUC__
122         __attribute__ ((format (printf, 2, 3)))
123 #endif
124         ;
125 /** \brief iconv converts buffer and appends to WRBUF
126     \param b WRBUF
127     \param cd iconv handle
128     \param buf buffer
129     \param size size of buffer
130 */
131 YAZ_EXPORT void wrbuf_iconv_write(WRBUF b, yaz_iconv_t cd, const char *buf,
132                                  size_t size);
133
134 /** \brief iconv converts buffer and appends to WRBUF as XML CDATA
135     \param b WRBUF
136     \param cd iconv handle
137     \param buf buffer
138     \param size size of buffer
139 */
140 YAZ_EXPORT void wrbuf_iconv_write_cdata(WRBUF b, yaz_iconv_t cd,
141                                        const char *buf, size_t size);
142
143 /** \brief iconv converts C-string and appends to WRBUF
144     \param b WRBUF
145     \param cd iconv handle
146     \param str C-string
147 */
148 YAZ_EXPORT void wrbuf_iconv_puts(WRBUF b, yaz_iconv_t cd, const char *str);
149
150 /** \brief iconv converts C-string and appends to WRBUF as XML CDATA
151     \param b WRBUF
152     \param cd iconv handle
153     \param str C-string
154 */
155 YAZ_EXPORT void wrbuf_iconv_puts_cdata(WRBUF b, yaz_iconv_t cd,
156                                        const char *str);
157
158 /** \brief iconv converts character and appends to WRBUF
159     \param b WRBUF
160     \param cd iconv handle
161     \param ch character
162 */
163 YAZ_EXPORT void wrbuf_iconv_putchar(WRBUF b, yaz_iconv_t cd, int ch);
164
165 /** \brief iconv reset(flush) to WRBUF
166     \param b
167     \param cd iconv handle
168     
169     This function calls iconv(cd, 0, 0, ..) to make it
170     flush any remaining content.
171 */
172 YAZ_EXPORT void wrbuf_iconv_reset(WRBUF b, yaz_iconv_t cd);
173
174 /** \brief chips traling blanks away from WRBUF
175     \param b WRBUF
176 */
177 YAZ_EXPORT void wrbuf_chop_right(WRBUF b);
178
179 /** \brief cut size of WRBUF
180     \param b WRBUF
181     \param no_to_remove number of bytes to remove
182  */
183 YAZ_EXPORT void wrbuf_cut_right(WRBUF b, size_t no_to_remove);
184
185 /** \brief grow WRBUF larger 
186     \param b WRBUF
187     \param minsize make WRBUF at least this size
188
189     This function is normally not used by applications
190 */
191 YAZ_EXPORT int wrbuf_grow(WRBUF b, size_t minsize);
192
193 #define wrbuf_len(b) ((b)->pos)
194 #define wrbuf_buf(b) ((b)->buf)
195
196 /** \brief returns WRBUF content as C-string
197     \param b WRBUF
198     \returns C-string
199 */
200 YAZ_EXPORT const char *wrbuf_cstr(WRBUF b);
201
202 #define wrbuf_putc(b, c) \
203     (((b)->pos >= (b)->size ? wrbuf_grow(b, 1) : 0),  \
204     (b)->buf[(b)->pos++] = (c), 0)
205
206 YAZ_END_CDECL
207
208 #endif
209 /*
210  * Local variables:
211  * c-basic-offset: 4
212  * c-file-style: "Stroustrup"
213  * indent-tabs-mode: nil
214  * End:
215  * vim: shiftwidth=4 tabstop=8 expandtab
216  */
217