1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2011 Index Data.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
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.
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.
30 * \brief Header for WRBUF (growing buffer)
36 #include <yaz/xmalloc.h>
37 #include <yaz/yaz-iconv.h>
41 /** \brief string buffer */
49 /** \brief construct WRBUF
52 YAZ_EXPORT WRBUF wrbuf_alloc(void);
54 /** \brief destroy WRBUF and its buffer
57 For YAZ 4.0.2 WRBUF b may be NULL.
59 YAZ_EXPORT void wrbuf_destroy(WRBUF b);
61 /** \brief empty WRBUF content (length of buffer set to 0)
64 YAZ_EXPORT void wrbuf_rewind(WRBUF b);
66 /** \brief append constant size buffer to WRBU
69 \param size size of buffer
71 YAZ_EXPORT void wrbuf_write(WRBUF b, const char *buf, size_t size);
73 /** \brief appends C-string to WRBUF
75 \param buf C-string (0-terminated)
77 YAZ_EXPORT void wrbuf_puts(WRBUF b, const char *buf);
79 /** \brief appends C-string to WRBUF - void pointer variant
81 \param client_data assumed WRBUF
83 void wrbuf_vp_puts(const char *buf, void *client_data);
85 /** \brief writes buffer of certain size to WRBUF and XML encode (as CDATA)
88 \param size size of CDATA
90 YAZ_EXPORT void wrbuf_xmlputs_n(WRBUF b, const char *cp, size_t size);
92 /** \brief writes C-String to WRBUF and XML encode (as CDATA)
94 \param cp CDATA buffer (0-terminated)
96 YAZ_EXPORT void wrbuf_xmlputs(WRBUF b, const char *cp);
98 /** \brief puts buf to WRBUF and replaces a single char
100 \param buf buffer to append (C-string)
101 \param from character "from"
102 \param to charcter "to"
104 YAZ_EXPORT void wrbuf_puts_replace_char(WRBUF b, const char *buf,
105 const char from, const char to);
107 /** \brief writes C-string to WRBUF and escape non-ASCII characters
111 Non-ASCII characters will be presented as \\xDD .
113 YAZ_EXPORT void wrbuf_puts_escaped(WRBUF b, const char *str);
115 /** \brief writes buffer to WRBUF and escape non-ASCII characters
118 \param len size of buffer
120 Non-ASCII characters will be presented as \\xDD .
122 YAZ_EXPORT void wrbuf_write_escaped(WRBUF b, const char *buf, size_t len);
124 /** \brief writes printf result to WRBUF
126 \param fmt printf-like format
128 YAZ_EXPORT void wrbuf_printf(WRBUF b, const char *fmt, ...)
130 __attribute__ ((format (printf, 2, 3)))
134 /** \brief general writer of string using iconv and cdata
136 \param cd iconv handle (0 for no conversion)
138 \param size size of buffer
139 \param cdata non-zero for CDATA; 0 for cdata
140 \returns -1 if invalid sequence was encountered (truncation in effect)
141 \returns 0 if buffer could be converted and written
143 int wrbuf_iconv_write_x(WRBUF b, yaz_iconv_t cd, const char *buf,
144 size_t size, int cdata);
146 /** \brief iconv converts buffer and appends to WRBUF
148 \param cd iconv handle
150 \param size size of buffer
152 YAZ_EXPORT void wrbuf_iconv_write(WRBUF b, yaz_iconv_t cd, const char *buf,
155 /** \brief iconv converts buffer and appends to WRBUF as XML CDATA
157 \param cd iconv handle
159 \param size size of buffer
161 YAZ_EXPORT void wrbuf_iconv_write_cdata(WRBUF b, yaz_iconv_t cd,
162 const char *buf, size_t size);
164 /** \brief iconv converts C-string and appends to WRBUF
166 \param cd iconv handle
169 YAZ_EXPORT void wrbuf_iconv_puts(WRBUF b, yaz_iconv_t cd, const char *str);
171 /** \brief iconv converts C-string and appends to WRBUF as XML CDATA
173 \param cd iconv handle
176 YAZ_EXPORT void wrbuf_iconv_puts_cdata(WRBUF b, yaz_iconv_t cd,
179 /** \brief iconv converts character and appends to WRBUF
181 \param cd iconv handle
184 YAZ_EXPORT void wrbuf_iconv_putchar(WRBUF b, yaz_iconv_t cd, int ch);
186 /** \brief iconv reset(flush) to WRBUF
188 \param cd iconv handle
190 This function calls iconv(cd, 0, 0, ..) to make it
191 flush any remaining content.
193 YAZ_EXPORT void wrbuf_iconv_reset(WRBUF b, yaz_iconv_t cd);
195 /** \brief chips traling blanks away from WRBUF
198 YAZ_EXPORT void wrbuf_chop_right(WRBUF b);
200 /** \brief cut size of WRBUF
202 \param no_to_remove number of bytes to remove
204 YAZ_EXPORT void wrbuf_cut_right(WRBUF b, size_t no_to_remove);
206 /** \brief grow WRBUF larger
208 \param minsize make WRBUF at least this size
210 This function is normally not used by applications
212 YAZ_EXPORT int wrbuf_grow(WRBUF b, size_t minsize);
214 #define wrbuf_len(b) ((b)->pos)
215 #define wrbuf_buf(b) ((b)->buf)
217 /** \brief returns WRBUF content as C-string
221 YAZ_EXPORT const char *wrbuf_cstr(WRBUF b);
223 #define wrbuf_putc(b, c) \
224 ((void) ((b)->pos >= (b)->size ? wrbuf_grow(b, 1) : 0), \
225 (b)->buf[(b)->pos++] = (c), 0)
233 * c-file-style: "Stroustrup"
234 * indent-tabs-mode: nil
236 * vim: shiftwidth=4 tabstop=8 expandtab