2 * Copyright (C) 1996-2002, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
6 * $Id: charmap.c,v 1.21 2002-04-05 12:54:29 adam Exp $
11 * Support module to handle character-conversions into and out of the
19 #include <yaz/yaz-util.h>
22 #define CHR_MAXSTR 1024
23 #define CHR_MAXEQUIV 32
25 const char *CHR_UNKNOWN = "\001";
26 const char *CHR_SPACE = "\002";
27 const char *CHR_BASE = "\003";
31 chr_t_entry *input; /* mapping table for input data */
32 chr_t_entry *q_input; /* mapping table for queries */
33 unsigned char *output[256]; /* return mapping - for display of registers */
34 int base_uppercase; /* Start of upper-case ordinals */
39 * Character map trie node.
43 chr_t_entry **children; /* array of children */
44 unsigned char **target; /* target for this node, if any */
48 * General argument structure for callback functions (internal use only)
50 typedef struct chrwork
53 char string[CHR_MAXSTR+1];
57 * Add an entry to the character map.
59 static chr_t_entry *set_map_string(chr_t_entry *root, NMEM nmem,
60 const char *from, int len, char *to,
67 root = (chr_t_entry *) nmem_malloc(nmem, sizeof(*root));
73 if (!root->target || !root->target[0] || strcmp(root->target[0], to))
76 root->target && root->target[0] && root->target[0][0] &&
77 strcmp (root->target[0], CHR_UNKNOWN))
79 yaz_log (LOG_WARN, "duplicate entry for charmap from '%s'",
82 root->target = (unsigned char **)
83 nmem_malloc(nmem, sizeof(*root->target)*2);
84 root->target[0] = (unsigned char *) nmem_strdup(nmem, to);
94 root->children = (chr_t_entry **)
95 nmem_malloc(nmem, sizeof(chr_t_entry*) * 256);
96 for (i = 0; i < 256; i++)
97 root->children[i] = 0;
99 if (!(root->children[(unsigned char) *from] =
100 set_map_string(root->children[(unsigned char) *from], nmem,
101 from + 1, len - 1, to, from_0)))
107 static chr_t_entry *find_entry(chr_t_entry *t, const char **from, int len)
111 if (len && t->children && t->children[(unsigned char) **from])
113 const char *pos = *from;
116 if ((res = find_entry(t->children[(unsigned char) *pos],
122 /* no children match. use ourselves, if we have a target */
123 return t->target ? t : 0;
126 static chr_t_entry *find_entry_x(chr_t_entry *t, const char **from, int *len)
131 { /* switch to next buffer */
137 if (*len > 0 && t->children && t->children[(unsigned char) **from])
139 const char *old_from = *from;
144 if ((res = find_entry_x(t->children[(unsigned char) *old_from],
151 /* no children match. use ourselves, if we have a target */
152 return t->target ? t : 0;
155 const char **chr_map_input_x(chrmaptab maptab, const char **from, int *len)
157 chr_t_entry *t = maptab->input;
160 if (!(res = find_entry_x(t, from, len)))
162 return (const char **) (res->target);
165 const char **chr_map_input(chrmaptab maptab, const char **from, int len)
167 chr_t_entry *t = maptab->input;
173 if (!(res = find_entry_x(t, from, len_tmp)))
175 return (const char **) (res->target);
178 const char *chr_map_output(chrmaptab maptab, const char **from, int len)
180 unsigned char c = ** (unsigned char **) from;
182 return (const char*) maptab->output[c];
185 unsigned char zebra_prim(char **s)
190 yaz_log (LOG_DEBUG, "prim %.3s", *s);
197 case '\\': c = '\\'; (*s)++; break;
198 case 'r': c = '\r'; (*s)++; break;
199 case 'n': c = '\n'; (*s)++; break;
200 case 't': c = '\t'; (*s)++; break;
201 case 's': c = ' '; (*s)++; break;
202 case 'x': sscanf(*s, "x%2x", &i); c = i; *s += 3; break;
213 sscanf(*s, "%3o", &i);
226 yaz_log (LOG_DEBUG, "out %d", c);
232 * Add an entry to the value space.
234 static void fun_addentry(const char *s, void *data, int num)
236 chrmaptab tab = (chrmaptab) data;
239 tmp[0] = num; tmp[1] = '\0';
240 tab->input = set_map_string(tab->input, tab->nmem, s, strlen(s), tmp, 0);
241 tab->output[num + tab->base_uppercase] =
242 (unsigned char *) nmem_strdup(tab->nmem, s);
247 * Add a space-entry to the value space.
249 static void fun_addspace(const char *s, void *data, int num)
251 chrmaptab tab = (chrmaptab) data;
252 tab->input = set_map_string(tab->input, tab->nmem, s, strlen(s),
253 (char*) CHR_SPACE, 0);
257 * Create a string containing the mapped characters provided.
259 static void fun_mkstring(const char *s, void *data, int num)
261 chrwork *arg = (chrwork *) data;
262 const char **res, *p = s;
264 res = chr_map_input(arg->map, &s, strlen(s));
265 if (*res == (char*) CHR_UNKNOWN)
266 logf(LOG_WARN, "Map: '%s' has no mapping", p);
267 strncat(arg->string, *res, CHR_MAXSTR - strlen(arg->string));
268 arg->string[CHR_MAXSTR] = '\0';
272 * Add a map to the string contained in the argument.
274 static void fun_add_map(const char *s, void *data, int num)
276 chrwork *arg = (chrwork *) data;
278 assert(arg->map->input);
279 logf (LOG_DEBUG, "set map %.*s", (int) strlen(s), s);
280 set_map_string(arg->map->input, arg->map->nmem, s, strlen(s), arg->string,
282 for (s = arg->string; *s; s++)
283 logf (LOG_DEBUG, " %3d", (unsigned char) *s);
287 * Add a query map to the string contained in the argument.
289 static void fun_add_qmap(const char *s, void *data, int num)
291 chrwork *arg = (chrwork *) data;
293 assert(arg->map->q_input);
294 logf (LOG_DEBUG, "set qmap %.*s", (int) strlen(s), s);
295 set_map_string(arg->map->q_input, arg->map->nmem, s,
296 strlen(s), arg->string, 0);
297 for (s = arg->string; *s; s++)
298 logf (LOG_DEBUG, " %3d", (unsigned char) *s);
302 static int scan_string(char *s,
303 void (*fun)(const char *c, void *data, int num),
304 void *data, int *num)
306 unsigned char c, str[1024], begin, end, *p;
314 begin = zebra_prim(&s);
317 logf(LOG_FATAL, "Bad range in char-map");
321 end = zebra_prim(&s);
324 logf(LOG_FATAL, "Bad range in char-map");
328 for (c = begin; c <= end; c++)
330 str[0] = c; str[1] = '\0';
331 (*fun)((char *) str, data, num ? (*num)++ : 0);
334 case '[': s++; abort(); break;
336 p = (unsigned char*) ++s;
337 /* Find the end-marker, ignoring escapes */
340 if (!(p = (unsigned char*) strchr((char*) p, ')')))
342 logf(LOG_FATAL, "Missing ')' in string");
346 while (*(p - 1) == '\\');
348 (*fun)(s, data, num ? (*num)++ : 0);
353 str[0] = c; str[1] = '\0';
354 (*fun)((char *) str, data, num ? (*num)++ : 0);
360 chrmaptab chrmaptab_create(const char *tabpath, const char *name, int map_only,
364 char line[512], *argv[50];
368 int argc, num = (int) *CHR_BASE, i;
371 logf (LOG_DEBUG, "maptab %s open", name);
372 if (!(f = yaz_fopen(tabpath, name, "r", tabroot)))
374 logf(LOG_WARN|LOG_ERRNO, "%s", name);
377 nmem = nmem_create ();
378 res = (chrmaptab) nmem_malloc(nmem, sizeof(*res));
380 res->input = (chr_t_entry *) nmem_malloc(res->nmem, sizeof(*res->input));
381 res->input->target = (unsigned char **)
382 nmem_malloc(res->nmem, sizeof(*res->input->target) * 2);
383 res->input->target[0] = (unsigned char*) CHR_UNKNOWN;
384 res->input->target[1] = 0;
385 res->input->children = (chr_t_entry **)
386 nmem_malloc(res->nmem, sizeof(res->input) * 256);
387 for (i = 0; i < 256; i++)
389 res->input->children[i] = (chr_t_entry *)
390 nmem_malloc(res->nmem, sizeof(*res->input));
391 res->input->children[i]->children = 0;
392 res->input->children[i]->target = (unsigned char **)
393 nmem_malloc (res->nmem, 2 * sizeof(unsigned char *));
394 res->input->children[i]->target[1] = 0;
397 res->input->children[i]->target[0] = (unsigned char *)
398 nmem_malloc (res->nmem, 2 * sizeof(unsigned char));
399 res->input->children[i]->target[0][0] = i;
400 res->input->children[i]->target[0][1] = 0;
403 res->input->children[i]->target[0] = (unsigned char*) CHR_UNKNOWN;
405 res->q_input = (chr_t_entry *)
406 nmem_malloc(res->nmem, sizeof(*res->q_input));
407 res->q_input->target = 0;
408 res->q_input->children = 0;
410 for (i = *CHR_BASE; i < 256; i++)
412 res->output[(int) *CHR_SPACE] = (unsigned char *) " ";
413 res->output[(int) *CHR_UNKNOWN] = (unsigned char*) "@";
414 res->base_uppercase = 0;
416 while (!errors && (argc = readconf_line(f, &lineno, line, 512, argv, 50)))
417 if (!map_only && !yaz_matchstr(argv[0], "lowercase"))
421 logf(LOG_FATAL, "Syntax error in charmap");
424 if (scan_string(argv[1], fun_addentry, res, &num) < 0)
426 logf(LOG_FATAL, "Bad value-set specification");
429 res->base_uppercase = num;
430 res->output[(int) *CHR_SPACE + num] = (unsigned char *) " ";
431 res->output[(int) *CHR_UNKNOWN + num] = (unsigned char*) "@";
432 num = (int) *CHR_BASE;
434 else if (!map_only && !yaz_matchstr(argv[0], "uppercase"))
436 if (!res->base_uppercase)
438 logf(LOG_FATAL, "Uppercase directive with no lowercase set");
443 logf(LOG_FATAL, "Missing arg for uppercase directive");
446 if (scan_string(argv[1], fun_addentry, res, &num) < 0)
448 logf(LOG_FATAL, "Bad value-set specification");
452 else if (!map_only && !yaz_matchstr(argv[0], "space"))
456 logf(LOG_FATAL, "Syntax error in charmap");
459 if (scan_string(argv[1], fun_addspace, res, 0) < 0)
461 logf(LOG_FATAL, "Bad space specification");
465 else if (!yaz_matchstr(argv[0], "map"))
471 logf(LOG_FATAL, "charmap directive map requires 2 args");
475 buf.string[0] = '\0';
476 if (scan_string(argv[2], fun_mkstring, &buf, 0) < 0)
478 logf(LOG_FATAL, "Bad map target");
481 if (scan_string(argv[1], fun_add_map, &buf, 0) < 0)
483 logf(LOG_FATAL, "Bad map source");
487 else if (!yaz_matchstr(argv[0], "qmap"))
493 logf(LOG_FATAL, "charmap directive qmap requires 2 args");
497 buf.string[0] = '\0';
498 if (scan_string(argv[2], fun_mkstring, &buf, 0) < 0)
500 logf(LOG_FATAL, "Bad qmap target");
503 if (scan_string(argv[1], fun_add_qmap, &buf, 0) < 0)
505 logf(LOG_FATAL, "Bad qmap source");
511 logf(LOG_WARN, "Syntax error at '%s' in %s", line, name);
517 chrmaptab_destroy(res);
520 logf (LOG_DEBUG, "maptab %s close %d errors", name, errors);
524 void chrmaptab_destroy(chrmaptab tab)
527 nmem_destroy (tab->nmem);