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