2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.6 1996-06-04 13:28:00 quinn
8 * More work on charmapping
10 * Revision 1.5 1996/06/04 08:32:15 quinn
11 * Moved default keymap to keychars.c
13 * Revision 1.4 1996/06/03 16:32:13 quinn
16 * Revision 1.3 1996/06/03 15:17:46 quinn
19 * Revision 1.2 1996/06/03 10:15:09 quinn
20 * Fixed bug in mapping function.
22 * Revision 1.1 1996/05/31 09:07:18 quinn
23 * Work on character-set handling
29 * Support module to handle character-conversions into and out of the
42 #define CHR_MAXSTR 1024
43 #define CHR_MAXEQUIV 32
45 const char *CHR_UNKNOWN = "\001";
46 const char *CHR_SPACE = "\002";
47 const char *CHR_BASE = "\003";
49 extern char *data1_tabpath;
52 * Character map trie node.
56 chr_t_entry **children; /* array of children */
57 unsigned char *target; /* target for this node, if any */
58 unsigned char *equiv; /* equivalent to, or sumthin */
62 * General argument structure for callback functions (internal use only)
64 typedef struct chrwork
67 char string[CHR_MAXSTR+1];
71 * Add an entry to the character map.
73 static chr_t_entry *set_map_string(chr_t_entry *root, char *from, int len,
78 root = xmalloc(sizeof(*root));
84 if (!root->target || (char*) root->target == CHR_SPACE ||
85 (char*) root->target == CHR_UNKNOWN)
86 root->target = (unsigned char *) xstrdup(to);
87 else if ((char*) to != CHR_SPACE)
88 logf(LOG_WARN, "Character map overlap");
96 root->children = xmalloc(sizeof(chr_t_entry*) * 256);
97 for (i = 0; i < 256; i++)
98 root->children[i] = 0;
100 if (!(root->children[(unsigned char) *from] =
101 set_map_string(root->children[(unsigned char) *from], from + 1,
108 int chr_map_chrs(chr_t_entry *t, char **from, int len, int *read, char **to,
114 while (len && t->children && t->children[(unsigned char) **from])
116 t = t->children[(unsigned char) **from];
120 /* if there were no matches, we are still at the root node,
121 which always has a null mapping */
122 for (s = t->target; *s && max; s++)
135 static chr_t_entry *find_entry(chr_t_entry *t, char **from, int len)
139 if (t->children && t->children[(unsigned char) **from])
144 if ((res = find_entry(t->children[(unsigned char) *pos],
150 /* no children match. use ourselves */
154 char **chr_map_input(chr_t_entry *t, char **from, int len)
156 static char *buf[2] = {0, 0};
159 if (!(res = find_entry(t, from, len)))
161 buf[0] = (char *) res->target;
167 char **chr_map_input(chr_t_entry *t, char **from, int len)
169 static char *buf[2] = {0, 0}, str[2] = {0, 0};
174 while (len && t->children && t->children[(unsigned char) **from])
176 t = t->children[(unsigned char) **from];
180 buf[0] = (char*) t->target;
182 else /* null mapping */
189 else if (isspace(**from))
190 buf[0] = (char*) CHR_SPACE;
192 buf[0] = (char*) CHR_UNKNOWN;
197 /* return (char*) t->target; */
202 static unsigned char prim(char **s)
213 case '\\': c = '\\'; (*s)++; break;
214 case 'r': c = '\r'; (*s)++; break;
215 case 'n': c = '\n'; (*s)++; break;
216 case 't': c = '\t'; (*s)++; break;
217 case 's': c = ' '; (*s)++; break;
218 case 'x': sscanf(*s, "x%2x", &i); c = i; *s += 3; break;
219 case '{': case '[': case '(': case '}': case ']': case ')':
222 default: sscanf(*s, "%3o", &i); c = i; *s += 3; break;
233 * Add an entry to the value space.
235 static void fun_addentry(char *s, void *data, int num)
237 chrmaptab *tab = data;
240 tmp[0] = num; tmp[1] = '\0';
241 tab->input = set_map_string(tab->input, s, strlen(s), tmp);
242 tab->output[num + tab->base_uppercase] = (unsigned char *) xstrdup(s);
247 * Add a space-entry to the value space.
249 static void fun_addspace(char *s, void *data, int num)
251 chrmaptab *tab = data;
252 tab->input = set_map_string(tab->input, s, strlen(s), (char*) CHR_SPACE);
256 * Create a string containing the mapped characters provided.
258 static void fun_mkstring(char *s, void *data, int num)
263 res = chr_map_input(arg->map->input, &s, strlen(s));
264 if (*res == (char*) CHR_UNKNOWN)
265 logf(LOG_WARN, "Map: '%s' has no mapping", p);
266 strncat(arg->string, *res, CHR_MAXSTR - strlen(arg->string));
267 arg->string[CHR_MAXSTR] = '\0';
271 * Add a map to the string contained in the argument.
273 static void fun_addmap(char *s, void *data, int num)
277 assert(arg->map->input);
278 set_map_string(arg->map->input, s, strlen(s), arg->string);
281 static int scan_string(char *s, void (*fun)(char *c, void *data, int num),
282 void *data, int *num)
284 unsigned char c, str[1024], begin, end, *p;
295 logf(LOG_FATAL, "Bad range in char-map");
302 logf(LOG_FATAL, "Bad range in char-map");
306 for (c = begin; c <= end; c++)
308 str[0] = c; str[1] = '\0';
309 (*fun)((char *) str, data, num ? (*num)++ : 0);
312 case '[': s++; abort(); break;
314 p = (unsigned char*) ++s;
315 /* Find the end-marker, ignoring escapes */
318 if (!(p = (unsigned char*) strchr((char*) p, ')')))
320 logf(LOG_FATAL, "Missing ')' in string");
324 while (*(p - 1) == '\\');
326 (*fun)(s, data, num ? (*num)++ : 0);
331 str[0] = c; str[1] = '\0';
332 (*fun)((char *) str, data, num ? (*num)++ : 0);
338 chrmaptab *chr_read_maptab(char *name)
341 char line[512], *argv[50];
342 chrmaptab *res = xmalloc(sizeof(*res));
343 int argc, num = (int) *CHR_BASE, i;
345 if (!(f = yaz_path_fopen(data1_tabpath, name, "r")))
347 logf(LOG_WARN|LOG_ERRNO, "%s", name);
350 res = xmalloc(sizeof(*res));
351 res->input = xmalloc(sizeof(*res->input));
352 res->input->target = (unsigned char*) CHR_UNKNOWN;
353 res->input->equiv = 0;
355 res->input->children = xmalloc(sizeof(res->input) * 256);
356 for (i = 0; i < 256; i++)
358 res->input->children[i] = xmalloc(sizeof(*res->input));
359 res->input->children[i]->children = 0;
360 res->input->children[i]->target = (unsigned char*) CHR_UNKNOWN;
361 res->input->children[i]->equiv = 0;
364 res->input->children = 0;
366 res->query_equiv = 0;
367 for (i = *CHR_BASE; i < 256; i++)
369 res->output[(int) *CHR_SPACE] = (unsigned char *) " ";
370 res->output[(int) *CHR_UNKNOWN] = (unsigned char*) "@";
371 res->base_uppercase = 0;
373 while ((argc = readconf_line(f, line, 512, argv, 50)))
374 if (!yaz_matchstr(argv[0], "lowercase"))
378 logf(LOG_FATAL, "Syntax error in charmap");
382 if (scan_string(argv[1], fun_addentry, res, &num) < 0)
384 logf(LOG_FATAL, "Bad value-set specification");
388 res->base_uppercase = num;
389 res->output[(int) *CHR_SPACE + num] = (unsigned char *) " ";
390 res->output[(int) *CHR_UNKNOWN + num] = (unsigned char*) "@";
391 num = (int) *CHR_BASE;
393 else if (!yaz_matchstr(argv[0], "uppercase"))
395 if (!res->base_uppercase)
397 logf(LOG_FATAL, "Uppercase directive with no lowercase set");
403 logf(LOG_FATAL, "Syntax error in charmap");
407 if (scan_string(argv[1], fun_addentry, res, &num) < 0)
409 logf(LOG_FATAL, "Bad value-set specification");
414 else if (!yaz_matchstr(argv[0], "space"))
418 logf(LOG_FATAL, "Syntax error in charmap");
422 if (scan_string(argv[1], fun_addspace, res, 0) < 0)
424 logf(LOG_FATAL, "Bad space specification");
429 else if (!yaz_matchstr(argv[0], "map"))
435 logf(LOG_FATAL, "charmap MAP directive requires 2 args");
440 buf.string[0] = '\0';
441 if (scan_string(argv[2], fun_mkstring, &buf, 0) < 0)
443 logf(LOG_FATAL, "Bad map target");
447 if (scan_string(argv[1], fun_addmap, &buf, 0) < 0)
449 logf(LOG_FATAL, "Bad map source");
457 logf(LOG_WARN, "Syntax error at '%s' in %s", line, file);