2 * Copyright (C) 1994-1998, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.6 1998-01-29 13:36:01 adam
8 * Structure word-list, free-form-text and document-text all
9 * trigger ranked search.
11 * Revision 1.5 1997/11/19 10:22:14 adam
12 * Bug fix (introduced by previous commit).
14 * Revision 1.4 1997/11/18 10:05:08 adam
15 * Changed character map facility so that admin can specify character
16 * mapping files for each register type, w, p, etc.
18 * Revision 1.3 1997/11/17 15:35:26 adam
19 * Bug fix. Relation=relevance wasn't observed.
21 * Revision 1.2 1997/10/31 12:39:30 adam
22 * Changed log message.
24 * Revision 1.1 1997/10/27 14:33:06 adam
25 * Moved towards generic character mapping depending on "structure"
26 * field in abstract syntax file. Fixed a few memory leaks. Fixed
27 * bug with negative integers when doing searches with relational
43 const char *maptab_name;
44 struct zebra_map *next;
50 struct zebra_map *map_list;
52 const char *temp_map_ptr[2];
55 void zebra_maps_close (ZebraMaps zms)
57 struct zebra_map *zm = zms->map_list;
61 chrmaptab_destroy (zm->maptab);
64 nmem_destroy (zms->nmem);
68 static void zebra_map_read (ZebraMaps zms, const char *name)
74 struct zebra_map **zm = 0;
76 if (!(f = yaz_path_fopen(zms->tabpath, name, "r")))
78 logf(LOG_WARN|LOG_ERRNO, "%s", name);
81 while ((argc = readconf_line(f, line, 512, argv, 10)))
83 if (!strcmp (argv[0], "index") && argc == 2)
89 *zm = nmem_malloc (zms->nmem, sizeof(**zm));
90 (*zm)->reg_type = argv[1][0];
91 (*zm)->maptab_name = NULL;
93 (*zm)->completeness = 0;
95 else if (zm && !strcmp (argv[0], "charmap") && argc == 2)
97 (*zm)->maptab_name = nmem_strdup (zms->nmem, argv[1]);
99 else if (zm && !strcmp (argv[0], "completeness") && argc == 2)
101 (*zm)->completeness = atoi (argv[1]);
108 static void zms_map_handle (void *p, const char *name, const char *value)
112 zebra_map_read (zms, value);
115 ZebraMaps zebra_maps_open (const char *tabpath, Res res)
117 ZebraMaps zms = xmalloc (sizeof(*zms));
119 zms->nmem = nmem_create ();
120 zms->tabpath = nmem_strdup (zms->nmem, tabpath);
121 zms->map_list = NULL;
123 zms->temp_map_str[0] = '\0';
124 zms->temp_map_str[1] = '\0';
126 zms->temp_map_ptr[0] = zms->temp_map_str;
127 zms->temp_map_ptr[1] = NULL;
129 if (!res_trav (res, "index", zms, zms_map_handle))
130 zebra_map_read (zms, "default.idx");
134 chrmaptab zebra_map_get (ZebraMaps zms, int reg_type)
136 struct zebra_map *zm;
138 for (zm = zms->map_list; zm; zm = zm->next)
139 if (reg_type == zm->reg_type)
143 logf (LOG_WARN, "Unknown register type: %c", reg_type);
148 if (!strcmp (zm->maptab_name, "@"))
150 if (!(zm->maptab = chrmaptab_create (zms->tabpath,
151 zm->maptab_name, 0)))
152 logf(LOG_WARN, "Failed to read character table %s",
155 logf(LOG_DEBUG, "Read character table %s", zm->maptab_name);
160 const char **zebra_maps_input (ZebraMaps zms, int reg_type,
161 const char **from, int len)
165 maptab = zebra_map_get (zms, reg_type);
167 return chr_map_input(maptab, from, len);
169 zms->temp_map_str[0] = **from;
172 return zms->temp_map_ptr;
175 const char *zebra_maps_output(ZebraMaps zms, int reg_type, const char **from)
178 unsigned char i = (unsigned char) **from;
179 static char buf[2] = {0,0};
181 maptab = zebra_map_get (zms, reg_type);
183 return chr_map_output (maptab, from, 1);
190 /* ------------------------------------ */
196 Z_AttributesPlusTerm *zapt;
199 static int attr_find (AttrType *src, oid_value *attributeSetP)
201 while (src->major < src->zapt->num_attributes)
203 Z_AttributeElement *element;
205 element = src->zapt->attributeList[src->major];
206 if (src->type == *element->attributeType)
208 switch (element->which)
210 case Z_AttributeValue_numeric:
212 if (element->attributeSet && attributeSetP)
216 attrset = oid_getentbyoid (element->attributeSet);
217 *attributeSetP = attrset->value;
219 return *element->value.numeric;
221 case Z_AttributeValue_complex:
222 if (src->minor >= element->value.complex->num_list ||
223 element->value.complex->list[src->minor]->which !=
224 Z_StringOrNumeric_numeric)
227 if (element->attributeSet && attributeSetP)
231 attrset = oid_getentbyoid (element->attributeSet);
232 *attributeSetP = attrset->value;
234 return *element->value.complex->list[src->minor-1]->u.numeric;
244 static void attr_init (AttrType *src, Z_AttributesPlusTerm *zapt,
253 /* ------------------------------------ */
255 int zebra_maps_is_complete (ZebraMaps zms, int reg_type)
257 struct zebra_map *zm;
259 for (zm = zms->map_list; zm; zm = zm->next)
260 if (reg_type == zm->reg_type)
261 return zm->completeness;
265 int zebra_maps_attr (ZebraMaps zms, Z_AttributesPlusTerm *zapt,
266 int *reg_type, char **search_type, int *complete_flag)
268 AttrType completeness;
271 int completeness_value;
275 attr_init (&structure, zapt, 4);
276 attr_init (&completeness, zapt, 6);
277 attr_init (&relation, zapt, 2);
279 completeness_value = attr_find (&completeness, NULL);
280 structure_value = attr_find (&structure, NULL);
281 relation_value = attr_find (&relation, NULL);
283 if (completeness_value == 2 || completeness_value == 3)
289 *search_type = "phrase";
290 if (relation_value == 102)
291 *search_type = "ranked";
293 switch (structure_value)
295 case 6: /* word list */
296 case 105: /* free-form-text */
297 case 106: /* document-text */
298 *search_type = "ranked";
305 case 108: /* string */
311 case 107: /* local-number */
312 *search_type = "local";
314 case 109: /* numeric string */