Bug fix. Relation=relevance wasn't observed.
[idzebra-moved-to-github.git] / util / zebramap.c
1 /*
2  * Copyright (C) 1994-1997, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: zebramap.c,v $
7  * Revision 1.3  1997-11-17 15:35:26  adam
8  * Bug fix. Relation=relevance wasn't observed.
9  *
10  * Revision 1.2  1997/10/31 12:39:30  adam
11  * Changed log message.
12  *
13  * Revision 1.1  1997/10/27 14:33:06  adam
14  * Moved towards generic character mapping depending on "structure"
15  * field in abstract syntax file. Fixed a few memory leaks. Fixed
16  * bug with negative integers when doing searches with relational
17  * operators.
18  *
19  */
20
21 #include <assert.h>
22 #include <ctype.h>
23
24 #include <yaz-util.h>
25 #include <charmap.h>
26 #include <zebramap.h>
27
28 struct zebra_map {
29     int reg_type;
30     chrmaptab maptab;
31     struct zebra_map *next;
32 };
33
34 struct zebra_maps {
35     char *tabpath;
36     NMEM nmem;
37     struct zebra_map *map_list;
38 };
39
40 void zebra_maps_close (ZebraMaps zms)
41 {
42     struct zebra_map *zm = zms->map_list;
43     while (zm)
44     {
45         struct zebra_map *zm_next = zm->next;
46
47         chrmaptab_destroy (zm->maptab);
48         xfree (zm);
49         zm = zm_next;
50     }
51     nmem_destroy (zms->nmem);
52     xfree (zms);
53 }
54
55 ZebraMaps zebra_maps_open (const char *tabpath)
56 {
57     ZebraMaps zms = xmalloc (sizeof(*zms));
58
59     zms->nmem = nmem_create ();
60     zms->tabpath = nmem_strdup (zms->nmem, tabpath);
61     zms->map_list = NULL;
62     return zms;
63 }
64
65 chrmaptab zebra_map_get (ZebraMaps zms, int reg_type)
66 {
67     char name[512];
68     struct zebra_map *zm;
69
70     for (zm = zms->map_list; zm; zm = zm->next)
71     {
72         if (reg_type == zm->reg_type)
73             return zm->maptab;
74     }
75     *name = '\0';
76     switch (reg_type)
77     {
78     case 'w':
79     case 'p':
80         strcat (name, "string");
81         break;
82     case 'n':
83         strcat (name, "numeric");
84         break;
85     case 'u':
86         strcat (name, "urx");
87         break;
88     default:
89         strcat (name, "null");
90     }
91     strcat (name, ".chr");
92
93     zm = xmalloc (sizeof(*zm));
94     zm->reg_type = reg_type;
95     zm->next = zms->map_list;
96     zms->map_list = zm;
97     if (!(zm->maptab = chrmaptab_create (zms->tabpath, name, 0)))
98         logf(LOG_WARN, "Failed to read character table %s", name);
99     else
100         logf(LOG_DEBUG, "Read character table %s", name);
101     return zm->maptab;
102 }
103
104 const char **zebra_maps_input (ZebraMaps zms, int reg_type,
105                                const char **from, int len)
106 {
107     static char str[2] = {0,0};
108     static const char *buf[2] = {0,0};
109     chrmaptab maptab;
110
111     maptab = zebra_map_get (zms, reg_type);
112     if (maptab)
113         return chr_map_input(maptab, from, len);
114         
115     if (isalnum(**from))
116     {
117         str[0] = isupper(**from) ? tolower(**from) : **from;
118         buf[0] = str;
119     }
120     else
121         buf[0] = (char*) CHR_SPACE;
122     (*from)++;
123     return buf;
124 }
125
126 const char *zebra_maps_output(ZebraMaps zms, int reg_type, const char **from)
127 {
128     chrmaptab maptab;
129     unsigned char i = (unsigned char) **from;
130     static char buf[2] = {0,0};
131
132     maptab = zebra_map_get (zms, reg_type);
133     if (maptab)
134         return chr_map_output (maptab, from, 1);
135     (*from)++;
136     buf[0] = i;
137     return buf;
138 }
139
140
141 /* ------------------------------------ */
142
143 typedef struct {
144     int type;
145     int major;
146     int minor;
147     Z_AttributesPlusTerm *zapt;
148 } AttrType;
149
150 static int attr_find (AttrType *src, oid_value *attributeSetP)
151 {
152     while (src->major < src->zapt->num_attributes)
153     {
154         Z_AttributeElement *element;
155
156         element = src->zapt->attributeList[src->major];
157         if (src->type == *element->attributeType)
158         {
159             switch (element->which) 
160             {
161             case Z_AttributeValue_numeric:
162                 ++(src->major);
163                 if (element->attributeSet && attributeSetP)
164                 {
165                     oident *attrset;
166
167                     attrset = oid_getentbyoid (element->attributeSet);
168                     *attributeSetP = attrset->value;
169                 }
170                 return *element->value.numeric;
171                 break;
172             case Z_AttributeValue_complex:
173                 if (src->minor >= element->value.complex->num_list ||
174                     element->value.complex->list[src->minor]->which !=  
175                     Z_StringOrNumeric_numeric)
176                     break;
177                 ++(src->minor);
178                 if (element->attributeSet && attributeSetP)
179                 {
180                     oident *attrset;
181
182                     attrset = oid_getentbyoid (element->attributeSet);
183                     *attributeSetP = attrset->value;
184                 }
185                 return *element->value.complex->list[src->minor-1]->u.numeric;
186             default:
187                 assert (0);
188             }
189         }
190         ++(src->major);
191     }
192     return -1;
193 }
194
195 static void attr_init (AttrType *src, Z_AttributesPlusTerm *zapt,
196                        int type)
197 {
198     src->zapt = zapt;
199     src->type = type;
200     src->major = 0;
201     src->minor = 0;
202 }
203
204 /* ------------------------------------ */
205
206 int zebra_maps_is_complete (ZebraMaps zms, int reg_type)
207 {
208     if (reg_type == 'p')
209         return 1;
210     return 0;
211 }
212
213 int zebra_maps_attr (ZebraMaps zms, Z_AttributesPlusTerm *zapt,
214                      int *reg_type, char **search_type, int *complete_flag)
215 {
216     AttrType completeness;
217     AttrType structure;
218     AttrType relation;
219     int completeness_value;
220     int structure_value;
221     int relation_value;
222
223     attr_init (&structure, zapt, 4);
224     attr_init (&completeness, zapt, 6);
225     attr_init (&relation, zapt, 2);
226
227     completeness_value = attr_find (&completeness, NULL);
228     structure_value = attr_find (&structure, NULL);
229     relation_value = attr_find (&relation, NULL);
230
231     if (completeness_value == 2 || completeness_value == 3)
232         *complete_flag = 1;
233     else
234         *complete_flag = 0;
235     *reg_type = 0;
236
237     *search_type = "phrase";
238     if (relation_value == 102)
239         *search_type = "ranked";
240     
241     switch (structure_value)
242     {
243     case -1:
244     case 1:   /* phrase */
245     case 2:   /* word */
246     case 3:   /* key */
247     case 6:   /* word list */
248     case 105: /* free-form-text */
249     case 106: /* document-text */
250     case 108: /* string */ 
251         if (*complete_flag)
252             *reg_type = 'p';
253         else
254             *reg_type = 'w';
255         break;
256     case 107: /* local-number */
257         *search_type = "local";
258         *reg_type = 0;
259     case 109: /* numeric string */
260         *reg_type = 'n';
261         break;
262     case 104: /* urx */
263         *reg_type = 'u';
264         break;
265     default:
266         return -1;
267     }
268     return 0;
269 }