1 /* $Id: zebramap.c,v 1.29 2002-12-16 22:59:34 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",
229 DEFAULT_PROFILE_PATH));
232 zms->tabroot = nmem_strdup (zms->nmem, base);
233 zms->map_list = NULL;
235 zms->temp_map_str[0] = '\0';
236 zms->temp_map_str[1] = '\0';
238 zms->temp_map_ptr[0] = zms->temp_map_str;
239 zms->temp_map_ptr[1] = NULL;
241 zms->lookup_array = (struct zebra_map**)
242 nmem_malloc (zms->nmem, sizeof(*zms->lookup_array)*256);
243 for (i = 0; i<256; i++)
244 zms->lookup_array[i] = 0;
245 if (!res || !res_trav (res, "index", zms, zms_map_handle))
246 zebra_map_read (zms, "default.idx");
248 zms->wrbuf_1 = wrbuf_alloc();
249 zms->wrbuf_2 = wrbuf_alloc();
254 struct zebra_map *zebra_map_get (ZebraMaps zms, unsigned reg_id)
256 return zms->lookup_array[reg_id];
259 chrmaptab zebra_charmap_get (ZebraMaps zms, unsigned reg_id)
261 struct zebra_map *zm = zebra_map_get (zms, reg_id);
264 zm = (struct zebra_map *) nmem_malloc (zms->nmem, sizeof(*zm));
265 logf (LOG_WARN, "Unknown register type: %c", reg_id);
268 zm->maptab_name = nmem_strdup (zms->nmem, "@");
270 zm->type = ZEBRA_MAP_TYPE_INDEX;
271 zm->completeness = 0;
272 zm->next = zms->map_list;
273 zms->map_list = zm->next;
275 zms->lookup_array[zm->reg_id & 255] = zm;
279 if (!zm->maptab_name || !yaz_matchstr (zm->maptab_name, "@"))
281 if (!(zm->maptab = chrmaptab_create (zms->tabpath,
284 logf(LOG_WARN, "Failed to read character table %s",
287 logf(LOG_DEBUG, "Read character table %s", zm->maptab_name);
292 const char **zebra_maps_input (ZebraMaps zms, unsigned reg_id,
293 const char **from, int len)
297 maptab = zebra_charmap_get (zms, reg_id);
299 return chr_map_input(maptab, from, len);
301 zms->temp_map_str[0] = **from;
304 return zms->temp_map_ptr;
308 int zebra_maps_input_tokens (ZebraMaps zms, unsigned reg_id,
309 const char *input_str, int input_len,
312 chrmaptab maptab = zebra_charmap_get (zms, reg_id);
321 wrbuf_write (wrbuf, input_str, input_len);
332 out = chr_map_input (maptab, str, len);
335 while (out && *out && **out == *CHR_SPACE)
336 out = chr_map_input (maptab, str, len);
341 const char *zebra_maps_output(ZebraMaps zms, unsigned reg_id,
344 chrmaptab maptab = zebra_charmap_get (zms, reg_id);
347 return chr_map_output (maptab, from, 1);
351 /* ------------------------------------ */
357 Z_AttributeElement **attributeList;
361 static int attr_find (AttrType *src, oid_value *attributeSetP)
363 while (src->major < src->num_attributes)
365 Z_AttributeElement *element;
367 element = src->attributeList[src->major];
368 if (src->type == *element->attributeType)
370 switch (element->which)
372 case Z_AttributeValue_numeric:
374 if (element->attributeSet && attributeSetP)
378 attrset = oid_getentbyoid (element->attributeSet);
379 *attributeSetP = attrset->value;
381 return *element->value.numeric;
383 case Z_AttributeValue_complex:
384 if (src->minor >= element->value.complex->num_list ||
385 element->value.complex->list[src->minor]->which !=
386 Z_StringOrNumeric_numeric)
389 if (element->attributeSet && attributeSetP)
393 attrset = oid_getentbyoid (element->attributeSet);
394 *attributeSetP = attrset->value;
396 return *element->value.complex->list[src->minor-1]->u.numeric;
406 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;
415 static void attr_init_AttrList (AttrType *src, Z_AttributeList *list, int type)
417 src->attributeList = list->attributes;
418 src->num_attributes = list->num_attributes;
424 /* ------------------------------------ */
426 int zebra_maps_is_complete (ZebraMaps zms, unsigned reg_id)
428 struct zebra_map *zm = zebra_map_get (zms, reg_id);
430 return zm->completeness;
434 int zebra_maps_is_positioned (ZebraMaps zms, unsigned reg_id)
436 struct zebra_map *zm = zebra_map_get (zms, reg_id);
438 return zm->positioned;
442 int zebra_maps_is_sort (ZebraMaps zms, unsigned reg_id)
444 struct zebra_map *zm = zebra_map_get (zms, reg_id);
446 return zm->type == ZEBRA_MAP_TYPE_SORT;
450 int zebra_maps_sort (ZebraMaps zms, Z_SortAttributes *sortAttributes,
456 attr_init_AttrList (&use, sortAttributes->list, 1);
457 attr_init_AttrList (&structure, sortAttributes->list, 4);
460 structure_value = attr_find (&structure, 0);
461 if (structure_value == 109)
463 return attr_find (&use, NULL);
466 int zebra_maps_attr (ZebraMaps zms, Z_AttributesPlusTerm *zapt,
467 unsigned *reg_id, char **search_type, char *rank_type,
468 int *complete_flag, int *sort_flag)
470 AttrType completeness;
473 AttrType sort_relation;
475 int completeness_value;
478 int sort_relation_value;
481 attr_init_APT (&structure, zapt, 4);
482 attr_init_APT (&completeness, zapt, 6);
483 attr_init_APT (&relation, zapt, 2);
484 attr_init_APT (&sort_relation, zapt, 7);
485 attr_init_APT (&weight, zapt, 9);
487 completeness_value = attr_find (&completeness, NULL);
488 structure_value = attr_find (&structure, NULL);
489 relation_value = attr_find (&relation, NULL);
490 sort_relation_value = attr_find (&sort_relation, NULL);
491 weight_value = attr_find (&weight, NULL);
493 if (completeness_value == 2 || completeness_value == 3)
499 *sort_flag = (sort_relation_value > 0) ? 1 : 0;
500 *search_type = "phrase";
501 strcpy (rank_type, "void");
502 if (relation_value == 102)
504 if (weight_value == -1)
506 sprintf (rank_type, "rank,%d", weight_value);
508 if (relation_value == 103)
510 *search_type = "always";
517 switch (structure_value)
519 case 6: /* word list */
520 *search_type = "and-list";
522 case 105: /* free-form-text */
523 *search_type = "or-list";
525 case 106: /* document-text */
526 *search_type = "or-list";
531 case 108: /* string */
532 *search_type = "phrase";
534 case 107: /* local-number */
535 *search_type = "local";
538 case 109: /* numeric string */
540 *search_type = "numeric";
544 *search_type = "phrase";
548 *search_type = "phrase";
552 *search_type = "phrase";
556 *search_type = "phrase";
564 int zebra_replace_sub(ZebraMaps zms, unsigned reg_id, const char *ex_list,
565 const char *input_str, int input_len, WRBUF wrbuf);
567 WRBUF zebra_replace(ZebraMaps zms, unsigned reg_id, const char *ex_list,
568 const char *input_str, int input_len)
570 struct zebra_map *zm = zebra_map_get (zms, reg_id);
572 wrbuf_rewind(zms->wrbuf_1);
573 wrbuf_write(zms->wrbuf_1, input_str, input_len);
574 if (!zm || !zm->replace_tokens)
578 logf (LOG_LOG, "in:%.*s:", wrbuf_len(zms->wrbuf_1),
579 wrbuf_buf(zms->wrbuf_1));
583 if (!zebra_replace_sub(zms, reg_id, ex_list, wrbuf_buf(zms->wrbuf_1),
584 wrbuf_len(zms->wrbuf_1), zms->wrbuf_2))
586 if (!zebra_replace_sub(zms, reg_id, ex_list, wrbuf_buf(zms->wrbuf_2),
587 wrbuf_len(zms->wrbuf_2), zms->wrbuf_1))
593 int zebra_replace_sub(ZebraMaps zms, unsigned reg_id, const char *ex_list,
594 const char *input_str, int input_len, WRBUF wrbuf)
598 struct zebra_map *zm = zebra_map_get (zms, reg_id);
601 for (i = -1; i <= input_len; )
603 struct zm_token *token;
604 char replace_string[128];
608 for (token = zm->replace_tokens; !replace_in && token;
612 int replace_done = 0;
617 if (!token->token_from[j])
622 if (ex_list && strchr (ex_list, token->token_from[j]))
624 if (i+j < 0 || j+i >= input_len)
627 c = input_str[j+i] & 255;
628 if (token->token_from[j] == ZEBRA_REPLACE_ANY)
632 replace_string[replace_out++] = c;
636 if (c != token->token_from[j])
642 const char *cp = token->token_to;
644 for (; cp && *cp; cp++)
645 replace_string[replace_out++] = *cp;
652 if (i >= 0 && i < input_len)
653 wrbuf_putc(wrbuf, input_str[i]);
660 wrbuf_write(wrbuf, replace_string, replace_out);
665 logf (LOG_LOG, "out:%.*s:", wrbuf_len(wrbuf), wrbuf_buf(wrbuf));