0d1cf07e6758c765ddd40bb24c80eaeacf07ac7b
[idzebra-moved-to-github.git] / util / zebramap.c
1 /* $Id: zebramap.c,v 1.32 2004-06-16 20:30:47 adam Exp $
2    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004
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                                              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         logf (LOG_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             logf(LOG_WARN, "Failed to read character table %s",
286                  zm->maptab_name);
287         else
288             logf(LOG_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)
295 {
296     chrmaptab maptab;
297
298     maptab = zebra_charmap_get (zms, reg_id);
299     if (maptab)
300         return chr_map_input(maptab, from, len);
301     
302     zms->temp_map_str[0] = **from;
303
304     (*from)++;
305     return zms->temp_map_ptr;
306 }
307
308 const char *zebra_maps_output(ZebraMaps zms, unsigned reg_id,
309                               const char **from)
310 {
311     chrmaptab maptab = zebra_charmap_get (zms, reg_id);
312     if (!maptab)
313         return 0;
314     return chr_map_output (maptab, from, 1);
315 }
316
317
318 /* ------------------------------------ */
319
320 typedef struct {
321     int type;
322     int major;
323     int minor;
324     Z_AttributeElement **attributeList;
325     int num_attributes;
326 } AttrType;
327
328 static int attr_find (AttrType *src, oid_value *attributeSetP)
329 {
330     while (src->major < src->num_attributes)
331     {
332         Z_AttributeElement *element;
333
334         element = src->attributeList[src->major];
335         if (src->type == *element->attributeType)
336         {
337             switch (element->which) 
338             {
339             case Z_AttributeValue_numeric:
340                 ++(src->major);
341                 if (element->attributeSet && attributeSetP)
342                 {
343                     oident *attrset;
344
345                     attrset = oid_getentbyoid (element->attributeSet);
346                     *attributeSetP = attrset->value;
347                 }
348                 return *element->value.numeric;
349                 break;
350             case Z_AttributeValue_complex:
351                 if (src->minor >= element->value.complex->num_list ||
352                     element->value.complex->list[src->minor]->which !=  
353                     Z_StringOrNumeric_numeric)
354                     break;
355                 ++(src->minor);
356                 if (element->attributeSet && attributeSetP)
357                 {
358                     oident *attrset;
359
360                     attrset = oid_getentbyoid (element->attributeSet);
361                     *attributeSetP = attrset->value;
362                 }
363                 return *element->value.complex->list[src->minor-1]->u.numeric;
364             default:
365                 assert (0);
366             }
367         }
368         ++(src->major);
369     }
370     return -1;
371 }
372
373 static void attr_init_APT (AttrType *src, Z_AttributesPlusTerm *zapt, int type)
374 {
375     src->attributeList = zapt->attributes->attributes;
376     src->num_attributes = zapt->attributes->num_attributes;
377     src->type = type;
378     src->major = 0;
379     src->minor = 0;
380 }
381
382 static void attr_init_AttrList (AttrType *src, Z_AttributeList *list, int type)
383 {
384     src->attributeList = list->attributes;
385     src->num_attributes = list->num_attributes;
386     src->type = type;
387     src->major = 0;
388     src->minor = 0;
389 }
390
391 /* ------------------------------------ */
392
393 int zebra_maps_is_complete (ZebraMaps zms, unsigned reg_id)
394
395     struct zebra_map *zm = zebra_map_get (zms, reg_id);
396     if (zm)
397         return zm->completeness;
398     return 0;
399 }
400
401 int zebra_maps_is_positioned (ZebraMaps zms, unsigned reg_id)
402 {
403     struct zebra_map *zm = zebra_map_get (zms, reg_id);
404     if (zm)
405         return zm->positioned;
406     return 0;
407 }
408     
409 int zebra_maps_is_sort (ZebraMaps zms, unsigned reg_id)
410 {
411     struct zebra_map *zm = zebra_map_get (zms, reg_id);
412     if (zm)
413         return zm->type == ZEBRA_MAP_TYPE_SORT;
414     return 0;
415 }
416
417 int zebra_maps_sort (ZebraMaps zms, Z_SortAttributes *sortAttributes,
418                      int *numerical)
419 {
420     AttrType use;
421     AttrType structure;
422     int structure_value;
423     attr_init_AttrList (&use, sortAttributes->list, 1);
424     attr_init_AttrList (&structure, sortAttributes->list, 4);
425
426     *numerical = 0;
427     structure_value = attr_find (&structure, 0);
428     if (structure_value == 109)
429         *numerical = 1;
430     return attr_find (&use, NULL);
431 }
432
433 int zebra_maps_attr (ZebraMaps zms, Z_AttributesPlusTerm *zapt,
434                      unsigned *reg_id, char **search_type, char *rank_type,
435                      int *complete_flag, int *sort_flag)
436 {
437     AttrType completeness;
438     AttrType structure;
439     AttrType relation;
440     AttrType sort_relation;
441     AttrType weight;
442     AttrType use;
443     int completeness_value;
444     int structure_value;
445     int relation_value;
446     int sort_relation_value;
447     int weight_value;
448     int use_value;
449
450     attr_init_APT (&structure, zapt, 4);
451     attr_init_APT (&completeness, zapt, 6);
452     attr_init_APT (&relation, zapt, 2);
453     attr_init_APT (&sort_relation, zapt, 7);
454     attr_init_APT (&weight, zapt, 9);
455     attr_init_APT (&use, zapt, 1);
456
457     completeness_value = attr_find (&completeness, NULL);
458     structure_value = attr_find (&structure, NULL);
459     relation_value = attr_find (&relation, NULL);
460     sort_relation_value = attr_find (&sort_relation, NULL);
461     weight_value = attr_find (&weight, NULL);
462     use_value = attr_find(&use, NULL);
463
464     if (completeness_value == 2 || completeness_value == 3)
465         *complete_flag = 1;
466     else
467         *complete_flag = 0;
468     *reg_id = 0;
469
470     *sort_flag = (sort_relation_value > 0) ? 1 : 0;
471     *search_type = "phrase";
472     strcpy (rank_type, "void");
473     if (relation_value == 102)
474     {
475         if (weight_value == -1)
476             weight_value = 34;
477         sprintf (rank_type, "rank,w=%d,u=%d", weight_value, use_value);
478     }
479     if (relation_value == 103)
480     {
481         *search_type = "always";
482         return 0;
483     }
484     if (*complete_flag)
485         *reg_id = 'p';
486     else
487         *reg_id = 'w';
488     switch (structure_value)
489     {
490     case 6:   /* word list */
491         *search_type = "and-list";
492         break;
493     case 105: /* free-form-text */
494         *search_type = "or-list";
495         break;
496     case 106: /* document-text */
497         *search_type = "or-list";
498         break;  
499     case -1:
500     case 1:   /* phrase */
501     case 2:   /* word */
502     case 108: /* string */ 
503         *search_type = "phrase";
504         break;
505     case 107: /* local-number */
506         *search_type = "local";
507         *reg_id = 0;
508         break;
509     case 109: /* numeric string */
510         *reg_id = 'n';
511         *search_type = "numeric";
512         break;
513     case 104: /* urx */
514         *reg_id = 'u';
515         *search_type = "phrase";
516         break;
517     case 3:   /* key */
518         *reg_id = '0';
519         *search_type = "phrase";
520         break;
521     case 4:  /* year */
522         *reg_id = 'y';
523         *search_type = "phrase";
524         break;
525     case 5:  /* date */
526         *reg_id = 'd';
527         *search_type = "phrase";
528         break;
529     default:
530         return -1;
531     }
532     return 0;
533 }
534
535 int zebra_replace_sub(ZebraMaps zms, unsigned reg_id, const char *ex_list,
536                       const char *input_str, int input_len, WRBUF wrbuf);
537
538 WRBUF zebra_replace(ZebraMaps zms, unsigned reg_id, const char *ex_list,
539                     const char *input_str, int input_len)
540 {
541     struct zebra_map *zm = zebra_map_get (zms, reg_id);
542
543     wrbuf_rewind(zms->wrbuf_1);
544     wrbuf_write(zms->wrbuf_1, input_str, input_len);
545     if (!zm || !zm->replace_tokens)
546         return zms->wrbuf_1;
547   
548 #if 0
549     logf (LOG_LOG, "in:%.*s:", wrbuf_len(zms->wrbuf_1),
550           wrbuf_buf(zms->wrbuf_1));
551 #endif
552     for (;;)
553     {
554         if (!zebra_replace_sub(zms, reg_id, ex_list, wrbuf_buf(zms->wrbuf_1),
555                                wrbuf_len(zms->wrbuf_1), zms->wrbuf_2))
556             return zms->wrbuf_2;
557         if (!zebra_replace_sub(zms, reg_id, ex_list, wrbuf_buf(zms->wrbuf_2),
558                                wrbuf_len(zms->wrbuf_2), zms->wrbuf_1))
559             return zms->wrbuf_1;
560     }
561     return 0;
562 }
563
564 int zebra_replace_sub(ZebraMaps zms, unsigned reg_id, const char *ex_list,
565                       const char *input_str, int input_len, WRBUF wrbuf)
566 {
567     int i = -1;
568     int no_replaces = 0;
569     struct zebra_map *zm = zebra_map_get (zms, reg_id);
570
571     wrbuf_rewind(wrbuf);
572     for (i = -1; i <= input_len; )
573     {
574         struct zm_token *token;
575         char replace_string[128];
576         int replace_out = 0;
577         int replace_in = 0;
578
579         for (token = zm->replace_tokens; !replace_in && token;
580              token = token->next)
581         {
582             int j = 0;
583             int replace_done = 0;
584             replace_out = 0;
585             for (;; j++)
586             {
587                 int c;
588                 if (!token->token_from[j])
589                 {
590                     replace_in = j;
591                     break;
592                 }
593                 if (ex_list && strchr (ex_list, token->token_from[j]))
594                     break;
595                 if (i+j < 0 || j+i >= input_len)
596                     c = ' ';
597                 else
598                     c = input_str[j+i] & 255;
599                 if (token->token_from[j] == ZEBRA_REPLACE_ANY)
600                 {
601                     if (c == ' ')
602                         break;
603                     replace_string[replace_out++] = c;
604                 }
605                 else
606                 {
607                     if (c != token->token_from[j])
608                     {
609                         break;
610                     }
611                     if (!replace_done)
612                     {
613                         const char *cp = token->token_to;
614                         replace_done = 1;
615                         for (; cp && *cp; cp++)
616                             replace_string[replace_out++] = *cp;
617                     }
618                 }
619             }
620         }
621         if (!replace_in)
622         {
623             if (i >= 0 && i < input_len)
624                 wrbuf_putc(wrbuf, input_str[i]);
625             i++;
626         }
627         else
628         {
629             no_replaces++;
630             if (replace_out)
631                 wrbuf_write(wrbuf, replace_string, replace_out);
632             i += replace_in;
633         }
634     }
635 #if 0
636     logf (LOG_LOG, "out:%.*s:", wrbuf_len(wrbuf), wrbuf_buf(wrbuf));
637 #endif
638     return no_replaces;
639 }