Merge icu_chain into zebramaps.
[idzebra-moved-to-github.git] / util / zebramap.c
1 /* $Id: zebramap.c,v 1.64 2007-11-05 13:58:01 adam Exp $
2    Copyright (C) 1995-2007
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 <attrfind.h>
29 #include <yaz/yaz-util.h>
30
31 #if HAVE_ICU
32 #include <yaz/icu_I18N.h>
33 #endif
34 #include <zebramap.h>
35
36 #define ZEBRA_MAP_TYPE_SORT  1
37 #define ZEBRA_MAP_TYPE_INDEX 2
38 #define ZEBRA_MAP_TYPE_STATICRANK 3
39
40 #define ZEBRA_REPLACE_ANY  300
41
42 struct zebra_map {
43     const char *id;
44     int completeness;
45     int positioned;
46     int alwaysmatches;
47     int first_in_field;
48     int type;
49     union {
50         struct {
51             int entry_size;
52         } sort;
53     } u;
54     chrmaptab maptab;
55     const char *maptab_name;
56     const char *locale;
57     zebra_maps_t zebra_maps;
58 #if YAZ_HAVE_XML2
59     xmlDocPtr doc;
60 #endif
61 #if HAVE_ICU
62     struct icu_chain *icu_chain;
63 #endif
64     struct zebra_map *next;
65 };
66
67 struct zebra_maps_s {
68     char *tabpath;
69     char *tabroot;
70     NMEM nmem;
71     char temp_map_str[2];
72     const char *temp_map_ptr[2];
73     WRBUF wrbuf_1;
74     int no_files_read;
75     zebra_map_t map_list;
76     zebra_map_t last_map;
77 };
78
79 void zebra_maps_close(zebra_maps_t zms)
80 {
81     struct zebra_map *zm = zms->map_list;
82     while (zm)
83     {
84         if (zm->maptab)
85             chrmaptab_destroy(zm->maptab);
86 #if HAVE_ICU
87         if (zm->icu_chain)
88             icu_chain_destroy(zm->icu_chain);
89 #endif
90 #if YAZ_HAVE_XML2
91         xmlFreeDoc(zm->doc);
92 #endif
93         zm = zm->next;
94     }
95     wrbuf_destroy(zms->wrbuf_1);
96     nmem_destroy(zms->nmem);
97     xfree(zms);
98 }
99
100 zebra_map_t zebra_add_map(zebra_maps_t zms, const char *index_type,
101                           int map_type)
102 {
103     zebra_map_t zm = (zebra_map_t) nmem_malloc(zms->nmem, sizeof(*zm));
104
105     zm->zebra_maps = zms;
106     zm->id = nmem_strdup(zms->nmem, index_type);
107     zm->maptab_name = 0;
108     zm->locale = 0;
109     zm->maptab = 0;
110     zm->type = map_type;
111     zm->completeness = 0;
112     zm->positioned = 0;
113     zm->alwaysmatches = 0;
114     zm->first_in_field = 0;
115
116     if (zms->last_map)
117         zms->last_map->next = zm;
118     else
119         zms->map_list = zm;
120     zms->last_map = zm;
121     zm->next = 0;
122 #if HAVE_ICU
123     zm->icu_chain = 0;
124 #endif
125 #if YAZ_HAVE_XML2
126     zm->doc = 0;
127 #endif
128     return zm;
129 }
130
131 static int parse_command(zebra_maps_t zms, int argc, char **argv,
132                          const char *fname, int lineno)
133 {
134     zebra_map_t zm = zms->last_map;
135     if (argc == 1)
136     {
137         yaz_log(YLOG_WARN, "%s:%d: Missing arguments for '%s'",
138                 fname, lineno, argv[0]);
139         return -1;
140     }
141     if (argc > 2)
142     {
143         yaz_log(YLOG_WARN, "%s:%d: Too many arguments for '%s'",
144                 fname, lineno, argv[0]);
145         return -1;
146     }
147     if (!yaz_matchstr(argv[0], "index"))
148     {
149         zm = zebra_add_map(zms, argv[1], ZEBRA_MAP_TYPE_INDEX);
150         zm->positioned = 1;
151     }
152     else if (!yaz_matchstr(argv[0], "sort"))
153     {
154         zm = zebra_add_map(zms, argv[1], ZEBRA_MAP_TYPE_SORT);
155         zm->u.sort.entry_size = 80;
156     }
157     else if (!yaz_matchstr(argv[0], "staticrank"))
158     {
159         zm = zebra_add_map(zms, argv[1], ZEBRA_MAP_TYPE_STATICRANK);
160         zm->completeness = 1;
161     }
162     else if (!zm)
163     {
164         yaz_log(YLOG_WARN, "%s:%d: Missing sort/index before '%s'",  
165                 fname, lineno, argv[0]);
166         return -1;
167     }
168     else if (!yaz_matchstr(argv[0], "charmap") && argc == 2)
169     {
170         if (zm->type != ZEBRA_MAP_TYPE_STATICRANK)
171             zm->maptab_name = nmem_strdup(zms->nmem, argv[1]);
172         else
173         {
174             yaz_log(YLOG_WARN|YLOG_FATAL, "%s:%d: charmap for "
175                     "staticrank is invalid", fname, lineno);
176             yaz_log(YLOG_LOG, "Type is %d", zm->type);
177             return -1;
178         }
179     }
180     else if (!yaz_matchstr(argv[0], "completeness") && argc == 2)
181     {
182         zm->completeness = atoi(argv[1]);
183     }
184     else if (!yaz_matchstr(argv[0], "position") && argc == 2)
185     {
186         zm->positioned = atoi(argv[1]);
187     }
188     else if (!yaz_matchstr(argv[0], "alwaysmatches") && argc == 2)
189     {
190         if (zm->type != ZEBRA_MAP_TYPE_STATICRANK)
191             zm->alwaysmatches = atoi(argv[1]);
192         else
193         {
194             yaz_log(YLOG_WARN|YLOG_FATAL, "%s:%d: alwaysmatches for "
195                     "staticrank is invalid", fname, lineno);
196             return -1;
197         }
198     }
199     else if (!yaz_matchstr(argv[0], "firstinfield") && argc == 2)
200     {
201         zm->first_in_field = atoi(argv[1]);
202     }
203     else if (!yaz_matchstr(argv[0], "entrysize") && argc == 2)
204     {
205         if (zm->type == ZEBRA_MAP_TYPE_SORT)
206             zm->u.sort.entry_size = atoi(argv[1]);
207         else
208         {
209             yaz_log(YLOG_WARN, 
210                     "%s:%d: entrysize only valid in sort section",  
211                     fname, lineno);
212             return -1;
213         }
214     }
215     else if (!yaz_matchstr(argv[0], "locale"))
216     {
217         zm->locale = nmem_strdup(zms->nmem, argv[1]);
218     }
219     else if (!yaz_matchstr(argv[0], "icuchain"))
220     {
221 #if YAZ_HAVE_XML2
222         zm->doc = xmlParseFile(argv[1]);
223         if (!zm->doc)
224         {
225             yaz_log(YLOG_WARN, "%s:%d: Could not load icuchain config '%s'",
226                     fname, lineno, argv[1]);
227             return -1;
228         }
229         else
230         {
231 #if HAVE_ICU
232             UErrorCode status;
233             xmlNode *xml_node = xmlDocGetRootElement(zm->doc);
234             zm->icu_chain = 
235                 icu_chain_xml_config(xml_node, zm->locale, 
236                                      zm->type == ZEBRA_MAP_TYPE_SORT,
237                                      &status);
238             if (!zm->icu_chain)
239             {
240                 yaz_log(YLOG_WARN, "%s:%d: Failed to load ICU chain %s",
241                         fname, lineno, argv[1]);
242             }
243 #else
244             yaz_log(YLOG_WARN, "%s:%d: ICU support unavailable",
245                     fname, lineno);
246             return -1;
247 #endif
248         }
249 #else
250         yaz_log(YLOG_WARN, "%s:%d: XML support unavailable",
251                 fname, lineno);
252         return -1;
253 #endif
254     }
255     else
256     {
257         yaz_log(YLOG_WARN, "%s:%d: Unrecognized directive '%s'",  
258                 fname, lineno, argv[0]);
259         return -1;
260     }
261     return 0;
262 }
263
264 ZEBRA_RES zebra_maps_read_file(zebra_maps_t zms, const char *fname)
265 {
266     FILE *f;
267     char line[512];
268     char *argv[10];
269     int argc;
270     int lineno = 0;
271     int failures = 0;
272
273     if (!(f = yaz_fopen(zms->tabpath, fname, "r", zms->tabroot)))
274     {
275         yaz_log(YLOG_ERRNO|YLOG_FATAL, "%s", fname);
276         return ZEBRA_FAIL;
277     }
278     while ((argc = readconf_line(f, &lineno, line, 512, argv, 10)))
279     {
280         int r = parse_command(zms, argc, argv, fname, lineno);
281         if (r)
282             failures++;
283     }
284     yaz_fclose(f);
285
286     if (failures)
287         return ZEBRA_FAIL;
288
289     (zms->no_files_read)++;
290     return ZEBRA_OK;
291 }
292
293 zebra_maps_t zebra_maps_open(Res res, const char *base_path,
294                              const char *profile_path)
295 {
296     zebra_maps_t zms = (zebra_maps_t) xmalloc(sizeof(*zms));
297
298     zms->nmem = nmem_create();
299     zms->tabpath = profile_path ? nmem_strdup(zms->nmem, profile_path) : 0;
300     zms->tabroot = 0;
301     if (base_path)
302         zms->tabroot = nmem_strdup(zms->nmem, base_path);
303     zms->map_list = 0;
304     zms->last_map = 0;
305
306     zms->temp_map_str[0] = '\0';
307     zms->temp_map_str[1] = '\0';
308
309     zms->temp_map_ptr[0] = zms->temp_map_str;
310     zms->temp_map_ptr[1] = NULL;
311
312     zms->wrbuf_1 = wrbuf_alloc();
313
314     zms->no_files_read = 0;
315     return zms;
316 }
317
318 zebra_map_t zebra_map_get(zebra_maps_t zms, const char *id)
319 {
320     zebra_map_t zm;
321     for (zm = zms->map_list; zm; zm = zm->next)
322         if (!strcmp(zm->id, id))
323             break;
324     return zm;
325 }
326
327 zebra_map_t zebra_map_get_or_add(zebra_maps_t zms, const char *id)
328 {
329     struct zebra_map *zm = zebra_map_get(zms, id);
330     if (!zm)
331     {
332         zm = zebra_add_map(zms, id, ZEBRA_MAP_TYPE_INDEX);
333         
334         /* no reason to warn if no maps are read from file */
335         if (zms->no_files_read)
336             yaz_log(YLOG_WARN, "Unknown register type: %s", id);
337
338         zm->maptab_name = nmem_strdup(zms->nmem, "@");
339         zm->completeness = 0;
340         zm->positioned = 1;
341     }
342     return zm;
343 }
344
345 chrmaptab zebra_charmap_get(zebra_map_t zm)
346 {
347     if (!zm->maptab)
348     {
349         if (!zm->maptab_name || !yaz_matchstr(zm->maptab_name, "@"))
350             return NULL;
351         if (!(zm->maptab = chrmaptab_create(zm->zebra_maps->tabpath,
352                                             zm->maptab_name,
353                                             zm->zebra_maps->tabroot)))
354             yaz_log(YLOG_WARN, "Failed to read character table %s",
355                     zm->maptab_name);
356         else
357             yaz_log(YLOG_DEBUG, "Read character table %s", zm->maptab_name);
358     }
359     return zm->maptab;
360 }
361
362 const char **zebra_maps_input(zebra_map_t zm,
363                               const char **from, int len, int first)
364 {
365     chrmaptab maptab = zebra_charmap_get(zm);
366     if (maptab)
367         return chr_map_input(maptab, from, len, first);
368     
369     zm->zebra_maps->temp_map_str[0] = **from;
370
371     (*from)++;
372     return zm->zebra_maps->temp_map_ptr;
373 }
374
375 const char **zebra_maps_search(zebra_map_t zm,
376                                const char **from, int len,  int *q_map_match)
377 {
378     chrmaptab maptab;
379     
380     *q_map_match = 0;
381     maptab = zebra_charmap_get(zm);
382     if (maptab)
383     {
384         const char **map;
385         map = chr_map_q_input(maptab, from, len, 0);
386         if (map && map[0])
387         {
388             *q_map_match = 1;
389             return map;
390         }
391         map = chr_map_input(maptab, from, len, 0);
392         if (map)
393             return map;
394     }
395     zm->zebra_maps->temp_map_str[0] = **from;
396
397     (*from)++;
398     return zm->zebra_maps->temp_map_ptr;
399 }
400
401 const char *zebra_maps_output(zebra_map_t zm,
402                               const char **from)
403 {
404     chrmaptab maptab = zebra_charmap_get(zm);
405     if (!maptab)
406         return 0;
407     return chr_map_output(maptab, from, 1);
408 }
409
410
411 /* ------------------------------------ */
412
413 int zebra_maps_is_complete(zebra_map_t zm)
414
415     if (zm)
416         return zm->completeness;
417     return 0;
418 }
419
420 int zebra_maps_is_positioned(zebra_map_t zm)
421 {
422     if (zm)
423         return zm->positioned;
424     return 0;
425 }
426
427 int zebra_maps_is_index(zebra_map_t zm)
428 {
429     if (zm)
430         return zm->type == ZEBRA_MAP_TYPE_INDEX;
431     return 0;
432 }
433
434 int zebra_maps_is_staticrank(zebra_map_t zm)
435 {
436     if (zm)
437         return zm->type == ZEBRA_MAP_TYPE_STATICRANK;
438     return 0;
439 }
440     
441 int zebra_maps_is_sort(zebra_map_t zm)
442 {
443     if (zm)
444         return zm->type == ZEBRA_MAP_TYPE_SORT;
445     return 0;
446 }
447
448 int zebra_maps_is_alwaysmatches(zebra_map_t zm)
449 {
450     if (zm)
451         return zm->alwaysmatches;
452     return 0;
453 }
454
455 int zebra_maps_is_first_in_field(zebra_map_t zm)
456 {
457     if (zm)
458         return zm->first_in_field;
459     return 0;
460 }
461
462 int zebra_maps_sort(zebra_maps_t zms, Z_SortAttributes *sortAttributes,
463                     int *numerical)
464 {
465     AttrType use;
466     AttrType structure;
467     int structure_value;
468     attr_init_AttrList(&use, sortAttributes->list, 1);
469     attr_init_AttrList(&structure, sortAttributes->list, 4);
470
471     *numerical = 0;
472     structure_value = attr_find(&structure, 0);
473     if (structure_value == 109)
474         *numerical = 1;
475     return attr_find(&use, NULL);
476 }
477
478 int zebra_maps_attr(zebra_maps_t zms, Z_AttributesPlusTerm *zapt,
479                     const char **index_type, char **search_type, char *rank_type,
480                     int *complete_flag, int *sort_flag)
481 {
482     AttrType completeness;
483     AttrType structure;
484     AttrType relation;
485     AttrType sort_relation;
486     AttrType weight;
487     AttrType use;
488     int completeness_value;
489     int structure_value;
490     const char *structure_str = 0;
491     int relation_value;
492     int sort_relation_value;
493     int weight_value;
494     int use_value;
495
496     attr_init_APT(&structure, zapt, 4);
497     attr_init_APT(&completeness, zapt, 6);
498     attr_init_APT(&relation, zapt, 2);
499     attr_init_APT(&sort_relation, zapt, 7);
500     attr_init_APT(&weight, zapt, 9);
501     attr_init_APT(&use, zapt, 1);
502
503     completeness_value = attr_find(&completeness, NULL);
504     structure_value = attr_find_ex(&structure, NULL, &structure_str);
505     relation_value = attr_find(&relation, NULL);
506     sort_relation_value = attr_find(&sort_relation, NULL);
507     weight_value = attr_find(&weight, NULL);
508     use_value = attr_find(&use, NULL);
509
510     if (completeness_value == 2 || completeness_value == 3)
511         *complete_flag = 1;
512     else
513         *complete_flag = 0;
514     *index_type = 0;
515
516     *sort_flag =(sort_relation_value > 0) ? 1 : 0;
517     *search_type = "phrase";
518     strcpy(rank_type, "void");
519     if (relation_value == 102)
520     {
521         if (weight_value == -1)
522             weight_value = 34;
523         sprintf(rank_type, "rank,w=%d,u=%d", weight_value, use_value);
524     }
525     if (*complete_flag)
526         *index_type = "p";
527     else
528         *index_type = "w";
529     switch (structure_value)
530     {
531     case 6:   /* word list */
532         *search_type = "and-list";
533         break;
534     case 105: /* free-form-text */
535         *search_type = "or-list";
536         break;
537     case 106: /* document-text */
538         *search_type = "or-list";
539         break;  
540     case -1:
541     case 1:   /* phrase */
542     case 2:   /* word */
543     case 108: /* string */ 
544         *search_type = "phrase";
545         break;
546     case 107: /* local-number */
547         *search_type = "local";
548         *index_type = 0;
549         break;
550     case 109: /* numeric string */
551         *index_type = "n";
552         *search_type = "numeric";
553         break;
554     case 104: /* urx */
555         *index_type = "u";
556         *search_type = "phrase";
557         break;
558     case 3:   /* key */
559         *index_type = "0";
560         *search_type = "phrase";
561         break;
562     case 4:  /* year */
563         *index_type = "y";
564         *search_type = "phrase";
565         break;
566     case 5:  /* date */
567         *index_type = "d";
568         *search_type = "phrase";
569         break;
570     case -2:
571         if (structure_str && *structure_str)
572             *index_type = structure_str;
573         else
574             return -1;
575         break;
576     default:
577         return -1;
578     }
579     return 0;
580 }
581
582 WRBUF zebra_replace(zebra_map_t zm, const char *ex_list,
583                     const char *input_str, int input_len)
584 {
585     wrbuf_rewind(zm->zebra_maps->wrbuf_1);
586     wrbuf_write(zm->zebra_maps->wrbuf_1, input_str, input_len);
587     return zm->zebra_maps->wrbuf_1;
588 }
589
590 /*
591  * Local variables:
592  * c-basic-offset: 4
593  * indent-tabs-mode: nil
594  * End:
595  * vim: shiftwidth=4 tabstop=8 expandtab
596  */
597