2 * Copyright (C) 1996-1997, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.12 1997-09-05 15:30:11 adam
8 * Changed prototype for chr_map_input - added const.
9 * Added support for C++, headers uses extern "C" for public definitions.
11 * Revision 1.11 1997/09/05 09:52:32 adam
12 * Extra argument added to function chr_read_maptab (tab path).
14 * Revision 1.10 1997/07/01 13:01:08 adam
15 * Bug fix in routine find_entry: didn't take into account the len arg.
17 * Revision 1.9 1996/10/29 13:48:14 adam
18 * Updated to use zebrautl.h instead of alexutil.h.
20 * Revision 1.8 1996/10/18 12:39:23 adam
21 * Uses LOG_DEBUG instead of LOG_WARN for "Character map overlap".
23 * Revision 1.7 1996/06/06 12:08:56 quinn
26 * Revision 1.6 1996/06/04 13:28:00 quinn
27 * More work on charmapping
29 * Revision 1.5 1996/06/04 08:32:15 quinn
30 * Moved default keymap to keychars.c
32 * Revision 1.4 1996/06/03 16:32:13 quinn
35 * Revision 1.3 1996/06/03 15:17:46 quinn
38 * Revision 1.2 1996/06/03 10:15:09 quinn
39 * Fixed bug in mapping function.
41 * Revision 1.1 1996/05/31 09:07:18 quinn
42 * Work on character-set handling
48 * Support module to handle character-conversions into and out of the
61 #define CHR_MAXSTR 1024
62 #define CHR_MAXEQUIV 32
64 const char *CHR_UNKNOWN = "\001";
65 const char *CHR_SPACE = "\002";
66 const char *CHR_BASE = "\003";
69 * Character map trie node.
73 chr_t_entry **children; /* array of children */
74 unsigned char *target; /* target for this node, if any */
75 unsigned char *equiv; /* equivalent to, or sumthin */
79 * General argument structure for callback functions (internal use only)
81 typedef struct chrwork
84 char string[CHR_MAXSTR+1];
88 * Add an entry to the character map.
90 static chr_t_entry *set_map_string(chr_t_entry *root, const char *from,
95 root = xmalloc(sizeof(*root));
101 if (!root->target || (char*) root->target == CHR_SPACE ||
102 (char*) root->target == CHR_UNKNOWN)
103 root->target = (unsigned char *) xstrdup(to);
104 else if ((char*) to != CHR_SPACE)
105 logf(LOG_DEBUG, "Character map overlap");
113 root->children = xmalloc(sizeof(chr_t_entry*) * 256);
114 for (i = 0; i < 256; i++)
115 root->children[i] = 0;
117 if (!(root->children[(unsigned char) *from] =
118 set_map_string(root->children[(unsigned char) *from], from + 1,
125 int chr_map_chrs(chr_t_entry *t, char **from, int len, int *read, char **to,
131 while (len && t->children && t->children[(unsigned char) **from])
133 t = t->children[(unsigned char) **from];
137 /* if there were no matches, we are still at the root node,
138 which always has a null mapping */
139 for (s = t->target; *s && max; s++)
151 static chr_t_entry *find_entry(chr_t_entry *t, const char **from, int len)
155 if (len && t->children && t->children[(unsigned char) **from])
157 const char *pos = *from;
160 if ((res = find_entry(t->children[(unsigned char) *pos],
166 /* no children match. use ourselves, if we have a target */
167 return t->target ? t : 0;
170 const char **chr_map_input(chr_t_entry *t, const char **from, int len)
172 static const char *buf[2] = {0, 0};
175 if (!(res = find_entry(t, from, len)))
177 buf[0] = (char *) res->target;
182 static unsigned char prim(char **s)
193 case '\\': c = '\\'; (*s)++; break;
194 case 'r': c = '\r'; (*s)++; break;
195 case 'n': c = '\n'; (*s)++; break;
196 case 't': c = '\t'; (*s)++; break;
197 case 's': c = ' '; (*s)++; break;
198 case 'x': sscanf(*s, "x%2x", &i); c = i; *s += 3; break;
199 case '{': case '[': case '(': case '}': case ']': case ')':
202 default: sscanf(*s, "%3o", &i); c = i; *s += 3; break;
213 * Add an entry to the value space.
215 static void fun_addentry(const char *s, void *data, int num)
217 chrmaptab *tab = data;
220 tmp[0] = num; tmp[1] = '\0';
221 tab->input = set_map_string(tab->input, s, strlen(s), tmp);
222 tab->output[num + tab->base_uppercase] = (unsigned char *) xstrdup(s);
227 * Add a space-entry to the value space.
229 static void fun_addspace(const char *s, void *data, int num)
231 chrmaptab *tab = data;
232 tab->input = set_map_string(tab->input, s, strlen(s), (char*) CHR_SPACE);
236 * Create a string containing the mapped characters provided.
238 static void fun_mkstring(const char *s, void *data, int num)
241 const char **res, *p = s;
243 res = chr_map_input(arg->map->input, &s, strlen(s));
244 if (*res == (char*) CHR_UNKNOWN)
245 logf(LOG_WARN, "Map: '%s' has no mapping", p);
246 strncat(arg->string, *res, CHR_MAXSTR - strlen(arg->string));
247 arg->string[CHR_MAXSTR] = '\0';
251 * Add a map to the string contained in the argument.
253 static void fun_addmap(const char *s, void *data, int num)
257 assert(arg->map->input);
258 set_map_string(arg->map->input, s, strlen(s), arg->string);
261 static int scan_string(char *s, void (*fun)(const char *c, void *data, int num),
262 void *data, int *num)
264 unsigned char c, str[1024], begin, end, *p;
275 logf(LOG_FATAL, "Bad range in char-map");
282 logf(LOG_FATAL, "Bad range in char-map");
286 for (c = begin; c <= end; c++)
288 str[0] = c; str[1] = '\0';
289 (*fun)((char *) str, data, num ? (*num)++ : 0);
292 case '[': s++; abort(); break;
294 p = (unsigned char*) ++s;
295 /* Find the end-marker, ignoring escapes */
298 if (!(p = (unsigned char*) strchr((char*) p, ')')))
300 logf(LOG_FATAL, "Missing ')' in string");
304 while (*(p - 1) == '\\');
306 (*fun)(s, data, num ? (*num)++ : 0);
311 str[0] = c; str[1] = '\0';
312 (*fun)((char *) str, data, num ? (*num)++ : 0);
318 chrmaptab *chr_read_maptab(const char *tabpath, const char *name)
321 char line[512], *argv[50];
322 chrmaptab *res = xmalloc(sizeof(*res));
323 int argc, num = (int) *CHR_BASE, i;
325 if (!(f = yaz_path_fopen(tabpath, name, "r")))
327 logf(LOG_WARN|LOG_ERRNO, "%s", name);
330 res = xmalloc(sizeof(*res));
331 res->input = xmalloc(sizeof(*res->input));
332 res->input->target = (unsigned char*) CHR_UNKNOWN;
333 res->input->equiv = 0;
335 res->input->children = xmalloc(sizeof(res->input) * 256);
336 for (i = 0; i < 256; i++)
338 res->input->children[i] = xmalloc(sizeof(*res->input));
339 res->input->children[i]->children = 0;
340 res->input->children[i]->target = (unsigned char*) CHR_UNKNOWN;
341 res->input->children[i]->equiv = 0;
344 res->input->children = 0;
346 res->query_equiv = 0;
347 for (i = *CHR_BASE; i < 256; i++)
349 res->output[(int) *CHR_SPACE] = (unsigned char *) " ";
350 res->output[(int) *CHR_UNKNOWN] = (unsigned char*) "@";
351 res->base_uppercase = 0;
353 while ((argc = readconf_line(f, line, 512, argv, 50)))
354 if (!yaz_matchstr(argv[0], "lowercase"))
358 logf(LOG_FATAL, "Syntax error in charmap");
362 if (scan_string(argv[1], fun_addentry, res, &num) < 0)
364 logf(LOG_FATAL, "Bad value-set specification");
368 res->base_uppercase = num;
369 res->output[(int) *CHR_SPACE + num] = (unsigned char *) " ";
370 res->output[(int) *CHR_UNKNOWN + num] = (unsigned char*) "@";
371 num = (int) *CHR_BASE;
373 else if (!yaz_matchstr(argv[0], "uppercase"))
375 if (!res->base_uppercase)
377 logf(LOG_FATAL, "Uppercase directive with no lowercase set");
383 logf(LOG_FATAL, "Syntax error in charmap");
387 if (scan_string(argv[1], fun_addentry, res, &num) < 0)
389 logf(LOG_FATAL, "Bad value-set specification");
394 else if (!yaz_matchstr(argv[0], "space"))
398 logf(LOG_FATAL, "Syntax error in charmap");
402 if (scan_string(argv[1], fun_addspace, res, 0) < 0)
404 logf(LOG_FATAL, "Bad space specification");
409 else if (!yaz_matchstr(argv[0], "map"))
415 logf(LOG_FATAL, "charmap MAP directive requires 2 args");
420 buf.string[0] = '\0';
421 if (scan_string(argv[2], fun_mkstring, &buf, 0) < 0)
423 logf(LOG_FATAL, "Bad map target");
427 if (scan_string(argv[1], fun_addmap, &buf, 0) < 0)
429 logf(LOG_FATAL, "Bad map source");
437 logf(LOG_WARN, "Syntax error at '%s' in %s", line, file);