2 * Copyright (C) 1994-1997, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.5 1997-11-19 10:22:14 adam
8 * Bug fix (introduced by previous commit).
10 * Revision 1.4 1997/11/18 10:05:08 adam
11 * Changed character map facility so that admin can specify character
12 * mapping files for each register type, w, p, etc.
14 * Revision 1.3 1997/11/17 15:35:26 adam
15 * Bug fix. Relation=relevance wasn't observed.
17 * Revision 1.2 1997/10/31 12:39:30 adam
18 * Changed log message.
20 * Revision 1.1 1997/10/27 14:33:06 adam
21 * Moved towards generic character mapping depending on "structure"
22 * field in abstract syntax file. Fixed a few memory leaks. Fixed
23 * bug with negative integers when doing searches with relational
39 const char *maptab_name;
40 struct zebra_map *next;
46 struct zebra_map *map_list;
48 const char *temp_map_ptr[2];
51 void zebra_maps_close (ZebraMaps zms)
53 struct zebra_map *zm = zms->map_list;
57 chrmaptab_destroy (zm->maptab);
60 nmem_destroy (zms->nmem);
64 static void zebra_map_read (ZebraMaps zms, const char *name)
70 struct zebra_map **zm = 0;
72 if (!(f = yaz_path_fopen(zms->tabpath, name, "r")))
74 logf(LOG_WARN|LOG_ERRNO, "%s", name);
77 while ((argc = readconf_line(f, line, 512, argv, 10)))
79 if (!strcmp (argv[0], "index") && argc == 2)
85 *zm = nmem_malloc (zms->nmem, sizeof(**zm));
86 (*zm)->reg_type = argv[1][0];
87 (*zm)->maptab_name = NULL;
89 (*zm)->completeness = 0;
91 else if (zm && !strcmp (argv[0], "charmap") && argc == 2)
93 (*zm)->maptab_name = nmem_strdup (zms->nmem, argv[1]);
95 else if (zm && !strcmp (argv[0], "completeness") && argc == 2)
97 (*zm)->completeness = atoi (argv[1]);
104 static void zms_map_handle (void *p, const char *name, const char *value)
108 zebra_map_read (zms, value);
111 ZebraMaps zebra_maps_open (const char *tabpath, Res res)
113 ZebraMaps zms = xmalloc (sizeof(*zms));
115 zms->nmem = nmem_create ();
116 zms->tabpath = nmem_strdup (zms->nmem, tabpath);
117 zms->map_list = NULL;
119 zms->temp_map_str[0] = '\0';
120 zms->temp_map_str[1] = '\0';
122 zms->temp_map_ptr[0] = zms->temp_map_str;
123 zms->temp_map_ptr[1] = NULL;
125 if (!res_trav (res, "index", zms, zms_map_handle))
126 zebra_map_read (zms, "default.idx");
130 chrmaptab zebra_map_get (ZebraMaps zms, int reg_type)
132 struct zebra_map *zm;
134 for (zm = zms->map_list; zm; zm = zm->next)
135 if (reg_type == zm->reg_type)
139 logf (LOG_WARN, "Unknown register type: %c", reg_type);
144 if (!strcmp (zm->maptab_name, "@"))
146 if (!(zm->maptab = chrmaptab_create (zms->tabpath,
147 zm->maptab_name, 0)))
148 logf(LOG_WARN, "Failed to read character table %s",
151 logf(LOG_DEBUG, "Read character table %s", zm->maptab_name);
156 const char **zebra_maps_input (ZebraMaps zms, int reg_type,
157 const char **from, int len)
161 maptab = zebra_map_get (zms, reg_type);
163 return chr_map_input(maptab, from, len);
165 zms->temp_map_str[0] = **from;
168 return zms->temp_map_ptr;
171 const char *zebra_maps_output(ZebraMaps zms, int reg_type, const char **from)
174 unsigned char i = (unsigned char) **from;
175 static char buf[2] = {0,0};
177 maptab = zebra_map_get (zms, reg_type);
179 return chr_map_output (maptab, from, 1);
186 /* ------------------------------------ */
192 Z_AttributesPlusTerm *zapt;
195 static int attr_find (AttrType *src, oid_value *attributeSetP)
197 while (src->major < src->zapt->num_attributes)
199 Z_AttributeElement *element;
201 element = src->zapt->attributeList[src->major];
202 if (src->type == *element->attributeType)
204 switch (element->which)
206 case Z_AttributeValue_numeric:
208 if (element->attributeSet && attributeSetP)
212 attrset = oid_getentbyoid (element->attributeSet);
213 *attributeSetP = attrset->value;
215 return *element->value.numeric;
217 case Z_AttributeValue_complex:
218 if (src->minor >= element->value.complex->num_list ||
219 element->value.complex->list[src->minor]->which !=
220 Z_StringOrNumeric_numeric)
223 if (element->attributeSet && attributeSetP)
227 attrset = oid_getentbyoid (element->attributeSet);
228 *attributeSetP = attrset->value;
230 return *element->value.complex->list[src->minor-1]->u.numeric;
240 static void attr_init (AttrType *src, Z_AttributesPlusTerm *zapt,
249 /* ------------------------------------ */
251 int zebra_maps_is_complete (ZebraMaps zms, int reg_type)
253 struct zebra_map *zm;
255 for (zm = zms->map_list; zm; zm = zm->next)
256 if (reg_type == zm->reg_type)
257 return zm->completeness;
261 int zebra_maps_attr (ZebraMaps zms, Z_AttributesPlusTerm *zapt,
262 int *reg_type, char **search_type, int *complete_flag)
264 AttrType completeness;
267 int completeness_value;
271 attr_init (&structure, zapt, 4);
272 attr_init (&completeness, zapt, 6);
273 attr_init (&relation, zapt, 2);
275 completeness_value = attr_find (&completeness, NULL);
276 structure_value = attr_find (&structure, NULL);
277 relation_value = attr_find (&relation, NULL);
279 if (completeness_value == 2 || completeness_value == 3)
285 *search_type = "phrase";
286 if (relation_value == 102)
287 *search_type = "ranked";
289 switch (structure_value)
295 case 6: /* word list */
296 case 105: /* free-form-text */
297 case 106: /* document-text */
298 case 108: /* string */
304 case 107: /* local-number */
305 *search_type = "local";
307 case 109: /* numeric string */