1 /* $Id: zebramap.c,v 1.49 2006-05-19 13:49:38 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>
31 #include <idzebra/zebramap.h>
33 #define ZEBRA_MAP_TYPE_SORT 1
34 #define ZEBRA_MAP_TYPE_INDEX 2
36 #define ZEBRA_REPLACE_ANY 300
52 const char *maptab_name;
53 struct zebra_map *next;
60 struct zebra_map *map_list;
62 const char *temp_map_ptr[2];
63 struct zebra_map **lookup_array;
68 void zebra_maps_close(ZebraMaps zms)
70 struct zebra_map *zm = zms->map_list;
74 chrmaptab_destroy(zm->maptab);
77 wrbuf_free(zms->wrbuf_1, 1);
78 nmem_destroy(zms->nmem);
82 ZEBRA_RES zebra_maps_read_file(ZebraMaps zms, const char *fname)
89 struct zebra_map **zm = 0, *zp;
91 if (!(f = yaz_fopen(zms->tabpath, fname, "r", zms->tabroot)))
93 yaz_log(YLOG_ERRNO|YLOG_FATAL, "%s", fname);
96 while ((argc = readconf_line(f, &lineno, line, 512, argv, 10)))
98 if (!yaz_matchstr(argv[0], "index") && argc == 2)
104 *zm = (struct zebra_map *) nmem_malloc(zms->nmem, sizeof(**zm));
105 (*zm)->reg_id = argv[1][0];
106 (*zm)->maptab_name = NULL;
107 (*zm)->maptab = NULL;
108 (*zm)->type = ZEBRA_MAP_TYPE_INDEX;
109 (*zm)->completeness = 0;
110 (*zm)->positioned = 1;
113 else if (!yaz_matchstr(argv[0], "sort") && argc == 2)
119 *zm = (struct zebra_map *) nmem_malloc(zms->nmem, sizeof(**zm));
120 (*zm)->reg_id = argv[1][0];
121 (*zm)->maptab_name = NULL;
122 (*zm)->type = ZEBRA_MAP_TYPE_SORT;
123 (*zm)->u.sort.entry_size = 80;
124 (*zm)->maptab = NULL;
125 (*zm)->completeness = 0;
126 (*zm)->positioned = 0;
129 else if (zm && !yaz_matchstr(argv[0], "charmap") && argc == 2)
131 (*zm)->maptab_name = nmem_strdup(zms->nmem, argv[1]);
133 else if (zm && !yaz_matchstr(argv[0], "completeness") && argc == 2)
135 (*zm)->completeness = atoi(argv[1]);
137 else if (zm && !yaz_matchstr(argv[0], "position") && argc == 2)
139 (*zm)->positioned = atoi(argv[1]);
141 else if (zm && !yaz_matchstr(argv[0], "entrysize") && argc == 2)
143 if ((*zm)->type == ZEBRA_MAP_TYPE_SORT)
144 (*zm)->u.sort.entry_size = atoi(argv[1]);
151 for (zp = zms->map_list; zp; zp = zp->next)
152 zms->lookup_array[zp->reg_id] = zp;
157 ZebraMaps zebra_maps_open(Res res, const char *base_path,
158 const char *profile_path)
160 ZebraMaps zms = (ZebraMaps) xmalloc(sizeof(*zms));
163 zms->nmem = nmem_create();
165 zms->tabpath = nmem_strdup(zms->nmem, profile_path);
168 zms->tabroot = nmem_strdup(zms->nmem, base_path);
169 zms->map_list = NULL;
171 zms->temp_map_str[0] = '\0';
172 zms->temp_map_str[1] = '\0';
174 zms->temp_map_ptr[0] = zms->temp_map_str;
175 zms->temp_map_ptr[1] = NULL;
177 zms->lookup_array = (struct zebra_map**)
178 nmem_malloc(zms->nmem, sizeof(*zms->lookup_array)*256);
179 zms->wrbuf_1 = wrbuf_alloc();
181 for (i = 0; i<256; i++)
182 zms->lookup_array[i] = 0;
186 struct zebra_map *zebra_map_get(ZebraMaps zms, unsigned reg_id)
188 assert(reg_id >= 0 && reg_id <= 255);
189 return zms->lookup_array[reg_id];
192 chrmaptab zebra_charmap_get(ZebraMaps zms, unsigned reg_id)
194 struct zebra_map *zm = zebra_map_get(zms, reg_id);
197 zm = (struct zebra_map *) nmem_malloc(zms->nmem, sizeof(*zm));
199 /* no reason to warn if no maps are installed at ALL */
201 yaz_log(YLOG_WARN, "Unknown register type: %c", reg_id);
204 zm->maptab_name = nmem_strdup(zms->nmem, "@");
206 zm->type = ZEBRA_MAP_TYPE_INDEX;
207 zm->completeness = 0;
208 zm->next = zms->map_list;
209 zms->map_list = zm->next;
211 zms->lookup_array[zm->reg_id & 255] = zm;
215 if (!zm->maptab_name || !yaz_matchstr(zm->maptab_name, "@"))
217 if (!(zm->maptab = chrmaptab_create(zms->tabpath,
220 yaz_log(YLOG_WARN, "Failed to read character table %s",
223 yaz_log(YLOG_DEBUG, "Read character table %s", zm->maptab_name);
228 const char **zebra_maps_input(ZebraMaps zms, unsigned reg_id,
229 const char **from, int len, int first)
233 maptab = zebra_charmap_get(zms, reg_id);
235 return chr_map_input(maptab, from, len, first);
237 zms->temp_map_str[0] = **from;
240 return zms->temp_map_ptr;
243 const char **zebra_maps_search(ZebraMaps zms, unsigned reg_id,
244 const char **from, int len, int *q_map_match)
249 maptab = zebra_charmap_get(zms, reg_id);
253 map = chr_map_q_input(maptab, from, len, 0);
259 map = chr_map_input(maptab, from, len, 0);
263 zms->temp_map_str[0] = **from;
266 return zms->temp_map_ptr;
269 const char *zebra_maps_output(ZebraMaps zms, unsigned reg_id,
272 chrmaptab maptab = zebra_charmap_get(zms, reg_id);
275 return chr_map_output(maptab, from, 1);
280 /* ------------------------------------ */
282 int zebra_maps_is_complete(ZebraMaps zms, unsigned reg_id)
284 struct zebra_map *zm = zebra_map_get(zms, reg_id);
286 return zm->completeness;
290 int zebra_maps_is_positioned(ZebraMaps zms, unsigned reg_id)
292 struct zebra_map *zm = zebra_map_get(zms, reg_id);
294 return zm->positioned;
298 int zebra_maps_is_sort(ZebraMaps zms, unsigned reg_id)
300 struct zebra_map *zm = zebra_map_get(zms, reg_id);
302 return zm->type == ZEBRA_MAP_TYPE_SORT;
306 int zebra_maps_sort(ZebraMaps zms, Z_SortAttributes *sortAttributes,
312 attr_init_AttrList(&use, sortAttributes->list, 1);
313 attr_init_AttrList(&structure, sortAttributes->list, 4);
316 structure_value = attr_find(&structure, 0);
317 if (structure_value == 109)
319 return attr_find(&use, NULL);
322 int zebra_maps_attr(ZebraMaps zms, Z_AttributesPlusTerm *zapt,
323 unsigned *reg_id, char **search_type, char *rank_type,
324 int *complete_flag, int *sort_flag)
326 AttrType completeness;
329 AttrType sort_relation;
332 int completeness_value;
335 int sort_relation_value;
339 attr_init_APT(&structure, zapt, 4);
340 attr_init_APT(&completeness, zapt, 6);
341 attr_init_APT(&relation, zapt, 2);
342 attr_init_APT(&sort_relation, zapt, 7);
343 attr_init_APT(&weight, zapt, 9);
344 attr_init_APT(&use, zapt, 1);
346 completeness_value = attr_find(&completeness, NULL);
347 structure_value = attr_find(&structure, NULL);
348 relation_value = attr_find(&relation, NULL);
349 sort_relation_value = attr_find(&sort_relation, NULL);
350 weight_value = attr_find(&weight, NULL);
351 use_value = attr_find(&use, NULL);
353 if (completeness_value == 2 || completeness_value == 3)
359 *sort_flag =(sort_relation_value > 0) ? 1 : 0;
360 *search_type = "phrase";
361 strcpy(rank_type, "void");
362 if (relation_value == 102)
364 if (weight_value == -1)
366 sprintf(rank_type, "rank,w=%d,u=%d", weight_value, use_value);
368 if (relation_value == 103)
370 *search_type = "always";
378 switch (structure_value)
380 case 6: /* word list */
381 *search_type = "and-list";
383 case 105: /* free-form-text */
384 *search_type = "or-list";
386 case 106: /* document-text */
387 *search_type = "or-list";
392 case 108: /* string */
393 *search_type = "phrase";
395 case 107: /* local-number */
396 *search_type = "local";
399 case 109: /* numeric string */
401 *search_type = "numeric";
405 *search_type = "phrase";
409 *search_type = "phrase";
413 *search_type = "phrase";
417 *search_type = "phrase";
425 WRBUF zebra_replace(ZebraMaps zms, unsigned reg_id, const char *ex_list,
426 const char *input_str, int input_len)
428 wrbuf_rewind(zms->wrbuf_1);
429 wrbuf_write(zms->wrbuf_1, input_str, input_len);
436 * indent-tabs-mode: nil
438 * vim: shiftwidth=4 tabstop=8 expandtab