1 /* $Id: zebramap.c,v 1.27 2002-08-02 19:26:57 adam Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
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
28 #include <yaz/yaz-util.h>
32 #define ZEBRA_MAP_TYPE_SORT 1
33 #define ZEBRA_MAP_TYPE_INDEX 2
35 #define ZEBRA_REPLACE_ANY 300
41 struct zm_token *next;
58 const char *maptab_name;
59 struct zebra_map *next;
60 struct zm_token *replace_tokens;
67 struct zebra_map *map_list;
69 const char *temp_map_ptr[2];
70 struct zebra_map **lookup_array;
71 WRBUF wrbuf_1, wrbuf_2;
74 void zebra_maps_close (ZebraMaps zms)
76 struct zebra_map *zm = zms->map_list;
80 chrmaptab_destroy (zm->maptab);
83 wrbuf_free (zms->wrbuf_1, 1);
84 wrbuf_free (zms->wrbuf_2, 1);
85 nmem_destroy (zms->nmem);
89 static void zebra_map_read (ZebraMaps zms, const char *name)
96 struct zebra_map **zm = 0, *zp;
98 if (!(f = yaz_fopen(zms->tabpath, name, "r", zms->tabroot)))
100 logf(LOG_WARN|LOG_ERRNO, "%s", name);
103 while ((argc = readconf_line(f, &lineno, line, 512, argv, 10)))
105 if (!yaz_matchstr (argv[0], "index") && argc == 2)
111 *zm = (struct zebra_map *) nmem_malloc (zms->nmem, sizeof(**zm));
112 (*zm)->reg_id = argv[1][0];
113 (*zm)->maptab_name = NULL;
114 (*zm)->maptab = NULL;
115 (*zm)->type = ZEBRA_MAP_TYPE_INDEX;
116 (*zm)->completeness = 0;
117 (*zm)->positioned = 1;
118 (*zm)->replace_tokens = 0;
120 else if (!yaz_matchstr (argv[0], "sort") && argc == 2)
126 *zm = (struct zebra_map *) nmem_malloc (zms->nmem, sizeof(**zm));
127 (*zm)->reg_id = argv[1][0];
128 (*zm)->maptab_name = NULL;
129 (*zm)->type = ZEBRA_MAP_TYPE_SORT;
130 (*zm)->u.sort.entry_size = 80;
131 (*zm)->maptab = NULL;
132 (*zm)->completeness = 0;
133 (*zm)->positioned = 0;
134 (*zm)->replace_tokens = 0;
136 else if (zm && !yaz_matchstr (argv[0], "charmap") && argc == 2)
138 (*zm)->maptab_name = nmem_strdup (zms->nmem, argv[1]);
140 else if (zm && !yaz_matchstr (argv[0], "completeness") && argc == 2)
142 (*zm)->completeness = atoi (argv[1]);
144 else if (zm && !yaz_matchstr (argv[0], "position") && argc == 2)
146 (*zm)->positioned = atoi (argv[1]);
148 else if (zm && !yaz_matchstr (argv[0], "entrysize") && argc == 2)
150 if ((*zm)->type == ZEBRA_MAP_TYPE_SORT)
151 (*zm)->u.sort.entry_size = atoi (argv[1]);
153 else if (zm && !yaz_matchstr (argv[0], "replace") && argc >= 2)
155 struct zm_token *token = nmem_malloc (zms->nmem, sizeof(*token));
156 token->next = (*zm)->replace_tokens;
157 (*zm)->replace_tokens = token;
159 logf (LOG_LOG, "replace %s", argv[1]);
161 token->token_from = 0;
165 int *dp = token->token_from = (int *)
166 nmem_malloc (zms->nmem, (1+strlen(cp))*sizeof(int));
175 *dp++ = ZEBRA_REPLACE_ANY;
180 *dp++ = zebra_prim(&cp);
182 logf (LOG_LOG, " char %2X %c", dp[-1], dp[-1]);
190 char *dp = token->token_to =
191 nmem_malloc (zms->nmem, strlen(cp)+1);
199 *dp++ = zebra_prim(&cp);
210 for (zp = zms->map_list; zp; zp = zp->next)
211 zms->lookup_array[zp->reg_id] = zp;
214 static void zms_map_handle (void *p, const char *name, const char *value)
216 ZebraMaps zms = (ZebraMaps) p;
218 zebra_map_read (zms, value);
221 ZebraMaps zebra_maps_open (Res res, const char *base)
223 ZebraMaps zms = (ZebraMaps) xmalloc (sizeof(*zms));
226 zms->nmem = nmem_create ();
227 zms->tabpath = nmem_strdup (zms->nmem,
228 res_get_def (res, "profilePath", "."));
231 zms->tabroot = nmem_strdup (zms->nmem, base);
232 zms->map_list = NULL;
234 zms->temp_map_str[0] = '\0';
235 zms->temp_map_str[1] = '\0';
237 zms->temp_map_ptr[0] = zms->temp_map_str;
238 zms->temp_map_ptr[1] = NULL;
240 zms->lookup_array = (struct zebra_map**)
241 nmem_malloc (zms->nmem, sizeof(*zms->lookup_array)*256);
242 for (i = 0; i<256; i++)
243 zms->lookup_array[i] = 0;
244 if (!res || !res_trav (res, "index", zms, zms_map_handle))
245 zebra_map_read (zms, "default.idx");
247 zms->wrbuf_1 = wrbuf_alloc();
248 zms->wrbuf_2 = wrbuf_alloc();
253 struct zebra_map *zebra_map_get (ZebraMaps zms, unsigned reg_id)
255 return zms->lookup_array[reg_id];
258 chrmaptab zebra_charmap_get (ZebraMaps zms, unsigned reg_id)
260 struct zebra_map *zm = zebra_map_get (zms, reg_id);
263 zm = (struct zebra_map *) nmem_malloc (zms->nmem, sizeof(*zm));
264 logf (LOG_WARN, "Unknown register type: %c", reg_id);
267 zm->maptab_name = nmem_strdup (zms->nmem, "@");
269 zm->type = ZEBRA_MAP_TYPE_INDEX;
270 zm->completeness = 0;
271 zm->next = zms->map_list;
272 zms->map_list = zm->next;
274 zms->lookup_array[zm->reg_id & 255] = zm;
278 if (!zm->maptab_name || !yaz_matchstr (zm->maptab_name, "@"))
280 if (!(zm->maptab = chrmaptab_create (zms->tabpath,
283 logf(LOG_WARN, "Failed to read character table %s",
286 logf(LOG_DEBUG, "Read character table %s", zm->maptab_name);
291 const char **zebra_maps_input (ZebraMaps zms, unsigned reg_id,
292 const char **from, int len)
296 maptab = zebra_charmap_get (zms, reg_id);
298 return chr_map_input(maptab, from, len);
300 zms->temp_map_str[0] = **from;
303 return zms->temp_map_ptr;
307 int zebra_maps_input_tokens (ZebraMaps zms, unsigned reg_id,
308 const char *input_str, int input_len,
311 chrmaptab maptab = zebra_charmap_get (zms, reg_id);
320 wrbuf_write (wrbuf, input_str, input_len);
331 out = chr_map_input (maptab, str, len);
334 while (out && *out && **out == *CHR_SPACE)
335 out = chr_map_input (maptab, str, len);
340 const char *zebra_maps_output(ZebraMaps zms, unsigned reg_id,
343 chrmaptab maptab = zebra_charmap_get (zms, reg_id);
346 return chr_map_output (maptab, from, 1);
350 /* ------------------------------------ */
356 Z_AttributeElement **attributeList;
360 static int attr_find (AttrType *src, oid_value *attributeSetP)
362 while (src->major < src->num_attributes)
364 Z_AttributeElement *element;
366 element = src->attributeList[src->major];
367 if (src->type == *element->attributeType)
369 switch (element->which)
371 case Z_AttributeValue_numeric:
373 if (element->attributeSet && attributeSetP)
377 attrset = oid_getentbyoid (element->attributeSet);
378 *attributeSetP = attrset->value;
380 return *element->value.numeric;
382 case Z_AttributeValue_complex:
383 if (src->minor >= element->value.complex->num_list ||
384 element->value.complex->list[src->minor]->which !=
385 Z_StringOrNumeric_numeric)
388 if (element->attributeSet && attributeSetP)
392 attrset = oid_getentbyoid (element->attributeSet);
393 *attributeSetP = attrset->value;
395 return *element->value.complex->list[src->minor-1]->u.numeric;
405 static void attr_init_APT (AttrType *src, Z_AttributesPlusTerm *zapt, int type)
408 src->attributeList = zapt->attributes->attributes;
409 src->num_attributes = zapt->attributes->num_attributes;
411 src->attributeList = zapt->attributeList;
412 src->num_attributes = zapt->num_attributes;
419 static void attr_init_AttrList (AttrType *src, Z_AttributeList *list, int type)
421 src->attributeList = list->attributes;
422 src->num_attributes = list->num_attributes;
428 /* ------------------------------------ */
430 int zebra_maps_is_complete (ZebraMaps zms, unsigned reg_id)
432 struct zebra_map *zm = zebra_map_get (zms, reg_id);
434 return zm->completeness;
438 int zebra_maps_is_positioned (ZebraMaps zms, unsigned reg_id)
440 struct zebra_map *zm = zebra_map_get (zms, reg_id);
442 return zm->positioned;
446 int zebra_maps_is_sort (ZebraMaps zms, unsigned reg_id)
448 struct zebra_map *zm = zebra_map_get (zms, reg_id);
450 return zm->type == ZEBRA_MAP_TYPE_SORT;
454 int zebra_maps_sort (ZebraMaps zms, Z_SortAttributes *sortAttributes,
460 attr_init_AttrList (&use, sortAttributes->list, 1);
461 attr_init_AttrList (&structure, sortAttributes->list, 4);
464 structure_value = attr_find (&structure, 0);
465 if (structure_value == 109)
467 return attr_find (&use, NULL);
470 int zebra_maps_attr (ZebraMaps zms, Z_AttributesPlusTerm *zapt,
471 unsigned *reg_id, char **search_type, char *rank_type,
472 int *complete_flag, int *sort_flag)
474 AttrType completeness;
477 AttrType sort_relation;
479 int completeness_value;
482 int sort_relation_value;
485 attr_init_APT (&structure, zapt, 4);
486 attr_init_APT (&completeness, zapt, 6);
487 attr_init_APT (&relation, zapt, 2);
488 attr_init_APT (&sort_relation, zapt, 7);
489 attr_init_APT (&weight, zapt, 9);
491 completeness_value = attr_find (&completeness, NULL);
492 structure_value = attr_find (&structure, NULL);
493 relation_value = attr_find (&relation, NULL);
494 sort_relation_value = attr_find (&sort_relation, NULL);
495 weight_value = attr_find (&weight, NULL);
497 if (completeness_value == 2 || completeness_value == 3)
503 *sort_flag = (sort_relation_value > 0) ? 1 : 0;
504 *search_type = "phrase";
505 strcpy (rank_type, "void");
506 if (relation_value == 102)
508 if (weight_value == -1)
510 sprintf (rank_type, "rank,%d", weight_value);
512 if (relation_value == 103)
514 *search_type = "always";
521 switch (structure_value)
523 case 6: /* word list */
524 *search_type = "and-list";
526 case 105: /* free-form-text */
527 *search_type = "or-list";
529 case 106: /* document-text */
530 *search_type = "or-list";
535 case 108: /* string */
536 *search_type = "phrase";
538 case 107: /* local-number */
539 *search_type = "local";
542 case 109: /* numeric string */
544 *search_type = "numeric";
548 *search_type = "phrase";
552 *search_type = "phrase";
556 *search_type = "phrase";
560 *search_type = "phrase";
568 int zebra_replace_sub(ZebraMaps zms, unsigned reg_id, const char *ex_list,
569 const char *input_str, int input_len, WRBUF wrbuf);
571 WRBUF zebra_replace(ZebraMaps zms, unsigned reg_id, const char *ex_list,
572 const char *input_str, int input_len)
574 struct zebra_map *zm = zebra_map_get (zms, reg_id);
576 wrbuf_rewind(zms->wrbuf_1);
577 wrbuf_write(zms->wrbuf_1, input_str, input_len);
578 if (!zm || !zm->replace_tokens)
582 logf (LOG_LOG, "in:%.*s:", wrbuf_len(zms->wrbuf_1),
583 wrbuf_buf(zms->wrbuf_1));
587 if (!zebra_replace_sub(zms, reg_id, ex_list, wrbuf_buf(zms->wrbuf_1),
588 wrbuf_len(zms->wrbuf_1), zms->wrbuf_2))
590 if (!zebra_replace_sub(zms, reg_id, ex_list, wrbuf_buf(zms->wrbuf_2),
591 wrbuf_len(zms->wrbuf_2), zms->wrbuf_1))
597 int zebra_replace_sub(ZebraMaps zms, unsigned reg_id, const char *ex_list,
598 const char *input_str, int input_len, WRBUF wrbuf)
602 struct zebra_map *zm = zebra_map_get (zms, reg_id);
605 for (i = -1; i <= input_len; )
607 struct zm_token *token;
608 char replace_string[128];
612 for (token = zm->replace_tokens; !replace_in && token;
616 int replace_done = 0;
621 if (!token->token_from[j])
626 if (ex_list && strchr (ex_list, token->token_from[j]))
628 if (i+j < 0 || j+i >= input_len)
631 c = input_str[j+i] & 255;
632 if (token->token_from[j] == ZEBRA_REPLACE_ANY)
636 replace_string[replace_out++] = c;
640 if (c != token->token_from[j])
646 const char *cp = token->token_to;
648 for (; cp && *cp; cp++)
649 replace_string[replace_out++] = *cp;
656 if (i >= 0 && i < input_len)
657 wrbuf_putc(wrbuf, input_str[i]);
664 wrbuf_write(wrbuf, replace_string, replace_out);
669 logf (LOG_LOG, "out:%.*s:", wrbuf_len(wrbuf), wrbuf_buf(wrbuf));