1 /* $Id: zebramap.c,v 1.32.2.6 2006-08-14 10:39:25 adam Exp $
2 Copyright (C) 1995-2005
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 this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
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
42 struct zm_token *next;
59 const char *maptab_name;
60 struct zebra_map *next;
61 struct zm_token *replace_tokens;
68 struct zebra_map *map_list;
70 const char *temp_map_ptr[2];
71 struct zebra_map **lookup_array;
72 WRBUF wrbuf_1, wrbuf_2;
75 void zebra_maps_close (ZebraMaps zms)
77 struct zebra_map *zm = zms->map_list;
81 chrmaptab_destroy (zm->maptab);
84 wrbuf_free (zms->wrbuf_1, 1);
85 wrbuf_free (zms->wrbuf_2, 1);
86 nmem_destroy (zms->nmem);
90 static void zebra_map_read (ZebraMaps zms, const char *name)
97 struct zebra_map **zm = 0, *zp;
99 if (!(f = yaz_fopen(zms->tabpath, name, "r", zms->tabroot)))
101 yaz_log(LOG_WARN|LOG_ERRNO, "%s", name);
104 while ((argc = readconf_line(f, &lineno, line, 512, argv, 10)))
106 if (!yaz_matchstr (argv[0], "index") && argc == 2)
112 *zm = (struct zebra_map *) nmem_malloc (zms->nmem, sizeof(**zm));
113 (*zm)->reg_id = argv[1][0];
114 (*zm)->maptab_name = NULL;
115 (*zm)->maptab = NULL;
116 (*zm)->type = ZEBRA_MAP_TYPE_INDEX;
117 (*zm)->completeness = 0;
118 (*zm)->positioned = 1;
119 (*zm)->replace_tokens = 0;
121 else if (!yaz_matchstr (argv[0], "sort") && argc == 2)
127 *zm = (struct zebra_map *) nmem_malloc (zms->nmem, sizeof(**zm));
128 (*zm)->reg_id = argv[1][0];
129 (*zm)->maptab_name = NULL;
130 (*zm)->type = ZEBRA_MAP_TYPE_SORT;
131 (*zm)->u.sort.entry_size = 80;
132 (*zm)->maptab = NULL;
133 (*zm)->completeness = 0;
134 (*zm)->positioned = 0;
135 (*zm)->replace_tokens = 0;
137 else if (zm && !yaz_matchstr (argv[0], "charmap") && argc == 2)
139 (*zm)->maptab_name = nmem_strdup (zms->nmem, argv[1]);
141 else if (zm && !yaz_matchstr (argv[0], "completeness") && argc == 2)
143 (*zm)->completeness = atoi (argv[1]);
145 else if (zm && !yaz_matchstr (argv[0], "position") && argc == 2)
147 (*zm)->positioned = atoi (argv[1]);
149 else if (zm && !yaz_matchstr (argv[0], "entrysize") && argc == 2)
151 if ((*zm)->type == ZEBRA_MAP_TYPE_SORT)
152 (*zm)->u.sort.entry_size = atoi (argv[1]);
154 else if (zm && !yaz_matchstr (argv[0], "replace") && argc >= 2)
156 struct zm_token *token = nmem_malloc (zms->nmem, sizeof(*token));
157 token->next = (*zm)->replace_tokens;
158 (*zm)->replace_tokens = token;
160 yaz_log (LOG_LOG, "replace %s", argv[1]);
162 token->token_from = 0;
166 int *dp = token->token_from = (int *)
167 nmem_malloc (zms->nmem, (1+strlen(cp))*sizeof(int));
176 *dp++ = ZEBRA_REPLACE_ANY;
181 *dp++ = zebra_prim(&cp);
183 yaz_log (LOG_LOG, " char %2X %c", dp[-1], dp[-1]);
191 char *dp = token->token_to =
192 nmem_malloc (zms->nmem, strlen(cp)+1);
200 *dp++ = zebra_prim(&cp);
211 for (zp = zms->map_list; zp; zp = zp->next)
212 zms->lookup_array[zp->reg_id] = zp;
215 static void zms_map_handle (void *p, const char *name, const char *value)
217 ZebraMaps zms = (ZebraMaps) p;
219 zebra_map_read (zms, value);
222 ZebraMaps zebra_maps_open (Res res, const char *base)
224 ZebraMaps zms = (ZebraMaps) xmalloc (sizeof(*zms));
227 zms->nmem = nmem_create ();
228 zms->tabpath = nmem_strdup (zms->nmem,
229 res_get_def (res, "profilePath",
230 DEFAULT_PROFILE_PATH));
233 zms->tabroot = nmem_strdup (zms->nmem, base);
234 zms->map_list = NULL;
236 zms->temp_map_str[0] = '\0';
237 zms->temp_map_str[1] = '\0';
239 zms->temp_map_ptr[0] = zms->temp_map_str;
240 zms->temp_map_ptr[1] = NULL;
242 zms->lookup_array = (struct zebra_map**)
243 nmem_malloc (zms->nmem, sizeof(*zms->lookup_array)*256);
244 for (i = 0; i<256; i++)
245 zms->lookup_array[i] = 0;
246 if (!res || !res_trav (res, "index", zms, zms_map_handle))
247 zebra_map_read (zms, "default.idx");
249 zms->wrbuf_1 = wrbuf_alloc();
250 zms->wrbuf_2 = wrbuf_alloc();
255 struct zebra_map *zebra_map_get (ZebraMaps zms, unsigned reg_id)
257 return zms->lookup_array[reg_id];
260 chrmaptab zebra_charmap_get (ZebraMaps zms, unsigned reg_id)
262 struct zebra_map *zm = zebra_map_get (zms, reg_id);
265 zm = (struct zebra_map *) nmem_malloc (zms->nmem, sizeof(*zm));
266 yaz_log (LOG_WARN, "Unknown register type: %c", reg_id);
269 zm->maptab_name = nmem_strdup (zms->nmem, "@");
271 zm->type = ZEBRA_MAP_TYPE_INDEX;
272 zm->completeness = 0;
273 zm->next = zms->map_list;
274 zm->replace_tokens = 0;
275 zms->map_list = zm->next;
277 zms->lookup_array[zm->reg_id & 255] = zm;
281 if (!zm->maptab_name || !strcmp (zm->maptab_name, "@"))
283 if (!(zm->maptab = chrmaptab_create (zms->tabpath,
286 yaz_log(LOG_WARN, "Failed to read character table %s",
289 yaz_log(LOG_DEBUG, "Read character table %s", zm->maptab_name);
294 const char **zebra_maps_input (ZebraMaps zms, unsigned reg_id,
295 const char **from, int len, int first)
299 maptab = zebra_charmap_get (zms, reg_id);
301 return chr_map_input(maptab, from, len, first);
303 zms->temp_map_str[0] = **from;
306 return zms->temp_map_ptr;
309 const char **zebra_maps_search(ZebraMaps zms, unsigned reg_id,
310 const char **from, int len, int *q_map_match)
315 maptab = zebra_charmap_get (zms, reg_id);
319 map = chr_map_q_input(maptab, from, len, 0);
325 map = chr_map_input(maptab, from, len, 0);
329 zms->temp_map_str[0] = **from;
332 return zms->temp_map_ptr;
335 const char *zebra_maps_output(ZebraMaps zms, unsigned reg_id,
338 chrmaptab maptab = zebra_charmap_get (zms, reg_id);
341 return chr_map_output (maptab, from, 1);
345 /* ------------------------------------ */
351 Z_AttributeElement **attributeList;
355 static int attr_find (AttrType *src, oid_value *attributeSetP)
357 while (src->major < src->num_attributes)
359 Z_AttributeElement *element;
361 element = src->attributeList[src->major];
362 if (src->type == *element->attributeType)
364 switch (element->which)
366 case Z_AttributeValue_numeric:
368 if (element->attributeSet && attributeSetP)
372 attrset = oid_getentbyoid (element->attributeSet);
373 *attributeSetP = attrset->value;
375 return *element->value.numeric;
377 case Z_AttributeValue_complex:
378 if (src->minor >= element->value.complex->num_list ||
379 element->value.complex->list[src->minor]->which !=
380 Z_StringOrNumeric_numeric)
383 if (element->attributeSet && attributeSetP)
387 attrset = oid_getentbyoid (element->attributeSet);
388 *attributeSetP = attrset->value;
390 return *element->value.complex->list[src->minor-1]->u.numeric;
400 static void attr_init_APT (AttrType *src, Z_AttributesPlusTerm *zapt, int type)
402 src->attributeList = zapt->attributes->attributes;
403 src->num_attributes = zapt->attributes->num_attributes;
409 static void attr_init_AttrList (AttrType *src, Z_AttributeList *list, int type)
411 src->attributeList = list->attributes;
412 src->num_attributes = list->num_attributes;
418 /* ------------------------------------ */
420 int zebra_maps_is_complete (ZebraMaps zms, unsigned reg_id)
422 struct zebra_map *zm = zebra_map_get (zms, reg_id);
424 return zm->completeness;
428 int zebra_maps_is_positioned (ZebraMaps zms, unsigned reg_id)
430 struct zebra_map *zm = zebra_map_get (zms, reg_id);
432 return zm->positioned;
436 int zebra_maps_is_sort (ZebraMaps zms, unsigned reg_id)
438 struct zebra_map *zm = zebra_map_get (zms, reg_id);
440 return zm->type == ZEBRA_MAP_TYPE_SORT;
444 int zebra_maps_sort (ZebraMaps zms, Z_SortAttributes *sortAttributes,
450 attr_init_AttrList (&use, sortAttributes->list, 1);
451 attr_init_AttrList (&structure, sortAttributes->list, 4);
454 structure_value = attr_find (&structure, 0);
455 if (structure_value == 109)
457 return attr_find (&use, NULL);
460 int zebra_maps_attr (ZebraMaps zms, Z_AttributesPlusTerm *zapt,
461 unsigned *reg_id, char **search_type, char *rank_type,
462 int *complete_flag, int *sort_flag)
464 AttrType completeness;
467 AttrType sort_relation;
470 int completeness_value;
473 int sort_relation_value;
477 attr_init_APT (&structure, zapt, 4);
478 attr_init_APT (&completeness, zapt, 6);
479 attr_init_APT (&relation, zapt, 2);
480 attr_init_APT (&sort_relation, zapt, 7);
481 attr_init_APT (&weight, zapt, 9);
482 attr_init_APT (&use, zapt, 1);
484 completeness_value = attr_find (&completeness, NULL);
485 structure_value = attr_find (&structure, NULL);
486 relation_value = attr_find (&relation, NULL);
487 sort_relation_value = attr_find (&sort_relation, NULL);
488 weight_value = attr_find (&weight, NULL);
489 use_value = attr_find(&use, NULL);
491 if (completeness_value == 2 || completeness_value == 3)
497 *sort_flag = (sort_relation_value > 0) ? 1 : 0;
498 *search_type = "phrase";
499 strcpy (rank_type, "void");
500 if (relation_value == 102)
502 if (weight_value == -1)
504 sprintf (rank_type, "rank,w=%d,u=%d", weight_value, use_value);
506 if (relation_value == 103)
508 *search_type = "always";
515 switch (structure_value)
517 case 6: /* word list */
518 *search_type = "and-list";
520 case 105: /* free-form-text */
521 *search_type = "or-list";
523 case 106: /* document-text */
524 *search_type = "or-list";
529 case 108: /* string */
530 *search_type = "phrase";
532 case 107: /* local-number */
533 *search_type = "local";
536 case 109: /* numeric string */
538 *search_type = "numeric";
542 *search_type = "phrase";
546 *search_type = "phrase";
550 *search_type = "phrase";
554 *search_type = "phrase";
562 int zebra_replace_sub(ZebraMaps zms, unsigned reg_id, const char *ex_list,
563 const char *input_str, int input_len, WRBUF wrbuf);
565 WRBUF zebra_replace(ZebraMaps zms, unsigned reg_id, const char *ex_list,
566 const char *input_str, int input_len)
568 struct zebra_map *zm = zebra_map_get (zms, reg_id);
570 wrbuf_rewind(zms->wrbuf_1);
571 wrbuf_write(zms->wrbuf_1, input_str, input_len);
572 if (!zm || !zm->replace_tokens)
576 yaz_log (LOG_LOG, "in:%.*s:", wrbuf_len(zms->wrbuf_1),
577 wrbuf_buf(zms->wrbuf_1));
581 if (!zebra_replace_sub(zms, reg_id, ex_list, wrbuf_buf(zms->wrbuf_1),
582 wrbuf_len(zms->wrbuf_1), zms->wrbuf_2))
584 if (!zebra_replace_sub(zms, reg_id, ex_list, wrbuf_buf(zms->wrbuf_2),
585 wrbuf_len(zms->wrbuf_2), zms->wrbuf_1))
591 int zebra_replace_sub(ZebraMaps zms, unsigned reg_id, const char *ex_list,
592 const char *input_str, int input_len, WRBUF wrbuf)
596 struct zebra_map *zm = zebra_map_get (zms, reg_id);
599 for (i = -1; i <= input_len; )
601 struct zm_token *token;
602 char replace_string[128];
606 for (token = zm->replace_tokens; !replace_in && token;
610 int replace_done = 0;
615 if (!token->token_from[j])
620 if (ex_list && strchr (ex_list, token->token_from[j]))
622 if (i+j < 0 || j+i >= input_len)
625 c = input_str[j+i] & 255;
626 if (token->token_from[j] == ZEBRA_REPLACE_ANY)
630 replace_string[replace_out++] = c;
634 if (c != token->token_from[j])
640 const char *cp = token->token_to;
642 for (; cp && *cp; cp++)
643 replace_string[replace_out++] = *cp;
650 if (i >= 0 && i < input_len)
651 wrbuf_putc(wrbuf, input_str[i]);
658 wrbuf_write(wrbuf, replace_string, replace_out);
663 yaz_log (LOG_LOG, "out:%.*s:", wrbuf_len(wrbuf), wrbuf_buf(wrbuf));