+static int convert_marc(void *info, WRBUF record, WRBUF wr_error)
+{
+ struct marc_info *mi = info;
+ int ret = 0;
+
+ yaz_iconv_t cd = yaz_iconv_open(mi->output_charset, mi->input_charset);
+ yaz_marc_t mt = yaz_marc_create();
+
+ yaz_marc_xml(mt, mi->output_format_mode);
+
+ if (cd)
+ yaz_marc_iconv(mt, cd);
+ if (mi->input_format_mode == YAZ_MARC_ISO2709)
+ {
+ int sz = yaz_marc_read_iso2709(mt, wrbuf_buf(record),
+ wrbuf_len(record));
+ if (sz > 0)
+ ret = 0;
+ else
+ ret = -1;
+ }
+ else if (mi->input_format_mode == YAZ_MARC_MARCXML ||
+ mi->input_format_mode == YAZ_MARC_TURBOMARC)
+ {
+ xmlDocPtr doc = xmlParseMemory(wrbuf_buf(record),
+ wrbuf_len(record));
+ if (!doc)
+ {
+ wrbuf_printf(wr_error, "xmlParseMemory failed");
+ ret = -1;
+ }
+ else
+ {
+ ret = yaz_marc_read_xml(mt, xmlDocGetRootElement(doc));
+ if (ret)
+ wrbuf_printf(wr_error, "yaz_marc_read_xml failed");
+ }
+ xmlFreeDoc(doc);
+ }
+ else
+ {
+ wrbuf_printf(wr_error, "unsupported input format");
+ ret = -1;
+ }
+ if (ret == 0)
+ {
+ wrbuf_rewind(record);
+ ret = yaz_marc_write_mode(mt, record);
+ if (ret)
+ wrbuf_printf(wr_error, "yaz_marc_write_mode failed");
+ }
+ if (cd)
+ yaz_iconv_close(cd);
+ yaz_marc_destroy(mt);
+ return ret;