1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2008 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)
42 void wrbuf_rewind(WRBUF b)
47 int wrbuf_grow(WRBUF b, int minsize)
57 if (b->size && !(b->buf =(char *)xrealloc(b->buf, b->size += togrow)))
59 else if (!b->size && !(b->buf = (char *)xmalloc(b->size = togrow)))
64 int wrbuf_write(WRBUF b, const char *buf, int size)
68 if (b->pos + size >= b->size)
70 memcpy(b->buf + b->pos, buf, size);
75 int wrbuf_puts(WRBUF b, const char *buf)
77 wrbuf_write(b, buf, strlen(buf));
81 int wrbuf_puts_replace_char(WRBUF b, const char *buf,
82 const char from, const char to)
95 void wrbuf_chop_right(WRBUF b)
97 while (b->pos && b->buf[b->pos-1] == ' ')
103 int wrbuf_xmlputs(WRBUF b, const char *cp)
105 return wrbuf_xmlputs_n(b, cp, strlen(cp));
108 int wrbuf_xmlputs_n(WRBUF b, const char *cp, int 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, "'");
144 void wrbuf_printf(WRBUF b, const char *fmt, ...)
150 yaz_vsnprintf(buf, sizeof(buf)-1, fmt, ap);
156 static int wrbuf_iconv_write_x(WRBUF b, yaz_iconv_t cd, const char *buf,
162 size_t inbytesleft = size;
163 const char *inp = buf;
166 size_t outbytesleft = sizeof(outbuf);
168 size_t r = yaz_iconv(cd, (char**) &inp, &inbytesleft,
169 &outp, &outbytesleft);
170 if (r == (size_t) (-1))
172 int e = yaz_iconv_error(cd);
173 if (e != YAZ_ICONV_E2BIG)
177 wrbuf_xmlputs_n(b, outbuf, outp - outbuf);
179 wrbuf_write(b, outbuf, outp - outbuf);
185 wrbuf_xmlputs_n(b, buf, size);
187 wrbuf_write(b, buf, size);
192 int wrbuf_iconv_write(WRBUF b, yaz_iconv_t cd, const char *buf, int size)
194 return wrbuf_iconv_write_x(b, cd, buf, size, 0);
197 int wrbuf_iconv_puts(WRBUF b, yaz_iconv_t cd, const char *strz)
199 return wrbuf_iconv_write(b, cd, strz, strlen(strz));
202 int wrbuf_iconv_putchar(WRBUF b, yaz_iconv_t cd, int ch)
206 return wrbuf_iconv_write(b, cd, buf, 1);
209 int wrbuf_iconv_write_cdata(WRBUF b, yaz_iconv_t cd, const char *buf, int size)
211 return wrbuf_iconv_write_x(b, cd, buf, size, 1);
214 int wrbuf_iconv_puts_cdata(WRBUF b, yaz_iconv_t cd, const char *strz)
216 return wrbuf_iconv_write_x(b, cd, strz, strlen(strz), 1);
219 void wrbuf_iconv_reset(WRBUF b, yaz_iconv_t cd)
224 size_t outbytesleft = sizeof(outbuf);
226 size_t r = yaz_iconv(cd, 0, 0, &outp, &outbytesleft);
227 if (r != (size_t) (-1))
228 wrbuf_write(b, outbuf, outp - outbuf);
232 const char *wrbuf_cstr(WRBUF b)
234 wrbuf_putc(b, '\0'); /* add '\0' */
235 (b->pos)--; /* don't include '\0' in count */
239 void wrbuf_cut_right(WRBUF b, size_t no_to_remove)
241 if (no_to_remove > b->pos)
242 no_to_remove = b->pos;
243 b->pos = b->pos - no_to_remove;
246 void wrbuf_puts_escaped(WRBUF b, const char *str)
248 wrbuf_write_escaped(b, str, strlen(str));
251 void wrbuf_write_escaped(WRBUF b, const char *str, size_t len)
254 for (i = 0; i < len; i++)
255 if (str[i] < ' ' || str[i] > 126)
256 wrbuf_printf(b, "\\x%02X", str[i] & 0xff);
258 wrbuf_putc(b, str[i]);
264 * indent-tabs-mode: nil
266 * vim: shiftwidth=4 tabstop=8 expandtab