1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2013 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_insert(WRBUF b, size_t pos, const char *buf, size_t size)
79 if (size <= 0 || pos > b->pos)
81 if (b->pos + size >= b->size)
83 memmove(b->buf + pos + size, b->buf + pos, b->pos - pos);
84 memcpy(b->buf + pos, buf, size);
88 void wrbuf_puts(WRBUF b, const char *buf)
90 wrbuf_write(b, buf, strlen(buf));
93 void wrbuf_vp_puts(const char *buf, void *client_data)
95 WRBUF b = (WRBUF) client_data;
99 void wrbuf_puts_replace_char(WRBUF b, const char *buf,
100 const char from, const char to)
112 void wrbuf_chop_right(WRBUF b)
114 while (b->pos && b->buf[b->pos-1] == ' ')
120 void wrbuf_xmlputs(WRBUF b, const char *cp)
122 wrbuf_xmlputs_n(b, cp, strlen(cp));
125 void wrbuf_xmlputs_n(WRBUF b, const char *cp, size_t size)
129 /* only TAB,CR,LF of ASCII CTRL are allowed in XML 1.0! */
130 if (*cp >= 0 && *cp <= 31)
131 if (*cp != 9 && *cp != 10 && *cp != 13)
133 cp++; /* we silently ignore (delete) these.. */
139 wrbuf_puts(b, "<");
142 wrbuf_puts(b, ">");
145 wrbuf_puts(b, "&");
148 wrbuf_puts(b, """);
151 wrbuf_puts(b, "'");
160 void wrbuf_printf(WRBUF b, const char *fmt, ...)
166 yaz_vsnprintf(buf, sizeof(buf)-1, fmt, ap);
172 int wrbuf_iconv_write_x(WRBUF b, yaz_iconv_t cd, const char *buf,
173 size_t size, int cdata)
179 size_t inbytesleft = size;
180 const char *inp = buf;
183 size_t outbytesleft = sizeof(outbuf);
185 size_t r = yaz_iconv(cd, (char**) &inp, &inbytesleft,
186 &outp, &outbytesleft);
187 if (r == (size_t) (-1))
189 int e = yaz_iconv_error(cd);
190 if (e != YAZ_ICONV_E2BIG)
197 wrbuf_xmlputs_n(b, outbuf, outp - outbuf);
199 wrbuf_write(b, outbuf, outp - outbuf);
205 wrbuf_xmlputs_n(b, buf, size);
207 wrbuf_write(b, buf, size);
212 void wrbuf_iconv_write(WRBUF b, yaz_iconv_t cd, const char *buf, size_t size)
214 wrbuf_iconv_write_x(b, cd, buf, size, 0);
217 void wrbuf_iconv_puts(WRBUF b, yaz_iconv_t cd, const char *strz)
219 wrbuf_iconv_write(b, cd, strz, strlen(strz));
222 void wrbuf_iconv_putchar(WRBUF b, yaz_iconv_t cd, int ch)
226 wrbuf_iconv_write(b, cd, buf, 1);
229 void wrbuf_iconv_write_cdata(WRBUF b, yaz_iconv_t cd, const char *buf, size_t size)
231 wrbuf_iconv_write_x(b, cd, buf, size, 1);
234 void wrbuf_iconv_puts_cdata(WRBUF b, yaz_iconv_t cd, const char *strz)
236 wrbuf_iconv_write_x(b, cd, strz, strlen(strz), 1);
239 void wrbuf_iconv_reset(WRBUF b, yaz_iconv_t cd)
244 size_t outbytesleft = sizeof(outbuf);
246 size_t r = yaz_iconv(cd, 0, 0, &outp, &outbytesleft);
247 if (r != (size_t) (-1))
248 wrbuf_write(b, outbuf, outp - outbuf);
252 const char *wrbuf_cstr(WRBUF b)
254 wrbuf_putc(b, '\0'); /* add '\0' */
255 (b->pos)--; /* don't include '\0' in count */
259 void wrbuf_cut_right(WRBUF b, size_t no_to_remove)
261 if (no_to_remove > b->pos)
262 no_to_remove = b->pos;
263 b->pos = b->pos - no_to_remove;
266 void wrbuf_puts_escaped(WRBUF b, const char *str)
268 wrbuf_write_escaped(b, str, strlen(str));
271 void wrbuf_write_escaped(WRBUF b, const char *str, size_t len)
274 for (i = 0; i < len; i++)
275 if (str[i] < ' ' || str[i] > 126)
276 wrbuf_printf(b, "\\x%02X", str[i] & 0xff);
278 wrbuf_putc(b, str[i]);
284 * c-file-style: "Stroustrup"
285 * indent-tabs-mode: nil
287 * vim: shiftwidth=4 tabstop=8 expandtab