Towards GPL
[idzebra-moved-to-github.git] / util / zebramap.c
1 /* $Id: zebramap.c,v 1.27 2002-08-02 19:26:57 adam Exp $
2    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
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
24
25 #include <assert.h>
26 #include <ctype.h>
27
28 #include <yaz/yaz-util.h>
29 #include <charmap.h>
30 #include <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         logf(LOG_WARN|LOG_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             logf (LOG_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                         logf (LOG_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     zms->tabroot = 0;
230     if (base)
231         zms->tabroot = nmem_strdup (zms->nmem, base);
232     zms->map_list = NULL;
233
234     zms->temp_map_str[0] = '\0';
235     zms->temp_map_str[1] = '\0';
236
237     zms->temp_map_ptr[0] = zms->temp_map_str;
238     zms->temp_map_ptr[1] = NULL;
239
240     zms->lookup_array = (struct zebra_map**)
241         nmem_malloc (zms->nmem, sizeof(*zms->lookup_array)*256);
242     for (i = 0; i<256; i++)
243         zms->lookup_array[i] = 0;
244     if (!res || !res_trav (res, "index", zms, zms_map_handle))
245         zebra_map_read (zms, "default.idx");
246
247     zms->wrbuf_1 = wrbuf_alloc();
248     zms->wrbuf_2 = wrbuf_alloc();
249
250     return zms;
251 }
252
253 struct zebra_map *zebra_map_get (ZebraMaps zms, unsigned reg_id)
254 {
255     return zms->lookup_array[reg_id];
256 }
257
258 chrmaptab zebra_charmap_get (ZebraMaps zms, unsigned reg_id)
259 {
260     struct zebra_map *zm = zebra_map_get (zms, reg_id);
261     if (!zm)
262     {
263         zm = (struct zebra_map *) nmem_malloc (zms->nmem, sizeof(*zm));
264         logf (LOG_WARN, "Unknown register type: %c", reg_id);
265
266         zm->reg_id = reg_id;
267         zm->maptab_name = nmem_strdup (zms->nmem, "@");
268         zm->maptab = NULL;
269         zm->type = ZEBRA_MAP_TYPE_INDEX;
270         zm->completeness = 0;
271         zm->next = zms->map_list;
272         zms->map_list = zm->next;
273
274         zms->lookup_array[zm->reg_id & 255] = zm;
275     }
276     if (!zm->maptab)
277     {
278         if (!zm->maptab_name || !yaz_matchstr (zm->maptab_name, "@"))
279             return NULL;
280         if (!(zm->maptab = chrmaptab_create (zms->tabpath,
281                                              zm->maptab_name, 0,
282                                              zms->tabroot)))
283             logf(LOG_WARN, "Failed to read character table %s",
284                  zm->maptab_name);
285         else
286             logf(LOG_DEBUG, "Read character table %s", zm->maptab_name);
287     }
288     return zm->maptab;
289 }
290
291 const char **zebra_maps_input (ZebraMaps zms, unsigned reg_id,
292                                const char **from, int len)
293 {
294     chrmaptab maptab;
295
296     maptab = zebra_charmap_get (zms, reg_id);
297     if (maptab)
298         return chr_map_input(maptab, from, len);
299     
300     zms->temp_map_str[0] = **from;
301
302     (*from)++;
303     return zms->temp_map_ptr;
304 }
305
306 #if 0
307 int zebra_maps_input_tokens (ZebraMaps zms, unsigned reg_id,
308                              const char *input_str, int input_len,
309                              WRBUF wrbuf)
310 {
311     chrmaptab maptab = zebra_charmap_get (zms, reg_id);
312     int len[4];
313     char *str[3];
314     int input_i = 0;
315     int first = 1;
316     const char **out;
317         
318     if (!maptab)
319     {
320         wrbuf_write (wrbuf, input_str, input_len);
321         return -1;
322     }
323     str[0] = " ";
324     len[0] = 1;
325     str[1] = input_str;
326     len[1] = input_len;
327     str[2] = " ";
328     len[2] = 1;
329     len[3] = -1;
330     
331     out = chr_map_input (maptab, str, len);
332     while (len[1] > 0)
333     {
334         while (out && *out && **out == *CHR_SPACE)
335             out = chr_map_input (maptab, str, len);
336     }
337 }
338 #endif
339
340 const char *zebra_maps_output(ZebraMaps zms, unsigned reg_id,
341                               const char **from)
342 {
343     chrmaptab maptab = zebra_charmap_get (zms, reg_id);
344     if (!maptab)
345         return 0;
346     return chr_map_output (maptab, from, 1);
347 }
348
349
350 /* ------------------------------------ */
351
352 typedef struct {
353     int type;
354     int major;
355     int minor;
356     Z_AttributeElement **attributeList;
357     int num_attributes;
358 } AttrType;
359
360 static int attr_find (AttrType *src, oid_value *attributeSetP)
361 {
362     while (src->major < src->num_attributes)
363     {
364         Z_AttributeElement *element;
365
366         element = src->attributeList[src->major];
367         if (src->type == *element->attributeType)
368         {
369             switch (element->which) 
370             {
371             case Z_AttributeValue_numeric:
372                 ++(src->major);
373                 if (element->attributeSet && attributeSetP)
374                 {
375                     oident *attrset;
376
377                     attrset = oid_getentbyoid (element->attributeSet);
378                     *attributeSetP = attrset->value;
379                 }
380                 return *element->value.numeric;
381                 break;
382             case Z_AttributeValue_complex:
383                 if (src->minor >= element->value.complex->num_list ||
384                     element->value.complex->list[src->minor]->which !=  
385                     Z_StringOrNumeric_numeric)
386                     break;
387                 ++(src->minor);
388                 if (element->attributeSet && attributeSetP)
389                 {
390                     oident *attrset;
391
392                     attrset = oid_getentbyoid (element->attributeSet);
393                     *attributeSetP = attrset->value;
394                 }
395                 return *element->value.complex->list[src->minor-1]->u.numeric;
396             default:
397                 assert (0);
398             }
399         }
400         ++(src->major);
401     }
402     return -1;
403 }
404
405 static void attr_init_APT (AttrType *src, Z_AttributesPlusTerm *zapt, int type)
406 {
407 #ifdef ASN_COMPILED
408     src->attributeList = zapt->attributes->attributes;
409     src->num_attributes = zapt->attributes->num_attributes;
410 #else
411     src->attributeList = zapt->attributeList;
412     src->num_attributes = zapt->num_attributes;
413 #endif
414     src->type = type;
415     src->major = 0;
416     src->minor = 0;
417 }
418
419 static void attr_init_AttrList (AttrType *src, Z_AttributeList *list, int type)
420 {
421     src->attributeList = list->attributes;
422     src->num_attributes = list->num_attributes;
423     src->type = type;
424     src->major = 0;
425     src->minor = 0;
426 }
427
428 /* ------------------------------------ */
429
430 int zebra_maps_is_complete (ZebraMaps zms, unsigned reg_id)
431
432     struct zebra_map *zm = zebra_map_get (zms, reg_id);
433     if (zm)
434         return zm->completeness;
435     return 0;
436 }
437
438 int zebra_maps_is_positioned (ZebraMaps zms, unsigned reg_id)
439 {
440     struct zebra_map *zm = zebra_map_get (zms, reg_id);
441     if (zm)
442         return zm->positioned;
443     return 0;
444 }
445     
446 int zebra_maps_is_sort (ZebraMaps zms, unsigned reg_id)
447 {
448     struct zebra_map *zm = zebra_map_get (zms, reg_id);
449     if (zm)
450         return zm->type == ZEBRA_MAP_TYPE_SORT;
451     return 0;
452 }
453
454 int zebra_maps_sort (ZebraMaps zms, Z_SortAttributes *sortAttributes,
455                      int *numerical)
456 {
457     AttrType use;
458     AttrType structure;
459     int structure_value;
460     attr_init_AttrList (&use, sortAttributes->list, 1);
461     attr_init_AttrList (&structure, sortAttributes->list, 4);
462
463     *numerical = 0;
464     structure_value = attr_find (&structure, 0);
465     if (structure_value == 109)
466         *numerical = 1;
467     return attr_find (&use, NULL);
468 }
469
470 int zebra_maps_attr (ZebraMaps zms, Z_AttributesPlusTerm *zapt,
471                      unsigned *reg_id, char **search_type, char *rank_type,
472                      int *complete_flag, int *sort_flag)
473 {
474     AttrType completeness;
475     AttrType structure;
476     AttrType relation;
477     AttrType sort_relation;
478     AttrType weight;
479     int completeness_value;
480     int structure_value;
481     int relation_value;
482     int sort_relation_value;
483     int weight_value;
484
485     attr_init_APT (&structure, zapt, 4);
486     attr_init_APT (&completeness, zapt, 6);
487     attr_init_APT (&relation, zapt, 2);
488     attr_init_APT (&sort_relation, zapt, 7);
489     attr_init_APT (&weight, zapt, 9);
490
491     completeness_value = attr_find (&completeness, NULL);
492     structure_value = attr_find (&structure, NULL);
493     relation_value = attr_find (&relation, NULL);
494     sort_relation_value = attr_find (&sort_relation, NULL);
495     weight_value = attr_find (&weight, NULL);
496
497     if (completeness_value == 2 || completeness_value == 3)
498         *complete_flag = 1;
499     else
500         *complete_flag = 0;
501     *reg_id = 0;
502
503     *sort_flag = (sort_relation_value > 0) ? 1 : 0;
504     *search_type = "phrase";
505     strcpy (rank_type, "void");
506     if (relation_value == 102)
507     {
508         if (weight_value == -1)
509             weight_value = 34;
510         sprintf (rank_type, "rank,%d", weight_value);
511     }
512     if (relation_value == 103)
513     {
514         *search_type = "always";
515         return 0;
516     }
517     if (*complete_flag)
518         *reg_id = 'p';
519     else
520         *reg_id = 'w';
521     switch (structure_value)
522     {
523     case 6:   /* word list */
524         *search_type = "and-list";
525         break;
526     case 105: /* free-form-text */
527         *search_type = "or-list";
528         break;
529     case 106: /* document-text */
530         *search_type = "or-list";
531         break;  
532     case -1:
533     case 1:   /* phrase */
534     case 2:   /* word */
535     case 108: /* string */ 
536         *search_type = "phrase";
537         break;
538     case 107: /* local-number */
539         *search_type = "local";
540         *reg_id = 0;
541         break;
542     case 109: /* numeric string */
543         *reg_id = 'n';
544         *search_type = "numeric";
545         break;
546     case 104: /* urx */
547         *reg_id = 'u';
548         *search_type = "phrase";
549         break;
550     case 3:   /* key */
551         *reg_id = '0';
552         *search_type = "phrase";
553         break;
554     case 4:  /* year */
555         *reg_id = 'y';
556         *search_type = "phrase";
557         break;
558     case 5:  /* date */
559         *reg_id = 'd';
560         *search_type = "phrase";
561         break;
562     default:
563         return -1;
564     }
565     return 0;
566 }
567
568 int zebra_replace_sub(ZebraMaps zms, unsigned reg_id, const char *ex_list,
569                       const char *input_str, int input_len, WRBUF wrbuf);
570
571 WRBUF zebra_replace(ZebraMaps zms, unsigned reg_id, const char *ex_list,
572                     const char *input_str, int input_len)
573 {
574     struct zebra_map *zm = zebra_map_get (zms, reg_id);
575
576     wrbuf_rewind(zms->wrbuf_1);
577     wrbuf_write(zms->wrbuf_1, input_str, input_len);
578     if (!zm || !zm->replace_tokens)
579         return zms->wrbuf_1;
580   
581 #if 0
582     logf (LOG_LOG, "in:%.*s:", wrbuf_len(zms->wrbuf_1),
583           wrbuf_buf(zms->wrbuf_1));
584 #endif
585     for (;;)
586     {
587         if (!zebra_replace_sub(zms, reg_id, ex_list, wrbuf_buf(zms->wrbuf_1),
588                                wrbuf_len(zms->wrbuf_1), zms->wrbuf_2))
589             return zms->wrbuf_2;
590         if (!zebra_replace_sub(zms, reg_id, ex_list, wrbuf_buf(zms->wrbuf_2),
591                                wrbuf_len(zms->wrbuf_2), zms->wrbuf_1))
592             return zms->wrbuf_1;
593     }
594     return 0;
595 }
596
597 int zebra_replace_sub(ZebraMaps zms, unsigned reg_id, const char *ex_list,
598                       const char *input_str, int input_len, WRBUF wrbuf)
599 {
600     int i = -1;
601     int no_replaces = 0;
602     struct zebra_map *zm = zebra_map_get (zms, reg_id);
603
604     wrbuf_rewind(wrbuf);
605     for (i = -1; i <= input_len; )
606     {
607         struct zm_token *token;
608         char replace_string[128];
609         int replace_out;
610         int replace_in = 0;
611
612         for (token = zm->replace_tokens; !replace_in && token;
613              token = token->next)
614         {
615             int j = 0;
616             int replace_done = 0;
617             replace_out = 0;
618             for (;; j++)
619             {
620                 int c;
621                 if (!token->token_from[j])
622                 {
623                     replace_in = j;
624                     break;
625                 }
626                 if (ex_list && strchr (ex_list, token->token_from[j]))
627                     break;
628                 if (i+j < 0 || j+i >= input_len)
629                     c = ' ';
630                 else
631                     c = input_str[j+i] & 255;
632                 if (token->token_from[j] == ZEBRA_REPLACE_ANY)
633                 {
634                     if (c == ' ')
635                         break;
636                     replace_string[replace_out++] = c;
637                 }
638                 else
639                 {
640                     if (c != token->token_from[j])
641                     {
642                         break;
643                     }
644                     if (!replace_done)
645                     {
646                         const char *cp = token->token_to;
647                         replace_done = 1;
648                         for (; cp && *cp; cp++)
649                             replace_string[replace_out++] = *cp;
650                     }
651                 }
652             }
653         }
654         if (!replace_in)
655         {
656             if (i >= 0 && i < input_len)
657                 wrbuf_putc(wrbuf, input_str[i]);
658             i++;
659         }
660         else
661         {
662             no_replaces++;
663             if (replace_out)
664                 wrbuf_write(wrbuf, replace_string, replace_out);
665             i += replace_in;
666         }
667     }
668 #if 0
669     logf (LOG_LOG, "out:%.*s:", wrbuf_len(wrbuf), wrbuf_buf(wrbuf));
670 #endif
671     return no_replaces;
672 }