2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.8 1996-10-18 12:39:23 adam
8 * Uses LOG_DEBUG instead of LOG_WARN for "Character map overlap".
10 * Revision 1.7 1996/06/06 12:08:56 quinn
13 * Revision 1.6 1996/06/04 13:28:00 quinn
14 * More work on charmapping
16 * Revision 1.5 1996/06/04 08:32:15 quinn
17 * Moved default keymap to keychars.c
19 * Revision 1.4 1996/06/03 16:32:13 quinn
22 * Revision 1.3 1996/06/03 15:17:46 quinn
25 * Revision 1.2 1996/06/03 10:15:09 quinn
26 * Fixed bug in mapping function.
28 * Revision 1.1 1996/05/31 09:07:18 quinn
29 * Work on character-set handling
35 * Support module to handle character-conversions into and out of the
48 #define CHR_MAXSTR 1024
49 #define CHR_MAXEQUIV 32
51 const char *CHR_UNKNOWN = "\001";
52 const char *CHR_SPACE = "\002";
53 const char *CHR_BASE = "\003";
55 extern char *data1_tabpath;
58 * Character map trie node.
62 chr_t_entry **children; /* array of children */
63 unsigned char *target; /* target for this node, if any */
64 unsigned char *equiv; /* equivalent to, or sumthin */
68 * General argument structure for callback functions (internal use only)
70 typedef struct chrwork
73 char string[CHR_MAXSTR+1];
77 * Add an entry to the character map.
79 static chr_t_entry *set_map_string(chr_t_entry *root, char *from, int len,
84 root = xmalloc(sizeof(*root));
90 if (!root->target || (char*) root->target == CHR_SPACE ||
91 (char*) root->target == CHR_UNKNOWN)
92 root->target = (unsigned char *) xstrdup(to);
93 else if ((char*) to != CHR_SPACE)
94 logf(LOG_DEBUG, "Character map overlap");
102 root->children = xmalloc(sizeof(chr_t_entry*) * 256);
103 for (i = 0; i < 256; i++)
104 root->children[i] = 0;
106 if (!(root->children[(unsigned char) *from] =
107 set_map_string(root->children[(unsigned char) *from], from + 1,
114 int chr_map_chrs(chr_t_entry *t, char **from, int len, int *read, char **to,
120 while (len && t->children && t->children[(unsigned char) **from])
122 t = t->children[(unsigned char) **from];
126 /* if there were no matches, we are still at the root node,
127 which always has a null mapping */
128 for (s = t->target; *s && max; s++)
141 static chr_t_entry *find_entry(chr_t_entry *t, char **from, int len)
145 if (t->children && t->children[(unsigned char) **from])
150 if ((res = find_entry(t->children[(unsigned char) *pos],
156 /* no children match. use ourselves, if we have a target */
157 return t->target ? t : 0;
160 char **chr_map_input(chr_t_entry *t, char **from, int len)
162 static char *buf[2] = {0, 0};
165 if (!(res = find_entry(t, from, len)))
167 buf[0] = (char *) res->target;
173 char **chr_map_input(chr_t_entry *t, char **from, int len)
175 static char *buf[2] = {0, 0}, str[2] = {0, 0};
180 while (len && t->children && t->children[(unsigned char) **from])
182 t = t->children[(unsigned char) **from];
186 buf[0] = (char*) t->target;
188 else /* null mapping */
195 else if (isspace(**from))
196 buf[0] = (char*) CHR_SPACE;
198 buf[0] = (char*) CHR_UNKNOWN;
203 /* return (char*) t->target; */
208 static unsigned char prim(char **s)
219 case '\\': c = '\\'; (*s)++; break;
220 case 'r': c = '\r'; (*s)++; break;
221 case 'n': c = '\n'; (*s)++; break;
222 case 't': c = '\t'; (*s)++; break;
223 case 's': c = ' '; (*s)++; break;
224 case 'x': sscanf(*s, "x%2x", &i); c = i; *s += 3; break;
225 case '{': case '[': case '(': case '}': case ']': case ')':
228 default: sscanf(*s, "%3o", &i); c = i; *s += 3; break;
239 * Add an entry to the value space.
241 static void fun_addentry(char *s, void *data, int num)
243 chrmaptab *tab = data;
246 tmp[0] = num; tmp[1] = '\0';
247 tab->input = set_map_string(tab->input, s, strlen(s), tmp);
248 tab->output[num + tab->base_uppercase] = (unsigned char *) xstrdup(s);
253 * Add a space-entry to the value space.
255 static void fun_addspace(char *s, void *data, int num)
257 chrmaptab *tab = data;
258 tab->input = set_map_string(tab->input, s, strlen(s), (char*) CHR_SPACE);
262 * Create a string containing the mapped characters provided.
264 static void fun_mkstring(char *s, void *data, int num)
269 res = chr_map_input(arg->map->input, &s, strlen(s));
270 if (*res == (char*) CHR_UNKNOWN)
271 logf(LOG_WARN, "Map: '%s' has no mapping", p);
272 strncat(arg->string, *res, CHR_MAXSTR - strlen(arg->string));
273 arg->string[CHR_MAXSTR] = '\0';
277 * Add a map to the string contained in the argument.
279 static void fun_addmap(char *s, void *data, int num)
283 assert(arg->map->input);
284 set_map_string(arg->map->input, s, strlen(s), arg->string);
287 static int scan_string(char *s, void (*fun)(char *c, void *data, int num),
288 void *data, int *num)
290 unsigned char c, str[1024], begin, end, *p;
301 logf(LOG_FATAL, "Bad range in char-map");
308 logf(LOG_FATAL, "Bad range in char-map");
312 for (c = begin; c <= end; c++)
314 str[0] = c; str[1] = '\0';
315 (*fun)((char *) str, data, num ? (*num)++ : 0);
318 case '[': s++; abort(); break;
320 p = (unsigned char*) ++s;
321 /* Find the end-marker, ignoring escapes */
324 if (!(p = (unsigned char*) strchr((char*) p, ')')))
326 logf(LOG_FATAL, "Missing ')' in string");
330 while (*(p - 1) == '\\');
332 (*fun)(s, data, num ? (*num)++ : 0);
337 str[0] = c; str[1] = '\0';
338 (*fun)((char *) str, data, num ? (*num)++ : 0);
344 chrmaptab *chr_read_maptab(char *name)
347 char line[512], *argv[50];
348 chrmaptab *res = xmalloc(sizeof(*res));
349 int argc, num = (int) *CHR_BASE, i;
351 if (!(f = yaz_path_fopen(data1_tabpath, name, "r")))
353 logf(LOG_WARN|LOG_ERRNO, "%s", name);
356 res = xmalloc(sizeof(*res));
357 res->input = xmalloc(sizeof(*res->input));
358 res->input->target = (unsigned char*) CHR_UNKNOWN;
359 res->input->equiv = 0;
361 res->input->children = xmalloc(sizeof(res->input) * 256);
362 for (i = 0; i < 256; i++)
364 res->input->children[i] = xmalloc(sizeof(*res->input));
365 res->input->children[i]->children = 0;
366 res->input->children[i]->target = (unsigned char*) CHR_UNKNOWN;
367 res->input->children[i]->equiv = 0;
370 res->input->children = 0;
372 res->query_equiv = 0;
373 for (i = *CHR_BASE; i < 256; i++)
375 res->output[(int) *CHR_SPACE] = (unsigned char *) " ";
376 res->output[(int) *CHR_UNKNOWN] = (unsigned char*) "@";
377 res->base_uppercase = 0;
379 while ((argc = readconf_line(f, line, 512, argv, 50)))
380 if (!yaz_matchstr(argv[0], "lowercase"))
384 logf(LOG_FATAL, "Syntax error in charmap");
388 if (scan_string(argv[1], fun_addentry, res, &num) < 0)
390 logf(LOG_FATAL, "Bad value-set specification");
394 res->base_uppercase = num;
395 res->output[(int) *CHR_SPACE + num] = (unsigned char *) " ";
396 res->output[(int) *CHR_UNKNOWN + num] = (unsigned char*) "@";
397 num = (int) *CHR_BASE;
399 else if (!yaz_matchstr(argv[0], "uppercase"))
401 if (!res->base_uppercase)
403 logf(LOG_FATAL, "Uppercase directive with no lowercase set");
409 logf(LOG_FATAL, "Syntax error in charmap");
413 if (scan_string(argv[1], fun_addentry, res, &num) < 0)
415 logf(LOG_FATAL, "Bad value-set specification");
420 else if (!yaz_matchstr(argv[0], "space"))
424 logf(LOG_FATAL, "Syntax error in charmap");
428 if (scan_string(argv[1], fun_addspace, res, 0) < 0)
430 logf(LOG_FATAL, "Bad space specification");
435 else if (!yaz_matchstr(argv[0], "map"))
441 logf(LOG_FATAL, "charmap MAP directive requires 2 args");
446 buf.string[0] = '\0';
447 if (scan_string(argv[2], fun_mkstring, &buf, 0) < 0)
449 logf(LOG_FATAL, "Bad map target");
453 if (scan_string(argv[1], fun_addmap, &buf, 0) < 0)
455 logf(LOG_FATAL, "Bad map source");
463 logf(LOG_WARN, "Syntax error at '%s' in %s", line, file);