X-Git-Url: http://git.indexdata.com/?p=yaz-moved-to-github.git;a=blobdiff_plain;f=ztest%2Fread-marc.c;h=4a91b3813fbb208fa80fef9eddb558be2156a2bb;hp=cc9ca29ca86402c557a957243e0e99c20035243e;hb=db70647849fbccb07d6feed7b7870fbac6cf07fb;hpb=1121eeb134f61c6c2510143858db59045c1b6757 diff --git a/ztest/read-marc.c b/ztest/read-marc.c index cc9ca29..4a91b38 100644 --- a/ztest/read-marc.c +++ b/ztest/read-marc.c @@ -1,19 +1,22 @@ -/* - * Copyright (c) 2002, Index Data. +/* This file is part of the YAZ toolkit. + * Copyright (C) 1995-2010 Index Data * See the file LICENSE for details. - * - * $Id: read-marc.c,v 1.4 2002-12-16 13:13:53 adam Exp $ */ -/* - * Little toy-thing to read a MARC records from a fixed array. +/** \file + * \brief Little toy-thing to read MARC records from a fixed array. */ + #include #include #include #include -char *marc_records[] = { +#include "ztest.h" + +#define NO_MARC_RECORDS 24 + +char *marc_records[NO_MARC_RECORDS] = { "\x30\x30\x33\x36\x36\x6E\x61\x6D\x20\x20\x32\x32\x30\x30\x31\x36" "\x39\x38\x61\x20\x34\x35\x30\x30\x30\x30\x31\x30\x30\x31\x33\x30" @@ -1532,88 +1535,43 @@ char *marc_records[] = { "\x38\x34\x32\x1E\x1D" }; -static int atoin (const char *buf, int n) -{ - int val = 0; - while (--n >= 0) - { - if (isdigit(*buf)) - val = val*10 + (*buf - '0'); - buf++; - } - return val; -} - -/* read one MARC record from a file */ -char *marc_read(FILE *inf, ODR odr) -{ - char length_str[5]; - size_t size; - char *buf; - - if (fread (length_str, 1, 5, inf) != 5) - return NULL; - size = atoin (length_str, 5); - if (size <= 6) - return NULL; - if (!(buf = (char*) odr_malloc (odr, size+1))) - return NULL; - if (fread (buf+5, 1, size-5, inf) != (size-5)) - { - xfree (buf); - return NULL; - } - memcpy (buf, length_str, 5); - buf[size] = '\0'; - return buf; -} - /* read MARC record from offset 'num' */ -char *dummy_marc_record (int num, ODR odr) +char *dummy_marc_record(int num, ODR odr) { - FILE *inf; - char *buf = 0; - - if (num == 98) - { /* this will generate a very bad MARC record (testing only) */ - buf = (char*) odr_malloc(odr, 2101); - memset(buf, '7', 2100); - buf[2100] = '\0'; - } - inf = fopen ("dummy-records", "r"); - if (!inf) - { /* file not there. Get them from fixed array */ - if (num < 1 || num > 24) - return 0; - return marc_records[num-1]; - } - /* OK, try to get proper MARC records from the file */ - while (--num >= 0) - { - buf = marc_read (inf, odr); - if (!buf) - break; - } - fclose(inf); - return buf; + if (num < 1) + return 0; + return marc_records[(num-1) % NO_MARC_RECORDS]; } /* read MARC record and convert to XML */ -char *dummy_xml_record (int num, ODR odr) +char *dummy_xml_record(int num, ODR odr) { - yaz_marc_t mt = yaz_marc_create(); - char *result; - int rlen; - char *rec = dummy_marc_record (num, odr); - int len; + char *rec = dummy_marc_record(num, odr); - if (!rec) - return 0; - - yaz_marc_xml(mt, YAZ_MARC_MARCXML); - len = yaz_marc_decode_buf (mt, rec, -1, &result, &rlen); - if (len > 1) - rec = (char *) odr_strdup(odr, result); - yaz_marc_destroy(mt); + if (rec) + { + const char *result; + size_t rlen; + int len; + yaz_marc_t mt = yaz_marc_create(); + yaz_marc_xml(mt, YAZ_MARC_MARCXML); + len = yaz_marc_decode_buf(mt, rec, -1, &result, &rlen); + if (len > 1) + { + rec = (char *) odr_malloc(odr, rlen+1); + memcpy(rec, result, rlen); + rec[rlen] = '\0'; + } + yaz_marc_destroy(mt); + } return rec; } +/* + * Local variables: + * c-basic-offset: 4 + * c-file-style: "Stroustrup" + * indent-tabs-mode: nil + * End: + * vim: shiftwidth=4 tabstop=8 expandtab + */ +