Separate malloc debug library. Removal of ASN_COMPILED-#ifdefs.
[yaz-moved-to-github.git] / zutil / logrpn.c
1 /*
2  * Copyright (C) 1995-2001, Index Data
3  * All rights reserved.
4  *
5  * $Id: logrpn.c,v 1.5 2001-11-13 23:00:43 adam Exp $
6  */
7 #include <stdio.h>
8
9 #include <yaz/log.h>
10 #include <yaz/logrpn.h>
11
12 static void attrStr (int type, int value, enum oid_value ast, char *str)
13 {
14     *str = '\0';
15     switch (ast)
16     {
17     case VAL_BIB1:
18     case VAL_EXP1:
19     case VAL_GILS:
20         switch (type)
21         {
22         case 1:
23             sprintf (str, "use");
24             break;
25         case 2:
26             switch (value)
27             {
28             case 1:
29                 sprintf (str, "relation=Less than");
30                 break;
31             case 2:
32                 sprintf (str, "relation=Less than or equal");
33                 break;
34             case 3:
35                 sprintf (str, "relation=Equal");
36                 break;
37             case 4:
38                 sprintf (str, "relation=Greater or equal");
39                 break;
40             case 5:
41                 sprintf (str, "relation=Greater than");
42                 break;
43             case 6:
44                 sprintf (str, "relation=Not equal");
45                 break;
46             case 100:
47                 sprintf (str, "relation=Phonetic");
48                 break;
49             case 101:
50                 sprintf (str, "relation=Stem");
51                 break;
52             case 102:
53                 sprintf (str, "relation=Relevance");
54                 break;
55             case 103:
56                 sprintf (str, "relation=AlwaysMatches");
57                 break;
58             default:
59                 sprintf (str, "relation");
60             }
61             break;
62         case 3:
63             switch (value)
64             {
65             case 1:
66                 sprintf (str, "position=First in field");
67                 break;
68             case 2:
69                 sprintf (str, "position=First in any subfield");
70                 break;
71             case 3:
72                 sprintf (str, "position=Any position in field");
73                 break;
74             default:
75                 sprintf (str, "position");
76             }
77             break;
78         case 4:
79             switch (value)
80             {
81             case 1:
82                 sprintf (str, "structure=Phrase");
83                 break;
84             case 2:
85                 sprintf (str, "structure=Word");
86                 break;
87             case 3:
88                 sprintf (str, "structure=Key");
89                 break;
90             case 4:
91                 sprintf (str, "structure=Year");
92                 break;
93             case 5:
94                 sprintf (str, "structure=Date");
95                 break;
96             case 6:
97                 sprintf (str, "structure=Word list");
98                 break;
99             case 100:
100                 sprintf (str, "structure=Date (un)");
101                 break;
102             case 101:
103                 sprintf (str, "structure=Name (norm)");
104                 break;
105             case 102:
106                 sprintf (str, "structure=Name (un)");
107                 break;
108             case 103:
109                 sprintf (str, "structure=Structure");
110                 break;
111             case 104:
112                 sprintf (str, "structure=urx");
113                 break;
114             case 105:
115                 sprintf (str, "structure=free-form-text");
116                 break;
117             case 106:
118                 sprintf (str, "structure=document-text");
119                 break;
120             case 107:
121                 sprintf (str, "structure=local-number");
122                 break;
123             case 108:
124                 sprintf (str, "structure=string");
125                 break;
126             case 109:
127                 sprintf (str, "structure=numeric string");
128                 break;
129             default:
130                 sprintf (str, "structure");
131             }
132             break;
133         case 5:
134             switch (value)
135             {
136             case 1:
137                 sprintf (str, "truncation=Right");
138                 break;
139             case 2:
140                 sprintf (str, "truncation=Left");
141                 break;
142             case 3:
143                 sprintf (str, "truncation=Left&right");
144                 break;
145             case 100:
146                 sprintf (str, "truncation=Do not truncate");
147                 break;
148             case 101:
149                 sprintf (str, "truncation=Process #");
150                 break;
151             case 102:
152                 sprintf (str, "truncation=re-1");
153                 break;
154             case 103:
155                 sprintf (str, "truncation=re-2");
156                 break;
157             default:
158                 sprintf (str, "truncation");
159             }
160             break;
161         case 6:
162             switch (value)
163             {
164             case 1:
165                 sprintf (str, "completeness=Incomplete subfield");
166                 break;
167             case 2:
168                 sprintf (str, "completeness=Complete subfield");
169                 break;
170             case 3:
171                 sprintf (str, "completeness=Complete field");
172                 break;
173             default:
174                 sprintf (str, "completeness");
175             }
176             break;
177         }
178         break;
179     default:
180         break;
181     }
182     if (*str)
183         sprintf (str + strlen(str), " (%d=%d)", type, value);
184     else
185         sprintf (str, "%d=%d", type, value);
186 }
187
188 /*
189  * zlog_attributes: print attributes of term
190  */
191 static void zlog_attributes (Z_AttributesPlusTerm *t, int level,
192                              enum oid_value ast)
193 {
194     int of, i;
195     char str[80];
196     int num_attributes = t->attributes->num_attributes;
197     
198     for (of = 0; of < num_attributes; of++)
199     {
200         const char *attset_name = "";
201         Z_AttributeElement *element;
202         element = t->attributes->attributes[of];
203         if (element->attributeSet)
204         {
205             oident *attrset;
206             attrset = oid_getentbyoid (element->attributeSet);
207             attset_name = attrset->desc;
208         }
209         switch (element->which) 
210         {
211         case Z_AttributeValue_numeric:
212             attrStr (*element->attributeType,
213                      *element->value.numeric, ast, str);
214             yaz_log (LOG_LOG, "%*.0s%s %s", level, "", attset_name, str);
215             break;
216         case Z_AttributeValue_complex:
217             yaz_log (LOG_LOG, "%*.0s%s attributeType=%d complex",
218                   level, "", attset_name, *element->attributeType);
219             for (i = 0; i<element->value.complex->num_list; i++)
220             {
221                 if (element->value.complex->list[i]->which ==
222                     Z_StringOrNumeric_string)
223                     yaz_log (LOG_LOG, "%*.0s  string: '%s'", level, "",
224                              element->value.complex->list[i]->u.string);
225                 else if (element->value.complex->list[i]->which ==
226                          Z_StringOrNumeric_numeric)
227                     yaz_log (LOG_LOG, "%*.0s  numeric: '%d'", level, "",
228                              *element->value.complex->list[i]->u.numeric);
229             }
230             break;
231         default:
232             yaz_log (LOG_LOG, "%.*s%s attribute unknown",
233                      level, "", attset_name);
234         }
235     }
236 }
237
238 static void zlog_structure (Z_RPNStructure *zs, int level, enum oid_value ast)
239 {
240     if (zs->which == Z_RPNStructure_complex)
241     {
242         switch (zs->u.complex->roperator->which)
243         {
244         case Z_Operator_and:
245             yaz_log (LOG_LOG, "%*.0s and", level, "");
246             break;
247         case Z_Operator_or:
248             yaz_log (LOG_LOG, "%*.0s or", level, "");
249             break;
250         case Z_Operator_and_not:
251             yaz_log (LOG_LOG, "%*.0s and-not", level, "");
252             break;
253         default:
254             yaz_log (LOG_LOG, "%*.0s unknown complex", level, "");
255             return;
256         }
257         zlog_structure (zs->u.complex->s1, level+2, ast);
258         zlog_structure (zs->u.complex->s2, level+2, ast);
259     }
260     else if (zs->which == Z_RPNStructure_simple)
261     {
262         if (zs->u.simple->which == Z_Operand_APT)
263         {
264             Z_AttributesPlusTerm *zapt = zs->u.simple->u.attributesPlusTerm;
265
266             if (zapt->term->which == Z_Term_general) 
267             {
268                 yaz_log (LOG_LOG, "%*.0s term '%.*s' (general)", level, "",
269                          zapt->term->u.general->len,
270                          zapt->term->u.general->buf);
271             }
272             else
273             {
274                 yaz_log (LOG_LOG, "%*.0s term (not general)", level, "");
275             }
276             zlog_attributes (zapt, level+2, ast);
277         }
278         else if (zs->u.simple->which == Z_Operand_resultSetId)
279         {
280             yaz_log (LOG_LOG, "%*.0s set '%s'", level, "",
281                      zs->u.simple->u.resultSetId);
282         }
283         else
284             yaz_log (LOG_LOG, "%*.0s unknown simple structure", level, "");
285     }
286     else
287         yaz_log (LOG_LOG, "%*.0s unknown structure", level, "");
288 }
289
290 void log_rpn_query (Z_RPNQuery *rpn)
291 {
292     oident *attrset;
293     enum oid_value ast;
294     
295     attrset = oid_getentbyoid (rpn->attributeSetId);
296     if (attrset)
297     {
298         ast = attrset->value;
299         yaz_log (LOG_LOG, "RPN query. Type: %s", attrset->desc);
300     } 
301     else
302     {
303         ast = VAL_NONE;
304         yaz_log (LOG_LOG, "RPN query. Unknown type");
305     }
306     zlog_structure (rpn->RPNStructure, 0, ast);
307 }
308
309 void log_scan_term (Z_AttributesPlusTerm *zapt, oid_value ast)
310 {
311     int level = 0;
312     if (zapt->term->which == Z_Term_general) 
313     {
314         yaz_log (LOG_LOG, "%*.0s term '%.*s' (general)", level, "",
315                  zapt->term->u.general->len, zapt->term->u.general->buf);
316     }
317     else
318         yaz_log (LOG_LOG, "%*.0s term (not general)", level, "");
319     zlog_attributes (zapt, level+2, ast);
320 }