2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.1 1996-05-31 09:07:18 quinn
8 * Work on character-set handling
14 * Support module to handle character-conversions into and out of the
25 const char *CHR_UNKNOWN = "\001";
26 const char *CHR_SPACE = "\002";
27 const char *CHR_BASE = "\003";
29 extern char *data1_tabpath;
32 * Character map trie node.
36 chr_t_entry **children; /* array of children */
37 unsigned char *target; /* target for this node, if any */
38 unsigned char *equiv; /* equivalent to, or sumthin */
42 * Add an entry to the character map.
44 static chr_t_entry *set_map_string(chr_t_entry *root, char *from, int len,
49 root = xmalloc(sizeof(*root));
54 root->target = (unsigned char *) xstrdup(to);
61 root->children = xmalloc(sizeof(chr_t_entry*) * 256);
62 for (i = 0; i < 256; i++)
63 root->children[i] = 0;
65 root->children[(unsigned char) *from] =
66 set_map_string(root->children[(unsigned char) *from], from + 1,
72 int chr_map_chrs(chr_t_entry *t, char **from, int len, int *read, char **to,
78 while (len && t->children && t->children[(unsigned char) **from])
80 t = t->children[(unsigned char) **from];
84 /* if there were no matches, we are still at the root node,
85 which always has a null mapping */
86 for (s = t->target; *s && max; s++)
97 char **chr_map_input(chr_t_entry *t, char **from, int len)
99 static char *buf[2] = {0, 0}, str[2] = {0, 0};
104 while (len && t->children && t->children[(unsigned char) **from])
106 t = t->children[(unsigned char) **from];
110 buf[0] = (char*) t->target;
112 else /* null mapping */
119 else if (isspace(**from))
120 buf[0] = (char*) CHR_SPACE;
122 buf[0] = (char*) CHR_UNKNOWN;
127 /* return (char*) t->target; */
130 static unsigned char prim(char **s)
141 case '\\': c = '\\'; (*s)++; break;
142 case 'r': c = '\r'; (*s)++; break;
143 case 'n': c = '\n'; (*s)++; break;
144 case 't': c = '\t'; (*s)++; break;
145 case 's': c = ' '; (*s)++; break;
146 case 'x': sscanf(*s, "x%2x", &i); c = i; *s += 3; break;
147 case '{': case '[': case '(': case '}': case ']': case ')':
150 default: sscanf(*s, "%3o", &i); c = i; *s += 3; break;
161 * Add an entry to the value space.
163 static void fun_addentry(char *s, void *data, int num)
165 chrmaptab *tab = data;
168 tmp[0] = num; tmp[1] = '\0';
169 tab->input = set_map_string(tab->input, s, strlen(s), tmp);
170 tab->output[num + tab->base_uppercase] = (unsigned char *) xstrdup(s);
175 * Add a space-entry to the value space.
177 static void fun_addspace(char *s, void *data, int num)
179 chrmaptab *tab = data;
180 tab->input = set_map_string(tab->input, s, strlen(s), (char*) CHR_SPACE);
183 static int scan_string(char *s, void (*fun)(char *c, void *data, int num),
184 void *data, int *num)
186 unsigned char c, str[1024], begin, end;
197 logf(LOG_FATAL, "Bad range in char-map");
204 logf(LOG_FATAL, "Bad range in char-map");
208 for (c = begin; c <= end; c++)
210 str[0] = c; str[1] = '\0';
211 (*fun)((char *) str, data, num ? (*num)++ : 0);
214 case '[': s++; abort(); break;
215 case '(': s++; abort(); break;
218 str[0] = c; str[1] = '\0';
219 (*fun)((char *) str, data, num ? (*num)++ : 0);
225 chrmaptab *chr_read_maptab(char *name)
228 char line[512], *argv[50];
229 chrmaptab *res = xmalloc(sizeof(*res));
230 int argc, num = (int) *CHR_BASE, i;
232 if (!(f = yaz_path_fopen(data1_tabpath, name, "r")))
234 logf(LOG_WARN|LOG_ERRNO, "%s", name);
237 res = xmalloc(sizeof(*res));
238 res->input = xmalloc(sizeof(*res->input));
239 res->input->target = (unsigned char*) CHR_UNKNOWN;
240 res->input->equiv = 0;
242 res->input->children = xmalloc(sizeof(res->input) * 256);
243 for (i = 0; i < 256; i++)
245 res->input->children[i] = xmalloc(sizeof(*res->input));
246 res->input->children[i]->children = 0;
247 res->input->children[i]->target = CHR_UNKNOWN;
248 res->input->children[i]->equiv = 0;
251 res->input->children = 0;
253 res->query_equiv = 0;
254 for (i = 0; i < 256; i++)
256 char *t = xmalloc(2);
260 res->output[i] = (unsigned char*)t;
262 res->output[(int) *CHR_SPACE] = (unsigned char *) " ";
263 res->output[(int) *CHR_UNKNOWN] = (unsigned char*) "@";
264 res->base_uppercase = 0;
266 while ((argc = readconf_line(f, line, 512, argv, 50)))
267 if (!yaz_matchstr(argv[0], "lowercase"))
271 logf(LOG_FATAL, "Syntax error in charmap");
275 if (scan_string(argv[1], fun_addentry, res, &num) < 0)
277 logf(LOG_FATAL, "Bad value-set specification");
281 res->base_uppercase = num;
282 res->output[(int) *CHR_SPACE + num] = (unsigned char *) " ";
283 res->output[(int) *CHR_UNKNOWN + num] = (unsigned char*) "@";
284 num = (int) *CHR_BASE;
286 else if (!yaz_matchstr(argv[0], "uppercase"))
288 if (!res->base_uppercase)
290 logf(LOG_FATAL, "Uppercase directive with no lowercase set");
296 logf(LOG_FATAL, "Syntax error in charmap");
300 if (scan_string(argv[1], fun_addentry, res, &num) < 0)
302 logf(LOG_FATAL, "Bad value-set specification");
307 else if (!yaz_matchstr(argv[0], "space"))
311 logf(LOG_FATAL, "Syntax error in charmap");
315 if (scan_string(argv[1], fun_addspace, res, 0))
317 logf(LOG_FATAL, "Bad space specification");
325 logf(LOG_WARN, "Syntax error at '%s' in %s", line, file);