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