X-Git-Url: http://git.indexdata.com/?p=yaz-moved-to-github.git;a=blobdiff_plain;f=src%2Fwrbuf.c;h=a51adee7b7bb15827286c399e61a41a001ec896d;hp=0af358263abeb138dcfb30fe0bd1425e4dc02e2a;hb=9afc8581574131c6d3ecea36c85740c07714318c;hpb=88d3bedf772316f87e1996f655ccf8d1e2589755 diff --git a/src/wrbuf.c b/src/wrbuf.c index 0af3582..a51adee 100644 --- a/src/wrbuf.c +++ b/src/wrbuf.c @@ -1,5 +1,5 @@ /* This file is part of the YAZ toolkit. - * Copyright (C) 1995-2010 Index Data + * Copyright (C) 1995-2013 Index Data * See the file LICENSE for details. */ @@ -35,8 +35,11 @@ WRBUF wrbuf_alloc(void) void wrbuf_destroy(WRBUF b) { - xfree(b->buf); - xfree(b); + if (b) + { + xfree(b->buf); + xfree(b); + } } void wrbuf_rewind(WRBUF b) @@ -71,17 +74,29 @@ void wrbuf_write(WRBUF b, const char *buf, size_t size) b->pos += size; } +void wrbuf_insert(WRBUF b, size_t pos, const char *buf, size_t size) +{ + if (size <= 0 || pos > b->pos) + return; + if (b->pos + size >= b->size) + wrbuf_grow(b, size); + memmove(b->buf + pos + size, b->buf + pos, b->pos - pos); + memcpy(b->buf + pos, buf, size); + b->pos += size; +} + void wrbuf_puts(WRBUF b, const char *buf) { wrbuf_write(b, buf, strlen(buf)); } -void wrbuf_vputs(const char *buf, void *client_data) +void wrbuf_vp_puts(const char *buf, void *client_data) { - wrbuf_write((WRBUF) client_data, buf, strlen(buf)); + WRBUF b = (WRBUF) client_data; + wrbuf_puts(b, buf); } -void wrbuf_puts_replace_char(WRBUF b, const char *buf, +void wrbuf_puts_replace_char(WRBUF b, const char *buf, const char from, const char to) { while(*buf) @@ -154,9 +169,10 @@ void wrbuf_printf(WRBUF b, const char *fmt, ...) va_end(ap); } -static void wrbuf_iconv_write_x(WRBUF b, yaz_iconv_t cd, const char *buf, - size_t size, int cdata) +int wrbuf_iconv_write_x(WRBUF b, yaz_iconv_t cd, const char *buf, + size_t size, int cdata) { + int ret = 0; if (cd) { char outbuf[128]; @@ -172,7 +188,10 @@ static void wrbuf_iconv_write_x(WRBUF b, yaz_iconv_t cd, const char *buf, { int e = yaz_iconv_error(cd); if (e != YAZ_ICONV_E2BIG) + { + ret = -1; break; + } } if (cdata) wrbuf_xmlputs_n(b, outbuf, outp - outbuf); @@ -187,6 +206,7 @@ static void wrbuf_iconv_write_x(WRBUF b, yaz_iconv_t cd, const char *buf, else wrbuf_write(b, buf, size); } + return ret; } void wrbuf_iconv_write(WRBUF b, yaz_iconv_t cd, const char *buf, size_t size)