1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2011 Index Data
3 * See the file LICENSE for details.
8 * \brief Implements WRBUF (growing buffer)
20 #include <yaz/wrbuf.h>
21 #include <yaz/snprintf.h>
22 #include <yaz/yaz-iconv.h>
24 WRBUF wrbuf_alloc(void)
28 if (!(n = (WRBUF)xmalloc(sizeof(*n))))
36 void wrbuf_destroy(WRBUF b)
45 void wrbuf_rewind(WRBUF b)
50 int wrbuf_grow(WRBUF b, size_t minsize)
60 if (b->size && !(b->buf =(char *)xrealloc(b->buf, b->size += togrow)))
62 else if (!b->size && !(b->buf = (char *)xmalloc(b->size = togrow)))
67 void wrbuf_write(WRBUF b, const char *buf, size_t size)
71 if (b->pos + size >= b->size)
73 memcpy(b->buf + b->pos, buf, size);
77 void wrbuf_puts(WRBUF b, const char *buf)
79 wrbuf_write(b, buf, strlen(buf));
82 void wrbuf_puts_replace_char(WRBUF b, const char *buf,
83 const char from, const char to)
95 void wrbuf_chop_right(WRBUF b)
97 while (b->pos && b->buf[b->pos-1] == ' ')
103 void wrbuf_xmlputs(WRBUF b, const char *cp)
105 wrbuf_xmlputs_n(b, cp, strlen(cp));
108 void wrbuf_xmlputs_n(WRBUF b, const char *cp, size_t size)
112 /* only TAB,CR,LF of ASCII CTRL are allowed in XML 1.0! */
113 if (*cp >= 0 && *cp <= 31)
114 if (*cp != 9 && *cp != 10 && *cp != 13)
116 cp++; /* we silently ignore (delete) these.. */
122 wrbuf_puts(b, "<");
125 wrbuf_puts(b, ">");
128 wrbuf_puts(b, "&");
131 wrbuf_puts(b, """);
134 wrbuf_puts(b, "'");
143 void wrbuf_printf(WRBUF b, const char *fmt, ...)
149 yaz_vsnprintf(buf, sizeof(buf)-1, fmt, ap);
155 static void wrbuf_iconv_write_x(WRBUF b, yaz_iconv_t cd, const char *buf,
156 size_t size, int cdata)
161 size_t inbytesleft = size;
162 const char *inp = buf;
165 size_t outbytesleft = sizeof(outbuf);
167 size_t r = yaz_iconv(cd, (char**) &inp, &inbytesleft,
168 &outp, &outbytesleft);
169 if (r == (size_t) (-1))
171 int e = yaz_iconv_error(cd);
172 if (e != YAZ_ICONV_E2BIG)
176 wrbuf_xmlputs_n(b, outbuf, outp - outbuf);
178 wrbuf_write(b, outbuf, outp - outbuf);
184 wrbuf_xmlputs_n(b, buf, size);
186 wrbuf_write(b, buf, size);
190 void wrbuf_iconv_write(WRBUF b, yaz_iconv_t cd, const char *buf, size_t size)
192 wrbuf_iconv_write_x(b, cd, buf, size, 0);
195 void wrbuf_iconv_puts(WRBUF b, yaz_iconv_t cd, const char *strz)
197 wrbuf_iconv_write(b, cd, strz, strlen(strz));
200 void wrbuf_iconv_putchar(WRBUF b, yaz_iconv_t cd, int ch)
204 wrbuf_iconv_write(b, cd, buf, 1);
207 void wrbuf_iconv_write_cdata(WRBUF b, yaz_iconv_t cd, const char *buf, size_t size)
209 wrbuf_iconv_write_x(b, cd, buf, size, 1);
212 void wrbuf_iconv_puts_cdata(WRBUF b, yaz_iconv_t cd, const char *strz)
214 wrbuf_iconv_write_x(b, cd, strz, strlen(strz), 1);
217 void wrbuf_iconv_reset(WRBUF b, yaz_iconv_t cd)
222 size_t outbytesleft = sizeof(outbuf);
224 size_t r = yaz_iconv(cd, 0, 0, &outp, &outbytesleft);
225 if (r != (size_t) (-1))
226 wrbuf_write(b, outbuf, outp - outbuf);
230 const char *wrbuf_cstr(WRBUF b)
232 wrbuf_putc(b, '\0'); /* add '\0' */
233 (b->pos)--; /* don't include '\0' in count */
237 void wrbuf_cut_right(WRBUF b, size_t no_to_remove)
239 if (no_to_remove > b->pos)
240 no_to_remove = b->pos;
241 b->pos = b->pos - no_to_remove;
244 void wrbuf_puts_escaped(WRBUF b, const char *str)
246 wrbuf_write_escaped(b, str, strlen(str));
249 void wrbuf_write_escaped(WRBUF b, const char *str, size_t len)
252 for (i = 0; i < len; i++)
253 if (str[i] < ' ' || str[i] > 126)
254 wrbuf_printf(b, "\\x%02X", str[i] & 0xff);
256 wrbuf_putc(b, str[i]);
262 * c-file-style: "Stroustrup"
263 * indent-tabs-mode: nil
265 * vim: shiftwidth=4 tabstop=8 expandtab