1 /* $Id: zebramap.c,v 1.36 2004-12-10 11:56:23 heikki 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 /* Old yaz-util includes */
30 #include <yaz/yconfig.h>
31 #include <yaz/yaz-version.h>
32 #include <yaz/xmalloc.h>
34 #include <yaz/tpath.h>
35 #include <yaz/options.h>
36 #include <yaz/wrbuf.h>
38 #include <yaz/readconf.h>
39 #include <yaz/marcdisp.h>
40 #include <yaz/yaz-iconv.h>
42 #include <idzebra/zebramap.h>
44 #define ZEBRA_MAP_TYPE_SORT 1
45 #define ZEBRA_MAP_TYPE_INDEX 2
47 #define ZEBRA_REPLACE_ANY 300
53 struct zm_token *next;
70 const char *maptab_name;
71 struct zebra_map *next;
72 struct zm_token *replace_tokens;
79 struct zebra_map *map_list;
81 const char *temp_map_ptr[2];
82 struct zebra_map **lookup_array;
83 WRBUF wrbuf_1, wrbuf_2;
86 void zebra_maps_close (ZebraMaps zms)
88 struct zebra_map *zm = zms->map_list;
92 chrmaptab_destroy (zm->maptab);
95 wrbuf_free (zms->wrbuf_1, 1);
96 wrbuf_free (zms->wrbuf_2, 1);
97 nmem_destroy (zms->nmem);
101 static void zebra_map_read (ZebraMaps zms, const char *name)
108 struct zebra_map **zm = 0, *zp;
110 if (!(f = yaz_fopen(zms->tabpath, name, "r", zms->tabroot)))
112 yaz_log(YLOG_WARN|YLOG_ERRNO, "%s", name);
115 while ((argc = readconf_line(f, &lineno, line, 512, argv, 10)))
117 if (!yaz_matchstr (argv[0], "index") && argc == 2)
123 *zm = (struct zebra_map *) nmem_malloc (zms->nmem, sizeof(**zm));
124 (*zm)->reg_id = argv[1][0];
125 (*zm)->maptab_name = NULL;
126 (*zm)->maptab = NULL;
127 (*zm)->type = ZEBRA_MAP_TYPE_INDEX;
128 (*zm)->completeness = 0;
129 (*zm)->positioned = 1;
130 (*zm)->replace_tokens = 0;
132 else if (!yaz_matchstr (argv[0], "sort") && argc == 2)
138 *zm = (struct zebra_map *) nmem_malloc (zms->nmem, sizeof(**zm));
139 (*zm)->reg_id = argv[1][0];
140 (*zm)->maptab_name = NULL;
141 (*zm)->type = ZEBRA_MAP_TYPE_SORT;
142 (*zm)->u.sort.entry_size = 80;
143 (*zm)->maptab = NULL;
144 (*zm)->completeness = 0;
145 (*zm)->positioned = 0;
146 (*zm)->replace_tokens = 0;
148 else if (zm && !yaz_matchstr (argv[0], "charmap") && argc == 2)
150 (*zm)->maptab_name = nmem_strdup (zms->nmem, argv[1]);
152 else if (zm && !yaz_matchstr (argv[0], "completeness") && argc == 2)
154 (*zm)->completeness = atoi (argv[1]);
156 else if (zm && !yaz_matchstr (argv[0], "position") && argc == 2)
158 (*zm)->positioned = atoi (argv[1]);
160 else if (zm && !yaz_matchstr (argv[0], "entrysize") && argc == 2)
162 if ((*zm)->type == ZEBRA_MAP_TYPE_SORT)
163 (*zm)->u.sort.entry_size = atoi (argv[1]);
165 else if (zm && !yaz_matchstr (argv[0], "replace") && argc >= 2)
167 struct zm_token *token = nmem_malloc (zms->nmem, sizeof(*token));
168 token->next = (*zm)->replace_tokens;
169 (*zm)->replace_tokens = token;
171 yaz_log (YLOG_LOG, "replace %s", argv[1]);
173 token->token_from = 0;
177 int *dp = token->token_from = (int *)
178 nmem_malloc (zms->nmem, (1+strlen(cp))*sizeof(int));
187 *dp++ = ZEBRA_REPLACE_ANY;
192 *dp++ = zebra_prim(&cp);
194 yaz_log (YLOG_LOG, " char %2X %c", dp[-1], dp[-1]);
202 char *dp = token->token_to =
203 nmem_malloc (zms->nmem, strlen(cp)+1);
211 *dp++ = zebra_prim(&cp);
222 for (zp = zms->map_list; zp; zp = zp->next)
223 zms->lookup_array[zp->reg_id] = zp;
226 static void zms_map_handle (void *p, const char *name, const char *value)
228 ZebraMaps zms = (ZebraMaps) p;
230 zebra_map_read (zms, value);
233 ZebraMaps zebra_maps_open (Res res, const char *base)
235 ZebraMaps zms = (ZebraMaps) xmalloc (sizeof(*zms));
238 zms->nmem = nmem_create ();
239 zms->tabpath = nmem_strdup (zms->nmem,
240 res_get_def (res, "profilePath",
241 DEFAULT_PROFILE_PATH));
244 zms->tabroot = nmem_strdup (zms->nmem, base);
245 zms->map_list = NULL;
247 zms->temp_map_str[0] = '\0';
248 zms->temp_map_str[1] = '\0';
250 zms->temp_map_ptr[0] = zms->temp_map_str;
251 zms->temp_map_ptr[1] = NULL;
253 zms->lookup_array = (struct zebra_map**)
254 nmem_malloc (zms->nmem, sizeof(*zms->lookup_array)*256);
255 for (i = 0; i<256; i++)
256 zms->lookup_array[i] = 0;
257 if (!res || !res_trav (res, "index", zms, zms_map_handle))
258 zebra_map_read (zms, "default.idx");
260 zms->wrbuf_1 = wrbuf_alloc();
261 zms->wrbuf_2 = wrbuf_alloc();
266 struct zebra_map *zebra_map_get (ZebraMaps zms, unsigned reg_id)
268 return zms->lookup_array[reg_id];
271 chrmaptab zebra_charmap_get (ZebraMaps zms, unsigned reg_id)
273 struct zebra_map *zm = zebra_map_get (zms, reg_id);
276 zm = (struct zebra_map *) nmem_malloc (zms->nmem, sizeof(*zm));
277 yaz_log (YLOG_WARN, "Unknown register type: %c", reg_id);
280 zm->maptab_name = nmem_strdup (zms->nmem, "@");
282 zm->type = ZEBRA_MAP_TYPE_INDEX;
283 zm->completeness = 0;
284 zm->next = zms->map_list;
285 zm->replace_tokens = 0;
286 zms->map_list = zm->next;
288 zms->lookup_array[zm->reg_id & 255] = zm;
292 if (!zm->maptab_name || !yaz_matchstr (zm->maptab_name, "@"))
294 if (!(zm->maptab = chrmaptab_create (zms->tabpath,
297 yaz_log(YLOG_WARN, "Failed to read character table %s",
300 yaz_log(YLOG_DEBUG, "Read character table %s", zm->maptab_name);
305 const char **zebra_maps_input (ZebraMaps zms, unsigned reg_id,
306 const char **from, int len, int first)
310 maptab = zebra_charmap_get (zms, reg_id);
312 return chr_map_input(maptab, from, len, first);
314 zms->temp_map_str[0] = **from;
317 return zms->temp_map_ptr;
320 const char *zebra_maps_output(ZebraMaps zms, unsigned reg_id,
323 chrmaptab maptab = zebra_charmap_get (zms, reg_id);
326 return chr_map_output (maptab, from, 1);
330 /* ------------------------------------ */
336 Z_AttributeElement **attributeList;
340 static int attr_find (AttrType *src, oid_value *attributeSetP)
342 while (src->major < src->num_attributes)
344 Z_AttributeElement *element;
346 element = src->attributeList[src->major];
347 if (src->type == *element->attributeType)
349 switch (element->which)
351 case Z_AttributeValue_numeric:
353 if (element->attributeSet && attributeSetP)
357 attrset = oid_getentbyoid (element->attributeSet);
358 *attributeSetP = attrset->value;
360 return *element->value.numeric;
362 case Z_AttributeValue_complex:
363 if (src->minor >= element->value.complex->num_list ||
364 element->value.complex->list[src->minor]->which !=
365 Z_StringOrNumeric_numeric)
368 if (element->attributeSet && attributeSetP)
372 attrset = oid_getentbyoid (element->attributeSet);
373 *attributeSetP = attrset->value;
375 return *element->value.complex->list[src->minor-1]->u.numeric;
385 static void attr_init_APT (AttrType *src, Z_AttributesPlusTerm *zapt, int type)
387 src->attributeList = zapt->attributes->attributes;
388 src->num_attributes = zapt->attributes->num_attributes;
394 static void attr_init_AttrList (AttrType *src, Z_AttributeList *list, int type)
396 src->attributeList = list->attributes;
397 src->num_attributes = list->num_attributes;
403 /* ------------------------------------ */
405 int zebra_maps_is_complete (ZebraMaps zms, unsigned reg_id)
407 struct zebra_map *zm = zebra_map_get (zms, reg_id);
409 return zm->completeness;
413 int zebra_maps_is_positioned (ZebraMaps zms, unsigned reg_id)
415 struct zebra_map *zm = zebra_map_get (zms, reg_id);
417 return zm->positioned;
421 int zebra_maps_is_sort (ZebraMaps zms, unsigned reg_id)
423 struct zebra_map *zm = zebra_map_get (zms, reg_id);
425 return zm->type == ZEBRA_MAP_TYPE_SORT;
429 int zebra_maps_sort (ZebraMaps zms, Z_SortAttributes *sortAttributes,
435 attr_init_AttrList (&use, sortAttributes->list, 1);
436 attr_init_AttrList (&structure, sortAttributes->list, 4);
439 structure_value = attr_find (&structure, 0);
440 if (structure_value == 109)
442 return attr_find (&use, NULL);
445 int zebra_maps_attr (ZebraMaps zms, Z_AttributesPlusTerm *zapt,
446 unsigned *reg_id, char **search_type, char *rank_type,
447 int *complete_flag, int *sort_flag)
449 AttrType completeness;
452 AttrType sort_relation;
455 int completeness_value;
458 int sort_relation_value;
462 attr_init_APT (&structure, zapt, 4);
463 attr_init_APT (&completeness, zapt, 6);
464 attr_init_APT (&relation, zapt, 2);
465 attr_init_APT (&sort_relation, zapt, 7);
466 attr_init_APT (&weight, zapt, 9);
467 attr_init_APT (&use, zapt, 1);
469 completeness_value = attr_find (&completeness, NULL);
470 structure_value = attr_find (&structure, NULL);
471 relation_value = attr_find (&relation, NULL);
472 sort_relation_value = attr_find (&sort_relation, NULL);
473 weight_value = attr_find (&weight, NULL);
474 use_value = attr_find(&use, NULL);
476 if (completeness_value == 2 || completeness_value == 3)
482 *sort_flag = (sort_relation_value > 0) ? 1 : 0;
483 *search_type = "phrase";
484 strcpy (rank_type, "void");
485 if (relation_value == 102)
487 if (weight_value == -1)
489 sprintf (rank_type, "rank,w=%d,u=%d", weight_value, use_value);
491 if (relation_value == 103)
493 *search_type = "always";
500 switch (structure_value)
502 case 6: /* word list */
503 *search_type = "and-list";
505 case 105: /* free-form-text */
506 *search_type = "or-list";
508 case 106: /* document-text */
509 *search_type = "or-list";
514 case 108: /* string */
515 *search_type = "phrase";
517 case 107: /* local-number */
518 *search_type = "local";
521 case 109: /* numeric string */
523 *search_type = "numeric";
527 *search_type = "phrase";
531 *search_type = "phrase";
535 *search_type = "phrase";
539 *search_type = "phrase";
547 int zebra_replace_sub(ZebraMaps zms, unsigned reg_id, const char *ex_list,
548 const char *input_str, int input_len, WRBUF wrbuf);
550 WRBUF zebra_replace(ZebraMaps zms, unsigned reg_id, const char *ex_list,
551 const char *input_str, int input_len)
553 struct zebra_map *zm = zebra_map_get (zms, reg_id);
555 wrbuf_rewind(zms->wrbuf_1);
556 wrbuf_write(zms->wrbuf_1, input_str, input_len);
557 if (!zm || !zm->replace_tokens)
561 yaz_log (YLOG_LOG, "in:%.*s:", wrbuf_len(zms->wrbuf_1),
562 wrbuf_buf(zms->wrbuf_1));
566 if (!zebra_replace_sub(zms, reg_id, ex_list, wrbuf_buf(zms->wrbuf_1),
567 wrbuf_len(zms->wrbuf_1), zms->wrbuf_2))
569 if (!zebra_replace_sub(zms, reg_id, ex_list, wrbuf_buf(zms->wrbuf_2),
570 wrbuf_len(zms->wrbuf_2), zms->wrbuf_1))
576 int zebra_replace_sub(ZebraMaps zms, unsigned reg_id, const char *ex_list,
577 const char *input_str, int input_len, WRBUF wrbuf)
581 struct zebra_map *zm = zebra_map_get (zms, reg_id);
584 for (i = -1; i <= input_len; )
586 struct zm_token *token;
587 char replace_string[128];
591 for (token = zm->replace_tokens; !replace_in && token;
595 int replace_done = 0;
600 if (!token->token_from[j])
605 if (ex_list && strchr (ex_list, token->token_from[j]))
607 if (i+j < 0 || j+i >= input_len)
610 c = input_str[j+i] & 255;
611 if (token->token_from[j] == ZEBRA_REPLACE_ANY)
615 replace_string[replace_out++] = c;
619 if (c != token->token_from[j])
625 const char *cp = token->token_to;
627 for (; cp && *cp; cp++)
628 replace_string[replace_out++] = *cp;
635 if (i >= 0 && i < input_len)
636 wrbuf_putc(wrbuf, input_str[i]);
643 wrbuf_write(wrbuf, replace_string, replace_out);
648 yaz_log (YLOG_LOG, "out:%.*s:", wrbuf_len(wrbuf), wrbuf_buf(wrbuf));