1 /* $Id: zebramap.c,v 1.37 2004-12-13 20:51:34 adam Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004
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
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(YLOG_WARN|YLOG_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 (YLOG_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 (YLOG_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 (YLOG_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 || !yaz_matchstr (zm->maptab_name, "@"))
283 if (!(zm->maptab = chrmaptab_create (zms->tabpath,
286 yaz_log(YLOG_WARN, "Failed to read character table %s",
289 yaz_log(YLOG_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_output(ZebraMaps zms, unsigned reg_id,
312 chrmaptab maptab = zebra_charmap_get (zms, reg_id);
315 return chr_map_output (maptab, from, 1);
319 /* ------------------------------------ */
325 Z_AttributeElement **attributeList;
329 static int attr_find (AttrType *src, oid_value *attributeSetP)
331 while (src->major < src->num_attributes)
333 Z_AttributeElement *element;
335 element = src->attributeList[src->major];
336 if (src->type == *element->attributeType)
338 switch (element->which)
340 case Z_AttributeValue_numeric:
342 if (element->attributeSet && attributeSetP)
346 attrset = oid_getentbyoid (element->attributeSet);
347 *attributeSetP = attrset->value;
349 return *element->value.numeric;
351 case Z_AttributeValue_complex:
352 if (src->minor >= element->value.complex->num_list ||
353 element->value.complex->list[src->minor]->which !=
354 Z_StringOrNumeric_numeric)
357 if (element->attributeSet && attributeSetP)
361 attrset = oid_getentbyoid (element->attributeSet);
362 *attributeSetP = attrset->value;
364 return *element->value.complex->list[src->minor-1]->u.numeric;
374 static void attr_init_APT (AttrType *src, Z_AttributesPlusTerm *zapt, int type)
376 src->attributeList = zapt->attributes->attributes;
377 src->num_attributes = zapt->attributes->num_attributes;
383 static void attr_init_AttrList (AttrType *src, Z_AttributeList *list, int type)
385 src->attributeList = list->attributes;
386 src->num_attributes = list->num_attributes;
392 /* ------------------------------------ */
394 int zebra_maps_is_complete (ZebraMaps zms, unsigned reg_id)
396 struct zebra_map *zm = zebra_map_get (zms, reg_id);
398 return zm->completeness;
402 int zebra_maps_is_positioned (ZebraMaps zms, unsigned reg_id)
404 struct zebra_map *zm = zebra_map_get (zms, reg_id);
406 return zm->positioned;
410 int zebra_maps_is_sort (ZebraMaps zms, unsigned reg_id)
412 struct zebra_map *zm = zebra_map_get (zms, reg_id);
414 return zm->type == ZEBRA_MAP_TYPE_SORT;
418 int zebra_maps_sort (ZebraMaps zms, Z_SortAttributes *sortAttributes,
424 attr_init_AttrList (&use, sortAttributes->list, 1);
425 attr_init_AttrList (&structure, sortAttributes->list, 4);
428 structure_value = attr_find (&structure, 0);
429 if (structure_value == 109)
431 return attr_find (&use, NULL);
434 int zebra_maps_attr (ZebraMaps zms, Z_AttributesPlusTerm *zapt,
435 unsigned *reg_id, char **search_type, char *rank_type,
436 int *complete_flag, int *sort_flag)
438 AttrType completeness;
441 AttrType sort_relation;
444 int completeness_value;
447 int sort_relation_value;
451 attr_init_APT (&structure, zapt, 4);
452 attr_init_APT (&completeness, zapt, 6);
453 attr_init_APT (&relation, zapt, 2);
454 attr_init_APT (&sort_relation, zapt, 7);
455 attr_init_APT (&weight, zapt, 9);
456 attr_init_APT (&use, zapt, 1);
458 completeness_value = attr_find (&completeness, NULL);
459 structure_value = attr_find (&structure, NULL);
460 relation_value = attr_find (&relation, NULL);
461 sort_relation_value = attr_find (&sort_relation, NULL);
462 weight_value = attr_find (&weight, NULL);
463 use_value = attr_find(&use, NULL);
465 if (completeness_value == 2 || completeness_value == 3)
471 *sort_flag = (sort_relation_value > 0) ? 1 : 0;
472 *search_type = "phrase";
473 strcpy (rank_type, "void");
474 if (relation_value == 102)
476 if (weight_value == -1)
478 sprintf (rank_type, "rank,w=%d,u=%d", weight_value, use_value);
480 if (relation_value == 103)
482 *search_type = "always";
489 switch (structure_value)
491 case 6: /* word list */
492 *search_type = "and-list";
494 case 105: /* free-form-text */
495 *search_type = "or-list";
497 case 106: /* document-text */
498 *search_type = "or-list";
503 case 108: /* string */
504 *search_type = "phrase";
506 case 107: /* local-number */
507 *search_type = "local";
510 case 109: /* numeric string */
512 *search_type = "numeric";
516 *search_type = "phrase";
520 *search_type = "phrase";
524 *search_type = "phrase";
528 *search_type = "phrase";
536 int zebra_replace_sub(ZebraMaps zms, unsigned reg_id, const char *ex_list,
537 const char *input_str, int input_len, WRBUF wrbuf);
539 WRBUF zebra_replace(ZebraMaps zms, unsigned reg_id, const char *ex_list,
540 const char *input_str, int input_len)
542 struct zebra_map *zm = zebra_map_get (zms, reg_id);
544 wrbuf_rewind(zms->wrbuf_1);
545 wrbuf_write(zms->wrbuf_1, input_str, input_len);
546 if (!zm || !zm->replace_tokens)
550 yaz_log (YLOG_LOG, "in:%.*s:", wrbuf_len(zms->wrbuf_1),
551 wrbuf_buf(zms->wrbuf_1));
555 if (!zebra_replace_sub(zms, reg_id, ex_list, wrbuf_buf(zms->wrbuf_1),
556 wrbuf_len(zms->wrbuf_1), zms->wrbuf_2))
558 if (!zebra_replace_sub(zms, reg_id, ex_list, wrbuf_buf(zms->wrbuf_2),
559 wrbuf_len(zms->wrbuf_2), zms->wrbuf_1))
565 int zebra_replace_sub(ZebraMaps zms, unsigned reg_id, const char *ex_list,
566 const char *input_str, int input_len, WRBUF wrbuf)
570 struct zebra_map *zm = zebra_map_get (zms, reg_id);
573 for (i = -1; i <= input_len; )
575 struct zm_token *token;
576 char replace_string[128];
580 for (token = zm->replace_tokens; !replace_in && token;
584 int replace_done = 0;
589 if (!token->token_from[j])
594 if (ex_list && strchr (ex_list, token->token_from[j]))
596 if (i+j < 0 || j+i >= input_len)
599 c = input_str[j+i] & 255;
600 if (token->token_from[j] == ZEBRA_REPLACE_ANY)
604 replace_string[replace_out++] = c;
608 if (c != token->token_from[j])
614 const char *cp = token->token_to;
616 for (; cp && *cp; cp++)
617 replace_string[replace_out++] = *cp;
624 if (i >= 0 && i < input_len)
625 wrbuf_putc(wrbuf, input_str[i]);
632 wrbuf_write(wrbuf, replace_string, replace_out);
637 yaz_log (YLOG_LOG, "out:%.*s:", wrbuf_len(wrbuf), wrbuf_buf(wrbuf));