yaz-ztest returns dummy OPAC records.
[yaz-moved-to-github.git] / ztest / read-grs.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2008 Index Data
3  * See the file LICENSE for details.
4  */
5
6 /*
7  * Little toy-thing to read a GRS-1 records from a file.
8  */
9
10 #include <stdio.h>
11 #include <ctype.h>
12 #include <stdlib.h>
13
14 #include <yaz/proto.h>
15 #include <yaz/log.h>
16
17 #define GRS_MAX_FIELDS 50
18
19 static Z_GenericRecord *read_grs1(FILE *f, ODR o)
20 {
21     char line[512], *buf;
22     int type, ivalue;
23     char value[512];
24     Z_GenericRecord *r = 0;
25
26     for (;;)
27     {
28         Z_TaggedElement *t;
29         Z_ElementData *c;
30
31         while (fgets(buf = line, 512, f))
32         {
33             while (*buf && isspace(*(unsigned char *) buf))
34                 buf++;
35             if (!*buf || *buf == '#')
36                 continue;
37             break;
38         }
39         if (*buf == '}')
40             return r;
41         if (sscanf(buf, "(%d,%[^)])", &type, value) != 2)
42         {
43             yaz_log(YLOG_WARN, "Bad data in '%s'", buf);
44             return 0;
45         }
46         if (!type && *value == '0')
47             return r;
48         if (!(buf = strchr(buf, ')')))
49             return 0;
50         buf++;
51         while (*buf && isspace(*(unsigned char *) buf))
52             buf++;
53         if (!*buf)
54             return 0;
55         if (!r)
56         {
57             r = (Z_GenericRecord *)odr_malloc(o, sizeof(*r));
58             r->elements = (Z_TaggedElement **)
59                 odr_malloc(o, sizeof(Z_TaggedElement*) * GRS_MAX_FIELDS);
60             r->num_elements = 0;
61         }
62         r->elements[r->num_elements] = t = (Z_TaggedElement *)
63             odr_malloc(o, sizeof(Z_TaggedElement));
64         t->tagType = odr_intdup(o, type);
65         t->tagValue = (Z_StringOrNumeric *)
66             odr_malloc(o, sizeof(Z_StringOrNumeric));
67         if ((ivalue = atoi(value)))
68         {
69             t->tagValue->which = Z_StringOrNumeric_numeric;
70             t->tagValue->u.numeric = odr_intdup(o, ivalue);
71         }
72         else
73         {
74             t->tagValue->which = Z_StringOrNumeric_string;
75             t->tagValue->u.string = (char *)odr_malloc(o, strlen(value)+1);
76             strcpy(t->tagValue->u.string, value);
77         }
78         t->tagOccurrence = 0;
79         t->metaData = 0;
80         t->appliedVariant = 0;
81         t->content = c = (Z_ElementData *)odr_malloc(o, sizeof(Z_ElementData));
82         if (*buf == '{')
83         {
84             c->which = Z_ElementData_subtree;
85             c->u.subtree = read_grs1(f, o);
86         }
87         else
88         {
89             c->which = Z_ElementData_string;
90             buf[strlen(buf)-1] = '\0';
91             c->u.string = odr_strdup(o, buf);
92         }
93         r->num_elements++;
94     }
95 }
96
97 Z_GenericRecord *dummy_grs_record (int num, ODR o)
98 {
99     FILE *f = fopen("dummy-grs", "r");
100     char line[512];
101     Z_GenericRecord *r = 0;
102     int n;
103
104     if (!f)
105         return 0;
106     while (fgets(line, 512, f))
107         if (*line == '#' && sscanf(line, "#%d", &n) == 1 && n == num)
108         {
109             r = read_grs1(f, o);
110             break;
111         }
112     fclose(f);
113     return r;
114 }
115
116 /*
117  * Local variables:
118  * c-basic-offset: 4
119  * indent-tabs-mode: nil
120  * End:
121  * vim: shiftwidth=4 tabstop=8 expandtab
122  */
123