From 22f541bb03c1fc3e9edbab5f4aaf01e9232bd003 Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Tue, 15 Mar 2011 09:43:18 +0100 Subject: [PATCH] Add new function wrbuf_iconv_write_x Is a generic WRBUF writer for iconv+cdata as an option . Returns if writing was successful or not. --- include/yaz/wrbuf.h | 13 +++++++++++++ src/wrbuf.c | 9 +++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/include/yaz/wrbuf.h b/include/yaz/wrbuf.h index b222216..56b5e60 100644 --- a/include/yaz/wrbuf.h +++ b/include/yaz/wrbuf.h @@ -124,6 +124,19 @@ YAZ_EXPORT void wrbuf_printf(WRBUF b, const char *fmt, ...) __attribute__ ((format (printf, 2, 3))) #endif ; + +/** \brief general writer of string using iconv and cdata + \param b WRBUF + \param cd iconv handle (0 for no conversion) + \param buf buffer + \param size size of buffer + \param cdata non-zero for CDATA; 0 for cdata + \returns -1 if invalid sequence was encountered (truncation in effect) + \returns 0 if buffer could be converted and written +*/ +int wrbuf_iconv_write_x(WRBUF b, yaz_iconv_t cd, const char *buf, + size_t size, int cdata); + /** \brief iconv converts buffer and appends to WRBUF \param b WRBUF \param cd iconv handle diff --git a/src/wrbuf.c b/src/wrbuf.c index fd73beb..06e49de 100644 --- a/src/wrbuf.c +++ b/src/wrbuf.c @@ -152,9 +152,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]; @@ -170,7 +171,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); @@ -185,6 +189,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) -- 1.7.10.4