* Copyright (C) 1995-2006, Index Data ApS
* See the file LICENSE for details.
*
- * $Id: marcdisp.c,v 1.33 2006-08-28 12:34:40 adam Exp $
+ * $Id: marcdisp.c,v 1.37 2006-12-13 11:25:17 adam Exp $
*/
/**
#include <yaz/marcdisp.h>
#include <yaz/wrbuf.h>
#include <yaz/yaz-util.h>
+#include <yaz/nmem_xml.h>
#if YAZ_HAVE_XML2
#include <libxml/parser.h>
yaz_iconv_t iconv_cd;
char subfield_str[8];
char endline_str[8];
+ char *leader_spec;
struct yaz_marc_node *nodes;
struct yaz_marc_node **nodes_pp;
struct yaz_marc_subfield **subfield_pp;
mt->debug = 0;
mt->m_wr = wrbuf_alloc();
mt->iconv_cd = 0;
+ mt->leader_spec = 0;
strcpy(mt->subfield_str, " $");
strcpy(mt->endline_str, "\n");
if (!mt)
return ;
nmem_destroy(mt->nmem);
- wrbuf_free (mt->m_wr, 1);
- xfree (mt);
+ wrbuf_free(mt->m_wr, 1);
+ xfree(mt->leader_spec);
+ xfree(mt);
}
+static int marc_exec_leader(const char *leader_spec, char *leader,
+ size_t size);
+
+
struct yaz_marc_node *yaz_marc_add_node(yaz_marc_t mt)
{
struct yaz_marc_node *n = nmem_malloc(mt->nmem, sizeof(*n));
struct yaz_marc_node *n = yaz_marc_add_node(mt);
n->which = YAZ_MARC_LEADER;
n->u.leader = nmem_strdupn(mt->nmem, leader, leader_len);
+ marc_exec_leader(mt->leader_spec, n->u.leader, leader_len);
}
void yaz_marc_add_controlfield(yaz_marc_t mt, const char *tag,
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;
+}
+
+
int yaz_marc_write_line(yaz_marc_t mt, WRBUF wr)
{
struct yaz_marc_node *n;
return yaz_marc_write_marcxchange(mt, wr, 0, 0); /* no format, type */
case YAZ_MARC_ISO2709:
return yaz_marc_write_iso2709(mt, wr);
+ case YAZ_MARC_CHECK:
+ return yaz_marc_write_check(mt, wr);
}
return -1;
}
int yaz_marc_write_marcxml(yaz_marc_t mt, WRBUF wr)
{
- yaz_marc_modify_leader(mt, 9, "a");
+ if (!mt->leader_spec)
+ yaz_marc_modify_leader(mt, 9, "a");
return yaz_marc_write_marcxml_ns(mt, wr, "http://www.loc.gov/MARC21/slim",
0, 0);
}
}
return 0;
}
+#endif
-int yaz_marc_read_xml(yaz_marc_t mt, const void *xmlnode)
+int yaz_marc_read_xml(yaz_marc_t mt, const xmlNode *ptr)
{
- const xmlNode *ptr = xmlnode;
+#if YAZ_HAVE_XML2
for(; ptr; ptr = ptr->next)
if (ptr->type == XML_ELEMENT_NODE)
{
if (yaz_marc_read_xml_leader(mt, &ptr))
return -1;
return yaz_marc_read_xml_fields(mt, ptr->next);
-}
#else
-int yaz_marc_read_xml(yaz_marc_t mt, const void *xmlnode)
-{
return -1;
-}
#endif
+}
int yaz_marc_read_iso2709(yaz_marc_t mt, const char *buf, int bsize)
{
return marc_display_ex (buf, outf, 0);
}
+int yaz_marc_leader_spec(yaz_marc_t mt, const char *leader_spec)
+{
+ xfree(mt->leader_spec);
+ mt->leader_spec = 0;
+ if (leader_spec)
+ {
+ char dummy_leader[24];
+ if (marc_exec_leader(leader_spec, dummy_leader, 24))
+ return -1;
+ mt->leader_spec = xstrdup(leader_spec);
+ }
+ return 0;
+}
+
+static int marc_exec_leader(const char *leader_spec, char *leader, size_t size)
+{
+ const char *cp = leader_spec;
+ while (cp)
+ {
+ char val[21];
+ int pos;
+ int no_read = 0, no = 0;
+
+ no = sscanf(cp, "%d=%20[^,]%n", &pos, val, &no_read);
+ if (no < 2 || no_read < 3)
+ return -1;
+ if (pos < 0 || pos >= size)
+ return -1;
+
+ if (*val == '\'')
+ {
+ const char *vp = strchr(val+1, '\'');
+ size_t len;
+
+ if (!vp)
+ return -1;
+ len = vp-val-1;
+ if (len + pos > size)
+ return -1;
+ memcpy(leader + pos, val+1, len);
+ }
+ else if (*val >= '0' && *val <= '9')
+ {
+ int ch = atoi(val);
+ leader[pos] = ch;
+ }
+ else
+ return -1;
+ cp += no_read;
+ if (*cp != ',')
+ break;
+
+ cp++;
+ }
+ return 0;
+}
+
+
/*
* Local variables:
* c-basic-offset: 4