Version 5.9.0
[yaz-moved-to-github.git] / ztest / read-grs.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) Index Data
3  * See the file LICENSE for details.
4  */
5 /** \file
6  * \brief Little toy-thing to read GRS-1 records from a file.
7  */
8 #if HAVE_CONFIG_H
9 #include <config.h>
10 #endif
11
12 #include <stdio.h>
13 #include <stdlib.h>
14
15 #include <yaz/proto.h>
16 #include <yaz/log.h>
17
18 #include "ztest.h"
19
20 #define GRS_MAX_FIELDS 50
21
22 static Z_GenericRecord *read_grs1(FILE *f, ODR o)
23 {
24     char line[512], *buf;
25     int type, ivalue;
26     char value[512];
27     Z_GenericRecord *r = 0;
28
29     for (;;)
30     {
31         Z_TaggedElement *t;
32         Z_ElementData *c;
33
34         while (fgets(buf = line, 512, f))
35         {
36             while (*buf && yaz_isspace(*buf))
37                 buf++;
38             if (!*buf || *buf == '#')
39                 continue;
40             break;
41         }
42         if (*buf == '}')
43             return r;
44         if (sscanf(buf, "(%d,%[^)])", &type, value) != 2)
45         {
46             yaz_log(YLOG_WARN, "Bad data in '%s'", buf);
47             return 0;
48         }
49         if (!type && *value == '0')
50             return r;
51         if (!(buf = strchr(buf, ')')))
52             return 0;
53         buf++;
54         while (*buf && yaz_isspace(*buf))
55             buf++;
56         if (!*buf)
57             return 0;
58         if (!r)
59         {
60             r = (Z_GenericRecord *)odr_malloc(o, sizeof(*r));
61             r->elements = (Z_TaggedElement **)
62                 odr_malloc(o, sizeof(Z_TaggedElement*) * GRS_MAX_FIELDS);
63             r->num_elements = 0;
64         }
65         r->elements[r->num_elements] = t = (Z_TaggedElement *)
66             odr_malloc(o, sizeof(Z_TaggedElement));
67         t->tagType = odr_intdup(o, type);
68         t->tagValue = (Z_StringOrNumeric *)
69             odr_malloc(o, sizeof(Z_StringOrNumeric));
70         if ((ivalue = atoi(value)))
71         {
72             t->tagValue->which = Z_StringOrNumeric_numeric;
73             t->tagValue->u.numeric = odr_intdup(o, ivalue);
74         }
75         else
76         {
77             t->tagValue->which = Z_StringOrNumeric_string;
78             t->tagValue->u.string = (char *)odr_malloc(o, strlen(value)+1);
79             strcpy(t->tagValue->u.string, value);
80         }
81         t->tagOccurrence = 0;
82         t->metaData = 0;
83         t->appliedVariant = 0;
84         t->content = c = (Z_ElementData *)odr_malloc(o, sizeof(Z_ElementData));
85         if (*buf == '{')
86         {
87             c->which = Z_ElementData_subtree;
88             c->u.subtree = read_grs1(f, o);
89         }
90         else
91         {
92             c->which = Z_ElementData_string;
93             buf[strlen(buf)-1] = '\0';
94             c->u.string = odr_strdup(o, buf);
95         }
96         r->num_elements++;
97     }
98 }
99
100 Z_GenericRecord *dummy_grs_record (int num, ODR o)
101 {
102     FILE *f = fopen("dummy-grs", "r");
103     char line[512];
104     Z_GenericRecord *r = 0;
105     int n;
106
107     if (!f)
108         return 0;
109     while (fgets(line, 512, f))
110         if (*line == '#' && sscanf(line, "#%d", &n) == 1 && n == num)
111         {
112             r = read_grs1(f, o);
113             break;
114         }
115     fclose(f);
116     return r;
117 }
118
119 /*
120  * Local variables:
121  * c-basic-offset: 4
122  * c-file-style: "Stroustrup"
123  * indent-tabs-mode: nil
124  * End:
125  * vim: shiftwidth=4 tabstop=8 expandtab
126  */
127