Changed character map facility so that admin can specify character
[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.4  1997-11-18 10:05:08  adam
8  * Changed character map facility so that admin can specify character
9  * mapping files for each register type, w, p, etc.
10  *
11  * Revision 1.3  1997/11/17 15:35:26  adam
12  * Bug fix. Relation=relevance wasn't observed.
13  *
14  * Revision 1.2  1997/10/31 12:39:30  adam
15  * Changed log message.
16  *
17  * Revision 1.1  1997/10/27 14:33:06  adam
18  * Moved towards generic character mapping depending on "structure"
19  * field in abstract syntax file. Fixed a few memory leaks. Fixed
20  * bug with negative integers when doing searches with relational
21  * operators.
22  *
23  */
24
25 #include <assert.h>
26 #include <ctype.h>
27
28 #include <yaz-util.h>
29 #include <charmap.h>
30 #include <zebramap.h>
31
32 struct zebra_map {
33     int reg_type;
34     int completeness;
35     chrmaptab maptab;
36     const char *maptab_name;
37     struct zebra_map *next;
38 };
39
40 struct zebra_maps {
41     char *tabpath;
42     NMEM nmem;
43     struct zebra_map *map_list;
44     char temp_map_str[2];
45     const char *temp_map_ptr[2];
46 };
47
48 void zebra_maps_close (ZebraMaps zms)
49 {
50     struct zebra_map *zm = zms->map_list;
51     while (zm)
52     {
53         struct zebra_map *zm_next = zm->next;
54
55         chrmaptab_destroy (zm->maptab);
56         xfree (zm);
57         zm = zm_next;
58     }
59     nmem_destroy (zms->nmem);
60     xfree (zms);
61 }
62
63 static void zebra_map_read (ZebraMaps zms, const char *name)
64 {
65     FILE *f;
66     char line[512];
67     char *argv[10];
68     int argc;
69     struct zebra_map **zm = 0;
70
71     if (!(f = yaz_path_fopen(zms->tabpath, name, "r")))
72     {
73         logf(LOG_WARN|LOG_ERRNO, "%s", name);
74         return ;
75     }
76     while ((argc = readconf_line(f, line, 512, argv, 10)))
77     {
78         if (!strcmp (argv[0], "index") && argc == 2)
79         {
80             if (!zm)
81                 zm = &zms->map_list;
82             else
83                 zm = &(*zm)->next;
84             *zm = nmem_malloc (zms->nmem, sizeof(**zm));
85             (*zm)->reg_type = argv[1][0];
86             (*zm)->maptab_name = NULL;
87             (*zm)->maptab = NULL;
88             (*zm)->completeness = 0;
89         }
90         else if (zm && !strcmp (argv[0], "charmap") && argc == 2)
91         {
92             (*zm)->maptab_name = nmem_strdup (zms->nmem, argv[1]);
93         }
94         else if (zm && !strcmp (argv[0], "completeness") && argc == 2)
95         {
96             (*zm)->completeness = atoi (argv[1]);
97         }
98     }
99     if (zm)
100         (*zm)->next = NULL;
101     fclose (f);
102 }
103 static void zms_map_handle (void *p, const char *name, const char *value)
104 {
105     ZebraMaps zms = p;
106     
107     zebra_map_read (zms, value);
108 }
109
110 ZebraMaps zebra_maps_open (const char *tabpath, Res res)
111 {
112     ZebraMaps zms = xmalloc (sizeof(*zms));
113
114     zms->nmem = nmem_create ();
115     zms->tabpath = nmem_strdup (zms->nmem, tabpath);
116     zms->map_list = NULL;
117
118     zms->temp_map_str[0] = '\0';
119     zms->temp_map_str[1] = '\0';
120
121     zms->temp_map_ptr[0] = zms->temp_map_str;
122     zms->temp_map_ptr[1] = NULL;
123     
124     if (!res_trav (res, "index", zms, zms_map_handle))
125         zebra_map_read (zms, "default.idx");
126     return zms;
127 }
128
129 chrmaptab zebra_map_get (ZebraMaps zms, int reg_type)
130 {
131     struct zebra_map *zm;
132     
133     for (zm = zms->map_list; zm; zm = zm->next)
134         if (reg_type == zm->reg_type)
135             break;
136     if (!zm)
137     {
138         logf (LOG_WARN, "unknown register type: %c", reg_type);
139         return NULL;
140     }
141     if (!zm->maptab)
142     {
143         if (!strcmp (zm->maptab_name, "@"))
144             return NULL;
145         if (!(zm->maptab = chrmaptab_create (zms->tabpath,
146                                              zm->maptab_name, 0)))
147             logf(LOG_WARN, "Failed to read character table %s",
148                  zm->maptab_name);
149         else
150             logf(LOG_DEBUG, "Read character table %s", zm->maptab_name);
151     }
152     return zm->maptab;
153 }
154
155 const char **zebra_maps_input (ZebraMaps zms, int reg_type,
156                                const char **from, int len)
157 {
158     chrmaptab maptab;
159
160     maptab = zebra_map_get (zms, reg_type);
161     if (maptab)
162         return chr_map_input(maptab, from, len);
163     
164     zms->temp_map_str[0] = **from;
165
166     (*from)++;
167     return zms->temp_map_ptr;
168 }
169
170 const char *zebra_maps_output(ZebraMaps zms, int reg_type, const char **from)
171 {
172     chrmaptab maptab;
173     unsigned char i = (unsigned char) **from;
174     static char buf[2] = {0,0};
175
176     maptab = zebra_map_get (zms, reg_type);
177     if (maptab)
178         return chr_map_output (maptab, from, 1);
179     (*from)++;
180     buf[0] = i;
181     return buf;
182 }
183
184
185 /* ------------------------------------ */
186
187 typedef struct {
188     int type;
189     int major;
190     int minor;
191     Z_AttributesPlusTerm *zapt;
192 } AttrType;
193
194 static int attr_find (AttrType *src, oid_value *attributeSetP)
195 {
196     while (src->major < src->zapt->num_attributes)
197     {
198         Z_AttributeElement *element;
199
200         element = src->zapt->attributeList[src->major];
201         if (src->type == *element->attributeType)
202         {
203             switch (element->which) 
204             {
205             case Z_AttributeValue_numeric:
206                 ++(src->major);
207                 if (element->attributeSet && attributeSetP)
208                 {
209                     oident *attrset;
210
211                     attrset = oid_getentbyoid (element->attributeSet);
212                     *attributeSetP = attrset->value;
213                 }
214                 return *element->value.numeric;
215                 break;
216             case Z_AttributeValue_complex:
217                 if (src->minor >= element->value.complex->num_list ||
218                     element->value.complex->list[src->minor]->which !=  
219                     Z_StringOrNumeric_numeric)
220                     break;
221                 ++(src->minor);
222                 if (element->attributeSet && attributeSetP)
223                 {
224                     oident *attrset;
225
226                     attrset = oid_getentbyoid (element->attributeSet);
227                     *attributeSetP = attrset->value;
228                 }
229                 return *element->value.complex->list[src->minor-1]->u.numeric;
230             default:
231                 assert (0);
232             }
233         }
234         ++(src->major);
235     }
236     return -1;
237 }
238
239 static void attr_init (AttrType *src, Z_AttributesPlusTerm *zapt,
240                        int type)
241 {
242     src->zapt = zapt;
243     src->type = type;
244     src->major = 0;
245     src->minor = 0;
246 }
247
248 /* ------------------------------------ */
249
250 int zebra_maps_is_complete (ZebraMaps zms, int reg_type)
251
252     struct zebra_map *zm;
253     
254     for (zm = zms->map_list; zm; zm = zm->next)
255         if (reg_type == zm->reg_type)
256             return zm->completeness;
257     return 0;
258 }
259
260 int zebra_maps_attr (ZebraMaps zms, Z_AttributesPlusTerm *zapt,
261                      int *reg_type, char **search_type, int *complete_flag)
262 {
263     AttrType completeness;
264     AttrType structure;
265     AttrType relation;
266     int completeness_value;
267     int structure_value;
268     int relation_value;
269
270     attr_init (&structure, zapt, 4);
271     attr_init (&completeness, zapt, 6);
272     attr_init (&relation, zapt, 2);
273
274     completeness_value = attr_find (&completeness, NULL);
275     structure_value = attr_find (&structure, NULL);
276     relation_value = attr_find (&relation, NULL);
277
278     if (completeness_value == 2 || completeness_value == 3)
279         *complete_flag = 1;
280     else
281         *complete_flag = 0;
282     *reg_type = 0;
283
284     *search_type = "phrase";
285     if (relation_value == 102)
286         *search_type = "ranked";
287     
288     switch (structure_value)
289     {
290     case -1:
291     case 1:   /* phrase */
292     case 2:   /* word */
293     case 3:   /* key */
294     case 6:   /* word list */
295     case 105: /* free-form-text */
296     case 106: /* document-text */
297     case 108: /* string */ 
298         if (*complete_flag)
299             *reg_type = 'p';
300         else
301             *reg_type = 'w';
302         break;
303     case 107: /* local-number */
304         *search_type = "local";
305         *reg_type = 0;
306     case 109: /* numeric string */
307         *reg_type = 'n';
308         break;
309     case 104: /* urx */
310         *reg_type = 'u';
311         break;
312     default:
313         return -1;
314     }
315     return 0;
316 }