From: Adam Dickmeiss Date: Thu, 5 Dec 2013 16:19:55 +0000 (+0100) Subject: Add wrbuf_iconv_json_{write,puts} X-Git-Tag: v5.0.5~15 X-Git-Url: http://sru.miketaylor.org.uk/cgi-bin?a=commitdiff_plain;h=574ae68dc570706b4468b29e82681efe15bc7180;p=yaz-moved-to-github.git Add wrbuf_iconv_json_{write,puts} --- diff --git a/include/yaz/json.h b/include/yaz/json.h index d3974ae..8a4272f 100644 --- a/include/yaz/json.h +++ b/include/yaz/json.h @@ -194,21 +194,6 @@ void json_write_wrbuf(struct json_node *node, WRBUF result); YAZ_EXPORT void json_write_wrbuf_pretty(struct json_node *node, WRBUF result); -/** \brief writes JSON text to WRBUF with escaping - \param b result - \param str input string to be encoded -*/ -YAZ_EXPORT -void wrbuf_json_puts(WRBUF b, const char *str); - -/** \brief writes JSON text to WRBUF with escaping - \param b result - \param cp char buffer - \param sz size of char buffer -*/ -YAZ_EXPORT -void wrbuf_json_write(WRBUF b, const char *cp, size_t sz); - YAZ_END_CDECL #endif diff --git a/include/yaz/wrbuf.h b/include/yaz/wrbuf.h index d716af0..3864c3e 100644 --- a/include/yaz/wrbuf.h +++ b/include/yaz/wrbuf.h @@ -232,6 +232,29 @@ YAZ_EXPORT const char *wrbuf_cstr(WRBUF b); ((void) ((b)->pos >= (b)->size ? wrbuf_grow(b, 1) : 0), \ (b)->buf[(b)->pos++] = (c), 0) + +/** \brief writes JSON text to WRBUF with escaping + \param b result + \param str input string to be encoded +*/ +YAZ_EXPORT +void wrbuf_json_puts(WRBUF b, const char *str); + +/** \brief writes JSON text to WRBUF with escaping + \param b result + \param cp char buffer + \param sz size of char buffer +*/ +YAZ_EXPORT +void wrbuf_json_write(WRBUF b, const char *cp, size_t sz); + +YAZ_EXPORT +void wrbuf_iconv_json_write(WRBUF b, yaz_iconv_t cd, + const char *buf, size_t size); + +YAZ_EXPORT +void wrbuf_iconv_json_puts(WRBUF b, yaz_iconv_t cd, const char *strz); + YAZ_END_CDECL #endif diff --git a/src/json.c b/src/json.c index 2364e43..5526287 100644 --- a/src/json.c +++ b/src/json.c @@ -462,46 +462,6 @@ struct json_node *json_parse(const char *json_str, const char **errmsg) return json_parse2(json_str, errmsg, 0); } -void wrbuf_json_write(WRBUF b, const char *cp, size_t sz) -{ - size_t i; - for (i = 0; i < sz; i++) - { - if (cp[i] > 0 && cp[i] < 32) - { - wrbuf_putc(b, '\\'); - switch (cp[i]) - { - case '\b': wrbuf_putc(b, 'b'); break; - case '\f': wrbuf_putc(b, 'f'); break; - case '\n': wrbuf_putc(b, 'n'); break; - case '\r': wrbuf_putc(b, 'r'); break; - case '\t': wrbuf_putc(b, 't'); break; - default: - wrbuf_printf(b, "u%04x", cp[i]); - } - } - else if (cp[i] == '"') - { - wrbuf_putc(b, '\\'); wrbuf_putc(b, '"'); - } - else if (cp[i] == '\\') - { - wrbuf_putc(b, '\\'); wrbuf_putc(b, '\\'); - } - else - { /* leave encoding as raw UTF-8 */ - wrbuf_putc(b, cp[i]); - } - } - -} - -void wrbuf_json_puts(WRBUF b, const char *str) -{ - wrbuf_json_write(b, str, strlen(str)); -} - static void json_indent(WRBUF result, int indent) { size_t l = wrbuf_len(result); diff --git a/src/wrbuf.c b/src/wrbuf.c index a51adee..871028d 100644 --- a/src/wrbuf.c +++ b/src/wrbuf.c @@ -193,18 +193,33 @@ int wrbuf_iconv_write_x(WRBUF b, yaz_iconv_t cd, const char *buf, break; } } - if (cdata) - wrbuf_xmlputs_n(b, outbuf, outp - outbuf); - else + switch (cdata) + { + case 0: wrbuf_write(b, outbuf, outp - outbuf); + break; + case 1: + wrbuf_xmlputs_n(b, outbuf, outp - outbuf); + break; + case 2: + wrbuf_json_write(b, outbuf, outp - outbuf); + break; + } } } else { - if (cdata) - wrbuf_xmlputs_n(b, buf, size); - else + switch (cdata) + { + case 0: wrbuf_write(b, buf, size); + break; + case 1: + wrbuf_xmlputs_n(b, buf, size); + break; + case 2: + wrbuf_json_write(b, buf, size); + } } return ret; } @@ -236,6 +251,17 @@ void wrbuf_iconv_puts_cdata(WRBUF b, yaz_iconv_t cd, const char *strz) wrbuf_iconv_write_x(b, cd, strz, strlen(strz), 1); } +void wrbuf_iconv_json_write(WRBUF b, yaz_iconv_t cd, + const char *buf, size_t size) +{ + wrbuf_iconv_write_x(b, cd, buf, size, 2); +} + +void wrbuf_iconv_json_puts(WRBUF b, yaz_iconv_t cd, const char *strz) +{ + wrbuf_iconv_write_x(b, cd, strz, strlen(strz), 2); +} + void wrbuf_iconv_reset(WRBUF b, yaz_iconv_t cd) { if (cd) @@ -278,6 +304,46 @@ void wrbuf_write_escaped(WRBUF b, const char *str, size_t len) wrbuf_putc(b, str[i]); } +void wrbuf_json_write(WRBUF b, const char *cp, size_t sz) +{ + size_t i; + for (i = 0; i < sz; i++) + { + if (cp[i] > 0 && cp[i] < 32) + { + wrbuf_putc(b, '\\'); + switch (cp[i]) + { + case '\b': wrbuf_putc(b, 'b'); break; + case '\f': wrbuf_putc(b, 'f'); break; + case '\n': wrbuf_putc(b, 'n'); break; + case '\r': wrbuf_putc(b, 'r'); break; + case '\t': wrbuf_putc(b, 't'); break; + default: + wrbuf_printf(b, "u%04x", cp[i]); + } + } + else if (cp[i] == '"') + { + wrbuf_putc(b, '\\'); wrbuf_putc(b, '"'); + } + else if (cp[i] == '\\') + { + wrbuf_putc(b, '\\'); wrbuf_putc(b, '\\'); + } + else + { /* leave encoding as raw UTF-8 */ + wrbuf_putc(b, cp[i]); + } + } + +} + +void wrbuf_json_puts(WRBUF b, const char *str) +{ + wrbuf_json_write(b, str, strlen(str)); +} + /* * Local variables: * c-basic-offset: 4