X-Git-Url: http://git.indexdata.com/?p=yaz-moved-to-github.git;a=blobdiff_plain;f=ztest%2Fread-grs.c;h=1726ef6abc83249af2885dd8f8edee20716098f3;hp=9cc9c48b4c3b2f70b679d89a610f3a5121d333db;hb=dc86a5103817732735d2591d3775114a86de94ae;hpb=eb14cf14b5b176a8e2b73ca251d625e47223f116 diff --git a/ztest/read-grs.c b/ztest/read-grs.c index 9cc9c48..1726ef6 100644 --- a/ztest/read-grs.c +++ b/ztest/read-grs.c @@ -1,21 +1,22 @@ -/* - * Copyright (c) 1995-2002, Index Data. +/* This file is part of the YAZ toolkit. + * Copyright (C) Index Data * See the file LICENSE for details. - * - * $Id: read-grs.c,v 1.7 2002-03-18 21:33:48 adam Exp $ */ - -/* - * Little toy-thing to read a GRS-1 records from a file. +/** \file + * \brief Little toy-thing to read GRS-1 records from a file. */ +#if HAVE_CONFIG_H +#include +#endif #include -#include #include #include #include +#include "ztest.h" + #define GRS_MAX_FIELDS 50 static Z_GenericRecord *read_grs1(FILE *f, ODR o) @@ -27,72 +28,72 @@ static Z_GenericRecord *read_grs1(FILE *f, ODR o) for (;;) { - Z_TaggedElement *t; - Z_ElementData *c; + Z_TaggedElement *t; + Z_ElementData *c; - while (fgets(buf = line, 512, f)) - { - while (*buf && isspace(*buf)) - buf++; - if (!*buf || *buf == '#') - continue; - break; - } - if (*buf == '}') - return r; - if (sscanf(buf, "(%d,%[^)])", &type, value) != 2) - { - yaz_log(LOG_WARN, "Bad data in '%s'", buf); - return 0; - } - if (!type && *value == '0') - return r; - if (!(buf = strchr(buf, ')'))) - return 0; - buf++; - while (*buf && isspace(*buf)) - buf++; - if (!*buf) - return 0; - if (!r) - { - r = (Z_GenericRecord *)odr_malloc(o, sizeof(*r)); - r->elements = (Z_TaggedElement **) + while (fgets(buf = line, 512, f)) + { + while (*buf && yaz_isspace(*buf)) + buf++; + if (!*buf || *buf == '#') + continue; + break; + } + if (*buf == '}') + return r; + if (sscanf(buf, "(%d,%[^)])", &type, value) != 2) + { + yaz_log(YLOG_WARN, "Bad data in '%s'", buf); + return 0; + } + if (!type && *value == '0') + return r; + if (!(buf = strchr(buf, ')'))) + return 0; + buf++; + while (*buf && yaz_isspace(*buf)) + buf++; + if (!*buf) + return 0; + if (!r) + { + r = (Z_GenericRecord *)odr_malloc(o, sizeof(*r)); + r->elements = (Z_TaggedElement **) odr_malloc(o, sizeof(Z_TaggedElement*) * GRS_MAX_FIELDS); - r->num_elements = 0; - } - r->elements[r->num_elements] = t = (Z_TaggedElement *) + r->num_elements = 0; + } + r->elements[r->num_elements] = t = (Z_TaggedElement *) odr_malloc(o, sizeof(Z_TaggedElement)); - t->tagType = odr_intdup(o, type); - t->tagValue = (Z_StringOrNumeric *) + t->tagType = odr_intdup(o, type); + t->tagValue = (Z_StringOrNumeric *) odr_malloc(o, sizeof(Z_StringOrNumeric)); - if ((ivalue = atoi(value))) - { - t->tagValue->which = Z_StringOrNumeric_numeric; - t->tagValue->u.numeric = odr_intdup(o, ivalue); - } - else - { - t->tagValue->which = Z_StringOrNumeric_string; - t->tagValue->u.string = (char *)odr_malloc(o, strlen(value)+1); - strcpy(t->tagValue->u.string, value); - } - t->tagOccurrence = 0; - t->metaData = 0; - t->appliedVariant = 0; - t->content = c = (Z_ElementData *)odr_malloc(o, sizeof(Z_ElementData)); - if (*buf == '{') - { - c->which = Z_ElementData_subtree; - c->u.subtree = read_grs1(f, o); - } - else - { - c->which = Z_ElementData_string; - buf[strlen(buf)-1] = '\0'; - c->u.string = odr_strdup(o, buf); - } - r->num_elements++; + if ((ivalue = atoi(value))) + { + t->tagValue->which = Z_StringOrNumeric_numeric; + t->tagValue->u.numeric = odr_intdup(o, ivalue); + } + else + { + t->tagValue->which = Z_StringOrNumeric_string; + t->tagValue->u.string = (char *)odr_malloc(o, strlen(value)+1); + strcpy(t->tagValue->u.string, value); + } + t->tagOccurrence = 0; + t->metaData = 0; + t->appliedVariant = 0; + t->content = c = (Z_ElementData *)odr_malloc(o, sizeof(Z_ElementData)); + if (*buf == '{') + { + c->which = Z_ElementData_subtree; + c->u.subtree = read_grs1(f, o); + } + else + { + c->which = Z_ElementData_string; + buf[strlen(buf)-1] = '\0'; + c->u.string = odr_strdup(o, buf); + } + r->num_elements++; } } @@ -104,14 +105,23 @@ Z_GenericRecord *dummy_grs_record (int num, ODR o) int n; if (!f) - return 0; + return 0; while (fgets(line, 512, f)) - if (*line == '#' && sscanf(line, "#%d", &n) == 1 && n == num) - { - r = read_grs1(f, o); - break; - } + if (*line == '#' && sscanf(line, "#%d", &n) == 1 && n == num) + { + r = read_grs1(f, o); + break; + } fclose(f); return r; } +/* + * Local variables: + * c-basic-offset: 4 + * c-file-style: "Stroustrup" + * indent-tabs-mode: nil + * End: + * vim: shiftwidth=4 tabstop=8 expandtab + */ +