2 * Copyright (C) 1994-1997, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.4 1997-11-18 10:05:08 adam
8 * Changed character map facility so that admin can specify character
9 * mapping files for each register type, w, p, etc.
11 * Revision 1.3 1997/11/17 15:35:26 adam
12 * Bug fix. Relation=relevance wasn't observed.
14 * Revision 1.2 1997/10/31 12:39:30 adam
15 * Changed log message.
17 * Revision 1.1 1997/10/27 14:33:06 adam
18 * Moved towards generic character mapping depending on "structure"
19 * field in abstract syntax file. Fixed a few memory leaks. Fixed
20 * bug with negative integers when doing searches with relational
36 const char *maptab_name;
37 struct zebra_map *next;
43 struct zebra_map *map_list;
45 const char *temp_map_ptr[2];
48 void zebra_maps_close (ZebraMaps zms)
50 struct zebra_map *zm = zms->map_list;
53 struct zebra_map *zm_next = zm->next;
55 chrmaptab_destroy (zm->maptab);
59 nmem_destroy (zms->nmem);
63 static void zebra_map_read (ZebraMaps zms, const char *name)
69 struct zebra_map **zm = 0;
71 if (!(f = yaz_path_fopen(zms->tabpath, name, "r")))
73 logf(LOG_WARN|LOG_ERRNO, "%s", name);
76 while ((argc = readconf_line(f, line, 512, argv, 10)))
78 if (!strcmp (argv[0], "index") && argc == 2)
84 *zm = nmem_malloc (zms->nmem, sizeof(**zm));
85 (*zm)->reg_type = argv[1][0];
86 (*zm)->maptab_name = NULL;
88 (*zm)->completeness = 0;
90 else if (zm && !strcmp (argv[0], "charmap") && argc == 2)
92 (*zm)->maptab_name = nmem_strdup (zms->nmem, argv[1]);
94 else if (zm && !strcmp (argv[0], "completeness") && argc == 2)
96 (*zm)->completeness = atoi (argv[1]);
103 static void zms_map_handle (void *p, const char *name, const char *value)
107 zebra_map_read (zms, value);
110 ZebraMaps zebra_maps_open (const char *tabpath, Res res)
112 ZebraMaps zms = xmalloc (sizeof(*zms));
114 zms->nmem = nmem_create ();
115 zms->tabpath = nmem_strdup (zms->nmem, tabpath);
116 zms->map_list = NULL;
118 zms->temp_map_str[0] = '\0';
119 zms->temp_map_str[1] = '\0';
121 zms->temp_map_ptr[0] = zms->temp_map_str;
122 zms->temp_map_ptr[1] = NULL;
124 if (!res_trav (res, "index", zms, zms_map_handle))
125 zebra_map_read (zms, "default.idx");
129 chrmaptab zebra_map_get (ZebraMaps zms, int reg_type)
131 struct zebra_map *zm;
133 for (zm = zms->map_list; zm; zm = zm->next)
134 if (reg_type == zm->reg_type)
138 logf (LOG_WARN, "unknown register type: %c", reg_type);
143 if (!strcmp (zm->maptab_name, "@"))
145 if (!(zm->maptab = chrmaptab_create (zms->tabpath,
146 zm->maptab_name, 0)))
147 logf(LOG_WARN, "Failed to read character table %s",
150 logf(LOG_DEBUG, "Read character table %s", zm->maptab_name);
155 const char **zebra_maps_input (ZebraMaps zms, int reg_type,
156 const char **from, int len)
160 maptab = zebra_map_get (zms, reg_type);
162 return chr_map_input(maptab, from, len);
164 zms->temp_map_str[0] = **from;
167 return zms->temp_map_ptr;
170 const char *zebra_maps_output(ZebraMaps zms, int reg_type, const char **from)
173 unsigned char i = (unsigned char) **from;
174 static char buf[2] = {0,0};
176 maptab = zebra_map_get (zms, reg_type);
178 return chr_map_output (maptab, from, 1);
185 /* ------------------------------------ */
191 Z_AttributesPlusTerm *zapt;
194 static int attr_find (AttrType *src, oid_value *attributeSetP)
196 while (src->major < src->zapt->num_attributes)
198 Z_AttributeElement *element;
200 element = src->zapt->attributeList[src->major];
201 if (src->type == *element->attributeType)
203 switch (element->which)
205 case Z_AttributeValue_numeric:
207 if (element->attributeSet && attributeSetP)
211 attrset = oid_getentbyoid (element->attributeSet);
212 *attributeSetP = attrset->value;
214 return *element->value.numeric;
216 case Z_AttributeValue_complex:
217 if (src->minor >= element->value.complex->num_list ||
218 element->value.complex->list[src->minor]->which !=
219 Z_StringOrNumeric_numeric)
222 if (element->attributeSet && attributeSetP)
226 attrset = oid_getentbyoid (element->attributeSet);
227 *attributeSetP = attrset->value;
229 return *element->value.complex->list[src->minor-1]->u.numeric;
239 static void attr_init (AttrType *src, Z_AttributesPlusTerm *zapt,
248 /* ------------------------------------ */
250 int zebra_maps_is_complete (ZebraMaps zms, int reg_type)
252 struct zebra_map *zm;
254 for (zm = zms->map_list; zm; zm = zm->next)
255 if (reg_type == zm->reg_type)
256 return zm->completeness;
260 int zebra_maps_attr (ZebraMaps zms, Z_AttributesPlusTerm *zapt,
261 int *reg_type, char **search_type, int *complete_flag)
263 AttrType completeness;
266 int completeness_value;
270 attr_init (&structure, zapt, 4);
271 attr_init (&completeness, zapt, 6);
272 attr_init (&relation, zapt, 2);
274 completeness_value = attr_find (&completeness, NULL);
275 structure_value = attr_find (&structure, NULL);
276 relation_value = attr_find (&relation, NULL);
278 if (completeness_value == 2 || completeness_value == 3)
284 *search_type = "phrase";
285 if (relation_value == 102)
286 *search_type = "ranked";
288 switch (structure_value)
294 case 6: /* word list */
295 case 105: /* free-form-text */
296 case 106: /* document-text */
297 case 108: /* string */
303 case 107: /* local-number */
304 *search_type = "local";
306 case 109: /* numeric string */