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