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