2 * Copyright (C) 1996-1998, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.15 1999-05-26 07:49:14 adam
10 * Revision 1.14 1998/10/13 20:09:18 adam
11 * Changed call to readconf_line.
13 * Revision 1.13 1997/10/27 14:33:06 adam
14 * Moved towards generic character mapping depending on "structure"
15 * field in abstract syntax file. Fixed a few memory leaks. Fixed
16 * bug with negative integers when doing searches with relational
19 * Revision 1.12 1997/09/05 15:30:11 adam
20 * Changed prototype for chr_map_input - added const.
21 * Added support for C++, headers uses extern "C" for public definitions.
23 * Revision 1.11 1997/09/05 09:52:32 adam
24 * Extra argument added to function chr_read_maptab (tab path).
26 * Revision 1.10 1997/07/01 13:01:08 adam
27 * Bug fix in routine find_entry: didn't take into account the len arg.
29 * Revision 1.9 1996/10/29 13:48:14 adam
30 * Updated to use zebrautl.h instead of alexutil.h.
32 * Revision 1.8 1996/10/18 12:39:23 adam
33 * Uses LOG_DEBUG instead of LOG_WARN for "Character map overlap".
35 * Revision 1.7 1996/06/06 12:08:56 quinn
38 * Revision 1.6 1996/06/04 13:28:00 quinn
39 * More work on charmapping
41 * Revision 1.5 1996/06/04 08:32:15 quinn
42 * Moved default keymap to keychars.c
44 * Revision 1.4 1996/06/03 16:32:13 quinn
47 * Revision 1.3 1996/06/03 15:17:46 quinn
50 * Revision 1.2 1996/06/03 10:15:09 quinn
51 * Fixed bug in mapping function.
53 * Revision 1.1 1996/05/31 09:07:18 quinn
54 * Work on character-set handling
60 * Support module to handle character-conversions into and out of the
71 #define CHR_MAXSTR 1024
72 #define CHR_MAXEQUIV 32
74 int chr_map_chrs(chr_t_entry *t, char **from, int len,
75 int *read, char **to, int max);
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 *query_equiv; /* mapping table for queries */
85 unsigned char *output[256]; /* return mapping - for display of registers */
86 int base_uppercase; /* Start of upper-case ordinals */
92 * Character map trie node.
96 chr_t_entry **children; /* array of children */
97 unsigned char *target; /* target for this node, if any */
98 unsigned char *equiv; /* equivalent to, or sumthin */
102 * General argument structure for callback functions (internal use only)
104 typedef struct chrwork
107 char string[CHR_MAXSTR+1];
111 * Add an entry to the character map.
113 static chr_t_entry *set_map_string(chr_t_entry *root, NMEM nmem,
114 const char *from, int len, char *to)
118 root = (chr_t_entry *) nmem_malloc(nmem, sizeof(*root));
124 if (!root->target || (char*) root->target == CHR_SPACE ||
125 (char*) root->target == CHR_UNKNOWN)
126 root->target = (unsigned char *) nmem_strdup(nmem, to);
127 else if ((char*) to != CHR_SPACE)
128 logf(LOG_DEBUG, "Character map overlap");
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 int chr_map_chrs(chr_t_entry *t, char **from, int len, int *read, char **to,
155 while (len && t->children && t->children[(unsigned char) **from])
157 t = t->children[(unsigned char) **from];
161 /* if there were no matches, we are still at the root node,
162 which always has a null mapping */
163 for (s = t->target; *s && max; s++)
175 static chr_t_entry *find_entry(chr_t_entry *t, const char **from, int len)
179 if (len && t->children && t->children[(unsigned char) **from])
181 const char *pos = *from;
184 if ((res = find_entry(t->children[(unsigned char) *pos],
190 /* no children match. use ourselves, if we have a target */
191 return t->target ? t : 0;
194 const char **chr_map_input(chrmaptab maptab, const char **from, int len)
196 chr_t_entry *t = maptab->input;
199 if (!(res = find_entry(t, from, len)))
201 maptab->tmp_buf[0] = (char*) res->target;
202 maptab->tmp_buf[1] = NULL;
203 return (const char **) maptab->tmp_buf;
206 const char *chr_map_output(chrmaptab maptab, const char **from, int len)
208 unsigned char c = ** (unsigned char **) from;
210 return (const char*) maptab->output[c];
213 static unsigned char prim(char **s)
224 case '\\': c = '\\'; (*s)++; break;
225 case 'r': c = '\r'; (*s)++; break;
226 case 'n': c = '\n'; (*s)++; break;
227 case 't': c = '\t'; (*s)++; break;
228 case 's': c = ' '; (*s)++; break;
229 case 'x': sscanf(*s, "x%2x", &i); c = i; *s += 3; break;
230 case '{': case '[': case '(': case '}': case ']': case ')':
233 default: sscanf(*s, "%3o", &i); c = i; *s += 3; break;
244 * Add an entry to the value space.
246 static void fun_addentry(const char *s, void *data, int num)
248 chrmaptab tab = (chrmaptab) data;
251 tmp[0] = num; tmp[1] = '\0';
252 tab->input = set_map_string(tab->input, tab->nmem, s, strlen(s), tmp);
253 tab->output[num + tab->base_uppercase] =
254 (unsigned char *) nmem_strdup(tab->nmem, s);
259 * Add a space-entry to the value space.
261 static void fun_addspace(const char *s, void *data, int num)
263 chrmaptab tab = (chrmaptab) data;
264 tab->input = set_map_string(tab->input, tab->nmem, s, strlen(s),
269 * Create a string containing the mapped characters provided.
271 static void fun_mkstring(const char *s, void *data, int num)
273 chrwork *arg = (chrwork *) data;
274 const char **res, *p = s;
276 res = chr_map_input(arg->map, &s, strlen(s));
277 if (*res == (char*) CHR_UNKNOWN)
278 logf(LOG_WARN, "Map: '%s' has no mapping", p);
279 strncat(arg->string, *res, CHR_MAXSTR - strlen(arg->string));
280 arg->string[CHR_MAXSTR] = '\0';
284 * Add a map to the string contained in the argument.
286 static void fun_addmap(const char *s, void *data, int num)
288 chrwork *arg = (chrwork *) data;
290 assert(arg->map->input);
291 set_map_string(arg->map->input, arg->map->nmem, s, strlen(s), arg->string);
294 static int scan_string(char *s,
295 void (*fun)(const char *c, void *data, int num),
296 void *data, int *num)
298 unsigned char c, str[1024], begin, end, *p;
309 logf(LOG_FATAL, "Bad range in char-map");
316 logf(LOG_FATAL, "Bad range in char-map");
320 for (c = begin; c <= end; c++)
322 str[0] = c; str[1] = '\0';
323 (*fun)((char *) str, data, num ? (*num)++ : 0);
326 case '[': s++; abort(); break;
328 p = (unsigned char*) ++s;
329 /* Find the end-marker, ignoring escapes */
332 if (!(p = (unsigned char*) strchr((char*) p, ')')))
334 logf(LOG_FATAL, "Missing ')' in string");
338 while (*(p - 1) == '\\');
340 (*fun)(s, data, num ? (*num)++ : 0);
345 str[0] = c; str[1] = '\0';
346 (*fun)((char *) str, data, num ? (*num)++ : 0);
352 chrmaptab chrmaptab_create(const char *tabpath, const char *name, int map_only)
355 char line[512], *argv[50];
358 int argc, num = (int) *CHR_BASE, i;
360 if (!(f = yaz_path_fopen(tabpath, name, "r")))
362 logf(LOG_WARN|LOG_ERRNO, "%s", name);
365 res = (chrmaptab) xmalloc(sizeof(*res));
366 res->nmem = nmem_create ();
367 res->tmp_buf = (char **)
368 nmem_malloc (res->nmem, sizeof(*res->tmp_buf) * 100);
369 res->input = (chr_t_entry *) nmem_malloc(res->nmem, sizeof(*res->input));
370 res->input->target = (unsigned char*) CHR_UNKNOWN;
371 res->input->equiv = 0;
372 res->input->children = (chr_t_entry **)
373 nmem_malloc(res->nmem, sizeof(res->input) * 256);
374 for (i = 0; i < 256; i++)
376 res->input->children[i] = (chr_t_entry *)
377 nmem_malloc(res->nmem, sizeof(*res->input));
378 res->input->children[i]->children = 0;
381 res->input->children[i]->target = (unsigned char *)
382 nmem_malloc (res->nmem, 2 * sizeof(char));
383 res->input->children[i]->target[0] = i;
384 res->input->children[i]->target[1] = 0;
387 res->input->children[i]->target = (unsigned char*) CHR_UNKNOWN;
388 res->input->children[i]->equiv = 0;
390 res->query_equiv = 0;
391 for (i = *CHR_BASE; i < 256; i++)
393 res->output[(int) *CHR_SPACE] = (unsigned char *) " ";
394 res->output[(int) *CHR_UNKNOWN] = (unsigned char*) "@";
395 res->base_uppercase = 0;
397 while ((argc = readconf_line(f, &lineno, line, 512, argv, 50)))
398 if (!map_only && !yaz_matchstr(argv[0], "lowercase"))
402 logf(LOG_FATAL, "Syntax error in charmap");
406 if (scan_string(argv[1], fun_addentry, res, &num) < 0)
408 logf(LOG_FATAL, "Bad value-set specification");
412 res->base_uppercase = num;
413 res->output[(int) *CHR_SPACE + num] = (unsigned char *) " ";
414 res->output[(int) *CHR_UNKNOWN + num] = (unsigned char*) "@";
415 num = (int) *CHR_BASE;
417 else if (!map_only && !yaz_matchstr(argv[0], "uppercase"))
419 if (!res->base_uppercase)
421 logf(LOG_FATAL, "Uppercase directive with no lowercase set");
427 logf(LOG_FATAL, "Syntax error in charmap");
431 if (scan_string(argv[1], fun_addentry, res, &num) < 0)
433 logf(LOG_FATAL, "Bad value-set specification");
438 else if (!map_only && !yaz_matchstr(argv[0], "space"))
442 logf(LOG_FATAL, "Syntax error in charmap");
446 if (scan_string(argv[1], fun_addspace, res, 0) < 0)
448 logf(LOG_FATAL, "Bad space specification");
453 else if (!yaz_matchstr(argv[0], "map"))
459 logf(LOG_FATAL, "charmap MAP directive requires 2 args");
464 buf.string[0] = '\0';
465 if (scan_string(argv[2], fun_mkstring, &buf, 0) < 0)
467 logf(LOG_FATAL, "Bad map target");
471 if (scan_string(argv[1], fun_addmap, &buf, 0) < 0)
473 logf(LOG_FATAL, "Bad map source");
480 logf(LOG_WARN, "Syntax error at '%s' in %s", line, name);
486 void chrmaptab_destroy(chrmaptab tab)
488 nmem_destroy (tab->nmem);