Minor fix.
[yaz-moved-to-github.git] / ztest / read-grs.c
1 /*
2  * Copyright (c) 1995, Index Data.
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: read-grs.c,v $
7  * Revision 1.2  1998-02-11 11:53:36  adam
8  * Changed code so that it compiles as C++.
9  *
10  * Revision 1.1  1997/09/01 08:55:53  adam
11  * New windows NT/95 port using MSV5.0. Test server ztest now in
12  * separate directory. When using NT, this test server may operate
13  * as an NT service. Note that the service.[ch] should be part of
14  * generic, but it isn't yet.
15  *
16  * Revision 1.1  1995/08/17 12:45:23  quinn
17  * Fixed minor problems with GRS-1. Added support in c&s.
18  *
19  *
20  */
21
22 /*
23  * Little toy-thing to read a GRS-1 record from a file.
24  */
25
26 #include <stdio.h>
27 #include <ctype.h>
28 #include <stdlib.h>
29
30 #include <proto.h>
31 #include <log.h>
32
33 #define GRS_MAX_FIELDS 50
34
35 Z_GenericRecord *read_grs1(FILE *f, ODR o)
36 {
37     char line[512], *buf;
38     int type, ivalue;
39     char value[512];
40     Z_GenericRecord *r = 0;
41
42     for (;;)
43     {
44         Z_TaggedElement *t;
45         Z_ElementData *c;
46
47         while (fgets(buf = line, 512, f))
48         {
49             while (*buf && isspace(*buf))
50                 buf++;
51             if (!*buf || *buf == '#')
52                 continue;
53             break;
54         }
55         if (*buf == '}')
56             return r;
57         if (sscanf(buf, "(%d,%[^)])", &type, value) != 2)
58         {
59             logf(LOG_WARN, "Bad data in '%s'", buf);
60             return 0;
61         }
62         if (!type && *value == '0')
63             return r;
64         if (!(buf = strchr(buf, ')')))
65             return 0;
66         buf++;
67         while (*buf && isspace(*buf))
68             buf++;
69         if (!*buf)
70             return 0;
71         if (!r)
72         {
73             r = (Z_GenericRecord *)odr_malloc(o, sizeof(*r));
74             r->elements = (Z_TaggedElement **)odr_malloc(o, sizeof(Z_TaggedElement*) *
75                 GRS_MAX_FIELDS);
76             r->num_elements = 0;
77         }
78         r->elements[r->num_elements] = t = (Z_TaggedElement *)odr_malloc(o,
79             sizeof(Z_TaggedElement));
80         t->tagType = (int *)odr_malloc(o, sizeof(int));
81         *t->tagType = type;
82         t->tagValue = (Z_StringOrNumeric *)odr_malloc(o, sizeof(Z_StringOrNumeric));
83         if ((ivalue = atoi(value)))
84         {
85             t->tagValue->which = Z_StringOrNumeric_numeric;
86             t->tagValue->u.numeric = (int *)odr_malloc(o, sizeof(int));
87             *t->tagValue->u.numeric = ivalue;
88         }
89         else
90         {
91             t->tagValue->which = Z_StringOrNumeric_string;
92             t->tagValue->u.string = (char *)odr_malloc(o, strlen(value)+1);
93             strcpy(t->tagValue->u.string, value);
94         }
95         t->tagOccurrence = 0;
96         t->metaData = 0;
97         t->appliedVariant = 0;
98         t->content = c = (Z_ElementData *)odr_malloc(o, sizeof(Z_ElementData));
99         if (*buf == '{')
100         {
101             c->which = Z_ElementData_subtree;
102             c->u.subtree = read_grs1(f, o);
103         }
104         else
105         {
106             c->which = Z_ElementData_string;
107             buf[strlen(buf)-1] = '\0';
108             c->u.string = (char *)odr_malloc(o, strlen(buf)+1);
109             strcpy(c->u.string, buf);
110         }
111         r->num_elements++;
112     }
113 }