2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.10 1997-07-01 13:01:08 adam
8 * Bug fix in routine find_entry: didn't take into account the len arg.
10 * Revision 1.9 1996/10/29 13:48:14 adam
11 * Updated to use zebrautl.h instead of alexutil.h.
13 * Revision 1.8 1996/10/18 12:39:23 adam
14 * Uses LOG_DEBUG instead of LOG_WARN for "Character map overlap".
16 * Revision 1.7 1996/06/06 12:08:56 quinn
19 * Revision 1.6 1996/06/04 13:28:00 quinn
20 * More work on charmapping
22 * Revision 1.5 1996/06/04 08:32:15 quinn
23 * Moved default keymap to keychars.c
25 * Revision 1.4 1996/06/03 16:32:13 quinn
28 * Revision 1.3 1996/06/03 15:17:46 quinn
31 * Revision 1.2 1996/06/03 10:15:09 quinn
32 * Fixed bug in mapping function.
34 * Revision 1.1 1996/05/31 09:07:18 quinn
35 * Work on character-set handling
41 * Support module to handle character-conversions into and out of the
54 #define CHR_MAXSTR 1024
55 #define CHR_MAXEQUIV 32
57 const char *CHR_UNKNOWN = "\001";
58 const char *CHR_SPACE = "\002";
59 const char *CHR_BASE = "\003";
61 extern char *data1_tabpath;
64 * Character map trie node.
68 chr_t_entry **children; /* array of children */
69 unsigned char *target; /* target for this node, if any */
70 unsigned char *equiv; /* equivalent to, or sumthin */
74 * General argument structure for callback functions (internal use only)
76 typedef struct chrwork
79 char string[CHR_MAXSTR+1];
83 * Add an entry to the character map.
85 static chr_t_entry *set_map_string(chr_t_entry *root, char *from, int len,
90 root = xmalloc(sizeof(*root));
96 if (!root->target || (char*) root->target == CHR_SPACE ||
97 (char*) root->target == CHR_UNKNOWN)
98 root->target = (unsigned char *) xstrdup(to);
99 else if ((char*) to != CHR_SPACE)
100 logf(LOG_DEBUG, "Character map overlap");
108 root->children = xmalloc(sizeof(chr_t_entry*) * 256);
109 for (i = 0; i < 256; i++)
110 root->children[i] = 0;
112 if (!(root->children[(unsigned char) *from] =
113 set_map_string(root->children[(unsigned char) *from], from + 1,
120 int chr_map_chrs(chr_t_entry *t, char **from, int len, int *read, char **to,
126 while (len && t->children && t->children[(unsigned char) **from])
128 t = t->children[(unsigned char) **from];
132 /* if there were no matches, we are still at the root node,
133 which always has a null mapping */
134 for (s = t->target; *s && max; s++)
147 static chr_t_entry *find_entry(chr_t_entry *t, char **from, int len)
151 if (len && t->children && t->children[(unsigned char) **from])
156 if ((res = find_entry(t->children[(unsigned char) *pos],
162 /* no children match. use ourselves, if we have a target */
163 return t->target ? t : 0;
166 char **chr_map_input(chr_t_entry *t, char **from, int len)
168 static char *buf[2] = {0, 0};
171 if (!(res = find_entry(t, from, len)))
173 buf[0] = (char *) res->target;
179 char **chr_map_input(chr_t_entry *t, char **from, int len)
181 static char *buf[2] = {0, 0}, str[2] = {0, 0};
186 while (len && t->children && t->children[(unsigned char) **from])
188 t = t->children[(unsigned char) **from];
192 buf[0] = (char*) t->target;
194 else /* null mapping */
201 else if (isspace(**from))
202 buf[0] = (char*) CHR_SPACE;
204 buf[0] = (char*) CHR_UNKNOWN;
209 /* return (char*) t->target; */
214 static unsigned char prim(char **s)
225 case '\\': c = '\\'; (*s)++; break;
226 case 'r': c = '\r'; (*s)++; break;
227 case 'n': c = '\n'; (*s)++; break;
228 case 't': c = '\t'; (*s)++; break;
229 case 's': c = ' '; (*s)++; break;
230 case 'x': sscanf(*s, "x%2x", &i); c = i; *s += 3; break;
231 case '{': case '[': case '(': case '}': case ']': case ')':
234 default: sscanf(*s, "%3o", &i); c = i; *s += 3; break;
245 * Add an entry to the value space.
247 static void fun_addentry(char *s, void *data, int num)
249 chrmaptab *tab = data;
252 tmp[0] = num; tmp[1] = '\0';
253 tab->input = set_map_string(tab->input, s, strlen(s), tmp);
254 tab->output[num + tab->base_uppercase] = (unsigned char *) xstrdup(s);
259 * Add a space-entry to the value space.
261 static void fun_addspace(char *s, void *data, int num)
263 chrmaptab *tab = data;
264 tab->input = set_map_string(tab->input, s, strlen(s), (char*) CHR_SPACE);
268 * Create a string containing the mapped characters provided.
270 static void fun_mkstring(char *s, void *data, int num)
275 res = chr_map_input(arg->map->input, &s, strlen(s));
276 if (*res == (char*) CHR_UNKNOWN)
277 logf(LOG_WARN, "Map: '%s' has no mapping", p);
278 strncat(arg->string, *res, CHR_MAXSTR - strlen(arg->string));
279 arg->string[CHR_MAXSTR] = '\0';
283 * Add a map to the string contained in the argument.
285 static void fun_addmap(char *s, void *data, int num)
289 assert(arg->map->input);
290 set_map_string(arg->map->input, s, strlen(s), arg->string);
293 static int scan_string(char *s, void (*fun)(char *c, void *data, int num),
294 void *data, int *num)
296 unsigned char c, str[1024], begin, end, *p;
307 logf(LOG_FATAL, "Bad range in char-map");
314 logf(LOG_FATAL, "Bad range in char-map");
318 for (c = begin; c <= end; c++)
320 str[0] = c; str[1] = '\0';
321 (*fun)((char *) str, data, num ? (*num)++ : 0);
324 case '[': s++; abort(); break;
326 p = (unsigned char*) ++s;
327 /* Find the end-marker, ignoring escapes */
330 if (!(p = (unsigned char*) strchr((char*) p, ')')))
332 logf(LOG_FATAL, "Missing ')' in string");
336 while (*(p - 1) == '\\');
338 (*fun)(s, data, num ? (*num)++ : 0);
343 str[0] = c; str[1] = '\0';
344 (*fun)((char *) str, data, num ? (*num)++ : 0);
350 chrmaptab *chr_read_maptab(char *name)
353 char line[512], *argv[50];
354 chrmaptab *res = xmalloc(sizeof(*res));
355 int argc, num = (int) *CHR_BASE, i;
357 if (!(f = yaz_path_fopen(data1_tabpath, name, "r")))
359 logf(LOG_WARN|LOG_ERRNO, "%s", name);
362 res = xmalloc(sizeof(*res));
363 res->input = xmalloc(sizeof(*res->input));
364 res->input->target = (unsigned char*) CHR_UNKNOWN;
365 res->input->equiv = 0;
367 res->input->children = xmalloc(sizeof(res->input) * 256);
368 for (i = 0; i < 256; i++)
370 res->input->children[i] = xmalloc(sizeof(*res->input));
371 res->input->children[i]->children = 0;
372 res->input->children[i]->target = (unsigned char*) CHR_UNKNOWN;
373 res->input->children[i]->equiv = 0;
376 res->input->children = 0;
378 res->query_equiv = 0;
379 for (i = *CHR_BASE; i < 256; i++)
381 res->output[(int) *CHR_SPACE] = (unsigned char *) " ";
382 res->output[(int) *CHR_UNKNOWN] = (unsigned char*) "@";
383 res->base_uppercase = 0;
385 while ((argc = readconf_line(f, line, 512, argv, 50)))
386 if (!yaz_matchstr(argv[0], "lowercase"))
390 logf(LOG_FATAL, "Syntax error in charmap");
394 if (scan_string(argv[1], fun_addentry, res, &num) < 0)
396 logf(LOG_FATAL, "Bad value-set specification");
400 res->base_uppercase = num;
401 res->output[(int) *CHR_SPACE + num] = (unsigned char *) " ";
402 res->output[(int) *CHR_UNKNOWN + num] = (unsigned char*) "@";
403 num = (int) *CHR_BASE;
405 else if (!yaz_matchstr(argv[0], "uppercase"))
407 if (!res->base_uppercase)
409 logf(LOG_FATAL, "Uppercase directive with no lowercase set");
415 logf(LOG_FATAL, "Syntax error in charmap");
419 if (scan_string(argv[1], fun_addentry, res, &num) < 0)
421 logf(LOG_FATAL, "Bad value-set specification");
426 else if (!yaz_matchstr(argv[0], "space"))
430 logf(LOG_FATAL, "Syntax error in charmap");
434 if (scan_string(argv[1], fun_addspace, res, 0) < 0)
436 logf(LOG_FATAL, "Bad space specification");
441 else if (!yaz_matchstr(argv[0], "map"))
447 logf(LOG_FATAL, "charmap MAP directive requires 2 args");
452 buf.string[0] = '\0';
453 if (scan_string(argv[2], fun_mkstring, &buf, 0) < 0)
455 logf(LOG_FATAL, "Bad map target");
459 if (scan_string(argv[1], fun_addmap, &buf, 0) < 0)
461 logf(LOG_FATAL, "Bad map source");
469 logf(LOG_WARN, "Syntax error at '%s' in %s", line, file);