/*
- * Copyright (c) 1995-2004, Index Data
+ * Copyright (C) 1995-2005, Index Data ApS
* See the file LICENSE for details.
*
- * $Id: marcdisp.c,v 1.6 2004-08-07 08:07:00 adam Exp $
+ * $Id: marcdisp.c,v 1.12 2005-01-15 19:47:14 adam Exp $
+ */
+
+/**
+ * \file marcdisp.c
+ * \brief Implements MARC display - and conversion utilities
*/
#if HAVE_CONFIG_H
/* ballout if bsize is known and record_length is less than that */
if (bsize != -1 && record_length > bsize)
return -1;
- if (isdigit(buf[10]))
+ if (isdigit(((const unsigned char *) buf)[10]))
indicator_length = atoi_n (buf+10, 1);
else
indicator_length = 2;
- if (isdigit(buf[11]))
+ if (isdigit(((const unsigned char *) buf)[11]))
identifier_length = atoi_n (buf+11, 1);
else
identifier_length = 2;
base_address = atoi_n (buf+12, 5);
length_data_entry = atoi_n (buf+20, 1);
+ if (buf[20] <= '0' || buf[20] >= '9')
+ {
+ wrbuf_printf(wr, "<!-- Length data entry should hold a digit. Assuming 4 -->\n");
+ length_data_entry = 4;
+ }
length_starting = atoi_n (buf+21, 1);
+ if (buf[21] <= '0' || buf[21] >= '9')
+ {
+ wrbuf_printf(wr, "<!-- Length starting should hold a digit. Assuming 5 -->\n");
+ length_starting = 5;
+ }
length_implementation = atoi_n (buf+22, 1);
if (mt->xml != YAZ_MARC_LINE)
int end_offset;
int i, j;
char tag[4];
- int identifier_flag = 1;
+ int identifier_flag = 0;
+ int entry_p0;
memcpy (tag, buf+entry_p, 3);
entry_p += 3;
+ entry_p0 = entry_p;
tag[3] = '\0';
data_length = atoi_n (buf+entry_p, length_data_entry);
entry_p += length_data_entry;
i = data_offset + base_address;
end_offset = i+data_length-1;
+ if (mt->debug)
+ {
+ wrbuf_printf(wr, "<!-- offset=%d data dlength=%d doffset=%d -->\n",
+ entry_p0, data_length, data_offset);
+ }
+
if (indicator_length < 4 && indicator_length > 0)
{
- if (buf[i + indicator_length] != ISO2709_IDFS)
- identifier_flag = 0;
+ if (buf[i + indicator_length] == ISO2709_IDFS)
+ identifier_flag = 1;
+ else if (buf[i + indicator_length + 1] == ISO2709_IDFS)
+ identifier_flag = 2;
}
- else if (!memcmp (tag, "00", 2))
- identifier_flag = 0;
+ else if (memcmp (tag, "00", 2))
+ identifier_flag = 1;
switch(mt->xml)
{
wrbuf_puts (wr, " ");
break;
case YAZ_MARC_SIMPLEXML:
- wrbuf_printf (wr, "<field tag=\"%s\"", tag);
+ wrbuf_printf (wr, "<field tag=\"");
+ marc_cdata(mt, tag, strlen(tag), wr);
+ wrbuf_printf(wr, "\"");
break;
case YAZ_MARC_OAIMARC:
if (identifier_flag)
- wrbuf_printf (wr, " <varfield id=\"%s\"", tag);
+ wrbuf_printf (wr, " <varfield id=\"");
else
- wrbuf_printf (wr, " <fixfield id=\"%s\"", tag);
+ wrbuf_printf (wr, " <fixfield id=\"");
+ marc_cdata(mt, tag, strlen(tag), wr);
+ wrbuf_printf(wr, "\"");
break;
case YAZ_MARC_MARCXML:
if (identifier_flag)
- wrbuf_printf (wr, " <datafield tag=\"%s\"", tag);
+ wrbuf_printf (wr, " <datafield tag=\"");
else
- wrbuf_printf (wr, " <controlfield tag=\"%s\"", tag);
+ wrbuf_printf (wr, " <controlfield tag=\"");
+ marc_cdata(mt, tag, strlen(tag), wr);
+ wrbuf_printf(wr, "\"");
}
if (identifier_flag)
{
+ i += identifier_flag-1;
for (j = 0; j<indicator_length; j++, i++)
{
switch(mt->xml)
wrbuf_putc(wr, buf[i]);
break;
case YAZ_MARC_SIMPLEXML:
- wrbuf_printf(wr, " Indicator%d=\"%c\"", j+1, buf[i]);
+ wrbuf_printf(wr, " Indicator%d=\"", j+1);
+ marc_cdata(mt, buf+i, 1, wr);
+ wrbuf_printf(wr, "\"");
break;
case YAZ_MARC_OAIMARC:
- wrbuf_printf(wr, " i%d=\"%c\"", j+1, buf[i]);
+ wrbuf_printf(wr, " i%d=\"", j+1);
+ marc_cdata(mt, buf+i, 1, wr);
+ wrbuf_printf(wr, "\"");
break;
case YAZ_MARC_MARCXML:
- wrbuf_printf(wr, " ind%d=\"%c\"", j+1, buf[i]);
+ wrbuf_printf(wr, " ind%d=\"", j+1);
+ marc_cdata(mt, buf+i, 1, wr);
+ wrbuf_printf(wr, "\"");
}
}
}
break;
case YAZ_MARC_LINE:
wrbuf_puts (wr, " $");
- for (j = 1; j<identifier_length; j++, i++)
- wrbuf_putc (wr, buf[i]);
+ marc_cdata(mt, buf+i, identifier_length-1, wr);
+ i = i+identifier_length-1;
wrbuf_putc (wr, ' ');
break;
case YAZ_MARC_SIMPLEXML:
wrbuf_puts (wr, " <subfield code=\"");
- for (j = 1; j<identifier_length; j++, i++)
- wrbuf_putc (wr, buf[i]);
+ marc_cdata(mt, buf+i, identifier_length-1, wr);
+ i = i+identifier_length-1;
wrbuf_puts (wr, "\">");
break;
case YAZ_MARC_OAIMARC:
wrbuf_puts (wr, " <subfield label=\"");
- for (j = 1; j<identifier_length; j++, i++)
- wrbuf_putc (wr, buf[i]);
+ marc_cdata(mt, buf+i, identifier_length-1, wr);
+ i = i+identifier_length-1;
wrbuf_puts (wr, "\">");
break;
case YAZ_MARC_MARCXML:
wrbuf_puts (wr, " <subfield code=\"");
- for (j = 1; j<identifier_length; j++, i++)
- wrbuf_putc (wr, buf[i]);
+ marc_cdata(mt, buf+i, identifier_length-1, wr);
+ i = i+identifier_length-1;
wrbuf_puts (wr, "\">");
break;
}
if (mt->xml == YAZ_MARC_LINE)
wrbuf_putc (wr, '\n');
if (i < end_offset)
- wrbuf_puts (wr, " <!-- separator but not at end of field -->\n");
+ wrbuf_printf(wr, " <!-- separator but not at end of field length=%d-->\n", data_length);
if (buf[i] != ISO2709_RS && buf[i] != ISO2709_FS)
- wrbuf_puts (wr, " <!-- no separator at end of field -->\n");
+ wrbuf_printf(wr, " <!-- no separator at end of field length=%d-->\n", data_length);
switch(mt->xml)
{
case YAZ_MARC_SIMPLEXML: