2 * Copyright (C) 1996-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.17 1999-09-08 12:13:21 adam
8 * Fixed minor bug "replace"-mappings. Removed some logging messages.
10 * Revision 1.16 1999/09/07 07:19:21 adam
11 * Work on character mapping. Implemented replace rules.
13 * Revision 1.15 1999/05/26 07:49:14 adam
16 * Revision 1.14 1998/10/13 20:09:18 adam
17 * Changed call to readconf_line.
19 * Revision 1.13 1997/10/27 14:33:06 adam
20 * Moved towards generic character mapping depending on "structure"
21 * field in abstract syntax file. Fixed a few memory leaks. Fixed
22 * bug with negative integers when doing searches with relational
25 * Revision 1.12 1997/09/05 15:30:11 adam
26 * Changed prototype for chr_map_input - added const.
27 * Added support for C++, headers uses extern "C" for public definitions.
29 * Revision 1.11 1997/09/05 09:52:32 adam
30 * Extra argument added to function chr_read_maptab (tab path).
32 * Revision 1.10 1997/07/01 13:01:08 adam
33 * Bug fix in routine find_entry: didn't take into account the len arg.
35 * Revision 1.9 1996/10/29 13:48:14 adam
36 * Updated to use zebrautl.h instead of alexutil.h.
38 * Revision 1.8 1996/10/18 12:39:23 adam
39 * Uses LOG_DEBUG instead of LOG_WARN for "Character map overlap".
41 * Revision 1.7 1996/06/06 12:08:56 quinn
44 * Revision 1.6 1996/06/04 13:28:00 quinn
45 * More work on charmapping
47 * Revision 1.5 1996/06/04 08:32:15 quinn
48 * Moved default keymap to keychars.c
50 * Revision 1.4 1996/06/03 16:32:13 quinn
53 * Revision 1.3 1996/06/03 15:17:46 quinn
56 * Revision 1.2 1996/06/03 10:15:09 quinn
57 * Fixed bug in mapping function.
59 * Revision 1.1 1996/05/31 09:07:18 quinn
60 * Work on character-set handling
66 * Support module to handle character-conversions into and out of the
77 #define CHR_MAXSTR 1024
78 #define CHR_MAXEQUIV 32
80 const char *CHR_UNKNOWN = "\001";
81 const char *CHR_SPACE = "\002";
82 const char *CHR_BASE = "\003";
86 chr_t_entry *input; /* mapping table for input data */
87 chr_t_entry *q_input; /* mapping table for queries */
88 unsigned char *output[256]; /* return mapping - for display of registers */
89 int base_uppercase; /* Start of upper-case ordinals */
94 * Character map trie node.
98 chr_t_entry **children; /* array of children */
99 unsigned char **target; /* target for this node, if any */
103 * General argument structure for callback functions (internal use only)
105 typedef struct chrwork
108 char string[CHR_MAXSTR+1];
112 * Add an entry to the character map.
114 static chr_t_entry *set_map_string(chr_t_entry *root, NMEM nmem,
115 const char *from, int len, char *to)
119 root = (chr_t_entry *) nmem_malloc(nmem, sizeof(*root));
125 if (!root->target || !root->target[0] || strcmp(root->target[0], to))
127 root->target = (unsigned char **)
128 nmem_malloc(nmem, sizeof(*root->target)*2);
129 root->target[0] = (unsigned char *) nmem_strdup(nmem, to);
139 root->children = (chr_t_entry **)
140 nmem_malloc(nmem, sizeof(chr_t_entry*) * 256);
141 for (i = 0; i < 256; i++)
142 root->children[i] = 0;
144 if (!(root->children[(unsigned char) *from] =
145 set_map_string(root->children[(unsigned char) *from], nmem,
146 from + 1, len - 1, to)))
152 static chr_t_entry *find_entry(chr_t_entry *t, const char **from, int len)
156 if (len && t->children && t->children[(unsigned char) **from])
158 const char *pos = *from;
161 if ((res = find_entry(t->children[(unsigned char) *pos],
167 /* no children match. use ourselves, if we have a target */
168 return t->target ? t : 0;
171 static chr_t_entry *find_entry_x(chr_t_entry *t, const char **from, int *len)
176 { /* switch to next buffer */
182 if (*len > 0 && t->children && t->children[(unsigned char) **from])
184 const char *old_from = *from;
189 if ((res = find_entry_x(t->children[(unsigned char) *old_from],
196 /* no children match. use ourselves, if we have a target */
197 return t->target ? t : 0;
200 const char **chr_map_input_x(chrmaptab maptab, const char **from, int *len)
202 chr_t_entry *t = maptab->input;
205 if (!(res = find_entry_x(t, from, len)))
207 return (const char **) (res->target);
210 const char **chr_map_input(chrmaptab maptab, const char **from, int len)
212 chr_t_entry *t = maptab->input;
218 if (!(res = find_entry_x(t, from, len_tmp)))
220 return (const char **) (res->target);
223 const char *chr_map_output(chrmaptab maptab, const char **from, int len)
225 unsigned char c = ** (unsigned char **) from;
227 return (const char*) maptab->output[c];
230 unsigned char zebra_prim(char **s)
241 case '\\': c = '\\'; (*s)++; break;
242 case 'r': c = '\r'; (*s)++; break;
243 case 'n': c = '\n'; (*s)++; break;
244 case 't': c = '\t'; (*s)++; break;
245 case 's': c = ' '; (*s)++; break;
246 case 'x': sscanf(*s, "x%2x", &i); c = i; *s += 3; break;
257 sscanf(*s, "%3o", &i);
273 * Add an entry to the value space.
275 static void fun_addentry(const char *s, void *data, int num)
277 chrmaptab tab = (chrmaptab) data;
280 tmp[0] = num; tmp[1] = '\0';
281 tab->input = set_map_string(tab->input, tab->nmem, s, strlen(s), tmp);
282 tab->output[num + tab->base_uppercase] =
283 (unsigned char *) nmem_strdup(tab->nmem, s);
288 * Add a space-entry to the value space.
290 static void fun_addspace(const char *s, void *data, int num)
292 chrmaptab tab = (chrmaptab) data;
293 tab->input = set_map_string(tab->input, tab->nmem, s, strlen(s),
298 * Create a string containing the mapped characters provided.
300 static void fun_mkstring(const char *s, void *data, int num)
302 chrwork *arg = (chrwork *) data;
303 const char **res, *p = s;
305 res = chr_map_input(arg->map, &s, strlen(s));
306 if (*res == (char*) CHR_UNKNOWN)
307 logf(LOG_WARN, "Map: '%s' has no mapping", p);
308 strncat(arg->string, *res, CHR_MAXSTR - strlen(arg->string));
309 arg->string[CHR_MAXSTR] = '\0';
313 * Add a map to the string contained in the argument.
315 static void fun_add_map(const char *s, void *data, int num)
317 chrwork *arg = (chrwork *) data;
319 assert(arg->map->input);
320 logf (LOG_DEBUG, "set map %.*s", (int) strlen(s), s);
321 set_map_string(arg->map->input, arg->map->nmem, s, strlen(s), arg->string);
322 for (s = arg->string; *s; s++)
323 logf (LOG_DEBUG, " %3d", (unsigned char) *s);
327 * Add a query map to the string contained in the argument.
329 static void fun_add_qmap(const char *s, void *data, int num)
331 chrwork *arg = (chrwork *) data;
333 assert(arg->map->q_input);
334 logf (LOG_DEBUG, "set qmap %.*s", (int) strlen(s), s);
335 set_map_string(arg->map->q_input, arg->map->nmem, s,
336 strlen(s), arg->string);
337 for (s = arg->string; *s; s++)
338 logf (LOG_DEBUG, " %3d", (unsigned char) *s);
342 static int scan_string(char *s,
343 void (*fun)(const char *c, void *data, int num),
344 void *data, int *num)
346 unsigned char c, str[1024], begin, end, *p;
354 begin = zebra_prim(&s);
357 logf(LOG_FATAL, "Bad range in char-map");
361 end = zebra_prim(&s);
364 logf(LOG_FATAL, "Bad range in char-map");
368 for (c = begin; c <= end; c++)
370 str[0] = c; str[1] = '\0';
371 (*fun)((char *) str, data, num ? (*num)++ : 0);
374 case '[': s++; abort(); break;
376 p = (unsigned char*) ++s;
377 /* Find the end-marker, ignoring escapes */
380 if (!(p = (unsigned char*) strchr((char*) p, ')')))
382 logf(LOG_FATAL, "Missing ')' in string");
386 while (*(p - 1) == '\\');
388 (*fun)(s, data, num ? (*num)++ : 0);
393 str[0] = c; str[1] = '\0';
394 (*fun)((char *) str, data, num ? (*num)++ : 0);
400 chrmaptab chrmaptab_create(const char *tabpath, const char *name, int map_only)
403 char line[512], *argv[50];
407 int argc, num = (int) *CHR_BASE, i;
410 logf (LOG_DEBUG, "maptab %s open", name);
411 if (!(f = yaz_path_fopen(tabpath, name, "r")))
413 logf(LOG_WARN|LOG_ERRNO, "%s", name);
416 nmem = nmem_create ();
417 res = (chrmaptab) nmem_malloc(nmem, sizeof(*res));
419 res->input = (chr_t_entry *) nmem_malloc(res->nmem, sizeof(*res->input));
420 res->input->target = (unsigned char **)
421 nmem_malloc(res->nmem, sizeof(*res->input->target) * 2);
422 res->input->target[0] = (unsigned char*) CHR_UNKNOWN;
423 res->input->target[1] = 0;
424 res->input->children = (chr_t_entry **)
425 nmem_malloc(res->nmem, sizeof(res->input) * 256);
426 for (i = 0; i < 256; i++)
428 res->input->children[i] = (chr_t_entry *)
429 nmem_malloc(res->nmem, sizeof(*res->input));
430 res->input->children[i]->children = 0;
431 res->input->children[i]->target = (unsigned char **)
432 nmem_malloc (res->nmem, 2 * sizeof(unsigned char *));
433 res->input->children[i]->target[1] = 0;
436 res->input->children[i]->target[0] = (unsigned char *)
437 nmem_malloc (res->nmem, 2 * sizeof(unsigned char));
438 res->input->children[i]->target[0][0] = i;
439 res->input->children[i]->target[0][1] = 0;
442 res->input->children[i]->target[0] = (unsigned char*) CHR_UNKNOWN;
444 res->q_input = (chr_t_entry *)
445 nmem_malloc(res->nmem, sizeof(*res->q_input));
446 res->q_input->target = 0;
447 res->q_input->children = 0;
449 for (i = *CHR_BASE; i < 256; i++)
451 res->output[(int) *CHR_SPACE] = (unsigned char *) " ";
452 res->output[(int) *CHR_UNKNOWN] = (unsigned char*) "@";
453 res->base_uppercase = 0;
455 while (!errors && (argc = readconf_line(f, &lineno, line, 512, argv, 50)))
456 if (!map_only && !yaz_matchstr(argv[0], "lowercase"))
460 logf(LOG_FATAL, "Syntax error in charmap");
463 if (scan_string(argv[1], fun_addentry, res, &num) < 0)
465 logf(LOG_FATAL, "Bad value-set specification");
468 res->base_uppercase = num;
469 res->output[(int) *CHR_SPACE + num] = (unsigned char *) " ";
470 res->output[(int) *CHR_UNKNOWN + num] = (unsigned char*) "@";
471 num = (int) *CHR_BASE;
473 else if (!map_only && !yaz_matchstr(argv[0], "uppercase"))
475 if (!res->base_uppercase)
477 logf(LOG_FATAL, "Uppercase directive with no lowercase set");
482 logf(LOG_FATAL, "Missing arg for uppercase directive");
485 if (scan_string(argv[1], fun_addentry, res, &num) < 0)
487 logf(LOG_FATAL, "Bad value-set specification");
491 else if (!map_only && !yaz_matchstr(argv[0], "space"))
495 logf(LOG_FATAL, "Syntax error in charmap");
498 if (scan_string(argv[1], fun_addspace, res, 0) < 0)
500 logf(LOG_FATAL, "Bad space specification");
504 else if (!yaz_matchstr(argv[0], "map"))
510 logf(LOG_FATAL, "charmap directive map requires 2 args");
514 buf.string[0] = '\0';
515 if (scan_string(argv[2], fun_mkstring, &buf, 0) < 0)
517 logf(LOG_FATAL, "Bad map target");
520 if (scan_string(argv[1], fun_add_map, &buf, 0) < 0)
522 logf(LOG_FATAL, "Bad map source");
526 else if (!yaz_matchstr(argv[0], "qmap"))
532 logf(LOG_FATAL, "charmap directive qmap requires 2 args");
536 buf.string[0] = '\0';
537 if (scan_string(argv[2], fun_mkstring, &buf, 0) < 0)
539 logf(LOG_FATAL, "Bad qmap target");
542 if (scan_string(argv[1], fun_add_qmap, &buf, 0) < 0)
544 logf(LOG_FATAL, "Bad qmap source");
550 logf(LOG_WARN, "Syntax error at '%s' in %s", line, name);
556 chrmaptab_destroy(res);
559 logf (LOG_DEBUG, "maptab %s close %d errors", name, errors);
563 void chrmaptab_destroy(chrmaptab tab)
566 nmem_destroy (tab->nmem);