1 /* $Id: zebramap.c,v 1.51 2006-06-23 11:21: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
53 const char *maptab_name;
54 struct zebra_map *next;
61 struct zebra_map *map_list;
63 const char *temp_map_ptr[2];
64 struct zebra_map **lookup_array;
69 void zebra_maps_close(ZebraMaps zms)
71 struct zebra_map *zm = zms->map_list;
75 chrmaptab_destroy(zm->maptab);
78 wrbuf_free(zms->wrbuf_1, 1);
79 nmem_destroy(zms->nmem);
83 ZEBRA_RES zebra_maps_read_file(ZebraMaps zms, const char *fname)
91 struct zebra_map **zm = 0, *zp;
93 if (!(f = yaz_fopen(zms->tabpath, fname, "r", zms->tabroot)))
95 yaz_log(YLOG_ERRNO|YLOG_FATAL, "%s", fname);
98 while ((argc = readconf_line(f, &lineno, line, 512, argv, 10)))
102 yaz_log(YLOG_WARN, "%s:%d: Missing arguments for '%s'",
103 fname, lineno, argv[0]);
109 yaz_log(YLOG_WARN, "%s:%d: Too many arguments for '%s'",
110 fname, lineno, argv[0]);
114 if (!yaz_matchstr(argv[0], "index"))
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;
130 else if (!yaz_matchstr(argv[0], "sort"))
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;
149 yaz_log(YLOG_WARN, "%s:%d: Missing sort/index before '%s'",
150 fname, lineno, argv[0]);
153 else if (!yaz_matchstr(argv[0], "charmap") && argc == 2)
155 (*zm)->maptab_name = nmem_strdup(zms->nmem, argv[1]);
157 else if (!yaz_matchstr(argv[0], "completeness") && argc == 2)
159 (*zm)->completeness = atoi(argv[1]);
161 else if (!yaz_matchstr(argv[0], "position") && argc == 2)
163 (*zm)->positioned = atoi(argv[1]);
165 else if (!yaz_matchstr(argv[0], "alwaysmatches") && argc == 2)
167 (*zm)->alwaysmatches = atoi(argv[1]);
169 else if (!yaz_matchstr(argv[0], "entrysize") && argc == 2)
171 if ((*zm)->type == ZEBRA_MAP_TYPE_SORT)
172 (*zm)->u.sort.entry_size = atoi(argv[1]);
176 yaz_log(YLOG_WARN, "%s:%d: Unrecognized directive '%s'",
177 fname, lineno, argv[0]);
185 for (zp = zms->map_list; zp; zp = zp->next)
186 zms->lookup_array[zp->reg_id] = zp;
193 ZebraMaps zebra_maps_open(Res res, const char *base_path,
194 const char *profile_path)
196 ZebraMaps zms = (ZebraMaps) xmalloc(sizeof(*zms));
199 zms->nmem = nmem_create();
201 zms->tabpath = nmem_strdup(zms->nmem, profile_path);
204 zms->tabroot = nmem_strdup(zms->nmem, base_path);
205 zms->map_list = NULL;
207 zms->temp_map_str[0] = '\0';
208 zms->temp_map_str[1] = '\0';
210 zms->temp_map_ptr[0] = zms->temp_map_str;
211 zms->temp_map_ptr[1] = NULL;
213 zms->lookup_array = (struct zebra_map**)
214 nmem_malloc(zms->nmem, sizeof(*zms->lookup_array)*256);
215 zms->wrbuf_1 = wrbuf_alloc();
217 for (i = 0; i<256; i++)
218 zms->lookup_array[i] = 0;
222 struct zebra_map *zebra_map_get(ZebraMaps zms, unsigned reg_id)
224 assert(reg_id >= 0 && reg_id <= 255);
225 return zms->lookup_array[reg_id];
228 chrmaptab zebra_charmap_get(ZebraMaps zms, unsigned reg_id)
230 struct zebra_map *zm = zebra_map_get(zms, reg_id);
233 zm = (struct zebra_map *) nmem_malloc(zms->nmem, sizeof(*zm));
235 /* no reason to warn if no maps are installed at ALL */
237 yaz_log(YLOG_WARN, "Unknown register type: %c", reg_id);
240 zm->maptab_name = nmem_strdup(zms->nmem, "@");
242 zm->type = ZEBRA_MAP_TYPE_INDEX;
243 zm->completeness = 0;
244 zm->next = zms->map_list;
245 zms->map_list = zm->next;
247 zms->lookup_array[zm->reg_id & 255] = zm;
251 if (!zm->maptab_name || !yaz_matchstr(zm->maptab_name, "@"))
253 if (!(zm->maptab = chrmaptab_create(zms->tabpath,
256 yaz_log(YLOG_WARN, "Failed to read character table %s",
259 yaz_log(YLOG_DEBUG, "Read character table %s", zm->maptab_name);
264 const char **zebra_maps_input(ZebraMaps zms, unsigned reg_id,
265 const char **from, int len, int first)
269 maptab = zebra_charmap_get(zms, reg_id);
271 return chr_map_input(maptab, from, len, first);
273 zms->temp_map_str[0] = **from;
276 return zms->temp_map_ptr;
279 const char **zebra_maps_search(ZebraMaps zms, unsigned reg_id,
280 const char **from, int len, int *q_map_match)
285 maptab = zebra_charmap_get(zms, reg_id);
289 map = chr_map_q_input(maptab, from, len, 0);
295 map = chr_map_input(maptab, from, len, 0);
299 zms->temp_map_str[0] = **from;
302 return zms->temp_map_ptr;
305 const char *zebra_maps_output(ZebraMaps zms, unsigned reg_id,
308 chrmaptab maptab = zebra_charmap_get(zms, reg_id);
311 return chr_map_output(maptab, from, 1);
315 /* ------------------------------------ */
317 int zebra_maps_is_complete(ZebraMaps zms, unsigned reg_id)
319 struct zebra_map *zm = zebra_map_get(zms, reg_id);
321 return zm->completeness;
325 int zebra_maps_is_positioned(ZebraMaps zms, unsigned reg_id)
327 struct zebra_map *zm = zebra_map_get(zms, reg_id);
329 return zm->positioned;
333 int zebra_maps_is_sort(ZebraMaps zms, unsigned reg_id)
335 struct zebra_map *zm = zebra_map_get(zms, reg_id);
337 return zm->type == ZEBRA_MAP_TYPE_SORT;
341 int zebra_maps_is_alwaysmatches(ZebraMaps zms, unsigned reg_id)
343 struct zebra_map *zm = zebra_map_get(zms, reg_id);
345 return zm->alwaysmatches;
349 int zebra_maps_sort(ZebraMaps zms, Z_SortAttributes *sortAttributes,
355 attr_init_AttrList(&use, sortAttributes->list, 1);
356 attr_init_AttrList(&structure, sortAttributes->list, 4);
359 structure_value = attr_find(&structure, 0);
360 if (structure_value == 109)
362 return attr_find(&use, NULL);
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)
369 AttrType completeness;
372 AttrType sort_relation;
375 int completeness_value;
378 int sort_relation_value;
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);
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);
396 if (completeness_value == 2 || completeness_value == 3)
402 *sort_flag =(sort_relation_value > 0) ? 1 : 0;
403 *search_type = "phrase";
404 strcpy(rank_type, "void");
405 if (relation_value == 102)
407 if (weight_value == -1)
409 sprintf(rank_type, "rank,w=%d,u=%d", weight_value, use_value);
415 switch (structure_value)
417 case 6: /* word list */
418 *search_type = "and-list";
420 case 105: /* free-form-text */
421 *search_type = "or-list";
423 case 106: /* document-text */
424 *search_type = "or-list";
429 case 108: /* string */
430 *search_type = "phrase";
432 case 107: /* local-number */
433 *search_type = "local";
436 case 109: /* numeric string */
438 *search_type = "numeric";
442 *search_type = "phrase";
446 *search_type = "phrase";
450 *search_type = "phrase";
454 *search_type = "phrase";
462 WRBUF zebra_replace(ZebraMaps zms, unsigned reg_id, const char *ex_list,
463 const char *input_str, int input_len)
465 wrbuf_rewind(zms->wrbuf_1);
466 wrbuf_write(zms->wrbuf_1, input_str, input_len);
473 * indent-tabs-mode: nil
475 * vim: shiftwidth=4 tabstop=8 expandtab