2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.2 1996-06-03 10:15:09 quinn
8 * Fixed bug in mapping function.
10 * Revision 1.1 1996/05/31 09:07:18 quinn
11 * Work on character-set handling
17 * Support module to handle character-conversions into and out of the
29 const char *CHR_UNKNOWN = "\001";
30 const char *CHR_SPACE = "\002";
31 const char *CHR_BASE = "\003";
33 extern char *data1_tabpath;
36 * Character map trie node.
40 chr_t_entry **children; /* array of children */
41 unsigned char *target; /* target for this node, if any */
42 unsigned char *equiv; /* equivalent to, or sumthin */
46 * Add an entry to the character map.
48 static chr_t_entry *set_map_string(chr_t_entry *root, char *from, int len,
53 root = xmalloc(sizeof(*root));
59 if (!root->target || (char*) root->target == CHR_SPACE ||
60 (char*) root->target == CHR_UNKNOWN)
61 root->target = (unsigned char *) xstrdup(to);
62 else if ((char*) to != CHR_SPACE)
64 logf(LOG_FATAL, "Character map overlap");
74 root->children = xmalloc(sizeof(chr_t_entry*) * 256);
75 for (i = 0; i < 256; i++)
76 root->children[i] = 0;
78 if (!(root->children[(unsigned char) *from] =
79 set_map_string(root->children[(unsigned char) *from], from + 1,
86 int chr_map_chrs(chr_t_entry *t, char **from, int len, int *read, char **to,
92 while (len && t->children && t->children[(unsigned char) **from])
94 t = t->children[(unsigned char) **from];
98 /* if there were no matches, we are still at the root node,
99 which always has a null mapping */
100 for (s = t->target; *s && max; s++)
113 static chr_t_entry *find_entry(chr_t_entry *t, char **from, int len)
117 if (t->children && t->children[(unsigned char) **from])
122 if ((res = find_entry(t->children[(unsigned char) *pos],
128 /* no children match. use ourselves */
132 char **chr_map_input(chr_t_entry *t, char **from, int len)
134 static char *buf[2] = {0, 0}, str[2] = {0, 0};
137 if (!t) /* no table loaded. Null mapping */
144 else if (isspace(**from))
145 buf[0] = (char*) CHR_SPACE;
147 buf[0] = (char*) CHR_UNKNOWN;
151 /* no children match. use our target string */
152 if (!(res = find_entry(t, from, len)))
154 buf[0] = (char *) res->target;
160 char **chr_map_input(chr_t_entry *t, char **from, int len)
162 static char *buf[2] = {0, 0}, str[2] = {0, 0};
167 while (len && t->children && t->children[(unsigned char) **from])
169 t = t->children[(unsigned char) **from];
173 buf[0] = (char*) t->target;
175 else /* null mapping */
182 else if (isspace(**from))
183 buf[0] = (char*) CHR_SPACE;
185 buf[0] = (char*) CHR_UNKNOWN;
190 /* return (char*) t->target; */
195 static unsigned char prim(char **s)
206 case '\\': c = '\\'; (*s)++; break;
207 case 'r': c = '\r'; (*s)++; break;
208 case 'n': c = '\n'; (*s)++; break;
209 case 't': c = '\t'; (*s)++; break;
210 case 's': c = ' '; (*s)++; break;
211 case 'x': sscanf(*s, "x%2x", &i); c = i; *s += 3; break;
212 case '{': case '[': case '(': case '}': case ']': case ')':
215 default: sscanf(*s, "%3o", &i); c = i; *s += 3; break;
226 * Add an entry to the value space.
228 static void fun_addentry(char *s, void *data, int num)
230 chrmaptab *tab = data;
233 tmp[0] = num; tmp[1] = '\0';
234 tab->input = set_map_string(tab->input, s, strlen(s), tmp);
235 tab->output[num + tab->base_uppercase] = (unsigned char *) xstrdup(s);
240 * Add a space-entry to the value space.
242 static void fun_addspace(char *s, void *data, int num)
244 chrmaptab *tab = data;
245 tab->input = set_map_string(tab->input, s, strlen(s), (char*) CHR_SPACE);
248 static int scan_string(char *s, void (*fun)(char *c, void *data, int num),
249 void *data, int *num)
251 unsigned char c, str[1024], begin, end, *p;
262 logf(LOG_FATAL, "Bad range in char-map");
269 logf(LOG_FATAL, "Bad range in char-map");
273 for (c = begin; c <= end; c++)
275 str[0] = c; str[1] = '\0';
276 (*fun)((char *) str, data, num ? (*num)++ : 0);
279 case '[': s++; abort(); break;
281 p = (unsigned char*) ++s;
282 /* Find the end-marker, ignoring escapes */
285 if (!(p = (unsigned char*) strchr((char*) p, ')')))
287 logf(LOG_FATAL, "Missing ')' in string");
291 while (*(p - 1) == '\\');
293 (*fun)(s, data, num ? (*num)++ : 0);
298 str[0] = c; str[1] = '\0';
299 (*fun)((char *) str, data, num ? (*num)++ : 0);
305 chrmaptab *chr_read_maptab(char *name)
308 char line[512], *argv[50];
309 chrmaptab *res = xmalloc(sizeof(*res));
310 int argc, num = (int) *CHR_BASE, i;
312 if (!(f = yaz_path_fopen(data1_tabpath, name, "r")))
314 logf(LOG_WARN|LOG_ERRNO, "%s", name);
317 res = xmalloc(sizeof(*res));
318 res->input = xmalloc(sizeof(*res->input));
319 res->input->target = (unsigned char*) CHR_UNKNOWN;
320 res->input->equiv = 0;
322 res->input->children = xmalloc(sizeof(res->input) * 256);
323 for (i = 0; i < 256; i++)
325 res->input->children[i] = xmalloc(sizeof(*res->input));
326 res->input->children[i]->children = 0;
327 res->input->children[i]->target = (unsigned char*) CHR_UNKNOWN;
328 res->input->children[i]->equiv = 0;
331 res->input->children = 0;
333 res->query_equiv = 0;
334 for (i = *CHR_BASE; i < 256; i++)
336 res->output[(int) *CHR_SPACE] = (unsigned char *) " ";
337 res->output[(int) *CHR_UNKNOWN] = (unsigned char*) "@";
338 res->base_uppercase = 0;
340 while ((argc = readconf_line(f, line, 512, argv, 50)))
341 if (!yaz_matchstr(argv[0], "lowercase"))
345 logf(LOG_FATAL, "Syntax error in charmap");
349 if (scan_string(argv[1], fun_addentry, res, &num) < 0)
351 logf(LOG_FATAL, "Bad value-set specification");
355 res->base_uppercase = num;
356 res->output[(int) *CHR_SPACE + num] = (unsigned char *) " ";
357 res->output[(int) *CHR_UNKNOWN + num] = (unsigned char*) "@";
358 num = (int) *CHR_BASE;
360 else if (!yaz_matchstr(argv[0], "uppercase"))
362 if (!res->base_uppercase)
364 logf(LOG_FATAL, "Uppercase directive with no lowercase set");
370 logf(LOG_FATAL, "Syntax error in charmap");
374 if (scan_string(argv[1], fun_addentry, res, &num) < 0)
376 logf(LOG_FATAL, "Bad value-set specification");
381 else if (!yaz_matchstr(argv[0], "space"))
385 logf(LOG_FATAL, "Syntax error in charmap");
389 if (scan_string(argv[1], fun_addspace, res, 0))
391 logf(LOG_FATAL, "Bad space specification");
399 logf(LOG_WARN, "Syntax error at '%s' in %s", line, file);