1 /* $Id: zebramap.c,v 1.53 2006-09-08 14:41:00 adam Exp $
2 Copyright (C) 1995-2006
5 This file is part of the Zebra server.
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
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
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
29 #include <yaz/yaz-util.h>
33 #define ZEBRA_MAP_TYPE_SORT 1
34 #define ZEBRA_MAP_TYPE_INDEX 2
36 #define ZEBRA_REPLACE_ANY 300
54 const char *maptab_name;
55 struct zebra_map *next;
62 struct zebra_map *map_list;
64 const char *temp_map_ptr[2];
65 struct zebra_map **lookup_array;
70 void zebra_maps_close(ZebraMaps zms)
72 struct zebra_map *zm = zms->map_list;
76 chrmaptab_destroy(zm->maptab);
79 wrbuf_free(zms->wrbuf_1, 1);
80 nmem_destroy(zms->nmem);
84 ZEBRA_RES zebra_maps_read_file(ZebraMaps zms, const char *fname)
92 struct zebra_map **zm = 0, *zp;
94 if (!(f = yaz_fopen(zms->tabpath, fname, "r", zms->tabroot)))
96 yaz_log(YLOG_ERRNO|YLOG_FATAL, "%s", fname);
99 while ((argc = readconf_line(f, &lineno, line, 512, argv, 10)))
103 yaz_log(YLOG_WARN, "%s:%d: Missing arguments for '%s'",
104 fname, lineno, argv[0]);
110 yaz_log(YLOG_WARN, "%s:%d: Too many arguments for '%s'",
111 fname, lineno, argv[0]);
115 if (!yaz_matchstr(argv[0], "index"))
121 *zm = (struct zebra_map *) nmem_malloc(zms->nmem, sizeof(**zm));
122 (*zm)->reg_id = argv[1][0];
123 (*zm)->maptab_name = NULL;
124 (*zm)->maptab = NULL;
125 (*zm)->type = ZEBRA_MAP_TYPE_INDEX;
126 (*zm)->completeness = 0;
127 (*zm)->positioned = 1;
128 (*zm)->alwaysmatches = 0;
129 (*zm)->first_in_field = 0;
132 else if (!yaz_matchstr(argv[0], "sort"))
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)->alwaysmatches = 0;
147 (*zm)->first_in_field = 0;
152 yaz_log(YLOG_WARN, "%s:%d: Missing sort/index before '%s'",
153 fname, lineno, argv[0]);
156 else if (!yaz_matchstr(argv[0], "charmap") && argc == 2)
158 (*zm)->maptab_name = nmem_strdup(zms->nmem, argv[1]);
160 else if (!yaz_matchstr(argv[0], "completeness") && argc == 2)
162 (*zm)->completeness = atoi(argv[1]);
164 else if (!yaz_matchstr(argv[0], "position") && argc == 2)
166 (*zm)->positioned = atoi(argv[1]);
168 else if (!yaz_matchstr(argv[0], "alwaysmatches") && argc == 2)
170 (*zm)->alwaysmatches = atoi(argv[1]);
172 else if (!yaz_matchstr(argv[0], "firstinfield") && argc == 2)
174 (*zm)->first_in_field = atoi(argv[1]);
176 else if (!yaz_matchstr(argv[0], "entrysize") && argc == 2)
178 if ((*zm)->type == ZEBRA_MAP_TYPE_SORT)
179 (*zm)->u.sort.entry_size = atoi(argv[1]);
183 yaz_log(YLOG_WARN, "%s:%d: Unrecognized directive '%s'",
184 fname, lineno, argv[0]);
192 for (zp = zms->map_list; zp; zp = zp->next)
193 zms->lookup_array[zp->reg_id] = zp;
200 ZebraMaps zebra_maps_open(Res res, const char *base_path,
201 const char *profile_path)
203 ZebraMaps zms = (ZebraMaps) xmalloc(sizeof(*zms));
206 zms->nmem = nmem_create();
208 zms->tabpath = nmem_strdup(zms->nmem, profile_path);
211 zms->tabroot = nmem_strdup(zms->nmem, base_path);
212 zms->map_list = NULL;
214 zms->temp_map_str[0] = '\0';
215 zms->temp_map_str[1] = '\0';
217 zms->temp_map_ptr[0] = zms->temp_map_str;
218 zms->temp_map_ptr[1] = NULL;
220 zms->lookup_array = (struct zebra_map**)
221 nmem_malloc(zms->nmem, sizeof(*zms->lookup_array)*256);
222 zms->wrbuf_1 = wrbuf_alloc();
224 for (i = 0; i<256; i++)
225 zms->lookup_array[i] = 0;
229 struct zebra_map *zebra_map_get(ZebraMaps zms, unsigned reg_id)
231 assert(reg_id >= 0 && reg_id <= 255);
232 return zms->lookup_array[reg_id];
235 chrmaptab zebra_charmap_get(ZebraMaps zms, unsigned reg_id)
237 struct zebra_map *zm = zebra_map_get(zms, reg_id);
240 zm = (struct zebra_map *) nmem_malloc(zms->nmem, sizeof(*zm));
242 /* no reason to warn if no maps are installed at ALL */
244 yaz_log(YLOG_WARN, "Unknown register type: %c", reg_id);
247 zm->maptab_name = nmem_strdup(zms->nmem, "@");
249 zm->type = ZEBRA_MAP_TYPE_INDEX;
250 zm->completeness = 0;
251 zm->next = zms->map_list;
252 zms->map_list = zm->next;
254 zms->lookup_array[zm->reg_id & 255] = zm;
258 if (!zm->maptab_name || !yaz_matchstr(zm->maptab_name, "@"))
260 if (!(zm->maptab = chrmaptab_create(zms->tabpath,
263 yaz_log(YLOG_WARN, "Failed to read character table %s",
266 yaz_log(YLOG_DEBUG, "Read character table %s", zm->maptab_name);
271 const char **zebra_maps_input(ZebraMaps zms, unsigned reg_id,
272 const char **from, int len, int first)
276 maptab = zebra_charmap_get(zms, reg_id);
278 return chr_map_input(maptab, from, len, first);
280 zms->temp_map_str[0] = **from;
283 return zms->temp_map_ptr;
286 const char **zebra_maps_search(ZebraMaps zms, unsigned reg_id,
287 const char **from, int len, int *q_map_match)
292 maptab = zebra_charmap_get(zms, reg_id);
296 map = chr_map_q_input(maptab, from, len, 0);
302 map = chr_map_input(maptab, from, len, 0);
306 zms->temp_map_str[0] = **from;
309 return zms->temp_map_ptr;
312 const char *zebra_maps_output(ZebraMaps zms, unsigned reg_id,
315 chrmaptab maptab = zebra_charmap_get(zms, reg_id);
318 return chr_map_output(maptab, from, 1);
322 /* ------------------------------------ */
324 int zebra_maps_is_complete(ZebraMaps zms, unsigned reg_id)
326 struct zebra_map *zm = zebra_map_get(zms, reg_id);
328 return zm->completeness;
332 int zebra_maps_is_positioned(ZebraMaps zms, unsigned reg_id)
334 struct zebra_map *zm = zebra_map_get(zms, reg_id);
336 return zm->positioned;
340 int zebra_maps_is_sort(ZebraMaps zms, unsigned reg_id)
342 struct zebra_map *zm = zebra_map_get(zms, reg_id);
344 return zm->type == ZEBRA_MAP_TYPE_SORT;
348 int zebra_maps_is_alwaysmatches(ZebraMaps zms, unsigned reg_id)
350 struct zebra_map *zm = zebra_map_get(zms, reg_id);
352 return zm->alwaysmatches;
356 int zebra_maps_is_first_in_field(ZebraMaps zms, unsigned reg_id)
358 struct zebra_map *zm = zebra_map_get(zms, reg_id);
360 return zm->first_in_field;
364 int zebra_maps_sort(ZebraMaps zms, Z_SortAttributes *sortAttributes,
370 attr_init_AttrList(&use, sortAttributes->list, 1);
371 attr_init_AttrList(&structure, sortAttributes->list, 4);
374 structure_value = attr_find(&structure, 0);
375 if (structure_value == 109)
377 return attr_find(&use, NULL);
380 int zebra_maps_attr(ZebraMaps zms, Z_AttributesPlusTerm *zapt,
381 unsigned *reg_id, char **search_type, char *rank_type,
382 int *complete_flag, int *sort_flag)
384 AttrType completeness;
387 AttrType sort_relation;
390 int completeness_value;
393 int sort_relation_value;
397 attr_init_APT(&structure, zapt, 4);
398 attr_init_APT(&completeness, zapt, 6);
399 attr_init_APT(&relation, zapt, 2);
400 attr_init_APT(&sort_relation, zapt, 7);
401 attr_init_APT(&weight, zapt, 9);
402 attr_init_APT(&use, zapt, 1);
404 completeness_value = attr_find(&completeness, NULL);
405 structure_value = attr_find(&structure, NULL);
406 relation_value = attr_find(&relation, NULL);
407 sort_relation_value = attr_find(&sort_relation, NULL);
408 weight_value = attr_find(&weight, NULL);
409 use_value = attr_find(&use, NULL);
411 if (completeness_value == 2 || completeness_value == 3)
417 *sort_flag =(sort_relation_value > 0) ? 1 : 0;
418 *search_type = "phrase";
419 strcpy(rank_type, "void");
420 if (relation_value == 102)
422 if (weight_value == -1)
424 sprintf(rank_type, "rank,w=%d,u=%d", weight_value, use_value);
430 switch (structure_value)
432 case 6: /* word list */
433 *search_type = "and-list";
435 case 105: /* free-form-text */
436 *search_type = "or-list";
438 case 106: /* document-text */
439 *search_type = "or-list";
444 case 108: /* string */
445 *search_type = "phrase";
447 case 107: /* local-number */
448 *search_type = "local";
451 case 109: /* numeric string */
453 *search_type = "numeric";
457 *search_type = "phrase";
461 *search_type = "phrase";
465 *search_type = "phrase";
469 *search_type = "phrase";
477 WRBUF zebra_replace(ZebraMaps zms, unsigned reg_id, const char *ex_list,
478 const char *input_str, int input_len)
480 wrbuf_rewind(zms->wrbuf_1);
481 wrbuf_write(zms->wrbuf_1, input_str, input_len);
488 * indent-tabs-mode: nil
490 * vim: shiftwidth=4 tabstop=8 expandtab