2 * Copyright (C) 1994-1998, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.7 1998-02-10 12:03:07 adam
10 * Revision 1.6 1998/01/29 13:36:01 adam
11 * Structure word-list, free-form-text and document-text all
12 * trigger ranked search.
14 * Revision 1.5 1997/11/19 10:22:14 adam
15 * Bug fix (introduced by previous commit).
17 * Revision 1.4 1997/11/18 10:05:08 adam
18 * Changed character map facility so that admin can specify character
19 * mapping files for each register type, w, p, etc.
21 * Revision 1.3 1997/11/17 15:35:26 adam
22 * Bug fix. Relation=relevance wasn't observed.
24 * Revision 1.2 1997/10/31 12:39:30 adam
25 * Changed log message.
27 * Revision 1.1 1997/10/27 14:33:06 adam
28 * Moved towards generic character mapping depending on "structure"
29 * field in abstract syntax file. Fixed a few memory leaks. Fixed
30 * bug with negative integers when doing searches with relational
47 const char *maptab_name;
48 struct zebra_map *next;
54 struct zebra_map *map_list;
56 const char *temp_map_ptr[2];
57 struct zebra_map **lookup_array;
60 void zebra_maps_close (ZebraMaps zms)
62 struct zebra_map *zm = zms->map_list;
66 chrmaptab_destroy (zm->maptab);
69 nmem_destroy (zms->nmem);
73 static void zebra_map_read (ZebraMaps zms, const char *name)
79 struct zebra_map **zm = 0, *zp;
81 if (!(f = yaz_path_fopen(zms->tabpath, name, "r")))
83 logf(LOG_WARN|LOG_ERRNO, "%s", name);
86 while ((argc = readconf_line(f, line, 512, argv, 10)))
88 if (!strcmp (argv[0], "index") && argc == 2)
94 *zm = nmem_malloc (zms->nmem, sizeof(**zm));
95 (*zm)->reg_type = argv[1][0];
96 (*zm)->maptab_name = NULL;
99 (*zm)->completeness = 0;
101 else if (!strcmp (argv[0], "sort") && argc == 2)
107 *zm = nmem_malloc (zms->nmem, sizeof(**zm));
108 (*zm)->reg_type = argv[1][0];
109 (*zm)->maptab_name = NULL;
110 (*zm)->sort_flag = 1;
111 (*zm)->maptab = NULL;
112 (*zm)->completeness = 0;
114 else if (zm && !strcmp (argv[0], "charmap") && argc == 2)
116 (*zm)->maptab_name = nmem_strdup (zms->nmem, argv[1]);
118 else if (zm && !strcmp (argv[0], "completeness") && argc == 2)
120 (*zm)->completeness = atoi (argv[1]);
127 for (zp = zms->map_list; zp; zp = zp->next)
128 zms->lookup_array[zp->reg_type & 255] = zp;
131 static void zms_map_handle (void *p, const char *name, const char *value)
135 zebra_map_read (zms, value);
138 ZebraMaps zebra_maps_open (const char *tabpath, Res res)
140 ZebraMaps zms = xmalloc (sizeof(*zms));
143 zms->nmem = nmem_create ();
144 zms->tabpath = nmem_strdup (zms->nmem, tabpath);
145 zms->map_list = NULL;
147 zms->temp_map_str[0] = '\0';
148 zms->temp_map_str[1] = '\0';
150 zms->temp_map_ptr[0] = zms->temp_map_str;
151 zms->temp_map_ptr[1] = NULL;
154 nmem_malloc (zms->nmem, sizeof(*zms->lookup_array)*256);
155 for (i = 0; i<256; i++)
156 zms->lookup_array[i] = 0;
157 if (!res_trav (res, "index", zms, zms_map_handle))
158 zebra_map_read (zms, "default.idx");
162 struct zebra_map *zebra_map_get (ZebraMaps zms, int reg_type)
164 return zms->lookup_array[reg_type];
167 chrmaptab zebra_charmap_get (ZebraMaps zms, int reg_type)
169 struct zebra_map *zm = zebra_map_get (zms, reg_type);
172 logf (LOG_WARN, "Unknown register type: %c", reg_type);
177 if (!strcmp (zm->maptab_name, "@"))
179 if (!(zm->maptab = chrmaptab_create (zms->tabpath,
180 zm->maptab_name, 0)))
181 logf(LOG_WARN, "Failed to read character table %s",
184 logf(LOG_DEBUG, "Read character table %s", zm->maptab_name);
189 const char **zebra_maps_input (ZebraMaps zms, int reg_type,
190 const char **from, int len)
194 maptab = zebra_charmap_get (zms, reg_type);
196 return chr_map_input(maptab, from, len);
198 zms->temp_map_str[0] = **from;
201 return zms->temp_map_ptr;
204 const char *zebra_maps_output(ZebraMaps zms, int reg_type, const char **from)
207 unsigned char i = (unsigned char) **from;
208 static char buf[2] = {0,0};
210 maptab = zebra_charmap_get (zms, reg_type);
212 return chr_map_output (maptab, from, 1);
219 /* ------------------------------------ */
225 Z_AttributeElement **attributeList;
229 static int attr_find (AttrType *src, oid_value *attributeSetP)
231 while (src->major < src->num_attributes)
233 Z_AttributeElement *element;
235 element = src->attributeList[src->major];
236 if (src->type == *element->attributeType)
238 switch (element->which)
240 case Z_AttributeValue_numeric:
242 if (element->attributeSet && attributeSetP)
246 attrset = oid_getentbyoid (element->attributeSet);
247 *attributeSetP = attrset->value;
249 return *element->value.numeric;
251 case Z_AttributeValue_complex:
252 if (src->minor >= element->value.complex->num_list ||
253 element->value.complex->list[src->minor]->which !=
254 Z_StringOrNumeric_numeric)
257 if (element->attributeSet && attributeSetP)
261 attrset = oid_getentbyoid (element->attributeSet);
262 *attributeSetP = attrset->value;
264 return *element->value.complex->list[src->minor-1]->u.numeric;
274 static void attr_init_APT (AttrType *src, Z_AttributesPlusTerm *zapt, int type)
277 src->attributeList = zapt->attributeList;
278 src->num_attributes = zapt->num_attributes;
284 static void attr_init_AttrList (AttrType *src, Z_AttributeList *list, int type)
286 src->attributeList = list->attributes;
287 src->num_attributes = list->num_attributes;
293 /* ------------------------------------ */
295 int zebra_maps_is_complete (ZebraMaps zms, int reg_type)
297 struct zebra_map *zm = zebra_map_get (zms, reg_type);
299 return zm->completeness;
303 int zebra_maps_is_sort (ZebraMaps zms, int reg_type)
305 struct zebra_map *zm = zebra_map_get (zms, reg_type);
307 return zm->sort_flag;
311 int zebra_maps_sort (ZebraMaps zms, Z_SortAttributes *sortAttributes)
314 attr_init_AttrList (&use, sortAttributes->list, 1);
316 return attr_find (&use, NULL);
319 int zebra_maps_attr (ZebraMaps zms, Z_AttributesPlusTerm *zapt,
320 int *reg_type, char **search_type, int *complete_flag)
322 AttrType completeness;
325 int completeness_value;
329 attr_init_APT (&structure, zapt, 4);
330 attr_init_APT (&completeness, zapt, 6);
331 attr_init_APT (&relation, zapt, 2);
333 completeness_value = attr_find (&completeness, NULL);
334 structure_value = attr_find (&structure, NULL);
335 relation_value = attr_find (&relation, NULL);
337 if (completeness_value == 2 || completeness_value == 3)
343 *search_type = "phrase";
344 if (relation_value == 102)
345 *search_type = "ranked";
347 switch (structure_value)
349 case 6: /* word list */
350 case 105: /* free-form-text */
351 case 106: /* document-text */
352 *search_type = "ranked";
359 case 108: /* string */
365 case 107: /* local-number */
366 *search_type = "local";
368 case 109: /* numeric string */