+static const char *record_iconv_return(ZOOM_record rec, int *len,
+ const char *buf, int sz,
+ const char *record_charset)
+{
+ char to[40];
+ char from[40];
+ yaz_iconv_t cd = 0;
+
+ *from = '\0';
+ strcpy(to, "UTF-8");
+ if (record_charset && *record_charset)
+ {
+ /* Use "from,to" or just "from" */
+ const char *cp =strchr(record_charset, ',');
+ int clen = strlen(record_charset);
+ if (cp && cp[1])
+ {
+ strncpy( to, cp+1, sizeof(to)-1);
+ to[sizeof(to)-1] = '\0';
+ clen = cp - record_charset;
+ }
+ if (clen > sizeof(from)-1)
+ clen = sizeof(from)-1;
+
+ if (clen)
+ strncpy(from, record_charset, clen);
+ from[clen] = '\0';
+ }
+
+ if (*from && *to && (cd = yaz_iconv_open(to, from)))
+ {
+ char outbuf[12];
+ size_t inbytesleft = sz;
+ const char *inp = buf;
+
+ if (!rec->wrbuf_iconv)
+ rec->wrbuf_iconv = wrbuf_alloc();
+
+ wrbuf_rewind(rec->wrbuf_iconv);
+
+ while (inbytesleft)
+ {
+ size_t outbytesleft = sizeof(outbuf);
+ char *outp = outbuf;
+ size_t r = yaz_iconv (cd, (char**) &inp,
+ &inbytesleft,
+ &outp, &outbytesleft);
+ if (r == (size_t) (-1))
+ {
+ int e = yaz_iconv_error(cd);
+ if (e != YAZ_ICONV_E2BIG)
+ break;
+ }
+ wrbuf_write(rec->wrbuf_iconv, outbuf, outp - outbuf);
+ }
+ wrbuf_puts(rec->wrbuf_iconv, "");
+ buf = wrbuf_buf(rec->wrbuf_iconv);
+ sz = wrbuf_len(rec->wrbuf_iconv);
+ yaz_iconv_close(cd);
+ }
+ if (len)
+ *len = sz;
+ return buf;
+}
+