1 /* $Id: zebramap.c,v 1.34 2004-09-28 12:39:55 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
28 #include <yaz/yaz-util.h>
30 #include <idzebra/zebramap.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 zm->replace_tokens = 0;
274 zms->map_list = zm->next;
276 zms->lookup_array[zm->reg_id & 255] = zm;
280 if (!zm->maptab_name || !yaz_matchstr (zm->maptab_name, "@"))
282 if (!(zm->maptab = chrmaptab_create (zms->tabpath,
285 logf(LOG_WARN, "Failed to read character table %s",
288 logf(LOG_DEBUG, "Read character table %s", zm->maptab_name);
293 const char **zebra_maps_input (ZebraMaps zms, unsigned reg_id,
294 const char **from, int len, int first)
298 maptab = zebra_charmap_get (zms, reg_id);
300 return chr_map_input(maptab, from, len, first);
302 zms->temp_map_str[0] = **from;
305 return zms->temp_map_ptr;
308 const char *zebra_maps_output(ZebraMaps zms, unsigned reg_id,
311 chrmaptab maptab = zebra_charmap_get (zms, reg_id);
314 return chr_map_output (maptab, from, 1);
318 /* ------------------------------------ */
324 Z_AttributeElement **attributeList;
328 static int attr_find (AttrType *src, oid_value *attributeSetP)
330 while (src->major < src->num_attributes)
332 Z_AttributeElement *element;
334 element = src->attributeList[src->major];
335 if (src->type == *element->attributeType)
337 switch (element->which)
339 case Z_AttributeValue_numeric:
341 if (element->attributeSet && attributeSetP)
345 attrset = oid_getentbyoid (element->attributeSet);
346 *attributeSetP = attrset->value;
348 return *element->value.numeric;
350 case Z_AttributeValue_complex:
351 if (src->minor >= element->value.complex->num_list ||
352 element->value.complex->list[src->minor]->which !=
353 Z_StringOrNumeric_numeric)
356 if (element->attributeSet && attributeSetP)
360 attrset = oid_getentbyoid (element->attributeSet);
361 *attributeSetP = attrset->value;
363 return *element->value.complex->list[src->minor-1]->u.numeric;
373 static void attr_init_APT (AttrType *src, Z_AttributesPlusTerm *zapt, int type)
375 src->attributeList = zapt->attributes->attributes;
376 src->num_attributes = zapt->attributes->num_attributes;
382 static void attr_init_AttrList (AttrType *src, Z_AttributeList *list, int type)
384 src->attributeList = list->attributes;
385 src->num_attributes = list->num_attributes;
391 /* ------------------------------------ */
393 int zebra_maps_is_complete (ZebraMaps zms, unsigned reg_id)
395 struct zebra_map *zm = zebra_map_get (zms, reg_id);
397 return zm->completeness;
401 int zebra_maps_is_positioned (ZebraMaps zms, unsigned reg_id)
403 struct zebra_map *zm = zebra_map_get (zms, reg_id);
405 return zm->positioned;
409 int zebra_maps_is_sort (ZebraMaps zms, unsigned reg_id)
411 struct zebra_map *zm = zebra_map_get (zms, reg_id);
413 return zm->type == ZEBRA_MAP_TYPE_SORT;
417 int zebra_maps_sort (ZebraMaps zms, Z_SortAttributes *sortAttributes,
423 attr_init_AttrList (&use, sortAttributes->list, 1);
424 attr_init_AttrList (&structure, sortAttributes->list, 4);
427 structure_value = attr_find (&structure, 0);
428 if (structure_value == 109)
430 return attr_find (&use, NULL);
433 int zebra_maps_attr (ZebraMaps zms, Z_AttributesPlusTerm *zapt,
434 unsigned *reg_id, char **search_type, char *rank_type,
435 int *complete_flag, int *sort_flag)
437 AttrType completeness;
440 AttrType sort_relation;
443 int completeness_value;
446 int sort_relation_value;
450 attr_init_APT (&structure, zapt, 4);
451 attr_init_APT (&completeness, zapt, 6);
452 attr_init_APT (&relation, zapt, 2);
453 attr_init_APT (&sort_relation, zapt, 7);
454 attr_init_APT (&weight, zapt, 9);
455 attr_init_APT (&use, zapt, 1);
457 completeness_value = attr_find (&completeness, NULL);
458 structure_value = attr_find (&structure, NULL);
459 relation_value = attr_find (&relation, NULL);
460 sort_relation_value = attr_find (&sort_relation, NULL);
461 weight_value = attr_find (&weight, NULL);
462 use_value = attr_find(&use, NULL);
464 if (completeness_value == 2 || completeness_value == 3)
470 *sort_flag = (sort_relation_value > 0) ? 1 : 0;
471 *search_type = "phrase";
472 strcpy (rank_type, "void");
473 if (relation_value == 102)
475 if (weight_value == -1)
477 sprintf (rank_type, "rank,w=%d,u=%d", weight_value, use_value);
479 if (relation_value == 103)
481 *search_type = "always";
488 switch (structure_value)
490 case 6: /* word list */
491 *search_type = "and-list";
493 case 105: /* free-form-text */
494 *search_type = "or-list";
496 case 106: /* document-text */
497 *search_type = "or-list";
502 case 108: /* string */
503 *search_type = "phrase";
505 case 107: /* local-number */
506 *search_type = "local";
509 case 109: /* numeric string */
511 *search_type = "numeric";
515 *search_type = "phrase";
519 *search_type = "phrase";
523 *search_type = "phrase";
527 *search_type = "phrase";
535 int zebra_replace_sub(ZebraMaps zms, unsigned reg_id, const char *ex_list,
536 const char *input_str, int input_len, WRBUF wrbuf);
538 WRBUF zebra_replace(ZebraMaps zms, unsigned reg_id, const char *ex_list,
539 const char *input_str, int input_len)
541 struct zebra_map *zm = zebra_map_get (zms, reg_id);
543 wrbuf_rewind(zms->wrbuf_1);
544 wrbuf_write(zms->wrbuf_1, input_str, input_len);
545 if (!zm || !zm->replace_tokens)
549 logf (LOG_LOG, "in:%.*s:", wrbuf_len(zms->wrbuf_1),
550 wrbuf_buf(zms->wrbuf_1));
554 if (!zebra_replace_sub(zms, reg_id, ex_list, wrbuf_buf(zms->wrbuf_1),
555 wrbuf_len(zms->wrbuf_1), zms->wrbuf_2))
557 if (!zebra_replace_sub(zms, reg_id, ex_list, wrbuf_buf(zms->wrbuf_2),
558 wrbuf_len(zms->wrbuf_2), zms->wrbuf_1))
564 int zebra_replace_sub(ZebraMaps zms, unsigned reg_id, const char *ex_list,
565 const char *input_str, int input_len, WRBUF wrbuf)
569 struct zebra_map *zm = zebra_map_get (zms, reg_id);
572 for (i = -1; i <= input_len; )
574 struct zm_token *token;
575 char replace_string[128];
579 for (token = zm->replace_tokens; !replace_in && token;
583 int replace_done = 0;
588 if (!token->token_from[j])
593 if (ex_list && strchr (ex_list, token->token_from[j]))
595 if (i+j < 0 || j+i >= input_len)
598 c = input_str[j+i] & 255;
599 if (token->token_from[j] == ZEBRA_REPLACE_ANY)
603 replace_string[replace_out++] = c;
607 if (c != token->token_from[j])
613 const char *cp = token->token_to;
615 for (; cp && *cp; cp++)
616 replace_string[replace_out++] = *cp;
623 if (i >= 0 && i < input_len)
624 wrbuf_putc(wrbuf, input_str[i]);
631 wrbuf_write(wrbuf, replace_string, replace_out);
636 logf (LOG_LOG, "out:%.*s:", wrbuf_len(wrbuf), wrbuf_buf(wrbuf));