X-Git-Url: http://git.indexdata.com/?p=yaz-moved-to-github.git;a=blobdiff_plain;f=ztest%2Fread-marc.c;h=7a54ddf3bedeae6a199b1d88017b34a0c061b166;hp=fdcc174236e2877ed5c556f256f1c2082da06a4b;hb=cca8a96be6d6ce6de52009e53e23876ce2fc195c;hpb=810fd9221e5d34c4f3c4f8e2b4410f947cbc2504 diff --git a/ztest/read-marc.c b/ztest/read-marc.c index fdcc174..7a54ddf 100644 --- a/ztest/read-marc.c +++ b/ztest/read-marc.c @@ -15,6 +15,20 @@ #include "ztest.h" +#if HAVE_SYS_TYPES_H +#include +#endif +#if HAVE_SYS_STAT_H +#include +#endif +#ifdef WIN32 +#include +#endif + +#if HAVE_UNISTD_H +#include +#endif + #define NO_MARC_RECORDS 24 char *marc_records[NO_MARC_RECORDS] = { @@ -1580,35 +1594,151 @@ char *dummy_marc_record(int num, ODR odr) } } +#define PZ_CBEGIN "\n" +#define PZ_CEND "\n" +#define PZ_BEGIN "\n" +#define PZ_END "\n" +#define PZ_METADATA(x, y) " " y "\n" + +static char *dummy_pz2_record(int num, ODR odr) +{ + const char *rec[] = { + PZ_CBEGIN + PZ_BEGIN + PZ_METADATA(author,"Jack Collins") + PZ_METADATA(medium, "book") + PZ_METADATA(date, "1995") + PZ_METADATA(title, "How to program a computer vol1") + PZ_END + PZ_BEGIN + PZ_METADATA(author,"Jack Collins") + PZ_METADATA(medium, "book") + PZ_METADATA(date, "1995") + PZ_METADATA(title, "How to program a computer vol2") + PZ_END + PZ_CEND + , + PZ_CBEGIN + PZ_BEGIN + PZ_METADATA(author,"Jack Collins") + PZ_METADATA(medium, "book") + PZ_METADATA(date, "1995") + PZ_METADATA(title, "How to program a computer vol3") + PZ_END + PZ_BEGIN + PZ_METADATA(author,"Jack Collins") + PZ_METADATA(medium, "book") + PZ_METADATA(date, "1995") + PZ_METADATA(title, "How to program a computer vol2") + PZ_END + PZ_CEND + , + PZ_BEGIN + PZ_METADATA(author,"Jack Collins") + PZ_METADATA(medium, "book") + PZ_METADATA(date, "1995") + PZ_METADATA(title, "How to program a computer vol1") + PZ_END + , + /* identical to first */ + PZ_CBEGIN + PZ_BEGIN + PZ_METADATA(author,"Jack Collins") + PZ_METADATA(medium, "book") + PZ_METADATA(date, "1995") + PZ_METADATA(title, "How to program a computer vol1") + PZ_END + PZ_BEGIN + PZ_METADATA(author,"Jack Collins") + PZ_METADATA(medium, "book") + PZ_METADATA(date, "1995") + PZ_METADATA(title, "How to program a computer vol4") + PZ_END + PZ_CEND + }; + if (num > 0 && num <= sizeof(rec)/sizeof(*rec)) + return odr_strdup(odr, rec[num - 1]); + return 0; +} + /* read MARC record and convert to XML */ char *dummy_xml_record(int num, ODR odr, const char *esn) { - char *rec = dummy_marc_record(num, odr); - - if (rec) + if (esn && !strcmp(esn, "pz2")) { - WRBUF w = wrbuf_alloc(); - yaz_marc_t mt = yaz_marc_create(); - - yaz_marc_xml(mt, YAZ_MARC_MARCXML); - if (esn && !strcmp(esn, "OP")) + return dummy_pz2_record(num, odr); + } + else if (!esn || !strcmp(esn, "marcxml") || !strcmp(esn, "OP")) + { + /* MARCXML and OPACXML */ + char *rec = dummy_marc_record(num, odr); + if (rec) { - /* generate OPACXML (OPAC in XML) */ - Z_OPACRecord *opac = dummy_opac(num, odr, rec); - yaz_opac_decode_wrbuf(mt, opac, w); + WRBUF w = wrbuf_alloc(); + yaz_marc_t mt = yaz_marc_create(); + yaz_marc_xml(mt, YAZ_MARC_MARCXML); + if (esn && !strcmp(esn, "OP")) + { + /* generate OPACXML (OPAC in XML) */ + Z_OPACRecord *opac = dummy_opac(num, odr, rec); + yaz_opac_decode_wrbuf(mt, opac, w); + } + else + { + /* generate MARCXML */ + yaz_marc_decode_wrbuf(mt, rec, -1, w); + } + rec = odr_strdup(odr, wrbuf_cstr(w)); + yaz_marc_destroy(mt); + wrbuf_destroy(w); + return rec; } - else + } + else + { + char *buf = 0; + const char *e = getenv("YAZ_ZTEST_XML_FETCH"); + if (e) { - /* generate MARCXML */ - yaz_marc_decode_wrbuf(mt, rec, -1, w); + WRBUF w = wrbuf_alloc(); + struct stat sbuf; + FILE *file = 0; + + wrbuf_printf(w, "%s%s.%d.xml", e, esn, num); + if (stat(wrbuf_cstr(w), &sbuf) == 0 && + (file = fopen(wrbuf_cstr(w), "rb"))) + { + buf = odr_malloc(odr, sbuf.st_size); + fread(buf, 1, sbuf.st_size, file); + } + if (file) + fclose(file); } - rec = odr_strdup(odr, wrbuf_cstr(w)); - yaz_marc_destroy(mt); - wrbuf_destroy(w); + return buf; + } + return 0; +} +char *dummy_json_record(int num, ODR odr, const char *esn) +{ + if (!esn || !strcmp(esn, "marcinjson")) + { + char *rec = dummy_marc_record(num, odr); + if (rec) + { + WRBUF w = wrbuf_alloc(); + yaz_marc_t mt = yaz_marc_create(); + yaz_marc_xml(mt, YAZ_MARC_JSON); + yaz_marc_decode_wrbuf(mt, rec, -1, w); + rec = odr_strdup(odr, wrbuf_cstr(w)); + yaz_marc_destroy(mt); + wrbuf_destroy(w); + return rec; + } } - return rec; + return 0; } + /* * Local variables: * c-basic-offset: 4