X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=src%2Fmarcdisp.c;h=994c5a62638b1bfc705c03c83b4563640ad8c03a;hb=b5fec6a001dbcd8b303a11cede8b6c55512dbda0;hp=1d408bec2d25a2c1ffbfd4d3af8030f0613228cf;hpb=b925ea17d6f146a28d745b0d34e9eec6eafda21f;p=yaz-moved-to-github.git diff --git a/src/marcdisp.c b/src/marcdisp.c index 1d408be..994c5a6 100644 --- a/src/marcdisp.c +++ b/src/marcdisp.c @@ -2,7 +2,7 @@ * 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 $ */ /** @@ -25,6 +25,7 @@ #include #include #include +#include #if YAZ_HAVE_XML2 #include @@ -87,6 +88,7 @@ struct yaz_marc_t_ { 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; @@ -99,6 +101,7 @@ yaz_marc_t yaz_marc_create(void) 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"); @@ -112,10 +115,15 @@ void yaz_marc_destroy(yaz_marc_t mt) 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)); @@ -158,6 +166,7 @@ void yaz_marc_add_leader(yaz_marc_t mt, const char *leader, size_t leader_len) 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, @@ -383,6 +392,41 @@ static void yaz_marc_reset(yaz_marc_t mt) 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; @@ -463,6 +507,8 @@ int yaz_marc_write_mode(yaz_marc_t mt, WRBUF wr) 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; } @@ -574,7 +620,8 @@ static int yaz_marc_write_marcxml_ns(yaz_marc_t mt, WRBUF wr, 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); } @@ -909,10 +956,11 @@ static int yaz_marc_read_xml_fields(yaz_marc_t mt, const xmlNode *ptr) } 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) { @@ -936,13 +984,10 @@ int yaz_marc_read_xml(yaz_marc_t mt, const void *xmlnode) 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) { @@ -1211,6 +1256,64 @@ int marc_display (const char *buf, FILE *outf) 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