+ yaz_marc_cprintf(mt,
+ "Leader character at offset %d is non-ASCII. "
+ "Setting value to '%c'", offset, ch_default);
+ leader[offset] = ch_default;
+ }
+}
+
+void yaz_marc_set_leader(yaz_marc_t mt, const char *leader_c,
+ int *indicator_length,
+ int *identifier_length,
+ int *base_address,
+ int *length_data_entry,
+ int *length_starting,
+ int *length_implementation)
+{
+ char leader[24];
+
+ memcpy(leader, leader_c, 24);
+
+ check_ascii(mt, leader, 5, 'a');
+ check_ascii(mt, leader, 6, 'a');
+ check_ascii(mt, leader, 7, 'a');
+ check_ascii(mt, leader, 8, '#');
+ check_ascii(mt, leader, 9, '#');
+ if (!atoi_n_check(leader+10, 1, indicator_length))
+ {
+ yaz_marc_cprintf(mt,
+ "Indicator length at offset 10 should hold a digit."
+ " Assuming 2");
+ leader[10] = '2';
+ *indicator_length = 2;
+ }
+ if (!atoi_n_check(leader+11, 1, identifier_length))
+ {
+ yaz_marc_cprintf(mt,
+ "Identifier length at offset 11 should hold a digit."
+ " Assuming 2");
+ leader[11] = '2';
+ *identifier_length = 2;
+ }
+ if (!atoi_n_check(leader+12, 5, base_address))
+ {
+ yaz_marc_cprintf(mt,
+ "Base address at offsets 12..16 should hold a number."
+ " Assuming 0");
+ *base_address = 0;
+ }
+ check_ascii(mt, leader, 17, '#');
+ check_ascii(mt, leader, 18, '#');
+ check_ascii(mt, leader, 19, '#');
+ if (!atoi_n_check(leader+20, 1, length_data_entry))
+ {
+ yaz_marc_cprintf(mt,
+ "Length data entry at offset 20 should hold a digit."
+ " Assuming 4");
+ *length_data_entry = 4;
+ leader[20] = '4';
+ }
+ if (!atoi_n_check(leader+21, 1, length_starting))
+ {
+ yaz_marc_cprintf(mt,
+ "Length starting at offset 21 should hold a digit."
+ " Assuming 5");
+ *length_starting = 5;
+ leader[21] = '5';
+ }
+ if (!atoi_n_check(leader+22, 1, length_implementation))
+ {
+ yaz_marc_cprintf(mt,
+ "Length implementation at offset 22 should hold a digit."
+ " Assuming 0");
+ *length_implementation = 0;
+ leader[22] = '0';
+ }
+ check_ascii(mt, leader, 23, '0');
+
+ if (mt->debug)
+ {
+ 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);
+ yaz_iconv(mt->iconv_cd, 0, 0, &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)