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