2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.9 1996-10-29 13:48:14 adam
8 * Updated to use zebrautl.h instead of alexutil.h.
10 * Revision 1.8 1996/10/18 12:39:23 adam
11 * Uses LOG_DEBUG instead of LOG_WARN for "Character map overlap".
13 * Revision 1.7 1996/06/06 12:08:56 quinn
16 * Revision 1.6 1996/06/04 13:28:00 quinn
17 * More work on charmapping
19 * Revision 1.5 1996/06/04 08:32:15 quinn
20 * Moved default keymap to keychars.c
22 * Revision 1.4 1996/06/03 16:32:13 quinn
25 * Revision 1.3 1996/06/03 15:17:46 quinn
28 * Revision 1.2 1996/06/03 10:15:09 quinn
29 * Fixed bug in mapping function.
31 * Revision 1.1 1996/05/31 09:07:18 quinn
32 * Work on character-set handling
38 * Support module to handle character-conversions into and out of the
51 #define CHR_MAXSTR 1024
52 #define CHR_MAXEQUIV 32
54 const char *CHR_UNKNOWN = "\001";
55 const char *CHR_SPACE = "\002";
56 const char *CHR_BASE = "\003";
58 extern char *data1_tabpath;
61 * Character map trie node.
65 chr_t_entry **children; /* array of children */
66 unsigned char *target; /* target for this node, if any */
67 unsigned char *equiv; /* equivalent to, or sumthin */
71 * General argument structure for callback functions (internal use only)
73 typedef struct chrwork
76 char string[CHR_MAXSTR+1];
80 * Add an entry to the character map.
82 static chr_t_entry *set_map_string(chr_t_entry *root, char *from, int len,
87 root = xmalloc(sizeof(*root));
93 if (!root->target || (char*) root->target == CHR_SPACE ||
94 (char*) root->target == CHR_UNKNOWN)
95 root->target = (unsigned char *) xstrdup(to);
96 else if ((char*) to != CHR_SPACE)
97 logf(LOG_DEBUG, "Character map overlap");
105 root->children = xmalloc(sizeof(chr_t_entry*) * 256);
106 for (i = 0; i < 256; i++)
107 root->children[i] = 0;
109 if (!(root->children[(unsigned char) *from] =
110 set_map_string(root->children[(unsigned char) *from], from + 1,
117 int chr_map_chrs(chr_t_entry *t, char **from, int len, int *read, char **to,
123 while (len && t->children && t->children[(unsigned char) **from])
125 t = t->children[(unsigned char) **from];
129 /* if there were no matches, we are still at the root node,
130 which always has a null mapping */
131 for (s = t->target; *s && max; s++)
144 static chr_t_entry *find_entry(chr_t_entry *t, char **from, int len)
148 if (t->children && t->children[(unsigned char) **from])
153 if ((res = find_entry(t->children[(unsigned char) *pos],
159 /* no children match. use ourselves, if we have a target */
160 return t->target ? t : 0;
163 char **chr_map_input(chr_t_entry *t, char **from, int len)
165 static char *buf[2] = {0, 0};
168 if (!(res = find_entry(t, from, len)))
170 buf[0] = (char *) res->target;
176 char **chr_map_input(chr_t_entry *t, char **from, int len)
178 static char *buf[2] = {0, 0}, str[2] = {0, 0};
183 while (len && t->children && t->children[(unsigned char) **from])
185 t = t->children[(unsigned char) **from];
189 buf[0] = (char*) t->target;
191 else /* null mapping */
198 else if (isspace(**from))
199 buf[0] = (char*) CHR_SPACE;
201 buf[0] = (char*) CHR_UNKNOWN;
206 /* return (char*) t->target; */
211 static unsigned char prim(char **s)
222 case '\\': c = '\\'; (*s)++; break;
223 case 'r': c = '\r'; (*s)++; break;
224 case 'n': c = '\n'; (*s)++; break;
225 case 't': c = '\t'; (*s)++; break;
226 case 's': c = ' '; (*s)++; break;
227 case 'x': sscanf(*s, "x%2x", &i); c = i; *s += 3; break;
228 case '{': case '[': case '(': case '}': case ']': case ')':
231 default: sscanf(*s, "%3o", &i); c = i; *s += 3; break;
242 * Add an entry to the value space.
244 static void fun_addentry(char *s, void *data, int num)
246 chrmaptab *tab = data;
249 tmp[0] = num; tmp[1] = '\0';
250 tab->input = set_map_string(tab->input, s, strlen(s), tmp);
251 tab->output[num + tab->base_uppercase] = (unsigned char *) xstrdup(s);
256 * Add a space-entry to the value space.
258 static void fun_addspace(char *s, void *data, int num)
260 chrmaptab *tab = data;
261 tab->input = set_map_string(tab->input, s, strlen(s), (char*) CHR_SPACE);
265 * Create a string containing the mapped characters provided.
267 static void fun_mkstring(char *s, void *data, int num)
272 res = chr_map_input(arg->map->input, &s, strlen(s));
273 if (*res == (char*) CHR_UNKNOWN)
274 logf(LOG_WARN, "Map: '%s' has no mapping", p);
275 strncat(arg->string, *res, CHR_MAXSTR - strlen(arg->string));
276 arg->string[CHR_MAXSTR] = '\0';
280 * Add a map to the string contained in the argument.
282 static void fun_addmap(char *s, void *data, int num)
286 assert(arg->map->input);
287 set_map_string(arg->map->input, s, strlen(s), arg->string);
290 static int scan_string(char *s, void (*fun)(char *c, void *data, int num),
291 void *data, int *num)
293 unsigned char c, str[1024], begin, end, *p;
304 logf(LOG_FATAL, "Bad range in char-map");
311 logf(LOG_FATAL, "Bad range in char-map");
315 for (c = begin; c <= end; c++)
317 str[0] = c; str[1] = '\0';
318 (*fun)((char *) str, data, num ? (*num)++ : 0);
321 case '[': s++; abort(); break;
323 p = (unsigned char*) ++s;
324 /* Find the end-marker, ignoring escapes */
327 if (!(p = (unsigned char*) strchr((char*) p, ')')))
329 logf(LOG_FATAL, "Missing ')' in string");
333 while (*(p - 1) == '\\');
335 (*fun)(s, data, num ? (*num)++ : 0);
340 str[0] = c; str[1] = '\0';
341 (*fun)((char *) str, data, num ? (*num)++ : 0);
347 chrmaptab *chr_read_maptab(char *name)
350 char line[512], *argv[50];
351 chrmaptab *res = xmalloc(sizeof(*res));
352 int argc, num = (int) *CHR_BASE, i;
354 if (!(f = yaz_path_fopen(data1_tabpath, name, "r")))
356 logf(LOG_WARN|LOG_ERRNO, "%s", name);
359 res = xmalloc(sizeof(*res));
360 res->input = xmalloc(sizeof(*res->input));
361 res->input->target = (unsigned char*) CHR_UNKNOWN;
362 res->input->equiv = 0;
364 res->input->children = xmalloc(sizeof(res->input) * 256);
365 for (i = 0; i < 256; i++)
367 res->input->children[i] = xmalloc(sizeof(*res->input));
368 res->input->children[i]->children = 0;
369 res->input->children[i]->target = (unsigned char*) CHR_UNKNOWN;
370 res->input->children[i]->equiv = 0;
373 res->input->children = 0;
375 res->query_equiv = 0;
376 for (i = *CHR_BASE; i < 256; i++)
378 res->output[(int) *CHR_SPACE] = (unsigned char *) " ";
379 res->output[(int) *CHR_UNKNOWN] = (unsigned char*) "@";
380 res->base_uppercase = 0;
382 while ((argc = readconf_line(f, line, 512, argv, 50)))
383 if (!yaz_matchstr(argv[0], "lowercase"))
387 logf(LOG_FATAL, "Syntax error in charmap");
391 if (scan_string(argv[1], fun_addentry, res, &num) < 0)
393 logf(LOG_FATAL, "Bad value-set specification");
397 res->base_uppercase = num;
398 res->output[(int) *CHR_SPACE + num] = (unsigned char *) " ";
399 res->output[(int) *CHR_UNKNOWN + num] = (unsigned char*) "@";
400 num = (int) *CHR_BASE;
402 else if (!yaz_matchstr(argv[0], "uppercase"))
404 if (!res->base_uppercase)
406 logf(LOG_FATAL, "Uppercase directive with no lowercase set");
412 logf(LOG_FATAL, "Syntax error in charmap");
416 if (scan_string(argv[1], fun_addentry, res, &num) < 0)
418 logf(LOG_FATAL, "Bad value-set specification");
423 else if (!yaz_matchstr(argv[0], "space"))
427 logf(LOG_FATAL, "Syntax error in charmap");
431 if (scan_string(argv[1], fun_addspace, res, 0) < 0)
433 logf(LOG_FATAL, "Bad space specification");
438 else if (!yaz_matchstr(argv[0], "map"))
444 logf(LOG_FATAL, "charmap MAP directive requires 2 args");
449 buf.string[0] = '\0';
450 if (scan_string(argv[2], fun_mkstring, &buf, 0) < 0)
452 logf(LOG_FATAL, "Bad map target");
456 if (scan_string(argv[1], fun_addmap, &buf, 0) < 0)
458 logf(LOG_FATAL, "Bad map source");
466 logf(LOG_WARN, "Syntax error at '%s' in %s", line, file);