Implemented the 'equivalent' directive for .chr-files.
[idzebra-moved-to-github.git] / util / zebramap.c
1 /* $Id: zebramap.c,v 1.40 2005-03-11 17:56:36 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 zm_token {
38     int *token_from;
39     char *token_to;
40     int token_min;
41     struct zm_token *next;
42 };
43
44 struct zebra_map {
45     unsigned reg_id;
46     int completeness;
47     int positioned;
48     int type;
49     union {
50         struct {
51             int dummy;
52         } index;
53         struct {
54             int entry_size;
55         } sort;
56     } u;
57     chrmaptab maptab;
58     const char *maptab_name;
59     struct zebra_map *next;
60     struct zm_token *replace_tokens;
61 };
62
63 struct zebra_maps {
64     char *tabpath;
65     char *tabroot;
66     NMEM nmem;
67     struct zebra_map *map_list;
68     char temp_map_str[2];
69     const char *temp_map_ptr[2];
70     struct zebra_map **lookup_array;
71     WRBUF wrbuf_1, wrbuf_2;
72 };
73
74 void zebra_maps_close (ZebraMaps zms)
75 {
76     struct zebra_map *zm = zms->map_list;
77     while (zm)
78     {
79         if (zm->maptab)
80             chrmaptab_destroy (zm->maptab);
81         zm = zm->next;
82     }
83     wrbuf_free (zms->wrbuf_1, 1);
84     wrbuf_free (zms->wrbuf_2, 1);
85     nmem_destroy (zms->nmem);
86     xfree (zms);
87 }
88
89 static void zebra_map_read (ZebraMaps zms, const char *name)
90 {
91     FILE *f;
92     char line[512];
93     char *argv[10];
94     int argc;
95     int lineno = 0;
96     struct zebra_map **zm = 0, *zp;
97
98     if (!(f = yaz_fopen(zms->tabpath, name, "r", zms->tabroot)))
99     {
100         yaz_log(YLOG_WARN|YLOG_ERRNO, "%s", name);
101         return ;
102     }
103     while ((argc = readconf_line(f, &lineno, line, 512, argv, 10)))
104     {
105         if (!yaz_matchstr (argv[0], "index") && argc == 2)
106         {
107             if (!zm)
108                 zm = &zms->map_list;
109             else
110                 zm = &(*zm)->next;
111             *zm = (struct zebra_map *) nmem_malloc (zms->nmem, sizeof(**zm));
112             (*zm)->reg_id = argv[1][0];
113             (*zm)->maptab_name = NULL;
114             (*zm)->maptab = NULL;
115             (*zm)->type = ZEBRA_MAP_TYPE_INDEX;
116             (*zm)->completeness = 0;
117             (*zm)->positioned = 1;
118             (*zm)->replace_tokens = 0;
119         }
120         else if (!yaz_matchstr (argv[0], "sort") && argc == 2)
121         {
122             if (!zm)
123                 zm = &zms->map_list;
124             else
125                 zm = &(*zm)->next;
126             *zm = (struct zebra_map *) nmem_malloc (zms->nmem, sizeof(**zm));
127             (*zm)->reg_id = argv[1][0];
128             (*zm)->maptab_name = NULL;
129             (*zm)->type = ZEBRA_MAP_TYPE_SORT;
130             (*zm)->u.sort.entry_size = 80;
131             (*zm)->maptab = NULL;
132             (*zm)->completeness = 0;
133             (*zm)->positioned = 0;
134             (*zm)->replace_tokens = 0;
135         }
136         else if (zm && !yaz_matchstr (argv[0], "charmap") && argc == 2)
137         {
138             (*zm)->maptab_name = nmem_strdup (zms->nmem, argv[1]);
139         }
140         else if (zm && !yaz_matchstr (argv[0], "completeness") && argc == 2)
141         {
142             (*zm)->completeness = atoi (argv[1]);
143         }
144         else if (zm && !yaz_matchstr (argv[0], "position") && argc == 2)
145         {
146             (*zm)->positioned = atoi (argv[1]);
147         }
148         else if (zm && !yaz_matchstr (argv[0], "entrysize") && argc == 2)
149         {
150             if ((*zm)->type == ZEBRA_MAP_TYPE_SORT)
151                 (*zm)->u.sort.entry_size = atoi (argv[1]);
152         }
153         else if (zm && !yaz_matchstr (argv[0], "replace") && argc >= 2)
154         {
155             struct zm_token *token = nmem_malloc (zms->nmem, sizeof(*token));
156             token->next = (*zm)->replace_tokens;
157             (*zm)->replace_tokens = token;
158 #if 0
159             yaz_log (YLOG_LOG, "replace %s", argv[1]);
160 #endif
161             token->token_from = 0;
162             if (argc >= 2)
163             {
164                 char *cp = argv[1];
165                 int *dp = token->token_from = (int *)
166                     nmem_malloc (zms->nmem, (1+strlen(cp))*sizeof(int));
167                 while (*cp)
168                     if (*cp == '$')
169                     {
170                         *dp++ = ' ';
171                         cp++;
172                     }
173                     else if (*cp == '.')
174                     {
175                         *dp++ = ZEBRA_REPLACE_ANY;
176                         cp++;
177                     }
178                     else
179                     {
180                         *dp++ = zebra_prim(&cp);
181 #if 0
182                         yaz_log (YLOG_LOG, "  char %2X %c", dp[-1], dp[-1]);
183 #endif
184                     }
185                 *dp = '\0';
186             }
187             if (argc >= 3)
188             {
189                 char *cp = argv[2];
190                 char *dp = token->token_to =
191                     nmem_malloc (zms->nmem, strlen(cp)+1);
192                 while (*cp)
193                     if (*cp == '$')
194                     {
195                         *dp++ = ' ';
196                         cp++;
197                     }
198                     else
199                         *dp++ = zebra_prim(&cp);
200                 *dp = '\0';
201             }
202             else
203                 token->token_to = 0;
204         }
205     }
206     if (zm)
207         (*zm)->next = NULL;
208     yaz_fclose (f);
209
210     for (zp = zms->map_list; zp; zp = zp->next)
211         zms->lookup_array[zp->reg_id] = zp;
212 }
213
214 static void zms_map_handle (void *p, const char *name, const char *value)
215 {
216     ZebraMaps zms = (ZebraMaps) p;
217     
218     zebra_map_read (zms, value);
219 }
220
221 ZebraMaps zebra_maps_open (Res res, const char *base)
222 {
223     ZebraMaps zms = (ZebraMaps) xmalloc (sizeof(*zms));
224     int i;
225
226     zms->nmem = nmem_create ();
227     zms->tabpath = nmem_strdup (zms->nmem,
228                                 res_get_def (res, "profilePath",
229                                              DEFAULT_PROFILE_PATH));
230     zms->tabroot = 0;
231     if (base)
232         zms->tabroot = nmem_strdup (zms->nmem, base);
233     zms->map_list = NULL;
234
235     zms->temp_map_str[0] = '\0';
236     zms->temp_map_str[1] = '\0';
237
238     zms->temp_map_ptr[0] = zms->temp_map_str;
239     zms->temp_map_ptr[1] = NULL;
240
241     zms->lookup_array = (struct zebra_map**)
242         nmem_malloc (zms->nmem, sizeof(*zms->lookup_array)*256);
243     for (i = 0; i<256; i++)
244         zms->lookup_array[i] = 0;
245     if (!res || !res_trav (res, "index", zms, zms_map_handle))
246         zebra_map_read (zms, "default.idx");
247
248     zms->wrbuf_1 = wrbuf_alloc();
249     zms->wrbuf_2 = wrbuf_alloc();
250
251     return zms;
252 }
253
254 struct zebra_map *zebra_map_get (ZebraMaps zms, unsigned reg_id)
255 {
256     return zms->lookup_array[reg_id];
257 }
258
259 chrmaptab zebra_charmap_get (ZebraMaps zms, unsigned reg_id)
260 {
261     struct zebra_map *zm = zebra_map_get (zms, reg_id);
262     if (!zm)
263     {
264         zm = (struct zebra_map *) nmem_malloc (zms->nmem, sizeof(*zm));
265         yaz_log (YLOG_WARN, "Unknown register type: %c", reg_id);
266
267         zm->reg_id = reg_id;
268         zm->maptab_name = nmem_strdup (zms->nmem, "@");
269         zm->maptab = NULL;
270         zm->type = ZEBRA_MAP_TYPE_INDEX;
271         zm->completeness = 0;
272         zm->next = zms->map_list;
273         zm->replace_tokens = 0;
274         zms->map_list = zm->next;
275
276         zms->lookup_array[zm->reg_id & 255] = zm;
277     }
278     if (!zm->maptab)
279     {
280         if (!zm->maptab_name || !yaz_matchstr (zm->maptab_name, "@"))
281             return NULL;
282         if (!(zm->maptab = chrmaptab_create (zms->tabpath,
283                                              zm->maptab_name, 0,
284                                              zms->tabroot)))
285             yaz_log(YLOG_WARN, "Failed to read character table %s",
286                  zm->maptab_name);
287         else
288             yaz_log(YLOG_DEBUG, "Read character table %s", zm->maptab_name);
289     }
290     return zm->maptab;
291 }
292
293 const char **zebra_maps_input (ZebraMaps zms, unsigned reg_id,
294                                const char **from, int len, int first)
295 {
296     chrmaptab maptab;
297
298     maptab = zebra_charmap_get (zms, reg_id);
299     if (maptab)
300         return chr_map_input(maptab, from, len, first);
301     
302     zms->temp_map_str[0] = **from;
303
304     (*from)++;
305     return zms->temp_map_ptr;
306 }
307
308 const char **zebra_maps_search(ZebraMaps zms, unsigned reg_id,
309                                const char **from, int len,  int *q_map_match)
310 {
311     chrmaptab maptab;
312     
313     *q_map_match = 0;
314     maptab = zebra_charmap_get (zms, reg_id);
315     if (maptab)
316     {
317         const char **map;
318         map = chr_map_q_input(maptab, from, len, 0);
319         if (map && map[0])
320         {
321             *q_map_match = 1;
322             return map;
323         }
324         map = chr_map_input(maptab, from, len, 0);
325         if (map)
326             return map;
327     }
328     zms->temp_map_str[0] = **from;
329
330     (*from)++;
331     return zms->temp_map_ptr;
332 }
333
334 const char *zebra_maps_output(ZebraMaps zms, unsigned reg_id,
335                               const char **from)
336 {
337     chrmaptab maptab = zebra_charmap_get (zms, reg_id);
338     if (!maptab)
339         return 0;
340     return chr_map_output (maptab, from, 1);
341 }
342
343
344 /* ------------------------------------ */
345
346 typedef struct {
347     int type;
348     int major;
349     int minor;
350     Z_AttributeElement **attributeList;
351     int num_attributes;
352 } AttrType;
353
354 static int attr_find (AttrType *src, oid_value *attributeSetP)
355 {
356     while (src->major < src->num_attributes)
357     {
358         Z_AttributeElement *element;
359
360         element = src->attributeList[src->major];
361         if (src->type == *element->attributeType)
362         {
363             switch (element->which) 
364             {
365             case Z_AttributeValue_numeric:
366                 ++(src->major);
367                 if (element->attributeSet && attributeSetP)
368                 {
369                     oident *attrset;
370
371                     attrset = oid_getentbyoid (element->attributeSet);
372                     *attributeSetP = attrset->value;
373                 }
374                 return *element->value.numeric;
375                 break;
376             case Z_AttributeValue_complex:
377                 if (src->minor >= element->value.complex->num_list ||
378                     element->value.complex->list[src->minor]->which !=  
379                     Z_StringOrNumeric_numeric)
380                     break;
381                 ++(src->minor);
382                 if (element->attributeSet && attributeSetP)
383                 {
384                     oident *attrset;
385
386                     attrset = oid_getentbyoid (element->attributeSet);
387                     *attributeSetP = attrset->value;
388                 }
389                 return *element->value.complex->list[src->minor-1]->u.numeric;
390             default:
391                 assert (0);
392             }
393         }
394         ++(src->major);
395     }
396     return -1;
397 }
398
399 static void attr_init_APT (AttrType *src, Z_AttributesPlusTerm *zapt, int type)
400 {
401     src->attributeList = zapt->attributes->attributes;
402     src->num_attributes = zapt->attributes->num_attributes;
403     src->type = type;
404     src->major = 0;
405     src->minor = 0;
406 }
407
408 static void attr_init_AttrList (AttrType *src, Z_AttributeList *list, int type)
409 {
410     src->attributeList = list->attributes;
411     src->num_attributes = list->num_attributes;
412     src->type = type;
413     src->major = 0;
414     src->minor = 0;
415 }
416
417 /* ------------------------------------ */
418
419 int zebra_maps_is_complete (ZebraMaps zms, unsigned reg_id)
420
421     struct zebra_map *zm = zebra_map_get (zms, reg_id);
422     if (zm)
423         return zm->completeness;
424     return 0;
425 }
426
427 int zebra_maps_is_positioned (ZebraMaps zms, unsigned reg_id)
428 {
429     struct zebra_map *zm = zebra_map_get (zms, reg_id);
430     if (zm)
431         return zm->positioned;
432     return 0;
433 }
434     
435 int zebra_maps_is_sort (ZebraMaps zms, unsigned reg_id)
436 {
437     struct zebra_map *zm = zebra_map_get (zms, reg_id);
438     if (zm)
439         return zm->type == ZEBRA_MAP_TYPE_SORT;
440     return 0;
441 }
442
443 int zebra_maps_sort (ZebraMaps zms, Z_SortAttributes *sortAttributes,
444                      int *numerical)
445 {
446     AttrType use;
447     AttrType structure;
448     int structure_value;
449     attr_init_AttrList (&use, sortAttributes->list, 1);
450     attr_init_AttrList (&structure, sortAttributes->list, 4);
451
452     *numerical = 0;
453     structure_value = attr_find (&structure, 0);
454     if (structure_value == 109)
455         *numerical = 1;
456     return attr_find (&use, NULL);
457 }
458
459 int zebra_maps_attr (ZebraMaps zms, Z_AttributesPlusTerm *zapt,
460                      unsigned *reg_id, char **search_type, char *rank_type,
461                      int *complete_flag, int *sort_flag)
462 {
463     AttrType completeness;
464     AttrType structure;
465     AttrType relation;
466     AttrType sort_relation;
467     AttrType weight;
468     AttrType use;
469     int completeness_value;
470     int structure_value;
471     int relation_value;
472     int sort_relation_value;
473     int weight_value;
474     int use_value;
475
476     attr_init_APT (&structure, zapt, 4);
477     attr_init_APT (&completeness, zapt, 6);
478     attr_init_APT (&relation, zapt, 2);
479     attr_init_APT (&sort_relation, zapt, 7);
480     attr_init_APT (&weight, zapt, 9);
481     attr_init_APT (&use, zapt, 1);
482
483     completeness_value = attr_find (&completeness, NULL);
484     structure_value = attr_find (&structure, NULL);
485     relation_value = attr_find (&relation, NULL);
486     sort_relation_value = attr_find (&sort_relation, NULL);
487     weight_value = attr_find (&weight, NULL);
488     use_value = attr_find(&use, NULL);
489
490     if (completeness_value == 2 || completeness_value == 3)
491         *complete_flag = 1;
492     else
493         *complete_flag = 0;
494     *reg_id = 0;
495
496     *sort_flag = (sort_relation_value > 0) ? 1 : 0;
497     *search_type = "phrase";
498     strcpy (rank_type, "void");
499     if (relation_value == 102)
500     {
501         if (weight_value == -1)
502             weight_value = 34;
503         sprintf (rank_type, "rank,w=%d,u=%d", weight_value, use_value);
504     }
505     if (relation_value == 103)
506     {
507         *search_type = "always";
508         return 0;
509     }
510     if (*complete_flag)
511         *reg_id = 'p';
512     else
513         *reg_id = 'w';
514     switch (structure_value)
515     {
516     case 6:   /* word list */
517         *search_type = "and-list";
518         break;
519     case 105: /* free-form-text */
520         *search_type = "or-list";
521         break;
522     case 106: /* document-text */
523         *search_type = "or-list";
524         break;  
525     case -1:
526     case 1:   /* phrase */
527     case 2:   /* word */
528     case 108: /* string */ 
529         *search_type = "phrase";
530         break;
531     case 107: /* local-number */
532         *search_type = "local";
533         *reg_id = 0;
534         break;
535     case 109: /* numeric string */
536         *reg_id = 'n';
537         *search_type = "numeric";
538         break;
539     case 104: /* urx */
540         *reg_id = 'u';
541         *search_type = "phrase";
542         break;
543     case 3:   /* key */
544         *reg_id = '0';
545         *search_type = "phrase";
546         break;
547     case 4:  /* year */
548         *reg_id = 'y';
549         *search_type = "phrase";
550         break;
551     case 5:  /* date */
552         *reg_id = 'd';
553         *search_type = "phrase";
554         break;
555     default:
556         return -1;
557     }
558     return 0;
559 }
560
561 int zebra_replace_sub(ZebraMaps zms, unsigned reg_id, const char *ex_list,
562                       const char *input_str, int input_len, WRBUF wrbuf);
563
564 WRBUF zebra_replace(ZebraMaps zms, unsigned reg_id, const char *ex_list,
565                     const char *input_str, int input_len)
566 {
567     struct zebra_map *zm = zebra_map_get (zms, reg_id);
568
569     wrbuf_rewind(zms->wrbuf_1);
570     wrbuf_write(zms->wrbuf_1, input_str, input_len);
571     if (!zm || !zm->replace_tokens)
572         return zms->wrbuf_1;
573   
574 #if 0
575     yaz_log (YLOG_LOG, "in:%.*s:", wrbuf_len(zms->wrbuf_1),
576           wrbuf_buf(zms->wrbuf_1));
577 #endif
578     for (;;)
579     {
580         if (!zebra_replace_sub(zms, reg_id, ex_list, wrbuf_buf(zms->wrbuf_1),
581                                wrbuf_len(zms->wrbuf_1), zms->wrbuf_2))
582             return zms->wrbuf_2;
583         if (!zebra_replace_sub(zms, reg_id, ex_list, wrbuf_buf(zms->wrbuf_2),
584                                wrbuf_len(zms->wrbuf_2), zms->wrbuf_1))
585             return zms->wrbuf_1;
586     }
587     return 0;
588 }
589
590 int zebra_replace_sub(ZebraMaps zms, unsigned reg_id, const char *ex_list,
591                       const char *input_str, int input_len, WRBUF wrbuf)
592 {
593     int i = -1;
594     int no_replaces = 0;
595     struct zebra_map *zm = zebra_map_get (zms, reg_id);
596
597     wrbuf_rewind(wrbuf);
598     for (i = -1; i <= input_len; )
599     {
600         struct zm_token *token;
601         char replace_string[128];
602         int replace_out = 0;
603         int replace_in = 0;
604
605         for (token = zm->replace_tokens; !replace_in && token;
606              token = token->next)
607         {
608             int j = 0;
609             int replace_done = 0;
610             replace_out = 0;
611             for (;; j++)
612             {
613                 int c;
614                 if (!token->token_from[j])
615                 {
616                     replace_in = j;
617                     break;
618                 }
619                 if (ex_list && strchr (ex_list, token->token_from[j]))
620                     break;
621                 if (i+j < 0 || j+i >= input_len)
622                     c = ' ';
623                 else
624                     c = input_str[j+i] & 255;
625                 if (token->token_from[j] == ZEBRA_REPLACE_ANY)
626                 {
627                     if (c == ' ')
628                         break;
629                     replace_string[replace_out++] = c;
630                 }
631                 else
632                 {
633                     if (c != token->token_from[j])
634                     {
635                         break;
636                     }
637                     if (!replace_done)
638                     {
639                         const char *cp = token->token_to;
640                         replace_done = 1;
641                         for (; cp && *cp; cp++)
642                             replace_string[replace_out++] = *cp;
643                     }
644                 }
645             }
646         }
647         if (!replace_in)
648         {
649             if (i >= 0 && i < input_len)
650                 wrbuf_putc(wrbuf, input_str[i]);
651             i++;
652         }
653         else
654         {
655             no_replaces++;
656             if (replace_out)
657                 wrbuf_write(wrbuf, replace_string, replace_out);
658             i += replace_in;
659         }
660     }
661 #if 0
662     yaz_log (YLOG_LOG, "out:%.*s:", wrbuf_len(wrbuf), wrbuf_buf(wrbuf));
663 #endif
664     return no_replaces;
665 }