2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.5 1996-06-04 08:32:15 quinn
8 * Moved default keymap to keychars.c
10 * Revision 1.4 1996/06/03 16:32:13 quinn
13 * Revision 1.3 1996/06/03 15:17:46 quinn
16 * Revision 1.2 1996/06/03 10:15:09 quinn
17 * Fixed bug in mapping function.
19 * Revision 1.1 1996/05/31 09:07:18 quinn
20 * Work on character-set handling
26 * Support module to handle character-conversions into and out of the
38 const char *CHR_UNKNOWN = "\001";
39 const char *CHR_SPACE = "\002";
40 const char *CHR_BASE = "\003";
42 extern char *data1_tabpath;
45 * Character map trie node.
49 chr_t_entry **children; /* array of children */
50 unsigned char *target; /* target for this node, if any */
51 unsigned char *equiv; /* equivalent to, or sumthin */
55 * Add an entry to the character map.
57 static chr_t_entry *set_map_string(chr_t_entry *root, char *from, int len,
62 root = xmalloc(sizeof(*root));
68 if (!root->target || (char*) root->target == CHR_SPACE ||
69 (char*) root->target == CHR_UNKNOWN)
70 root->target = (unsigned char *) xstrdup(to);
71 else if ((char*) to != CHR_SPACE)
72 logf(LOG_WARN, "Character map overlap");
80 root->children = xmalloc(sizeof(chr_t_entry*) * 256);
81 for (i = 0; i < 256; i++)
82 root->children[i] = 0;
84 if (!(root->children[(unsigned char) *from] =
85 set_map_string(root->children[(unsigned char) *from], from + 1,
92 int chr_map_chrs(chr_t_entry *t, char **from, int len, int *read, char **to,
98 while (len && t->children && t->children[(unsigned char) **from])
100 t = t->children[(unsigned char) **from];
104 /* if there were no matches, we are still at the root node,
105 which always has a null mapping */
106 for (s = t->target; *s && max; s++)
119 static chr_t_entry *find_entry(chr_t_entry *t, char **from, int len)
123 if (t->children && t->children[(unsigned char) **from])
128 if ((res = find_entry(t->children[(unsigned char) *pos],
134 /* no children match. use ourselves */
138 char **chr_map_input(chr_t_entry *t, char **from, int len)
140 static char *buf[2] = {0, 0};
143 if (!(res = find_entry(t, from, len)))
145 buf[0] = (char *) res->target;
151 char **chr_map_input(chr_t_entry *t, char **from, int len)
153 static char *buf[2] = {0, 0}, str[2] = {0, 0};
158 while (len && t->children && t->children[(unsigned char) **from])
160 t = t->children[(unsigned char) **from];
164 buf[0] = (char*) t->target;
166 else /* null mapping */
173 else if (isspace(**from))
174 buf[0] = (char*) CHR_SPACE;
176 buf[0] = (char*) CHR_UNKNOWN;
181 /* return (char*) t->target; */
186 static unsigned char prim(char **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;
203 case '{': case '[': case '(': case '}': case ']': case ')':
206 default: sscanf(*s, "%3o", &i); c = i; *s += 3; break;
217 * Add an entry to the value space.
219 static void fun_addentry(char *s, void *data, int num)
221 chrmaptab *tab = data;
224 tmp[0] = num; tmp[1] = '\0';
225 tab->input = set_map_string(tab->input, s, strlen(s), tmp);
226 tab->output[num + tab->base_uppercase] = (unsigned char *) xstrdup(s);
231 * Add a space-entry to the value space.
233 static void fun_addspace(char *s, void *data, int num)
235 chrmaptab *tab = data;
236 tab->input = set_map_string(tab->input, s, strlen(s), (char*) CHR_SPACE);
239 static int scan_string(char *s, void (*fun)(char *c, void *data, int num),
240 void *data, int *num)
242 unsigned char c, str[1024], begin, end, *p;
253 logf(LOG_FATAL, "Bad range in char-map");
260 logf(LOG_FATAL, "Bad range in char-map");
264 for (c = begin; c <= end; c++)
266 str[0] = c; str[1] = '\0';
267 (*fun)((char *) str, data, num ? (*num)++ : 0);
270 case '[': s++; abort(); break;
272 p = (unsigned char*) ++s;
273 /* Find the end-marker, ignoring escapes */
276 if (!(p = (unsigned char*) strchr((char*) p, ')')))
278 logf(LOG_FATAL, "Missing ')' in string");
282 while (*(p - 1) == '\\');
284 (*fun)(s, data, num ? (*num)++ : 0);
289 str[0] = c; str[1] = '\0';
290 (*fun)((char *) str, data, num ? (*num)++ : 0);
296 chrmaptab *chr_read_maptab(char *name)
299 char line[512], *argv[50];
300 chrmaptab *res = xmalloc(sizeof(*res));
301 int argc, num = (int) *CHR_BASE, i;
303 if (!(f = yaz_path_fopen(data1_tabpath, name, "r")))
305 logf(LOG_WARN|LOG_ERRNO, "%s", name);
308 res = xmalloc(sizeof(*res));
309 res->input = xmalloc(sizeof(*res->input));
310 res->input->target = (unsigned char*) CHR_UNKNOWN;
311 res->input->equiv = 0;
313 res->input->children = xmalloc(sizeof(res->input) * 256);
314 for (i = 0; i < 256; i++)
316 res->input->children[i] = xmalloc(sizeof(*res->input));
317 res->input->children[i]->children = 0;
318 res->input->children[i]->target = (unsigned char*) CHR_UNKNOWN;
319 res->input->children[i]->equiv = 0;
322 res->input->children = 0;
324 res->query_equiv = 0;
325 for (i = *CHR_BASE; i < 256; i++)
327 res->output[(int) *CHR_SPACE] = (unsigned char *) " ";
328 res->output[(int) *CHR_UNKNOWN] = (unsigned char*) "@";
329 res->base_uppercase = 0;
331 while ((argc = readconf_line(f, line, 512, argv, 50)))
332 if (!yaz_matchstr(argv[0], "lowercase"))
336 logf(LOG_FATAL, "Syntax error in charmap");
340 if (scan_string(argv[1], fun_addentry, res, &num) < 0)
342 logf(LOG_FATAL, "Bad value-set specification");
346 res->base_uppercase = num;
347 res->output[(int) *CHR_SPACE + num] = (unsigned char *) " ";
348 res->output[(int) *CHR_UNKNOWN + num] = (unsigned char*) "@";
349 num = (int) *CHR_BASE;
351 else if (!yaz_matchstr(argv[0], "uppercase"))
353 if (!res->base_uppercase)
355 logf(LOG_FATAL, "Uppercase directive with no lowercase set");
361 logf(LOG_FATAL, "Syntax error in charmap");
365 if (scan_string(argv[1], fun_addentry, res, &num) < 0)
367 logf(LOG_FATAL, "Bad value-set specification");
372 else if (!yaz_matchstr(argv[0], "space"))
376 logf(LOG_FATAL, "Syntax error in charmap");
380 if (scan_string(argv[1], fun_addspace, res, 0))
382 logf(LOG_FATAL, "Bad space specification");
390 logf(LOG_WARN, "Syntax error at '%s' in %s", line, file);