+ yaz_marc_cprintf(mt, "Indicator length %5d", *indicator_length);
+ yaz_marc_cprintf(mt, "Identifier length %5d", *identifier_length);
+ yaz_marc_cprintf(mt, "Base address %5d", *base_address);
+ yaz_marc_cprintf(mt, "Length data entry %5d", *length_data_entry);
+ yaz_marc_cprintf(mt, "Length starting %5d", *length_starting);
+ yaz_marc_cprintf(mt, "Length implementation %5d", *length_implementation);
+ }
+ yaz_marc_add_leader(mt, leader, 24);
+}
+
+void yaz_marc_subfield_str(yaz_marc_t mt, const char *s)
+{
+ strncpy(mt->subfield_str, s, sizeof(mt->subfield_str)-1);
+ mt->subfield_str[sizeof(mt->subfield_str)-1] = '\0';
+}
+
+void yaz_marc_endline_str(yaz_marc_t mt, const char *s)
+{
+ strncpy(mt->endline_str, s, sizeof(mt->endline_str)-1);
+ mt->endline_str[sizeof(mt->endline_str)-1] = '\0';
+}
+
+/* try to guess how many bytes the identifier really is! */
+static size_t cdata_one_character(yaz_marc_t mt, const char *buf)
+{
+ if (mt->iconv_cd)
+ {
+ size_t i;
+ for (i = 1; i<5; i++)
+ {
+ char outbuf[12];
+ size_t outbytesleft = sizeof(outbuf);
+ char *outp = outbuf;
+ const char *inp = buf;
+
+ size_t inbytesleft = i;
+ size_t r = yaz_iconv(mt->iconv_cd, (char**) &inp, &inbytesleft,
+ &outp, &outbytesleft);
+ if (r != (size_t) (-1))
+ return i; /* got a complete sequence */
+ }
+ return 1; /* giving up */
+ }
+ return 1; /* we don't know */
+}
+
+void yaz_marc_reset(yaz_marc_t mt)
+{
+ nmem_reset(mt->nmem);
+ mt->nodes = 0;
+ mt->nodes_pp = &mt->nodes;
+ mt->subfield_pp = 0;
+}
+
+int yaz_marc_write_check(yaz_marc_t mt, WRBUF wr)
+{
+ struct yaz_marc_node *n;
+ int identifier_length;
+ const char *leader = 0;
+
+ for (n = mt->nodes; n; n = n->next)
+ if (n->which == YAZ_MARC_LEADER)
+ {
+ leader = n->u.leader;
+ break;
+ }
+
+ if (!leader)
+ return -1;
+ if (!atoi_n_check(leader+11, 1, &identifier_length))
+ return -1;
+
+ for (n = mt->nodes; n; n = n->next)
+ {
+ switch(n->which)
+ {
+ case YAZ_MARC_COMMENT:
+ wrbuf_iconv_write(wr, mt->iconv_cd,
+ n->u.comment, strlen(n->u.comment));
+ wrbuf_puts(wr, ")\n");
+ break;
+ default:
+ break;
+ }
+ }
+ return 0;
+}
+
+static size_t get_subfield_len(yaz_marc_t mt, const char *data,
+ int identifier_length)
+{
+ /* if identifier length is 2 (most MARCs) or less (probably an error),
+ the code is a single character .. However we've
+ seen multibyte codes, so see how big it really is */
+ if (identifier_length > 2)
+ return identifier_length - 1;
+ else
+ return cdata_one_character(mt, data);
+}
+
+int yaz_marc_write_line(yaz_marc_t mt, WRBUF wr)
+{
+ struct yaz_marc_node *n;
+ int identifier_length;
+ const char *leader = 0;
+
+ for (n = mt->nodes; n; n = n->next)
+ if (n->which == YAZ_MARC_LEADER)
+ {
+ leader = n->u.leader;
+ break;
+ }
+
+ if (!leader)
+ return -1;
+ if (!atoi_n_check(leader+11, 1, &identifier_length))
+ return -1;
+
+ for (n = mt->nodes; n; n = n->next)
+ {
+ struct yaz_marc_subfield *s;
+ switch(n->which)