1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2011 Index Data
3 * See the file LICENSE for details.
6 * \brief Little toy-thing to read GRS-1 records from a file.
16 #include <yaz/proto.h>
21 #define GRS_MAX_FIELDS 50
23 static Z_GenericRecord *read_grs1(FILE *f, ODR o)
28 Z_GenericRecord *r = 0;
35 while (fgets(buf = line, 512, f))
37 while (*buf && isspace(*(unsigned char *) buf))
39 if (!*buf || *buf == '#')
45 if (sscanf(buf, "(%d,%[^)])", &type, value) != 2)
47 yaz_log(YLOG_WARN, "Bad data in '%s'", buf);
50 if (!type && *value == '0')
52 if (!(buf = strchr(buf, ')')))
55 while (*buf && isspace(*(unsigned char *) buf))
61 r = (Z_GenericRecord *)odr_malloc(o, sizeof(*r));
62 r->elements = (Z_TaggedElement **)
63 odr_malloc(o, sizeof(Z_TaggedElement*) * GRS_MAX_FIELDS);
66 r->elements[r->num_elements] = t = (Z_TaggedElement *)
67 odr_malloc(o, sizeof(Z_TaggedElement));
68 t->tagType = odr_intdup(o, type);
69 t->tagValue = (Z_StringOrNumeric *)
70 odr_malloc(o, sizeof(Z_StringOrNumeric));
71 if ((ivalue = atoi(value)))
73 t->tagValue->which = Z_StringOrNumeric_numeric;
74 t->tagValue->u.numeric = odr_intdup(o, ivalue);
78 t->tagValue->which = Z_StringOrNumeric_string;
79 t->tagValue->u.string = (char *)odr_malloc(o, strlen(value)+1);
80 strcpy(t->tagValue->u.string, value);
84 t->appliedVariant = 0;
85 t->content = c = (Z_ElementData *)odr_malloc(o, sizeof(Z_ElementData));
88 c->which = Z_ElementData_subtree;
89 c->u.subtree = read_grs1(f, o);
93 c->which = Z_ElementData_string;
94 buf[strlen(buf)-1] = '\0';
95 c->u.string = odr_strdup(o, buf);
101 Z_GenericRecord *dummy_grs_record (int num, ODR o)
103 FILE *f = fopen("dummy-grs", "r");
105 Z_GenericRecord *r = 0;
110 while (fgets(line, 512, f))
111 if (*line == '#' && sscanf(line, "#%d", &n) == 1 && n == num)
123 * c-file-style: "Stroustrup"
124 * indent-tabs-mode: nil
126 * vim: shiftwidth=4 tabstop=8 expandtab