1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) Index Data
3 * See the file LICENSE for details.
8 * \brief Implements WRBUF (growing buffer)
21 #include <yaz/wrbuf.h>
22 #include <yaz/snprintf.h>
23 #include <yaz/yaz-iconv.h>
25 WRBUF wrbuf_alloc(void)
29 if (!(n = (WRBUF)xmalloc(sizeof(*n))))
38 void wrbuf_destroy(WRBUF b)
47 void wrbuf_rewind(WRBUF b)
52 int wrbuf_grow(WRBUF b, size_t minsize)
62 b->buf = (char *) xrealloc(b->buf, 1 + (b->size += togrow));
68 void wrbuf_write(WRBUF b, const char *buf, size_t size)
72 if (b->pos + size >= b->size)
74 memcpy(b->buf + b->pos, buf, size);
78 void wrbuf_insert(WRBUF b, size_t pos, const char *buf, size_t size)
80 if (size <= 0 || pos > b->pos)
82 if (b->pos + size >= b->size)
84 memmove(b->buf + pos + size, b->buf + pos, b->pos - pos);
85 memcpy(b->buf + pos, buf, size);
89 void wrbuf_puts(WRBUF b, const char *buf)
91 wrbuf_write(b, buf, strlen(buf));
94 void wrbuf_vp_puts(const char *buf, void *client_data)
96 WRBUF b = (WRBUF) client_data;
100 void wrbuf_puts_replace_char(WRBUF b, const char *buf,
101 const char from, const char to)
113 void wrbuf_chop_right(WRBUF b)
115 while (b->pos && b->buf[b->pos-1] == ' ')
121 void wrbuf_xmlputs(WRBUF b, const char *cp)
123 wrbuf_xmlputs_n(b, cp, strlen(cp));
126 void wrbuf_xmlputs_n(WRBUF b, const char *cp, size_t size)
130 /* only TAB,CR,LF of ASCII CTRL are allowed in XML 1.0! */
131 if (*cp >= 0 && *cp <= 31)
132 if (*cp != 9 && *cp != 10 && *cp != 13)
134 cp++; /* we silently ignore (delete) these.. */
140 wrbuf_puts(b, "<");
143 wrbuf_puts(b, ">");
146 wrbuf_puts(b, "&");
149 wrbuf_puts(b, """);
152 wrbuf_puts(b, "'");
161 void wrbuf_printf(WRBUF b, const char *fmt, ...)
167 yaz_vsnprintf(buf, sizeof(buf)-1, fmt, ap);
173 int wrbuf_iconv_write2(WRBUF b, yaz_iconv_t cd, const char *buf,
175 void (*wfunc)(WRBUF, const char *, size_t))
181 size_t inbytesleft = size;
182 const char *inp = buf;
185 size_t outbytesleft = sizeof(outbuf);
187 size_t r = yaz_iconv(cd, (char**) &inp, &inbytesleft,
188 &outp, &outbytesleft);
189 if (r == (size_t) (-1))
191 int e = yaz_iconv_error(cd);
192 if (e != YAZ_ICONV_E2BIG)
198 (*wfunc)(b, outbuf, outp - outbuf);
202 (*wfunc)(b, buf, size);
206 int wrbuf_iconv_write_x(WRBUF b, yaz_iconv_t cd, const char *buf,
207 size_t size, int cdata)
209 return wrbuf_iconv_write2(b, cd, buf, size,
210 cdata ? wrbuf_xmlputs_n : wrbuf_write);
213 void wrbuf_iconv_write(WRBUF b, yaz_iconv_t cd, const char *buf, size_t size)
215 wrbuf_iconv_write2(b, cd, buf, size, wrbuf_write);
218 void wrbuf_iconv_puts(WRBUF b, yaz_iconv_t cd, const char *strz)
220 wrbuf_iconv_write(b, cd, strz, strlen(strz));
223 void wrbuf_iconv_putchar(WRBUF b, yaz_iconv_t cd, int ch)
227 wrbuf_iconv_write(b, cd, buf, 1);
230 void wrbuf_iconv_write_cdata(WRBUF b, yaz_iconv_t cd, const char *buf, size_t size)
232 wrbuf_iconv_write2(b, cd, buf, size, wrbuf_xmlputs_n);
235 void wrbuf_iconv_puts_cdata(WRBUF b, yaz_iconv_t cd, const char *strz)
237 wrbuf_iconv_write2(b, cd, strz, strlen(strz), wrbuf_xmlputs_n);
240 void wrbuf_iconv_json_write(WRBUF b, yaz_iconv_t cd,
241 const char *buf, size_t size)
243 wrbuf_iconv_write2(b, cd, buf, size, wrbuf_json_write);
246 void wrbuf_iconv_json_puts(WRBUF b, yaz_iconv_t cd, const char *strz)
248 wrbuf_iconv_write2(b, cd, strz, strlen(strz), wrbuf_json_write);
251 void wrbuf_iconv_reset(WRBUF b, yaz_iconv_t cd)
256 size_t outbytesleft = sizeof(outbuf);
258 size_t r = yaz_iconv(cd, 0, 0, &outp, &outbytesleft);
259 if (r != (size_t) (-1))
260 wrbuf_write(b, outbuf, outp - outbuf);
264 const char *wrbuf_cstr(WRBUF b)
266 assert(b && b->pos <= b->size);
267 b->buf[b->pos] = '\0';
271 const char *wrbuf_cstr_null(WRBUF b)
273 if (!b || b->pos == 0)
275 assert(b->pos <= b->size);
276 b->buf[b->pos] = '\0';
280 void wrbuf_cut_right(WRBUF b, size_t no_to_remove)
282 if (no_to_remove > b->pos)
283 no_to_remove = b->pos;
284 b->pos = b->pos - no_to_remove;
287 void wrbuf_puts_escaped(WRBUF b, const char *str)
289 wrbuf_write_escaped(b, str, strlen(str));
292 void wrbuf_write_escaped(WRBUF b, const char *str, size_t len)
295 for (i = 0; i < len; i++)
296 if (str[i] < ' ' || str[i] > 126)
297 wrbuf_printf(b, "\\x%02X", str[i] & 0xff);
299 wrbuf_putc(b, str[i]);
302 void wrbuf_json_write(WRBUF b, const char *cp, size_t sz)
305 for (i = 0; i < sz; i++)
307 if (cp[i] > 0 && cp[i] < 32)
312 case '\b': wrbuf_putc(b, 'b'); break;
313 case '\f': wrbuf_putc(b, 'f'); break;
314 case '\n': wrbuf_putc(b, 'n'); break;
315 case '\r': wrbuf_putc(b, 'r'); break;
316 case '\t': wrbuf_putc(b, 't'); break;
318 wrbuf_printf(b, "u%04x", cp[i]);
321 else if (cp[i] == '"')
323 wrbuf_putc(b, '\\'); wrbuf_putc(b, '"');
325 else if (cp[i] == '\\')
327 wrbuf_putc(b, '\\'); wrbuf_putc(b, '\\');
330 { /* leave encoding as raw UTF-8 */
331 wrbuf_putc(b, cp[i]);
337 void wrbuf_json_puts(WRBUF b, const char *str)
339 wrbuf_json_write(b, str, strlen(str));
345 * c-file-style: "Stroustrup"
346 * indent-tabs-mode: nil
348 * vim: shiftwidth=4 tabstop=8 expandtab