Work on Explain
[yaz-moved-to-github.git] / retrieval / d1_expout.c
1 /*
2  * Copyright (c) 1995, Index Data.
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: d1_expout.c,v $
7  * Revision 1.1  1995-12-14 11:09:51  quinn
8  * Work on Explain
9  *
10  *
11  */
12
13 #include <assert.h>
14 #include <string.h>
15 #include <stdlib.h>
16
17 #include <log.h>
18 #include <proto.h>
19 #include <data1.h>
20
21 static int *f_integer(data1_node *c, ODR o)
22 {
23     int *r;
24     char intbuf[64];
25
26     if (!c->child || c->child->which != DATA1N_data ||
27         c->child->u.data.len > 63)
28         return 0;
29     r = odr_malloc(o, sizeof(*r));
30     sprintf(intbuf, "%.*s", 63, c->child->u.data.data);
31     *r = atoi(intbuf);
32     return r;
33 }
34
35 static char *f_string(data1_node *c, ODR o)
36 {
37     char *r;
38
39     if (!c->child || c->child->which != DATA1N_data)
40         return 0;
41     r = odr_malloc(o, c->child->u.data.len+1);
42     memcpy(r, c->child->u.data.data, c->child->u.data.len);
43     r[c->child->u.data.len] = '\0';
44     return r;
45 }
46
47 static bool_t *f_bool(data1_node *c, ODR o)
48 {
49     return 0;
50 }
51
52 static Z_IntUnit *f_intunit(data1_node *c, ODR o)
53 {
54     return 0;
55 }
56
57 static Z_HumanString *f_humstring(data1_node *c, ODR o)
58 {
59     Z_HumanString *r;
60     Z_HumanStringUnit *u;
61
62     if (!c->child || c->child->which != DATA1N_data)
63         return 0;
64     r = odr_malloc(o, sizeof(*r));
65     r->num_strings = 1;
66     r->strings = odr_malloc(o, sizeof(Z_HumanStringUnit*));
67     r->strings[0] = u = odr_malloc(o, sizeof(*u));
68     u->language = 0;
69     u->text = odr_malloc(o, c->child->u.data.len+1);
70     memcpy(u->text, c->child->u.data.data, c->child->u.data.len);
71     u->text[c->child->u.data.len] = '\0';
72     return r;
73 }
74
75 static Z_CommonInfo *f_commonInfo(data1_node *n, int select, ODR o)
76 {
77     return 0;
78 }
79
80 static Z_AccessInfo *f_accessInfo(data1_node *n, int select, ODR o)
81 {
82     return 0;
83 }
84
85 static Z_ContactInfo *f_contactInfo(data1_node *n, ODR o)
86 {
87     return 0;
88 }
89
90 static Z_TargetInfo *f_targetInfo(data1_node *n, int select, ODR o)
91 {
92     Z_TargetInfo *res = odr_malloc(o, sizeof(*res));
93     data1_node *c;
94     static bool_t fl = 0;
95
96     res->commonInfo = 0;
97     res->name = 0;
98     res->recentNews = 0;
99     res->icon = 0;
100     res->namedResultSets = &fl;
101     res->multipleDbSearch = &fl;
102     res->maxResultSets = 0;
103     res->maxResultSize = 0;
104     res->maxTerms = 0;
105     res->timeoutInterval = 0;
106     res->welcomeMessage = 0;
107     res->contactInfo = 0;
108     res->description = 0;
109     res->num_nicknames = 0;
110     res->nicknames = 0;
111     res->usageRest = 0;
112     res->paymentAddr = 0;
113     res->hours = 0;
114     res->num_dbCombinations = 0;
115     res->dbCombinations = 0;
116     res->num_addresses = 0;
117     res->addresses = 0;
118     res->commonAccessInfo = 0;
119
120     for (c = n->child; c; c = c->next)
121     {
122         if (c->which != DATA1N_tag || !c->u.tag.element)
123         {
124             logf(LOG_WARN, "Malformed explain record");
125             return 0;
126         }
127         if (select && !c->u.tag.node_selected)
128             continue;
129         switch (c->u.tag.element->tag->value.numeric)
130         {
131             case 600: res->commonInfo = f_commonInfo(c, select, o);break;
132             case 102: res->name = f_string(c, o); break;
133             case 103: res->recentNews = f_humstring(c, o); break;
134             case 104: break; /* icon */
135             case 105: res->namedResultSets = f_bool(c, o); break;
136             case 106: res->multipleDbSearch = f_bool(c, o); break;
137             case 107: res->maxResultSets = f_integer(c, o); break;
138             case 108: res->maxResultSize = f_integer(c, o); break;
139             case 109: res->maxTerms = f_integer(c, o); break;
140             case 110: res->timeoutInterval = f_intunit(c, o); break;
141             case 111: res->welcomeMessage = f_humstring(c, o); break;
142             case 112: res->contactInfo = f_contactInfo(c, o); break;
143             case 113: res->description = f_humstring(c, o); break;
144             case 114: break; /* nicknames */
145             case 115: res->usageRest = f_humstring(c, o); break;
146             case 116: res->paymentAddr = f_humstring(c, o); break;
147             case 117: res->hours = f_humstring(c, o); break;
148             case 118: break; /* dbcombinations */
149             case 119: break; /* addresses */
150             case 500: res->commonAccessInfo = f_accessInfo(c, select, o); break;
151             default:
152                 logf(LOG_WARN, "Unknown target-info element");
153         }
154     }
155     return res;
156 }
157
158 static Z_DatabaseInfo *f_databaseInfo(data1_node *n, int select, ODR o)
159 {
160     return 0;
161 }
162
163 Z_ExplainRecord *data1_nodetoexplain(data1_node *n, int select, ODR o)
164 {
165     Z_ExplainRecord *res = odr_malloc(o, sizeof(*res));
166
167     assert(n->which == DATA1N_root);
168     if (strcmp(n->u.root.type, "explain"))
169     {
170         logf(LOG_WARN, "Attempt to convert a non-Explain record");
171         return 0;
172     }
173     if (n->num_children != 1 || n->child->which != DATA1N_tag ||
174         !n->u.tag.element)
175     {
176         logf(LOG_WARN, "Explain record should have one exactly one child");
177         return 0;
178     }
179     switch (n->child->u.tag.element->tag->value.numeric)
180     {
181         case 0: res->which = Z_Explain_targetInfo;
182             if (!(res->u.targetInfo = f_targetInfo(n->child, select, o)))
183                 return 0;
184             break;
185         case 1: res->which = Z_Explain_databaseInfo;
186             if (!(res->u.databaseInfo = f_databaseInfo(n->child, select, o)))
187                 return 0;
188             break;
189         default:
190             logf(LOG_WARN, "Unknown explain category");
191             return 0;
192     }
193     return res;
194 }