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