2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.4 1996-06-03 16:32:13 quinn
10 * Revision 1.3 1996/06/03 15:17:46 quinn
13 * Revision 1.2 1996/06/03 10:15:09 quinn
14 * Fixed bug in mapping function.
16 * Revision 1.1 1996/05/31 09:07:18 quinn
17 * Work on character-set handling
23 * Support module to handle character-conversions into and out of the
35 const char *CHR_UNKNOWN = "\001";
36 const char *CHR_SPACE = "\002";
37 const char *CHR_BASE = "\003";
39 extern char *data1_tabpath;
42 * Character map trie node.
46 chr_t_entry **children; /* array of children */
47 unsigned char *target; /* target for this node, if any */
48 unsigned char *equiv; /* equivalent to, or sumthin */
52 * Add an entry to the character map.
54 static chr_t_entry *set_map_string(chr_t_entry *root, char *from, int len,
59 root = xmalloc(sizeof(*root));
65 if (!root->target || (char*) root->target == CHR_SPACE ||
66 (char*) root->target == CHR_UNKNOWN)
67 root->target = (unsigned char *) xstrdup(to);
68 else if ((char*) to != CHR_SPACE)
69 logf(LOG_WARN, "Character map overlap");
77 root->children = xmalloc(sizeof(chr_t_entry*) * 256);
78 for (i = 0; i < 256; i++)
79 root->children[i] = 0;
81 if (!(root->children[(unsigned char) *from] =
82 set_map_string(root->children[(unsigned char) *from], from + 1,
89 int chr_map_chrs(chr_t_entry *t, char **from, int len, int *read, char **to,
95 while (len && t->children && t->children[(unsigned char) **from])
97 t = t->children[(unsigned char) **from];
101 /* if there were no matches, we are still at the root node,
102 which always has a null mapping */
103 for (s = t->target; *s && max; s++)
116 static chr_t_entry *find_entry(chr_t_entry *t, char **from, int len)
120 if (t->children && t->children[(unsigned char) **from])
125 if ((res = find_entry(t->children[(unsigned char) *pos],
131 /* no children match. use ourselves */
135 char **chr_map_input(chr_t_entry *t, char **from, int len)
137 static char *buf[2] = {0, 0}, str[2] = {0, 0};
140 if (!t) /* no table loaded. Null mapping */
148 buf[0] = (char*) CHR_SPACE;
152 /* no children match. use our target string */
153 if (!(res = find_entry(t, from, len)))
155 buf[0] = (char *) res->target;
161 char **chr_map_input(chr_t_entry *t, char **from, int len)
163 static char *buf[2] = {0, 0}, str[2] = {0, 0};
168 while (len && t->children && t->children[(unsigned char) **from])
170 t = t->children[(unsigned char) **from];
174 buf[0] = (char*) t->target;
176 else /* null mapping */
183 else if (isspace(**from))
184 buf[0] = (char*) CHR_SPACE;
186 buf[0] = (char*) CHR_UNKNOWN;
191 /* return (char*) t->target; */
196 static unsigned char prim(char **s)
207 case '\\': c = '\\'; (*s)++; break;
208 case 'r': c = '\r'; (*s)++; break;
209 case 'n': c = '\n'; (*s)++; break;
210 case 't': c = '\t'; (*s)++; break;
211 case 's': c = ' '; (*s)++; break;
212 case 'x': sscanf(*s, "x%2x", &i); c = i; *s += 3; break;
213 case '{': case '[': case '(': case '}': case ']': case ')':
216 default: sscanf(*s, "%3o", &i); c = i; *s += 3; break;
227 * Add an entry to the value space.
229 static void fun_addentry(char *s, void *data, int num)
231 chrmaptab *tab = data;
234 tmp[0] = num; tmp[1] = '\0';
235 tab->input = set_map_string(tab->input, s, strlen(s), tmp);
236 tab->output[num + tab->base_uppercase] = (unsigned char *) xstrdup(s);
241 * Add a space-entry to the value space.
243 static void fun_addspace(char *s, void *data, int num)
245 chrmaptab *tab = data;
246 tab->input = set_map_string(tab->input, s, strlen(s), (char*) CHR_SPACE);
249 static int scan_string(char *s, void (*fun)(char *c, void *data, int num),
250 void *data, int *num)
252 unsigned char c, str[1024], begin, end, *p;
263 logf(LOG_FATAL, "Bad range in char-map");
270 logf(LOG_FATAL, "Bad range in char-map");
274 for (c = begin; c <= end; c++)
276 str[0] = c; str[1] = '\0';
277 (*fun)((char *) str, data, num ? (*num)++ : 0);
280 case '[': s++; abort(); break;
282 p = (unsigned char*) ++s;
283 /* Find the end-marker, ignoring escapes */
286 if (!(p = (unsigned char*) strchr((char*) p, ')')))
288 logf(LOG_FATAL, "Missing ')' in string");
292 while (*(p - 1) == '\\');
294 (*fun)(s, data, num ? (*num)++ : 0);
299 str[0] = c; str[1] = '\0';
300 (*fun)((char *) str, data, num ? (*num)++ : 0);
306 chrmaptab *chr_read_maptab(char *name)
309 char line[512], *argv[50];
310 chrmaptab *res = xmalloc(sizeof(*res));
311 int argc, num = (int) *CHR_BASE, i;
313 if (!(f = yaz_path_fopen(data1_tabpath, name, "r")))
315 logf(LOG_WARN|LOG_ERRNO, "%s", name);
318 res = xmalloc(sizeof(*res));
319 res->input = xmalloc(sizeof(*res->input));
320 res->input->target = (unsigned char*) CHR_UNKNOWN;
321 res->input->equiv = 0;
323 res->input->children = xmalloc(sizeof(res->input) * 256);
324 for (i = 0; i < 256; i++)
326 res->input->children[i] = xmalloc(sizeof(*res->input));
327 res->input->children[i]->children = 0;
328 res->input->children[i]->target = (unsigned char*) CHR_UNKNOWN;
329 res->input->children[i]->equiv = 0;
332 res->input->children = 0;
334 res->query_equiv = 0;
335 for (i = *CHR_BASE; i < 256; i++)
337 res->output[(int) *CHR_SPACE] = (unsigned char *) " ";
338 res->output[(int) *CHR_UNKNOWN] = (unsigned char*) "@";
339 res->base_uppercase = 0;
341 while ((argc = readconf_line(f, line, 512, argv, 50)))
342 if (!yaz_matchstr(argv[0], "lowercase"))
346 logf(LOG_FATAL, "Syntax error in charmap");
350 if (scan_string(argv[1], fun_addentry, res, &num) < 0)
352 logf(LOG_FATAL, "Bad value-set specification");
356 res->base_uppercase = num;
357 res->output[(int) *CHR_SPACE + num] = (unsigned char *) " ";
358 res->output[(int) *CHR_UNKNOWN + num] = (unsigned char*) "@";
359 num = (int) *CHR_BASE;
361 else if (!yaz_matchstr(argv[0], "uppercase"))
363 if (!res->base_uppercase)
365 logf(LOG_FATAL, "Uppercase directive with no lowercase set");
371 logf(LOG_FATAL, "Syntax error in charmap");
375 if (scan_string(argv[1], fun_addentry, res, &num) < 0)
377 logf(LOG_FATAL, "Bad value-set specification");
382 else if (!yaz_matchstr(argv[0], "space"))
386 logf(LOG_FATAL, "Syntax error in charmap");
390 if (scan_string(argv[1], fun_addspace, res, 0))
392 logf(LOG_FATAL, "Bad space specification");
400 logf(LOG_WARN, "Syntax error at '%s' in %s", line, file);