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