2 * Copyright (C) 1996-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.18 1999-11-30 13:48:04 adam
8 * Improved installation. Updated for inclusion of YAZ header files.
10 * Revision 1.17 1999/09/08 12:13:21 adam
11 * Fixed minor bug "replace"-mappings. Removed some logging messages.
13 * Revision 1.16 1999/09/07 07:19:21 adam
14 * Work on character mapping. Implemented replace rules.
16 * Revision 1.15 1999/05/26 07:49:14 adam
19 * Revision 1.14 1998/10/13 20:09:18 adam
20 * Changed call to readconf_line.
22 * Revision 1.13 1997/10/27 14:33:06 adam
23 * Moved towards generic character mapping depending on "structure"
24 * field in abstract syntax file. Fixed a few memory leaks. Fixed
25 * bug with negative integers when doing searches with relational
28 * Revision 1.12 1997/09/05 15:30:11 adam
29 * Changed prototype for chr_map_input - added const.
30 * Added support for C++, headers uses extern "C" for public definitions.
32 * Revision 1.11 1997/09/05 09:52:32 adam
33 * Extra argument added to function chr_read_maptab (tab path).
35 * Revision 1.10 1997/07/01 13:01:08 adam
36 * Bug fix in routine find_entry: didn't take into account the len arg.
38 * Revision 1.9 1996/10/29 13:48:14 adam
39 * Updated to use zebrautl.h instead of alexutil.h.
41 * Revision 1.8 1996/10/18 12:39:23 adam
42 * Uses LOG_DEBUG instead of LOG_WARN for "Character map overlap".
44 * Revision 1.7 1996/06/06 12:08:56 quinn
47 * Revision 1.6 1996/06/04 13:28:00 quinn
48 * More work on charmapping
50 * Revision 1.5 1996/06/04 08:32:15 quinn
51 * Moved default keymap to keychars.c
53 * Revision 1.4 1996/06/03 16:32:13 quinn
56 * Revision 1.3 1996/06/03 15:17:46 quinn
59 * Revision 1.2 1996/06/03 10:15:09 quinn
60 * Fixed bug in mapping function.
62 * Revision 1.1 1996/05/31 09:07:18 quinn
63 * Work on character-set handling
69 * Support module to handle character-conversions into and out of the
77 #include <yaz/yaz-util.h>
80 #define CHR_MAXSTR 1024
81 #define CHR_MAXEQUIV 32
83 const char *CHR_UNKNOWN = "\001";
84 const char *CHR_SPACE = "\002";
85 const char *CHR_BASE = "\003";
89 chr_t_entry *input; /* mapping table for input data */
90 chr_t_entry *q_input; /* mapping table for queries */
91 unsigned char *output[256]; /* return mapping - for display of registers */
92 int base_uppercase; /* Start of upper-case ordinals */
97 * Character map trie node.
101 chr_t_entry **children; /* array of children */
102 unsigned char **target; /* target for this node, if any */
106 * General argument structure for callback functions (internal use only)
108 typedef struct chrwork
111 char string[CHR_MAXSTR+1];
115 * Add an entry to the character map.
117 static chr_t_entry *set_map_string(chr_t_entry *root, NMEM nmem,
118 const char *from, int len, char *to)
122 root = (chr_t_entry *) nmem_malloc(nmem, sizeof(*root));
128 if (!root->target || !root->target[0] || strcmp(root->target[0], to))
130 root->target = (unsigned char **)
131 nmem_malloc(nmem, sizeof(*root->target)*2);
132 root->target[0] = (unsigned char *) nmem_strdup(nmem, to);
142 root->children = (chr_t_entry **)
143 nmem_malloc(nmem, sizeof(chr_t_entry*) * 256);
144 for (i = 0; i < 256; i++)
145 root->children[i] = 0;
147 if (!(root->children[(unsigned char) *from] =
148 set_map_string(root->children[(unsigned char) *from], nmem,
149 from + 1, len - 1, to)))
155 static chr_t_entry *find_entry(chr_t_entry *t, const char **from, int len)
159 if (len && t->children && t->children[(unsigned char) **from])
161 const char *pos = *from;
164 if ((res = find_entry(t->children[(unsigned char) *pos],
170 /* no children match. use ourselves, if we have a target */
171 return t->target ? t : 0;
174 static chr_t_entry *find_entry_x(chr_t_entry *t, const char **from, int *len)
179 { /* switch to next buffer */
185 if (*len > 0 && t->children && t->children[(unsigned char) **from])
187 const char *old_from = *from;
192 if ((res = find_entry_x(t->children[(unsigned char) *old_from],
199 /* no children match. use ourselves, if we have a target */
200 return t->target ? t : 0;
203 const char **chr_map_input_x(chrmaptab maptab, const char **from, int *len)
205 chr_t_entry *t = maptab->input;
208 if (!(res = find_entry_x(t, from, len)))
210 return (const char **) (res->target);
213 const char **chr_map_input(chrmaptab maptab, const char **from, int len)
215 chr_t_entry *t = maptab->input;
221 if (!(res = find_entry_x(t, from, len_tmp)))
223 return (const char **) (res->target);
226 const char *chr_map_output(chrmaptab maptab, const char **from, int len)
228 unsigned char c = ** (unsigned char **) from;
230 return (const char*) maptab->output[c];
233 unsigned char zebra_prim(char **s)
244 case '\\': c = '\\'; (*s)++; break;
245 case 'r': c = '\r'; (*s)++; break;
246 case 'n': c = '\n'; (*s)++; break;
247 case 't': c = '\t'; (*s)++; break;
248 case 's': c = ' '; (*s)++; break;
249 case 'x': sscanf(*s, "x%2x", &i); c = i; *s += 3; break;
260 sscanf(*s, "%3o", &i);
276 * Add an entry to the value space.
278 static void fun_addentry(const char *s, void *data, int num)
280 chrmaptab tab = (chrmaptab) data;
283 tmp[0] = num; tmp[1] = '\0';
284 tab->input = set_map_string(tab->input, tab->nmem, s, strlen(s), tmp);
285 tab->output[num + tab->base_uppercase] =
286 (unsigned char *) nmem_strdup(tab->nmem, s);
291 * Add a space-entry to the value space.
293 static void fun_addspace(const char *s, void *data, int num)
295 chrmaptab tab = (chrmaptab) data;
296 tab->input = set_map_string(tab->input, tab->nmem, s, strlen(s),
301 * Create a string containing the mapped characters provided.
303 static void fun_mkstring(const char *s, void *data, int num)
305 chrwork *arg = (chrwork *) data;
306 const char **res, *p = s;
308 res = chr_map_input(arg->map, &s, strlen(s));
309 if (*res == (char*) CHR_UNKNOWN)
310 logf(LOG_WARN, "Map: '%s' has no mapping", p);
311 strncat(arg->string, *res, CHR_MAXSTR - strlen(arg->string));
312 arg->string[CHR_MAXSTR] = '\0';
316 * Add a map to the string contained in the argument.
318 static void fun_add_map(const char *s, void *data, int num)
320 chrwork *arg = (chrwork *) data;
322 assert(arg->map->input);
323 logf (LOG_DEBUG, "set map %.*s", (int) strlen(s), s);
324 set_map_string(arg->map->input, arg->map->nmem, s, strlen(s), arg->string);
325 for (s = arg->string; *s; s++)
326 logf (LOG_DEBUG, " %3d", (unsigned char) *s);
330 * Add a query map to the string contained in the argument.
332 static void fun_add_qmap(const char *s, void *data, int num)
334 chrwork *arg = (chrwork *) data;
336 assert(arg->map->q_input);
337 logf (LOG_DEBUG, "set qmap %.*s", (int) strlen(s), s);
338 set_map_string(arg->map->q_input, arg->map->nmem, s,
339 strlen(s), arg->string);
340 for (s = arg->string; *s; s++)
341 logf (LOG_DEBUG, " %3d", (unsigned char) *s);
345 static int scan_string(char *s,
346 void (*fun)(const char *c, void *data, int num),
347 void *data, int *num)
349 unsigned char c, str[1024], begin, end, *p;
357 begin = zebra_prim(&s);
360 logf(LOG_FATAL, "Bad range in char-map");
364 end = zebra_prim(&s);
367 logf(LOG_FATAL, "Bad range in char-map");
371 for (c = begin; c <= end; c++)
373 str[0] = c; str[1] = '\0';
374 (*fun)((char *) str, data, num ? (*num)++ : 0);
377 case '[': s++; abort(); break;
379 p = (unsigned char*) ++s;
380 /* Find the end-marker, ignoring escapes */
383 if (!(p = (unsigned char*) strchr((char*) p, ')')))
385 logf(LOG_FATAL, "Missing ')' in string");
389 while (*(p - 1) == '\\');
391 (*fun)(s, data, num ? (*num)++ : 0);
396 str[0] = c; str[1] = '\0';
397 (*fun)((char *) str, data, num ? (*num)++ : 0);
403 chrmaptab chrmaptab_create(const char *tabpath, const char *name, int map_only)
406 char line[512], *argv[50];
410 int argc, num = (int) *CHR_BASE, i;
413 logf (LOG_DEBUG, "maptab %s open", name);
414 if (!(f = yaz_path_fopen(tabpath, name, "r")))
416 logf(LOG_WARN|LOG_ERRNO, "%s", name);
419 nmem = nmem_create ();
420 res = (chrmaptab) nmem_malloc(nmem, sizeof(*res));
422 res->input = (chr_t_entry *) nmem_malloc(res->nmem, sizeof(*res->input));
423 res->input->target = (unsigned char **)
424 nmem_malloc(res->nmem, sizeof(*res->input->target) * 2);
425 res->input->target[0] = (unsigned char*) CHR_UNKNOWN;
426 res->input->target[1] = 0;
427 res->input->children = (chr_t_entry **)
428 nmem_malloc(res->nmem, sizeof(res->input) * 256);
429 for (i = 0; i < 256; i++)
431 res->input->children[i] = (chr_t_entry *)
432 nmem_malloc(res->nmem, sizeof(*res->input));
433 res->input->children[i]->children = 0;
434 res->input->children[i]->target = (unsigned char **)
435 nmem_malloc (res->nmem, 2 * sizeof(unsigned char *));
436 res->input->children[i]->target[1] = 0;
439 res->input->children[i]->target[0] = (unsigned char *)
440 nmem_malloc (res->nmem, 2 * sizeof(unsigned char));
441 res->input->children[i]->target[0][0] = i;
442 res->input->children[i]->target[0][1] = 0;
445 res->input->children[i]->target[0] = (unsigned char*) CHR_UNKNOWN;
447 res->q_input = (chr_t_entry *)
448 nmem_malloc(res->nmem, sizeof(*res->q_input));
449 res->q_input->target = 0;
450 res->q_input->children = 0;
452 for (i = *CHR_BASE; i < 256; i++)
454 res->output[(int) *CHR_SPACE] = (unsigned char *) " ";
455 res->output[(int) *CHR_UNKNOWN] = (unsigned char*) "@";
456 res->base_uppercase = 0;
458 while (!errors && (argc = readconf_line(f, &lineno, line, 512, argv, 50)))
459 if (!map_only && !yaz_matchstr(argv[0], "lowercase"))
463 logf(LOG_FATAL, "Syntax error in charmap");
466 if (scan_string(argv[1], fun_addentry, res, &num) < 0)
468 logf(LOG_FATAL, "Bad value-set specification");
471 res->base_uppercase = num;
472 res->output[(int) *CHR_SPACE + num] = (unsigned char *) " ";
473 res->output[(int) *CHR_UNKNOWN + num] = (unsigned char*) "@";
474 num = (int) *CHR_BASE;
476 else if (!map_only && !yaz_matchstr(argv[0], "uppercase"))
478 if (!res->base_uppercase)
480 logf(LOG_FATAL, "Uppercase directive with no lowercase set");
485 logf(LOG_FATAL, "Missing arg for uppercase directive");
488 if (scan_string(argv[1], fun_addentry, res, &num) < 0)
490 logf(LOG_FATAL, "Bad value-set specification");
494 else if (!map_only && !yaz_matchstr(argv[0], "space"))
498 logf(LOG_FATAL, "Syntax error in charmap");
501 if (scan_string(argv[1], fun_addspace, res, 0) < 0)
503 logf(LOG_FATAL, "Bad space specification");
507 else if (!yaz_matchstr(argv[0], "map"))
513 logf(LOG_FATAL, "charmap directive map requires 2 args");
517 buf.string[0] = '\0';
518 if (scan_string(argv[2], fun_mkstring, &buf, 0) < 0)
520 logf(LOG_FATAL, "Bad map target");
523 if (scan_string(argv[1], fun_add_map, &buf, 0) < 0)
525 logf(LOG_FATAL, "Bad map source");
529 else if (!yaz_matchstr(argv[0], "qmap"))
535 logf(LOG_FATAL, "charmap directive qmap requires 2 args");
539 buf.string[0] = '\0';
540 if (scan_string(argv[2], fun_mkstring, &buf, 0) < 0)
542 logf(LOG_FATAL, "Bad qmap target");
545 if (scan_string(argv[1], fun_add_qmap, &buf, 0) < 0)
547 logf(LOG_FATAL, "Bad qmap source");
553 logf(LOG_WARN, "Syntax error at '%s' in %s", line, name);
559 chrmaptab_destroy(res);
562 logf (LOG_DEBUG, "maptab %s close %d errors", name, errors);
566 void chrmaptab_destroy(chrmaptab tab)
569 nmem_destroy (tab->nmem);