Fixed a problem in snippets handling that occurred when using string
[idzebra-moved-to-github.git] / util / zebramap.c
1 /* $Id: zebramap.c,v 1.43 2005-08-30 12:23:02 adam Exp $
2    Copyright (C) 1995-2005
3    Index Data ApS
4
5 This file is part of the Zebra server.
6
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra.  If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.
21 */
22
23 #include <assert.h>
24 #include <stdlib.h>
25 #include <ctype.h>
26
27 #include <charmap.h>
28 #include <yaz/yaz-util.h>
29
30 #include <idzebra/zebramap.h>
31
32 #define ZEBRA_MAP_TYPE_SORT  1
33 #define ZEBRA_MAP_TYPE_INDEX 2
34
35 #define ZEBRA_REPLACE_ANY  300
36
37 struct zebra_map {
38     unsigned reg_id;
39     int completeness;
40     int positioned;
41     int type;
42     union {
43         struct {
44             int dummy;
45         } index;
46         struct {
47             int entry_size;
48         } sort;
49     } u;
50     chrmaptab maptab;
51     const char *maptab_name;
52     struct zebra_map *next;
53 };
54
55 struct zebra_maps {
56     char *tabpath;
57     char *tabroot;
58     NMEM nmem;
59     struct zebra_map *map_list;
60     char temp_map_str[2];
61     const char *temp_map_ptr[2];
62     struct zebra_map **lookup_array;
63     WRBUF wrbuf_1;
64 };
65
66 void zebra_maps_close (ZebraMaps zms)
67 {
68     struct zebra_map *zm = zms->map_list;
69     while (zm)
70     {
71         if (zm->maptab)
72             chrmaptab_destroy (zm->maptab);
73         zm = zm->next;
74     }
75     wrbuf_free (zms->wrbuf_1, 1);
76     nmem_destroy (zms->nmem);
77     xfree (zms);
78 }
79
80 static void zebra_map_read (ZebraMaps zms, const char *name)
81 {
82     FILE *f;
83     char line[512];
84     char *argv[10];
85     int argc;
86     int lineno = 0;
87     struct zebra_map **zm = 0, *zp;
88
89     if (!(f = yaz_fopen(zms->tabpath, name, "r", zms->tabroot)))
90     {
91         yaz_log(YLOG_WARN|YLOG_ERRNO, "%s", name);
92         return ;
93     }
94     while ((argc = readconf_line(f, &lineno, line, 512, argv, 10)))
95     {
96         if (!yaz_matchstr (argv[0], "index") && argc == 2)
97         {
98             if (!zm)
99                 zm = &zms->map_list;
100             else
101                 zm = &(*zm)->next;
102             *zm = (struct zebra_map *) nmem_malloc (zms->nmem, sizeof(**zm));
103             (*zm)->reg_id = argv[1][0];
104             (*zm)->maptab_name = NULL;
105             (*zm)->maptab = NULL;
106             (*zm)->type = ZEBRA_MAP_TYPE_INDEX;
107             (*zm)->completeness = 0;
108             (*zm)->positioned = 1;
109         }
110         else if (!yaz_matchstr (argv[0], "sort") && argc == 2)
111         {
112             if (!zm)
113                 zm = &zms->map_list;
114             else
115                 zm = &(*zm)->next;
116             *zm = (struct zebra_map *) nmem_malloc (zms->nmem, sizeof(**zm));
117             (*zm)->reg_id = argv[1][0];
118             (*zm)->maptab_name = NULL;
119             (*zm)->type = ZEBRA_MAP_TYPE_SORT;
120             (*zm)->u.sort.entry_size = 80;
121             (*zm)->maptab = NULL;
122             (*zm)->completeness = 0;
123             (*zm)->positioned = 0;
124         }
125         else if (zm && !yaz_matchstr (argv[0], "charmap") && argc == 2)
126         {
127             (*zm)->maptab_name = nmem_strdup (zms->nmem, argv[1]);
128         }
129         else if (zm && !yaz_matchstr (argv[0], "completeness") && argc == 2)
130         {
131             (*zm)->completeness = atoi (argv[1]);
132         }
133         else if (zm && !yaz_matchstr (argv[0], "position") && argc == 2)
134         {
135             (*zm)->positioned = atoi (argv[1]);
136         }
137         else if (zm && !yaz_matchstr (argv[0], "entrysize") && argc == 2)
138         {
139             if ((*zm)->type == ZEBRA_MAP_TYPE_SORT)
140                 (*zm)->u.sort.entry_size = atoi (argv[1]);
141         }
142     }
143     if (zm)
144         (*zm)->next = NULL;
145     yaz_fclose (f);
146
147     for (zp = zms->map_list; zp; zp = zp->next)
148         zms->lookup_array[zp->reg_id] = zp;
149 }
150
151 static void zms_map_handle (void *p, const char *name, const char *value)
152 {
153     ZebraMaps zms = (ZebraMaps) p;
154     
155     zebra_map_read (zms, value);
156 }
157
158 ZebraMaps zebra_maps_open (Res res, const char *base)
159 {
160     ZebraMaps zms = (ZebraMaps) xmalloc (sizeof(*zms));
161     int i;
162
163     zms->nmem = nmem_create ();
164     zms->tabpath = nmem_strdup (zms->nmem,
165                                 res_get_def (res, "profilePath",
166                                              DEFAULT_PROFILE_PATH));
167     zms->tabroot = 0;
168     if (base)
169         zms->tabroot = nmem_strdup (zms->nmem, base);
170     zms->map_list = NULL;
171
172     zms->temp_map_str[0] = '\0';
173     zms->temp_map_str[1] = '\0';
174
175     zms->temp_map_ptr[0] = zms->temp_map_str;
176     zms->temp_map_ptr[1] = NULL;
177
178     zms->lookup_array = (struct zebra_map**)
179         nmem_malloc (zms->nmem, sizeof(*zms->lookup_array)*256);
180     for (i = 0; i<256; i++)
181         zms->lookup_array[i] = 0;
182     if (!res || !res_trav (res, "index", zms, zms_map_handle))
183         zebra_map_read (zms, "default.idx");
184
185     zms->wrbuf_1 = wrbuf_alloc();
186
187     return zms;
188 }
189
190 struct zebra_map *zebra_map_get (ZebraMaps zms, unsigned reg_id)
191 {
192     assert(reg_id >= 0 && reg_id <= 255);
193     return zms->lookup_array[reg_id];
194 }
195
196 chrmaptab zebra_charmap_get (ZebraMaps zms, unsigned reg_id)
197 {
198     struct zebra_map *zm = zebra_map_get (zms, reg_id);
199     if (!zm)
200     {
201         zm = (struct zebra_map *) nmem_malloc (zms->nmem, sizeof(*zm));
202         yaz_log (YLOG_WARN, "Unknown register type: %c", reg_id);
203
204         zm->reg_id = reg_id;
205         zm->maptab_name = nmem_strdup (zms->nmem, "@");
206         zm->maptab = NULL;
207         zm->type = ZEBRA_MAP_TYPE_INDEX;
208         zm->completeness = 0;
209         zm->next = zms->map_list;
210         zms->map_list = zm->next;
211
212         zms->lookup_array[zm->reg_id & 255] = zm;
213     }
214     if (!zm->maptab)
215     {
216         if (!zm->maptab_name || !yaz_matchstr (zm->maptab_name, "@"))
217             return NULL;
218         if (!(zm->maptab = chrmaptab_create (zms->tabpath,
219                                              zm->maptab_name,
220                                              zms->tabroot)))
221             yaz_log(YLOG_WARN, "Failed to read character table %s",
222                  zm->maptab_name);
223         else
224             yaz_log(YLOG_DEBUG, "Read character table %s", zm->maptab_name);
225     }
226     return zm->maptab;
227 }
228
229 const char **zebra_maps_input (ZebraMaps zms, unsigned reg_id,
230                                const char **from, int len, int first)
231 {
232     chrmaptab maptab;
233
234     maptab = zebra_charmap_get (zms, reg_id);
235     if (maptab)
236         return chr_map_input(maptab, from, len, first);
237     
238     zms->temp_map_str[0] = **from;
239
240     (*from)++;
241     return zms->temp_map_ptr;
242 }
243
244 const char **zebra_maps_search(ZebraMaps zms, unsigned reg_id,
245                                const char **from, int len,  int *q_map_match)
246 {
247     chrmaptab maptab;
248     
249     *q_map_match = 0;
250     maptab = zebra_charmap_get (zms, reg_id);
251     if (maptab)
252     {
253         const char **map;
254         map = chr_map_q_input(maptab, from, len, 0);
255         if (map && map[0])
256         {
257             *q_map_match = 1;
258             return map;
259         }
260         map = chr_map_input(maptab, from, len, 0);
261         if (map)
262             return map;
263     }
264     zms->temp_map_str[0] = **from;
265
266     (*from)++;
267     return zms->temp_map_ptr;
268 }
269
270 const char *zebra_maps_output(ZebraMaps zms, unsigned reg_id,
271                               const char **from)
272 {
273     chrmaptab maptab = zebra_charmap_get (zms, reg_id);
274     if (!maptab)
275         return 0;
276     return chr_map_output (maptab, from, 1);
277 }
278
279
280 /* ------------------------------------ */
281
282 typedef struct {
283     int type;
284     int major;
285     int minor;
286     Z_AttributeElement **attributeList;
287     int num_attributes;
288 } AttrType;
289
290 static int attr_find (AttrType *src, oid_value *attributeSetP)
291 {
292     while (src->major < src->num_attributes)
293     {
294         Z_AttributeElement *element;
295
296         element = src->attributeList[src->major];
297         if (src->type == *element->attributeType)
298         {
299             switch (element->which) 
300             {
301             case Z_AttributeValue_numeric:
302                 ++(src->major);
303                 if (element->attributeSet && attributeSetP)
304                 {
305                     oident *attrset;
306
307                     attrset = oid_getentbyoid (element->attributeSet);
308                     *attributeSetP = attrset->value;
309                 }
310                 return *element->value.numeric;
311                 break;
312             case Z_AttributeValue_complex:
313                 if (src->minor >= element->value.complex->num_list ||
314                     element->value.complex->list[src->minor]->which !=  
315                     Z_StringOrNumeric_numeric)
316                     break;
317                 ++(src->minor);
318                 if (element->attributeSet && attributeSetP)
319                 {
320                     oident *attrset;
321
322                     attrset = oid_getentbyoid (element->attributeSet);
323                     *attributeSetP = attrset->value;
324                 }
325                 return *element->value.complex->list[src->minor-1]->u.numeric;
326             default:
327                 assert (0);
328             }
329         }
330         ++(src->major);
331     }
332     return -1;
333 }
334
335 static void attr_init_APT (AttrType *src, Z_AttributesPlusTerm *zapt, int type)
336 {
337     src->attributeList = zapt->attributes->attributes;
338     src->num_attributes = zapt->attributes->num_attributes;
339     src->type = type;
340     src->major = 0;
341     src->minor = 0;
342 }
343
344 static void attr_init_AttrList (AttrType *src, Z_AttributeList *list, int type)
345 {
346     src->attributeList = list->attributes;
347     src->num_attributes = list->num_attributes;
348     src->type = type;
349     src->major = 0;
350     src->minor = 0;
351 }
352
353 /* ------------------------------------ */
354
355 int zebra_maps_is_complete (ZebraMaps zms, unsigned reg_id)
356
357     struct zebra_map *zm = zebra_map_get (zms, reg_id);
358     if (zm)
359         return zm->completeness;
360     return 0;
361 }
362
363 int zebra_maps_is_positioned (ZebraMaps zms, unsigned reg_id)
364 {
365     struct zebra_map *zm = zebra_map_get (zms, reg_id);
366     if (zm)
367         return zm->positioned;
368     return 0;
369 }
370     
371 int zebra_maps_is_sort (ZebraMaps zms, unsigned reg_id)
372 {
373     struct zebra_map *zm = zebra_map_get (zms, reg_id);
374     if (zm)
375         return zm->type == ZEBRA_MAP_TYPE_SORT;
376     return 0;
377 }
378
379 int zebra_maps_sort (ZebraMaps zms, Z_SortAttributes *sortAttributes,
380                      int *numerical)
381 {
382     AttrType use;
383     AttrType structure;
384     int structure_value;
385     attr_init_AttrList (&use, sortAttributes->list, 1);
386     attr_init_AttrList (&structure, sortAttributes->list, 4);
387
388     *numerical = 0;
389     structure_value = attr_find (&structure, 0);
390     if (structure_value == 109)
391         *numerical = 1;
392     return attr_find (&use, NULL);
393 }
394
395 int zebra_maps_attr (ZebraMaps zms, Z_AttributesPlusTerm *zapt,
396                      unsigned *reg_id, char **search_type, char *rank_type,
397                      int *complete_flag, int *sort_flag)
398 {
399     AttrType completeness;
400     AttrType structure;
401     AttrType relation;
402     AttrType sort_relation;
403     AttrType weight;
404     AttrType use;
405     int completeness_value;
406     int structure_value;
407     int relation_value;
408     int sort_relation_value;
409     int weight_value;
410     int use_value;
411
412     attr_init_APT (&structure, zapt, 4);
413     attr_init_APT (&completeness, zapt, 6);
414     attr_init_APT (&relation, zapt, 2);
415     attr_init_APT (&sort_relation, zapt, 7);
416     attr_init_APT (&weight, zapt, 9);
417     attr_init_APT (&use, zapt, 1);
418
419     completeness_value = attr_find (&completeness, NULL);
420     structure_value = attr_find (&structure, NULL);
421     relation_value = attr_find (&relation, NULL);
422     sort_relation_value = attr_find (&sort_relation, NULL);
423     weight_value = attr_find (&weight, NULL);
424     use_value = attr_find(&use, NULL);
425
426     if (completeness_value == 2 || completeness_value == 3)
427         *complete_flag = 1;
428     else
429         *complete_flag = 0;
430     *reg_id = 0;
431
432     *sort_flag = (sort_relation_value > 0) ? 1 : 0;
433     *search_type = "phrase";
434     strcpy (rank_type, "void");
435     if (relation_value == 102)
436     {
437         if (weight_value == -1)
438             weight_value = 34;
439         sprintf (rank_type, "rank,w=%d,u=%d", weight_value, use_value);
440     }
441     if (relation_value == 103)
442     {
443         *search_type = "always";
444         return 0;
445     }
446     if (*complete_flag)
447         *reg_id = 'p';
448     else
449         *reg_id = 'w';
450     switch (structure_value)
451     {
452     case 6:   /* word list */
453         *search_type = "and-list";
454         break;
455     case 105: /* free-form-text */
456         *search_type = "or-list";
457         break;
458     case 106: /* document-text */
459         *search_type = "or-list";
460         break;  
461     case -1:
462     case 1:   /* phrase */
463     case 2:   /* word */
464     case 108: /* string */ 
465         *search_type = "phrase";
466         break;
467     case 107: /* local-number */
468         *search_type = "local";
469         *reg_id = 0;
470         break;
471     case 109: /* numeric string */
472         *reg_id = 'n';
473         *search_type = "numeric";
474         break;
475     case 104: /* urx */
476         *reg_id = 'u';
477         *search_type = "phrase";
478         break;
479     case 3:   /* key */
480         *reg_id = '0';
481         *search_type = "phrase";
482         break;
483     case 4:  /* year */
484         *reg_id = 'y';
485         *search_type = "phrase";
486         break;
487     case 5:  /* date */
488         *reg_id = 'd';
489         *search_type = "phrase";
490         break;
491     default:
492         return -1;
493     }
494     return 0;
495 }
496
497 WRBUF zebra_replace(ZebraMaps zms, unsigned reg_id, const char *ex_list,
498                     const char *input_str, int input_len)
499 {
500     wrbuf_rewind(zms->wrbuf_1);
501     wrbuf_write(zms->wrbuf_1, input_str, input_len);
502     return zms->wrbuf_1;
503 }
504