2 * Copyright (C) 1996-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.16 1999-09-07 07:19:21 adam
8 * Work on character mapping. Implemented replace rules.
10 * Revision 1.15 1999/05/26 07:49:14 adam
13 * Revision 1.14 1998/10/13 20:09:18 adam
14 * Changed call to readconf_line.
16 * Revision 1.13 1997/10/27 14:33:06 adam
17 * Moved towards generic character mapping depending on "structure"
18 * field in abstract syntax file. Fixed a few memory leaks. Fixed
19 * bug with negative integers when doing searches with relational
22 * Revision 1.12 1997/09/05 15:30:11 adam
23 * Changed prototype for chr_map_input - added const.
24 * Added support for C++, headers uses extern "C" for public definitions.
26 * Revision 1.11 1997/09/05 09:52:32 adam
27 * Extra argument added to function chr_read_maptab (tab path).
29 * Revision 1.10 1997/07/01 13:01:08 adam
30 * Bug fix in routine find_entry: didn't take into account the len arg.
32 * Revision 1.9 1996/10/29 13:48:14 adam
33 * Updated to use zebrautl.h instead of alexutil.h.
35 * Revision 1.8 1996/10/18 12:39:23 adam
36 * Uses LOG_DEBUG instead of LOG_WARN for "Character map overlap".
38 * Revision 1.7 1996/06/06 12:08:56 quinn
41 * Revision 1.6 1996/06/04 13:28:00 quinn
42 * More work on charmapping
44 * Revision 1.5 1996/06/04 08:32:15 quinn
45 * Moved default keymap to keychars.c
47 * Revision 1.4 1996/06/03 16:32:13 quinn
50 * Revision 1.3 1996/06/03 15:17:46 quinn
53 * Revision 1.2 1996/06/03 10:15:09 quinn
54 * Fixed bug in mapping function.
56 * Revision 1.1 1996/05/31 09:07:18 quinn
57 * Work on character-set handling
63 * Support module to handle character-conversions into and out of the
74 #define CHR_MAXSTR 1024
75 #define CHR_MAXEQUIV 32
77 const char *CHR_UNKNOWN = "\001";
78 const char *CHR_SPACE = "\002";
79 const char *CHR_BASE = "\003";
83 chr_t_entry *input; /* mapping table for input data */
84 chr_t_entry *q_input; /* mapping table for queries */
85 unsigned char *output[256]; /* return mapping - for display of registers */
86 int base_uppercase; /* Start of upper-case ordinals */
91 * Character map trie node.
95 chr_t_entry **children; /* array of children */
96 unsigned char **target; /* target for this node, if any */
100 * General argument structure for callback functions (internal use only)
102 typedef struct chrwork
105 char string[CHR_MAXSTR+1];
109 * Add an entry to the character map.
111 static chr_t_entry *set_map_string(chr_t_entry *root, NMEM nmem,
112 const char *from, int len, char *to)
116 root = (chr_t_entry *) nmem_malloc(nmem, sizeof(*root));
122 if (!root->target || !root->target[0] || strcmp(root->target[0], to))
124 root->target = (unsigned char **)
125 nmem_malloc(nmem, sizeof(*root->target)*2);
126 root->target[0] = (unsigned char *) nmem_strdup(nmem, to);
136 root->children = (chr_t_entry **)
137 nmem_malloc(nmem, sizeof(chr_t_entry*) * 256);
138 for (i = 0; i < 256; i++)
139 root->children[i] = 0;
141 if (!(root->children[(unsigned char) *from] =
142 set_map_string(root->children[(unsigned char) *from], nmem,
143 from + 1, len - 1, to)))
149 static chr_t_entry *find_entry(chr_t_entry *t, const char **from, int len)
153 if (len && t->children && t->children[(unsigned char) **from])
155 const char *pos = *from;
158 if ((res = find_entry(t->children[(unsigned char) *pos],
164 /* no children match. use ourselves, if we have a target */
165 return t->target ? t : 0;
168 static chr_t_entry *find_entry_x(chr_t_entry *t, const char **from, int *len)
173 { /* switch to next buffer */
179 if (*len > 0 && t->children && t->children[(unsigned char) **from])
181 const char *old_from = *from;
186 if ((res = find_entry_x(t->children[(unsigned char) *old_from],
193 /* no children match. use ourselves, if we have a target */
194 return t->target ? t : 0;
197 const char **chr_map_input_x(chrmaptab maptab, const char **from, int *len)
199 chr_t_entry *t = maptab->input;
202 if (!(res = find_entry_x(t, from, len)))
204 return (const char **) (res->target);
207 const char **chr_map_input(chrmaptab maptab, const char **from, int len)
209 chr_t_entry *t = maptab->input;
215 if (!(res = find_entry_x(t, from, len_tmp)))
217 return (const char **) (res->target);
220 const char *chr_map_output(chrmaptab maptab, const char **from, int len)
222 unsigned char c = ** (unsigned char **) from;
224 return (const char*) maptab->output[c];
227 unsigned char zebra_prim(char **s)
238 case '\\': c = '\\'; (*s)++; break;
239 case 'r': c = '\r'; (*s)++; break;
240 case 'n': c = '\n'; (*s)++; break;
241 case 't': c = '\t'; (*s)++; break;
242 case 's': c = ' '; (*s)++; break;
243 case 'x': sscanf(*s, "x%2x", &i); c = i; *s += 3; break;
244 case '{': case '[': case '(': case '}': case ']': case ')': case '$':
248 sscanf(*s, "%3o", &i); c = i; *s += 3; break;
259 * Add an entry to the value space.
261 static void fun_addentry(const char *s, void *data, int num)
263 chrmaptab tab = (chrmaptab) data;
266 tmp[0] = num; tmp[1] = '\0';
267 tab->input = set_map_string(tab->input, tab->nmem, s, strlen(s), tmp);
268 tab->output[num + tab->base_uppercase] =
269 (unsigned char *) nmem_strdup(tab->nmem, s);
274 * Add a space-entry to the value space.
276 static void fun_addspace(const char *s, void *data, int num)
278 chrmaptab tab = (chrmaptab) data;
279 tab->input = set_map_string(tab->input, tab->nmem, s, strlen(s),
284 * Create a string containing the mapped characters provided.
286 static void fun_mkstring(const char *s, void *data, int num)
288 chrwork *arg = (chrwork *) data;
289 const char **res, *p = s;
291 res = chr_map_input(arg->map, &s, strlen(s));
292 if (*res == (char*) CHR_UNKNOWN)
293 logf(LOG_WARN, "Map: '%s' has no mapping", p);
294 strncat(arg->string, *res, CHR_MAXSTR - strlen(arg->string));
295 arg->string[CHR_MAXSTR] = '\0';
299 * Add a map to the string contained in the argument.
301 static void fun_add_map(const char *s, void *data, int num)
303 chrwork *arg = (chrwork *) data;
305 assert(arg->map->input);
306 logf (LOG_LOG, "set map %.*s", (int) strlen(s), s);
307 set_map_string(arg->map->input, arg->map->nmem, s, strlen(s), arg->string);
308 for (s = arg->string; *s; s++)
309 logf (LOG_LOG, " %3d", (unsigned char) *s);
313 * Add a query map to the string contained in the argument.
315 static void fun_add_qmap(const char *s, void *data, int num)
317 chrwork *arg = (chrwork *) data;
319 assert(arg->map->q_input);
320 logf (LOG_LOG, "set qmap %.*s", (int) strlen(s), s);
321 set_map_string(arg->map->q_input, arg->map->nmem, s,
322 strlen(s), arg->string);
323 for (s = arg->string; *s; s++)
324 logf (LOG_LOG, " %3d", (unsigned char) *s);
328 static int scan_string(char *s,
329 void (*fun)(const char *c, void *data, int num),
330 void *data, int *num)
332 unsigned char c, str[1024], begin, end, *p;
340 begin = zebra_prim(&s);
343 logf(LOG_FATAL, "Bad range in char-map");
347 end = zebra_prim(&s);
350 logf(LOG_FATAL, "Bad range in char-map");
354 for (c = begin; c <= end; c++)
356 str[0] = c; str[1] = '\0';
357 (*fun)((char *) str, data, num ? (*num)++ : 0);
360 case '[': s++; abort(); break;
362 p = (unsigned char*) ++s;
363 /* Find the end-marker, ignoring escapes */
366 if (!(p = (unsigned char*) strchr((char*) p, ')')))
368 logf(LOG_FATAL, "Missing ')' in string");
372 while (*(p - 1) == '\\');
374 (*fun)(s, data, num ? (*num)++ : 0);
379 str[0] = c; str[1] = '\0';
380 (*fun)((char *) str, data, num ? (*num)++ : 0);
386 chrmaptab chrmaptab_create(const char *tabpath, const char *name, int map_only)
389 char line[512], *argv[50];
393 int argc, num = (int) *CHR_BASE, i;
396 logf (LOG_LOG, "maptab %s open", name);
397 if (!(f = yaz_path_fopen(tabpath, name, "r")))
399 logf(LOG_WARN|LOG_ERRNO, "%s", name);
402 nmem = nmem_create ();
403 res = (chrmaptab) nmem_malloc(nmem, sizeof(*res));
405 res->input = (chr_t_entry *) nmem_malloc(res->nmem, sizeof(*res->input));
406 res->input->target = (unsigned char **)
407 nmem_malloc(res->nmem, sizeof(*res->input->target) * 2);
408 res->input->target[0] = (unsigned char*) CHR_UNKNOWN;
409 res->input->target[1] = 0;
410 res->input->children = (chr_t_entry **)
411 nmem_malloc(res->nmem, sizeof(res->input) * 256);
412 for (i = 0; i < 256; i++)
414 res->input->children[i] = (chr_t_entry *)
415 nmem_malloc(res->nmem, sizeof(*res->input));
416 res->input->children[i]->children = 0;
417 res->input->children[i]->target = (unsigned char **)
418 nmem_malloc (res->nmem, 2 * sizeof(unsigned char *));
419 res->input->children[i]->target[1] = 0;
422 res->input->children[i]->target[0] = (unsigned char *)
423 nmem_malloc (res->nmem, 2 * sizeof(unsigned char));
424 res->input->children[i]->target[0][0] = i;
425 res->input->children[i]->target[0][1] = 0;
428 res->input->children[i]->target[0] = (unsigned char*) CHR_UNKNOWN;
430 res->q_input = (chr_t_entry *)
431 nmem_malloc(res->nmem, sizeof(*res->q_input));
432 res->q_input->target = 0;
433 res->q_input->children = 0;
435 for (i = *CHR_BASE; i < 256; i++)
437 res->output[(int) *CHR_SPACE] = (unsigned char *) " ";
438 res->output[(int) *CHR_UNKNOWN] = (unsigned char*) "@";
439 res->base_uppercase = 0;
441 while (!errors && (argc = readconf_line(f, &lineno, line, 512, argv, 50)))
442 if (!map_only && !yaz_matchstr(argv[0], "lowercase"))
446 logf(LOG_FATAL, "Syntax error in charmap");
449 if (scan_string(argv[1], fun_addentry, res, &num) < 0)
451 logf(LOG_FATAL, "Bad value-set specification");
454 res->base_uppercase = num;
455 res->output[(int) *CHR_SPACE + num] = (unsigned char *) " ";
456 res->output[(int) *CHR_UNKNOWN + num] = (unsigned char*) "@";
457 num = (int) *CHR_BASE;
459 else if (!map_only && !yaz_matchstr(argv[0], "uppercase"))
461 if (!res->base_uppercase)
463 logf(LOG_FATAL, "Uppercase directive with no lowercase set");
468 logf(LOG_FATAL, "Missing arg for uppercase directive");
471 if (scan_string(argv[1], fun_addentry, res, &num) < 0)
473 logf(LOG_FATAL, "Bad value-set specification");
477 else if (!map_only && !yaz_matchstr(argv[0], "space"))
481 logf(LOG_FATAL, "Syntax error in charmap");
484 if (scan_string(argv[1], fun_addspace, res, 0) < 0)
486 logf(LOG_FATAL, "Bad space specification");
490 else if (!yaz_matchstr(argv[0], "map"))
496 logf(LOG_FATAL, "charmap directive map requires 2 args");
500 buf.string[0] = '\0';
501 if (scan_string(argv[2], fun_mkstring, &buf, 0) < 0)
503 logf(LOG_FATAL, "Bad map target");
506 if (scan_string(argv[1], fun_add_map, &buf, 0) < 0)
508 logf(LOG_FATAL, "Bad map source");
512 else if (!yaz_matchstr(argv[0], "qmap"))
518 logf(LOG_FATAL, "charmap directive qmap requires 2 args");
522 buf.string[0] = '\0';
523 if (scan_string(argv[2], fun_mkstring, &buf, 0) < 0)
525 logf(LOG_FATAL, "Bad qmap target");
528 if (scan_string(argv[1], fun_add_qmap, &buf, 0) < 0)
530 logf(LOG_FATAL, "Bad qmap source");
536 logf(LOG_WARN, "Syntax error at '%s' in %s", line, name);
542 chrmaptab_destroy(res);
545 logf (LOG_LOG, "maptab %s close %d errors", name, errors);
549 void chrmaptab_destroy(chrmaptab tab)
552 nmem_destroy (tab->nmem);