Definitions in zebramap.h are private. ZebraMaps no longer used in
[idzebra-moved-to-github.git] / util / zebramap.c
1 /* $Id: zebramap.c,v 1.52 2006-08-15 14:28:35 adam Exp $
2    Copyright (C) 1995-2006
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 #include <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 zebra_map {
39     unsigned reg_id;
40     int completeness;
41     int positioned;
42     int alwaysmatches;
43     int type;
44     union {
45         struct {
46             int dummy;
47         } index;
48         struct {
49             int entry_size;
50         } sort;
51     } u;
52     chrmaptab maptab;
53     const char *maptab_name;
54     struct zebra_map *next;
55 };
56
57 struct zebra_maps {
58     char *tabpath;
59     char *tabroot;
60     NMEM nmem;
61     struct zebra_map *map_list;
62     char temp_map_str[2];
63     const char *temp_map_ptr[2];
64     struct zebra_map **lookup_array;
65     WRBUF wrbuf_1;
66     int no_maps;
67 };
68
69 void zebra_maps_close(ZebraMaps zms)
70 {
71     struct zebra_map *zm = zms->map_list;
72     while (zm)
73     {
74         if (zm->maptab)
75             chrmaptab_destroy(zm->maptab);
76         zm = zm->next;
77     }
78     wrbuf_free(zms->wrbuf_1, 1);
79     nmem_destroy(zms->nmem);
80     xfree(zms);
81 }
82
83 ZEBRA_RES zebra_maps_read_file(ZebraMaps zms, const char *fname)
84 {
85     FILE *f;
86     char line[512];
87     char *argv[10];
88     int argc;
89     int lineno = 0;
90     int failures = 0;
91     struct zebra_map **zm = 0, *zp;
92
93     if (!(f = yaz_fopen(zms->tabpath, fname, "r", zms->tabroot)))
94     {
95         yaz_log(YLOG_ERRNO|YLOG_FATAL, "%s", fname);
96         return ZEBRA_FAIL;
97     }
98     while ((argc = readconf_line(f, &lineno, line, 512, argv, 10)))
99     {
100         if (argc == 1)
101         {
102             yaz_log(YLOG_WARN, "%s:%d: Missing arguments for '%s'",
103                     fname, lineno, argv[0]);
104             failures++;
105             break;
106         }
107         if (argc > 2)
108         {
109             yaz_log(YLOG_WARN, "%s:%d: Too many arguments for '%s'",
110                     fname, lineno, argv[0]);
111             failures++;
112             break;
113         }
114         if (!yaz_matchstr(argv[0], "index"))
115         {
116             if (!zm)
117                 zm = &zms->map_list;
118             else
119                 zm = &(*zm)->next;
120             *zm = (struct zebra_map *) nmem_malloc(zms->nmem, sizeof(**zm));
121             (*zm)->reg_id = argv[1][0];
122             (*zm)->maptab_name = NULL;
123             (*zm)->maptab = NULL;
124             (*zm)->type = ZEBRA_MAP_TYPE_INDEX;
125             (*zm)->completeness = 0;
126             (*zm)->positioned = 1;
127             (*zm)->alwaysmatches = 0;
128             zms->no_maps++;
129         }
130         else if (!yaz_matchstr(argv[0], "sort"))
131         {
132             if (!zm)
133                 zm = &zms->map_list;
134             else
135                 zm = &(*zm)->next;
136             *zm = (struct zebra_map *) nmem_malloc(zms->nmem, sizeof(**zm));
137             (*zm)->reg_id = argv[1][0];
138             (*zm)->maptab_name = NULL;
139             (*zm)->type = ZEBRA_MAP_TYPE_SORT;
140             (*zm)->u.sort.entry_size = 80;
141             (*zm)->maptab = NULL;
142             (*zm)->completeness = 0;
143             (*zm)->positioned = 0;
144             (*zm)->alwaysmatches = 0;
145             zms->no_maps++;
146         }
147         else if (!zm)
148         {
149             yaz_log(YLOG_WARN, "%s:%d: Missing sort/index before '%s'",  
150                     fname, lineno, argv[0]);
151             failures++;
152         }
153         else if (!yaz_matchstr(argv[0], "charmap") && argc == 2)
154         {
155             (*zm)->maptab_name = nmem_strdup(zms->nmem, argv[1]);
156         }
157         else if (!yaz_matchstr(argv[0], "completeness") && argc == 2)
158         {
159             (*zm)->completeness = atoi(argv[1]);
160         }
161         else if (!yaz_matchstr(argv[0], "position") && argc == 2)
162         {
163             (*zm)->positioned = atoi(argv[1]);
164         }
165         else if (!yaz_matchstr(argv[0], "alwaysmatches") && argc == 2)
166         {
167             (*zm)->alwaysmatches = atoi(argv[1]);
168         }
169         else if (!yaz_matchstr(argv[0], "entrysize") && argc == 2)
170         {
171             if ((*zm)->type == ZEBRA_MAP_TYPE_SORT)
172                 (*zm)->u.sort.entry_size = atoi(argv[1]);
173         }
174         else
175         {
176             yaz_log(YLOG_WARN, "%s:%d: Unrecognized directive '%s'",  
177                     fname, lineno, argv[0]);
178             failures++;
179         }
180     }
181     if (zm)
182         (*zm)->next = NULL;
183     yaz_fclose(f);
184
185     for (zp = zms->map_list; zp; zp = zp->next)
186         zms->lookup_array[zp->reg_id] = zp;
187
188     if (failures)
189         return ZEBRA_FAIL;
190     return ZEBRA_OK;
191 }
192
193 ZebraMaps zebra_maps_open(Res res, const char *base_path,
194                           const char *profile_path)
195 {
196     ZebraMaps zms = (ZebraMaps) xmalloc(sizeof(*zms));
197     int i;
198
199     zms->nmem = nmem_create();
200     zms->no_maps = 0;
201     zms->tabpath = nmem_strdup(zms->nmem, profile_path);
202     zms->tabroot = 0;
203     if (base_path)
204         zms->tabroot = nmem_strdup(zms->nmem, base_path);
205     zms->map_list = NULL;
206
207     zms->temp_map_str[0] = '\0';
208     zms->temp_map_str[1] = '\0';
209
210     zms->temp_map_ptr[0] = zms->temp_map_str;
211     zms->temp_map_ptr[1] = NULL;
212
213     zms->lookup_array = (struct zebra_map**)
214         nmem_malloc(zms->nmem, sizeof(*zms->lookup_array)*256);
215     zms->wrbuf_1 = wrbuf_alloc();
216
217     for (i = 0; i<256; i++)
218         zms->lookup_array[i] = 0;
219     return zms;
220 }
221
222 struct zebra_map *zebra_map_get(ZebraMaps zms, unsigned reg_id)
223 {
224     assert(reg_id >= 0 && reg_id <= 255);
225     return zms->lookup_array[reg_id];
226 }
227
228 chrmaptab zebra_charmap_get(ZebraMaps zms, unsigned reg_id)
229 {
230     struct zebra_map *zm = zebra_map_get(zms, reg_id);
231     if (!zm)
232     {
233         zm = (struct zebra_map *) nmem_malloc(zms->nmem, sizeof(*zm));
234         
235         /* no reason to warn if no maps are installed at ALL */
236         if (zms->no_maps)
237             yaz_log(YLOG_WARN, "Unknown register type: %c", reg_id);
238
239         zm->reg_id = reg_id;
240         zm->maptab_name = nmem_strdup(zms->nmem, "@");
241         zm->maptab = NULL;
242         zm->type = ZEBRA_MAP_TYPE_INDEX;
243         zm->completeness = 0;
244         zm->next = zms->map_list;
245         zms->map_list = zm->next;
246
247         zms->lookup_array[zm->reg_id & 255] = zm;
248     }
249     if (!zm->maptab)
250     {
251         if (!zm->maptab_name || !yaz_matchstr(zm->maptab_name, "@"))
252             return NULL;
253         if (!(zm->maptab = chrmaptab_create(zms->tabpath,
254                                             zm->maptab_name,
255                                             zms->tabroot)))
256             yaz_log(YLOG_WARN, "Failed to read character table %s",
257                     zm->maptab_name);
258         else
259             yaz_log(YLOG_DEBUG, "Read character table %s", zm->maptab_name);
260     }
261     return zm->maptab;
262 }
263
264 const char **zebra_maps_input(ZebraMaps zms, unsigned reg_id,
265                               const char **from, int len, int first)
266 {
267     chrmaptab maptab;
268
269     maptab = zebra_charmap_get(zms, reg_id);
270     if (maptab)
271         return chr_map_input(maptab, from, len, first);
272     
273     zms->temp_map_str[0] = **from;
274
275     (*from)++;
276     return zms->temp_map_ptr;
277 }
278
279 const char **zebra_maps_search(ZebraMaps zms, unsigned reg_id,
280                                const char **from, int len,  int *q_map_match)
281 {
282     chrmaptab maptab;
283     
284     *q_map_match = 0;
285     maptab = zebra_charmap_get(zms, reg_id);
286     if (maptab)
287     {
288         const char **map;
289         map = chr_map_q_input(maptab, from, len, 0);
290         if (map && map[0])
291         {
292             *q_map_match = 1;
293             return map;
294         }
295         map = chr_map_input(maptab, from, len, 0);
296         if (map)
297             return map;
298     }
299     zms->temp_map_str[0] = **from;
300
301     (*from)++;
302     return zms->temp_map_ptr;
303 }
304
305 const char *zebra_maps_output(ZebraMaps zms, unsigned reg_id,
306                               const char **from)
307 {
308     chrmaptab maptab = zebra_charmap_get(zms, reg_id);
309     if (!maptab)
310         return 0;
311     return chr_map_output(maptab, from, 1);
312 }
313
314
315 /* ------------------------------------ */
316
317 int zebra_maps_is_complete(ZebraMaps zms, unsigned reg_id)
318
319     struct zebra_map *zm = zebra_map_get(zms, reg_id);
320     if (zm)
321         return zm->completeness;
322     return 0;
323 }
324
325 int zebra_maps_is_positioned(ZebraMaps zms, unsigned reg_id)
326 {
327     struct zebra_map *zm = zebra_map_get(zms, reg_id);
328     if (zm)
329         return zm->positioned;
330     return 0;
331 }
332     
333 int zebra_maps_is_sort(ZebraMaps zms, unsigned reg_id)
334 {
335     struct zebra_map *zm = zebra_map_get(zms, reg_id);
336     if (zm)
337         return zm->type == ZEBRA_MAP_TYPE_SORT;
338     return 0;
339 }
340
341 int zebra_maps_is_alwaysmatches(ZebraMaps zms, unsigned reg_id)
342 {
343     struct zebra_map *zm = zebra_map_get(zms, reg_id);
344     if (zm)
345         return zm->alwaysmatches;
346     return 0;
347 }
348
349 int zebra_maps_sort(ZebraMaps zms, Z_SortAttributes *sortAttributes,
350                     int *numerical)
351 {
352     AttrType use;
353     AttrType structure;
354     int structure_value;
355     attr_init_AttrList(&use, sortAttributes->list, 1);
356     attr_init_AttrList(&structure, sortAttributes->list, 4);
357
358     *numerical = 0;
359     structure_value = attr_find(&structure, 0);
360     if (structure_value == 109)
361         *numerical = 1;
362     return attr_find(&use, NULL);
363 }
364
365 int zebra_maps_attr(ZebraMaps zms, Z_AttributesPlusTerm *zapt,
366                     unsigned *reg_id, char **search_type, char *rank_type,
367                     int *complete_flag, int *sort_flag)
368 {
369     AttrType completeness;
370     AttrType structure;
371     AttrType relation;
372     AttrType sort_relation;
373     AttrType weight;
374     AttrType use;
375     int completeness_value;
376     int structure_value;
377     int relation_value;
378     int sort_relation_value;
379     int weight_value;
380     int use_value;
381
382     attr_init_APT(&structure, zapt, 4);
383     attr_init_APT(&completeness, zapt, 6);
384     attr_init_APT(&relation, zapt, 2);
385     attr_init_APT(&sort_relation, zapt, 7);
386     attr_init_APT(&weight, zapt, 9);
387     attr_init_APT(&use, zapt, 1);
388
389     completeness_value = attr_find(&completeness, NULL);
390     structure_value = attr_find(&structure, NULL);
391     relation_value = attr_find(&relation, NULL);
392     sort_relation_value = attr_find(&sort_relation, NULL);
393     weight_value = attr_find(&weight, NULL);
394     use_value = attr_find(&use, NULL);
395
396     if (completeness_value == 2 || completeness_value == 3)
397         *complete_flag = 1;
398     else
399         *complete_flag = 0;
400     *reg_id = 0;
401
402     *sort_flag =(sort_relation_value > 0) ? 1 : 0;
403     *search_type = "phrase";
404     strcpy(rank_type, "void");
405     if (relation_value == 102)
406     {
407         if (weight_value == -1)
408             weight_value = 34;
409         sprintf(rank_type, "rank,w=%d,u=%d", weight_value, use_value);
410     }
411     if (*complete_flag)
412         *reg_id = 'p';
413     else
414         *reg_id = 'w';
415     switch (structure_value)
416     {
417     case 6:   /* word list */
418         *search_type = "and-list";
419         break;
420     case 105: /* free-form-text */
421         *search_type = "or-list";
422         break;
423     case 106: /* document-text */
424         *search_type = "or-list";
425         break;  
426     case -1:
427     case 1:   /* phrase */
428     case 2:   /* word */
429     case 108: /* string */ 
430         *search_type = "phrase";
431         break;
432     case 107: /* local-number */
433         *search_type = "local";
434         *reg_id = 0;
435         break;
436     case 109: /* numeric string */
437         *reg_id = 'n';
438         *search_type = "numeric";
439         break;
440     case 104: /* urx */
441         *reg_id = 'u';
442         *search_type = "phrase";
443         break;
444     case 3:   /* key */
445         *reg_id = '0';
446         *search_type = "phrase";
447         break;
448     case 4:  /* year */
449         *reg_id = 'y';
450         *search_type = "phrase";
451         break;
452     case 5:  /* date */
453         *reg_id = 'd';
454         *search_type = "phrase";
455         break;
456     default:
457         return -1;
458     }
459     return 0;
460 }
461
462 WRBUF zebra_replace(ZebraMaps zms, unsigned reg_id, const char *ex_list,
463                     const char *input_str, int input_len)
464 {
465     wrbuf_rewind(zms->wrbuf_1);
466     wrbuf_write(zms->wrbuf_1, input_str, input_len);
467     return zms->wrbuf_1;
468 }
469
470 /*
471  * Local variables:
472  * c-basic-offset: 4
473  * indent-tabs-mode: nil
474  * End:
475  * vim: shiftwidth=4 tabstop=8 expandtab
476  */
477