2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.11 1997-09-05 09:52:32 adam
8 * Extra argument added to function chr_read_maptab (tab path).
10 * Revision 1.10 1997/07/01 13:01:08 adam
11 * Bug fix in routine find_entry: didn't take into account the len arg.
13 * Revision 1.9 1996/10/29 13:48:14 adam
14 * Updated to use zebrautl.h instead of alexutil.h.
16 * Revision 1.8 1996/10/18 12:39:23 adam
17 * Uses LOG_DEBUG instead of LOG_WARN for "Character map overlap".
19 * Revision 1.7 1996/06/06 12:08:56 quinn
22 * Revision 1.6 1996/06/04 13:28:00 quinn
23 * More work on charmapping
25 * Revision 1.5 1996/06/04 08:32:15 quinn
26 * Moved default keymap to keychars.c
28 * Revision 1.4 1996/06/03 16:32:13 quinn
31 * Revision 1.3 1996/06/03 15:17:46 quinn
34 * Revision 1.2 1996/06/03 10:15:09 quinn
35 * Fixed bug in mapping function.
37 * Revision 1.1 1996/05/31 09:07:18 quinn
38 * Work on character-set handling
44 * Support module to handle character-conversions into and out of the
57 #define CHR_MAXSTR 1024
58 #define CHR_MAXEQUIV 32
60 const char *CHR_UNKNOWN = "\001";
61 const char *CHR_SPACE = "\002";
62 const char *CHR_BASE = "\003";
65 * Character map trie node.
69 chr_t_entry **children; /* array of children */
70 unsigned char *target; /* target for this node, if any */
71 unsigned char *equiv; /* equivalent to, or sumthin */
75 * General argument structure for callback functions (internal use only)
77 typedef struct chrwork
80 char string[CHR_MAXSTR+1];
84 * Add an entry to the character map.
86 static chr_t_entry *set_map_string(chr_t_entry *root, char *from, int len,
91 root = xmalloc(sizeof(*root));
97 if (!root->target || (char*) root->target == CHR_SPACE ||
98 (char*) root->target == CHR_UNKNOWN)
99 root->target = (unsigned char *) xstrdup(to);
100 else if ((char*) to != CHR_SPACE)
101 logf(LOG_DEBUG, "Character map overlap");
109 root->children = xmalloc(sizeof(chr_t_entry*) * 256);
110 for (i = 0; i < 256; i++)
111 root->children[i] = 0;
113 if (!(root->children[(unsigned char) *from] =
114 set_map_string(root->children[(unsigned char) *from], from + 1,
121 int chr_map_chrs(chr_t_entry *t, char **from, int len, int *read, char **to,
127 while (len && t->children && t->children[(unsigned char) **from])
129 t = t->children[(unsigned char) **from];
133 /* if there were no matches, we are still at the root node,
134 which always has a null mapping */
135 for (s = t->target; *s && max; s++)
148 static chr_t_entry *find_entry(chr_t_entry *t, char **from, int len)
152 if (len && t->children && t->children[(unsigned char) **from])
157 if ((res = find_entry(t->children[(unsigned char) *pos],
163 /* no children match. use ourselves, if we have a target */
164 return t->target ? t : 0;
167 char **chr_map_input(chr_t_entry *t, char **from, int len)
169 static char *buf[2] = {0, 0};
172 if (!(res = find_entry(t, from, len)))
174 buf[0] = (char *) res->target;
180 char **chr_map_input(chr_t_entry *t, char **from, int len)
182 static char *buf[2] = {0, 0}, str[2] = {0, 0};
187 while (len && t->children && t->children[(unsigned char) **from])
189 t = t->children[(unsigned char) **from];
193 buf[0] = (char*) t->target;
195 else /* null mapping */
202 else if (isspace(**from))
203 buf[0] = (char*) CHR_SPACE;
205 buf[0] = (char*) CHR_UNKNOWN;
210 /* return (char*) t->target; */
215 static unsigned char prim(char **s)
226 case '\\': c = '\\'; (*s)++; break;
227 case 'r': c = '\r'; (*s)++; break;
228 case 'n': c = '\n'; (*s)++; break;
229 case 't': c = '\t'; (*s)++; break;
230 case 's': c = ' '; (*s)++; break;
231 case 'x': sscanf(*s, "x%2x", &i); c = i; *s += 3; break;
232 case '{': case '[': case '(': case '}': case ']': case ')':
235 default: sscanf(*s, "%3o", &i); c = i; *s += 3; break;
246 * Add an entry to the value space.
248 static void fun_addentry(char *s, void *data, int num)
250 chrmaptab *tab = data;
253 tmp[0] = num; tmp[1] = '\0';
254 tab->input = set_map_string(tab->input, s, strlen(s), tmp);
255 tab->output[num + tab->base_uppercase] = (unsigned char *) xstrdup(s);
260 * Add a space-entry to the value space.
262 static void fun_addspace(char *s, void *data, int num)
264 chrmaptab *tab = data;
265 tab->input = set_map_string(tab->input, s, strlen(s), (char*) CHR_SPACE);
269 * Create a string containing the mapped characters provided.
271 static void fun_mkstring(char *s, void *data, int num)
276 res = chr_map_input(arg->map->input, &s, strlen(s));
277 if (*res == (char*) CHR_UNKNOWN)
278 logf(LOG_WARN, "Map: '%s' has no mapping", p);
279 strncat(arg->string, *res, CHR_MAXSTR - strlen(arg->string));
280 arg->string[CHR_MAXSTR] = '\0';
284 * Add a map to the string contained in the argument.
286 static void fun_addmap(char *s, void *data, int num)
290 assert(arg->map->input);
291 set_map_string(arg->map->input, s, strlen(s), arg->string);
294 static int scan_string(char *s, void (*fun)(char *c, void *data, int num),
295 void *data, int *num)
297 unsigned char c, str[1024], begin, end, *p;
308 logf(LOG_FATAL, "Bad range in char-map");
315 logf(LOG_FATAL, "Bad range in char-map");
319 for (c = begin; c <= end; c++)
321 str[0] = c; str[1] = '\0';
322 (*fun)((char *) str, data, num ? (*num)++ : 0);
325 case '[': s++; abort(); break;
327 p = (unsigned char*) ++s;
328 /* Find the end-marker, ignoring escapes */
331 if (!(p = (unsigned char*) strchr((char*) p, ')')))
333 logf(LOG_FATAL, "Missing ')' in string");
337 while (*(p - 1) == '\\');
339 (*fun)(s, data, num ? (*num)++ : 0);
344 str[0] = c; str[1] = '\0';
345 (*fun)((char *) str, data, num ? (*num)++ : 0);
351 chrmaptab *chr_read_maptab(const char *tabpath, const char *name)
354 char line[512], *argv[50];
355 chrmaptab *res = xmalloc(sizeof(*res));
356 int argc, num = (int) *CHR_BASE, i;
358 if (!(f = yaz_path_fopen(tabpath, name, "r")))
360 logf(LOG_WARN|LOG_ERRNO, "%s", name);
363 res = xmalloc(sizeof(*res));
364 res->input = xmalloc(sizeof(*res->input));
365 res->input->target = (unsigned char*) CHR_UNKNOWN;
366 res->input->equiv = 0;
368 res->input->children = xmalloc(sizeof(res->input) * 256);
369 for (i = 0; i < 256; i++)
371 res->input->children[i] = xmalloc(sizeof(*res->input));
372 res->input->children[i]->children = 0;
373 res->input->children[i]->target = (unsigned char*) CHR_UNKNOWN;
374 res->input->children[i]->equiv = 0;
377 res->input->children = 0;
379 res->query_equiv = 0;
380 for (i = *CHR_BASE; i < 256; i++)
382 res->output[(int) *CHR_SPACE] = (unsigned char *) " ";
383 res->output[(int) *CHR_UNKNOWN] = (unsigned char*) "@";
384 res->base_uppercase = 0;
386 while ((argc = readconf_line(f, line, 512, argv, 50)))
387 if (!yaz_matchstr(argv[0], "lowercase"))
391 logf(LOG_FATAL, "Syntax error in charmap");
395 if (scan_string(argv[1], fun_addentry, res, &num) < 0)
397 logf(LOG_FATAL, "Bad value-set specification");
401 res->base_uppercase = num;
402 res->output[(int) *CHR_SPACE + num] = (unsigned char *) " ";
403 res->output[(int) *CHR_UNKNOWN + num] = (unsigned char*) "@";
404 num = (int) *CHR_BASE;
406 else if (!yaz_matchstr(argv[0], "uppercase"))
408 if (!res->base_uppercase)
410 logf(LOG_FATAL, "Uppercase directive with no lowercase set");
416 logf(LOG_FATAL, "Syntax error in charmap");
420 if (scan_string(argv[1], fun_addentry, res, &num) < 0)
422 logf(LOG_FATAL, "Bad value-set specification");
427 else if (!yaz_matchstr(argv[0], "space"))
431 logf(LOG_FATAL, "Syntax error in charmap");
435 if (scan_string(argv[1], fun_addspace, res, 0) < 0)
437 logf(LOG_FATAL, "Bad space specification");
442 else if (!yaz_matchstr(argv[0], "map"))
448 logf(LOG_FATAL, "charmap MAP directive requires 2 args");
453 buf.string[0] = '\0';
454 if (scan_string(argv[2], fun_mkstring, &buf, 0) < 0)
456 logf(LOG_FATAL, "Bad map target");
460 if (scan_string(argv[1], fun_addmap, &buf, 0) < 0)
462 logf(LOG_FATAL, "Bad map source");
470 logf(LOG_WARN, "Syntax error at '%s' in %s", line, file);