2 * Copyright (c) 1995-2003, Index Data.
3 * See the file LICENSE for details.
5 * $Id: read-grs.c,v 1.9 2004-11-18 15:18:14 heikki Exp $
9 * Little toy-thing to read a GRS-1 records from a file.
16 #include <yaz/proto.h>
19 #define GRS_MAX_FIELDS 50
21 static Z_GenericRecord *read_grs1(FILE *f, ODR o)
26 Z_GenericRecord *r = 0;
33 while (fgets(buf = line, 512, f))
35 while (*buf && isspace(*buf))
37 if (!*buf || *buf == '#')
43 if (sscanf(buf, "(%d,%[^)])", &type, value) != 2)
45 yaz_log(YLOG_WARN, "Bad data in '%s'", buf);
48 if (!type && *value == '0')
50 if (!(buf = strchr(buf, ')')))
53 while (*buf && isspace(*buf))
59 r = (Z_GenericRecord *)odr_malloc(o, sizeof(*r));
60 r->elements = (Z_TaggedElement **)
61 odr_malloc(o, sizeof(Z_TaggedElement*) * GRS_MAX_FIELDS);
64 r->elements[r->num_elements] = t = (Z_TaggedElement *)
65 odr_malloc(o, sizeof(Z_TaggedElement));
66 t->tagType = odr_intdup(o, type);
67 t->tagValue = (Z_StringOrNumeric *)
68 odr_malloc(o, sizeof(Z_StringOrNumeric));
69 if ((ivalue = atoi(value)))
71 t->tagValue->which = Z_StringOrNumeric_numeric;
72 t->tagValue->u.numeric = odr_intdup(o, ivalue);
76 t->tagValue->which = Z_StringOrNumeric_string;
77 t->tagValue->u.string = (char *)odr_malloc(o, strlen(value)+1);
78 strcpy(t->tagValue->u.string, value);
82 t->appliedVariant = 0;
83 t->content = c = (Z_ElementData *)odr_malloc(o, sizeof(Z_ElementData));
86 c->which = Z_ElementData_subtree;
87 c->u.subtree = read_grs1(f, o);
91 c->which = Z_ElementData_string;
92 buf[strlen(buf)-1] = '\0';
93 c->u.string = odr_strdup(o, buf);
99 Z_GenericRecord *dummy_grs_record (int num, ODR o)
101 FILE *f = fopen("dummy-grs", "r");
103 Z_GenericRecord *r = 0;
108 while (fgets(line, 512, f))
109 if (*line == '#' && sscanf(line, "#%d", &n) == 1 && n == num)