1 /* $Id: zebramap.c,v 1.30 2003-03-26 16:41:48 adam Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003
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;
307 const char *zebra_maps_output(ZebraMaps zms, unsigned reg_id,
310 chrmaptab maptab = zebra_charmap_get (zms, reg_id);
313 return chr_map_output (maptab, from, 1);
317 /* ------------------------------------ */
323 Z_AttributeElement **attributeList;
327 static int attr_find (AttrType *src, oid_value *attributeSetP)
329 while (src->major < src->num_attributes)
331 Z_AttributeElement *element;
333 element = src->attributeList[src->major];
334 if (src->type == *element->attributeType)
336 switch (element->which)
338 case Z_AttributeValue_numeric:
340 if (element->attributeSet && attributeSetP)
344 attrset = oid_getentbyoid (element->attributeSet);
345 *attributeSetP = attrset->value;
347 return *element->value.numeric;
349 case Z_AttributeValue_complex:
350 if (src->minor >= element->value.complex->num_list ||
351 element->value.complex->list[src->minor]->which !=
352 Z_StringOrNumeric_numeric)
355 if (element->attributeSet && attributeSetP)
359 attrset = oid_getentbyoid (element->attributeSet);
360 *attributeSetP = attrset->value;
362 return *element->value.complex->list[src->minor-1]->u.numeric;
372 static void attr_init_APT (AttrType *src, Z_AttributesPlusTerm *zapt, int type)
374 src->attributeList = zapt->attributes->attributes;
375 src->num_attributes = zapt->attributes->num_attributes;
381 static void attr_init_AttrList (AttrType *src, Z_AttributeList *list, int type)
383 src->attributeList = list->attributes;
384 src->num_attributes = list->num_attributes;
390 /* ------------------------------------ */
392 int zebra_maps_is_complete (ZebraMaps zms, unsigned reg_id)
394 struct zebra_map *zm = zebra_map_get (zms, reg_id);
396 return zm->completeness;
400 int zebra_maps_is_positioned (ZebraMaps zms, unsigned reg_id)
402 struct zebra_map *zm = zebra_map_get (zms, reg_id);
404 return zm->positioned;
408 int zebra_maps_is_sort (ZebraMaps zms, unsigned reg_id)
410 struct zebra_map *zm = zebra_map_get (zms, reg_id);
412 return zm->type == ZEBRA_MAP_TYPE_SORT;
416 int zebra_maps_sort (ZebraMaps zms, Z_SortAttributes *sortAttributes,
422 attr_init_AttrList (&use, sortAttributes->list, 1);
423 attr_init_AttrList (&structure, sortAttributes->list, 4);
426 structure_value = attr_find (&structure, 0);
427 if (structure_value == 109)
429 return attr_find (&use, NULL);
432 int zebra_maps_attr (ZebraMaps zms, Z_AttributesPlusTerm *zapt,
433 unsigned *reg_id, char **search_type, char *rank_type,
434 int *complete_flag, int *sort_flag)
436 AttrType completeness;
439 AttrType sort_relation;
442 int completeness_value;
445 int sort_relation_value;
449 attr_init_APT (&structure, zapt, 4);
450 attr_init_APT (&completeness, zapt, 6);
451 attr_init_APT (&relation, zapt, 2);
452 attr_init_APT (&sort_relation, zapt, 7);
453 attr_init_APT (&weight, zapt, 9);
454 attr_init_APT (&use, zapt, 1);
456 completeness_value = attr_find (&completeness, NULL);
457 structure_value = attr_find (&structure, NULL);
458 relation_value = attr_find (&relation, NULL);
459 sort_relation_value = attr_find (&sort_relation, NULL);
460 weight_value = attr_find (&weight, NULL);
461 use_value = attr_find(&use, NULL);
463 if (completeness_value == 2 || completeness_value == 3)
469 *sort_flag = (sort_relation_value > 0) ? 1 : 0;
470 *search_type = "phrase";
471 strcpy (rank_type, "void");
472 if (relation_value == 102)
474 if (weight_value == -1)
476 sprintf (rank_type, "rank,w=%d,u=%d", weight_value, use_value);
478 if (relation_value == 103)
480 *search_type = "always";
487 switch (structure_value)
489 case 6: /* word list */
490 *search_type = "and-list";
492 case 105: /* free-form-text */
493 *search_type = "or-list";
495 case 106: /* document-text */
496 *search_type = "or-list";
501 case 108: /* string */
502 *search_type = "phrase";
504 case 107: /* local-number */
505 *search_type = "local";
508 case 109: /* numeric string */
510 *search_type = "numeric";
514 *search_type = "phrase";
518 *search_type = "phrase";
522 *search_type = "phrase";
526 *search_type = "phrase";
534 int zebra_replace_sub(ZebraMaps zms, unsigned reg_id, const char *ex_list,
535 const char *input_str, int input_len, WRBUF wrbuf);
537 WRBUF zebra_replace(ZebraMaps zms, unsigned reg_id, const char *ex_list,
538 const char *input_str, int input_len)
540 struct zebra_map *zm = zebra_map_get (zms, reg_id);
542 wrbuf_rewind(zms->wrbuf_1);
543 wrbuf_write(zms->wrbuf_1, input_str, input_len);
544 if (!zm || !zm->replace_tokens)
548 logf (LOG_LOG, "in:%.*s:", wrbuf_len(zms->wrbuf_1),
549 wrbuf_buf(zms->wrbuf_1));
553 if (!zebra_replace_sub(zms, reg_id, ex_list, wrbuf_buf(zms->wrbuf_1),
554 wrbuf_len(zms->wrbuf_1), zms->wrbuf_2))
556 if (!zebra_replace_sub(zms, reg_id, ex_list, wrbuf_buf(zms->wrbuf_2),
557 wrbuf_len(zms->wrbuf_2), zms->wrbuf_1))
563 int zebra_replace_sub(ZebraMaps zms, unsigned reg_id, const char *ex_list,
564 const char *input_str, int input_len, WRBUF wrbuf)
568 struct zebra_map *zm = zebra_map_get (zms, reg_id);
571 for (i = -1; i <= input_len; )
573 struct zm_token *token;
574 char replace_string[128];
578 for (token = zm->replace_tokens; !replace_in && token;
582 int replace_done = 0;
587 if (!token->token_from[j])
592 if (ex_list && strchr (ex_list, token->token_from[j]))
594 if (i+j < 0 || j+i >= input_len)
597 c = input_str[j+i] & 255;
598 if (token->token_from[j] == ZEBRA_REPLACE_ANY)
602 replace_string[replace_out++] = c;
606 if (c != token->token_from[j])
612 const char *cp = token->token_to;
614 for (; cp && *cp; cp++)
615 replace_string[replace_out++] = *cp;
622 if (i >= 0 && i < input_len)
623 wrbuf_putc(wrbuf, input_str[i]);
630 wrbuf_write(wrbuf, replace_string, replace_out);
635 logf (LOG_LOG, "out:%.*s:", wrbuf_len(wrbuf), wrbuf_buf(wrbuf));