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