1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2008 Index Data
3 * See the file LICENSE for details.
7 * Little toy-thing to read a GRS-1 records from a file.
14 #include <yaz/proto.h>
17 #define GRS_MAX_FIELDS 50
19 static Z_GenericRecord *read_grs1(FILE *f, ODR o)
24 Z_GenericRecord *r = 0;
31 while (fgets(buf = line, 512, f))
33 while (*buf && isspace(*(unsigned char *) buf))
35 if (!*buf || *buf == '#')
41 if (sscanf(buf, "(%d,%[^)])", &type, value) != 2)
43 yaz_log(YLOG_WARN, "Bad data in '%s'", buf);
46 if (!type && *value == '0')
48 if (!(buf = strchr(buf, ')')))
51 while (*buf && isspace(*(unsigned char *) buf))
57 r = (Z_GenericRecord *)odr_malloc(o, sizeof(*r));
58 r->elements = (Z_TaggedElement **)
59 odr_malloc(o, sizeof(Z_TaggedElement*) * GRS_MAX_FIELDS);
62 r->elements[r->num_elements] = t = (Z_TaggedElement *)
63 odr_malloc(o, sizeof(Z_TaggedElement));
64 t->tagType = odr_intdup(o, type);
65 t->tagValue = (Z_StringOrNumeric *)
66 odr_malloc(o, sizeof(Z_StringOrNumeric));
67 if ((ivalue = atoi(value)))
69 t->tagValue->which = Z_StringOrNumeric_numeric;
70 t->tagValue->u.numeric = odr_intdup(o, ivalue);
74 t->tagValue->which = Z_StringOrNumeric_string;
75 t->tagValue->u.string = (char *)odr_malloc(o, strlen(value)+1);
76 strcpy(t->tagValue->u.string, value);
80 t->appliedVariant = 0;
81 t->content = c = (Z_ElementData *)odr_malloc(o, sizeof(Z_ElementData));
84 c->which = Z_ElementData_subtree;
85 c->u.subtree = read_grs1(f, o);
89 c->which = Z_ElementData_string;
90 buf[strlen(buf)-1] = '\0';
91 c->u.string = odr_strdup(o, buf);
97 Z_GenericRecord *dummy_grs_record (int num, ODR o)
99 FILE *f = fopen("dummy-grs", "r");
101 Z_GenericRecord *r = 0;
106 while (fgets(line, 512, f))
107 if (*line == '#' && sscanf(line, "#%d", &n) == 1 && n == num)
119 * indent-tabs-mode: nil
121 * vim: shiftwidth=4 tabstop=8 expandtab