Removed map_only arg from chrmaptab_create. Added skeleton for proper
[idzebra-moved-to-github.git] / util / charmap.c
1 /* $Id: charmap.c,v 1.38 2005-06-15 21:31:45 adam Exp $
2    Copyright (C) 1995-2005
3    Index Data ApS
4
5 This file is part of the Zebra server.
6
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra.  If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.
21 */
22
23
24
25 /*
26  * Support module to handle character-conversions into and out of the
27  * Zebra dictionary.
28  */
29
30 #include <ctype.h>
31 #include <string.h>
32 #include <stdlib.h>
33 #include <assert.h>
34
35 typedef unsigned ucs4_t;
36
37 #include <charmap.h>
38
39 #include <yaz/yaz-util.h>
40
41 #define CHR_MAXSTR 1024
42 #define CHR_MAXEQUIV 32
43
44 const unsigned char CHR_FIELD_BEGIN = '^';
45
46 const char *CHR_UNKNOWN = "\001";
47 const char *CHR_SPACE   = "\002";
48 const char *CHR_CUT     = "\003";
49 const char *CHR_BASE    = "\005";
50
51 struct chrmaptab_info
52 {
53     chr_t_entry *input;         /* mapping table for input data */
54     chr_t_entry *q_input;       /* mapping table for queries */
55     unsigned char *output[256]; /* return mapping - for display of registers */
56     int base_uppercase;         /* Start of upper-case ordinals */
57     NMEM nmem;
58 };
59
60 /*
61  * Character map trie node.
62  */
63 struct chr_t_entry
64 {
65     chr_t_entry **children;  /* array of children */
66     unsigned char **target;  /* target for this node, if any */
67 };
68
69 /*
70  * General argument structure for callback functions (internal use only)
71  */
72 typedef struct chrwork 
73 {
74     chrmaptab map;
75     char string[CHR_MAXSTR+1];
76 } chrwork;
77
78 /*
79  * Callback for equivalent stuff
80  */
81 typedef struct
82 {
83     NMEM nmem;
84     int no_eq;
85     char *eq[CHR_MAXEQUIV];
86 } chr_equiv_work;
87 /*
88  * Add an entry to the character map.
89  */
90 static chr_t_entry *set_map_string(chr_t_entry *root, NMEM nmem,
91                                    const char *from, int len, char *to,
92                                    const char *from_0)
93 {
94     if (!from_0)
95         from_0 = from;
96     if (!root)
97     {
98         root = (chr_t_entry *) nmem_malloc(nmem, sizeof(*root));
99         root->children = 0;
100         root->target = 0;
101     }
102     if (!len)
103     {
104         if (!root->target || !root->target[0] || strcmp(root->target[0], to))
105         {
106             if (from_0 && 
107                 root->target && root->target[0] && root->target[0][0] &&
108                 strcmp (root->target[0], CHR_UNKNOWN))
109             {
110                 yaz_log (YLOG_WARN, "duplicate entry for charmap from '%s'",
111                          from_0);
112             }
113             root->target = (unsigned char **)
114                 nmem_malloc(nmem, sizeof(*root->target)*2);
115             root->target[0] = (unsigned char *) nmem_strdup(nmem, to);
116             root->target[1] = 0;
117         }
118     }
119     else
120     {
121         if (!root->children)
122         {
123             int i;
124
125             root->children = (chr_t_entry **)
126                 nmem_malloc(nmem, sizeof(chr_t_entry*) * 256);
127             for (i = 0; i < 256; i++)
128                 root->children[i] = 0;
129         }
130         if (!(root->children[(unsigned char) *from] =
131             set_map_string(root->children[(unsigned char) *from], nmem,
132                            from + 1, len - 1, to, from_0)))
133             return 0;
134     }
135     return root;
136 }
137
138 static chr_t_entry *find_entry(chr_t_entry *t, const char **from, int len)
139 {
140     chr_t_entry *res;
141
142     if (len && t->children && t->children[(unsigned char) **from])
143     {
144         const char *pos = *from;
145
146         (*from)++;
147         if ((res = find_entry(t->children[(unsigned char) *pos],
148             from, len - 1)))
149             return res;
150         /* no match */
151         *from = pos;
152     }
153     /* no children match. use ourselves, if we have a target */
154     return t->target ? t : 0;
155 }
156
157 static chr_t_entry *find_entry_x(chr_t_entry *t, const char **from, int *len, int first)
158 {
159     chr_t_entry *res;
160
161     while (*len <= 0)
162     {   /* switch to next buffer */
163         if (*len < 0)
164             break;
165         from++;
166         len++;
167     }
168     if (*len > 0 && t->children)
169     {
170         const char *old_from = *from;
171         int old_len = *len;
172
173         res = 0;
174
175         if (first && t->children[CHR_FIELD_BEGIN])
176         {
177             if ((res = find_entry_x(t->children[CHR_FIELD_BEGIN], from, len, 0)) && res != t->children[CHR_FIELD_BEGIN])
178                 return res;
179             else
180                 res = 0;
181             /* otherwhise there was no match on beginning of field, move on */
182         } 
183         
184         if (!res && t->children[(unsigned char) **from])
185         {
186             (*len)--;
187             (*from)++;
188             if ((res = find_entry_x(t->children[(unsigned char) *old_from],
189                                     from, len, 0)))
190                 return res;
191             /* no match */
192             *len = old_len;
193             *from = old_from;
194         }
195     }
196     /* no children match. use ourselves, if we have a target */
197     return t->target ? t : 0;
198 }
199
200 const char **chr_map_input_x(chrmaptab maptab, const char **from, int *len, int first)
201 {
202     chr_t_entry *t = maptab->input;
203     chr_t_entry *res;
204
205     if (!(res = find_entry_x(t, from, len, first)))
206         abort();
207     return (const char **) (res->target);
208 }
209
210 const char **chr_map_input(chrmaptab maptab, const char **from, int len, int first)
211 {
212     chr_t_entry *t = maptab->input;
213     chr_t_entry *res;
214     int len_tmp[2];
215
216     len_tmp[0] = len;
217     len_tmp[1] = -1;
218     if (!(res = find_entry_x(t, from, len_tmp, first)))
219         abort();
220     return (const char **) (res->target);
221 }
222
223 const char **chr_map_q_input(chrmaptab maptab,
224                              const char **from, int len, int first)
225 {
226     chr_t_entry *t = maptab->q_input;
227     chr_t_entry *res;
228     int len_tmp[2];
229     
230     len_tmp[0] = len;
231     len_tmp[1] = -1;
232     if (!(res = find_entry_x(t, from, len_tmp, first)))
233         return 0;
234     return (const char **) (res->target);
235 }
236
237 const char *chr_map_output(chrmaptab maptab, const char **from, int len)
238 {
239     unsigned char c = ** (unsigned char **) from;
240     (*from)++;
241     return (const char*) maptab->output[c];
242 }
243
244 static int zebra_ucs4_strlen(ucs4_t *s)
245 {
246     int i = 0;
247     while (*s++)
248         i++;
249     return i;
250 }
251
252 ucs4_t zebra_prim_w(ucs4_t **s)
253 {
254     ucs4_t c;
255     ucs4_t i = 0;
256     char fmtstr[8];
257
258     yaz_log (YLOG_DEBUG, "prim_w %.3s", (char *) *s);
259     if (**s == '\\' && 1[*s])
260     {
261         (*s)++;
262         c = **s;
263         switch (c)
264         {
265         case '\\': c = '\\'; (*s)++; break;
266         case 'r': c = '\r'; (*s)++; break;
267         case 'n': c = '\n'; (*s)++; break;
268         case 't': c = '\t'; (*s)++; break;
269         case 's': c = ' '; (*s)++; break;
270         case 'x': 
271             if (zebra_ucs4_strlen(*s) >= 3)
272             {
273                 fmtstr[0] = (*s)[1];
274                 fmtstr[1] = (*s)[2];
275                 fmtstr[2] = 0;
276                 sscanf(fmtstr, "%x", &i);
277                 c = i;
278                 *s += 3;
279             }
280             break;
281         case '0':
282         case '1':
283         case '2':
284         case '3':
285         case '4':
286         case '5':
287         case '6':
288         case '7':
289         case '8':
290         case '9':
291             if (zebra_ucs4_strlen(*s) >= 3)
292             {
293                 fmtstr[0] = (*s)[0];
294                 fmtstr[1] = (*s)[1];
295                 fmtstr[2] = (*s)[2];
296                 fmtstr[3] = 0;
297                 sscanf(fmtstr, "%o", &i);
298                 c = i;
299                 *s += 3;
300             }
301             break;
302         case 'L':
303             if (zebra_ucs4_strlen(*s) >= 5)
304             {
305                 fmtstr[0] = (*s)[1];
306                 fmtstr[1] = (*s)[2];
307                 fmtstr[2] = (*s)[3];
308                 fmtstr[3] = (*s)[4];
309                 fmtstr[4] = 0;
310                 sscanf(fmtstr, "%x", &i);
311                 c = i;
312                 *s += 5;
313             }
314             break;
315         default:
316             (*s)++;
317         }
318     }
319     else
320     {
321         c = **s;
322         ++(*s);
323     }
324     yaz_log (YLOG_DEBUG, "out %d", c);
325     return c;
326 }
327
328 /*
329  * Callback function.
330  * Add an entry to the value space.
331  */
332 static void fun_addentry(const char *s, void *data, int num)
333 {
334     chrmaptab tab = (chrmaptab) data;
335     char tmp[2];
336     
337     tmp[0] = num; tmp[1] = '\0';
338     tab->input = set_map_string(tab->input, tab->nmem, s, strlen(s), tmp, 0);
339     tab->output[num + tab->base_uppercase] =
340         (unsigned char *) nmem_strdup(tab->nmem, s);
341 }
342
343 /* 
344  * Callback function.
345  * Add a space-entry to the value space.
346  */
347 static void fun_addspace(const char *s, void *data, int num)
348 {
349     chrmaptab tab = (chrmaptab) data;
350     tab->input = set_map_string(tab->input, tab->nmem, s, strlen(s),
351                                 (char*) CHR_SPACE, 0);
352 }
353
354 /* 
355  * Callback function.
356  * Add a space-entry to the value space.
357  */
358 static void fun_addcut(const char *s, void *data, int num)
359 {
360     chrmaptab tab = (chrmaptab) data;
361     tab->input = set_map_string(tab->input, tab->nmem, s, strlen(s),
362                                 (char*) CHR_CUT, 0);
363 }
364
365 /*
366  * Create a string containing the mapped characters provided.
367  */
368 static void fun_mkstring(const char *s, void *data, int num)
369 {
370     chrwork *arg = (chrwork *) data;
371     const char **res, *p = s;
372
373     res = chr_map_input(arg->map, &s, strlen(s), 0);
374     if (*res == (char*) CHR_UNKNOWN)
375         yaz_log(YLOG_WARN, "Map: '%s' has no mapping", p);
376     strncat(arg->string, *res, CHR_MAXSTR - strlen(arg->string));
377     arg->string[CHR_MAXSTR] = '\0';
378 }
379
380 /*
381  * Create an unmodified string (scan_string handler).
382  */
383 static void fun_add_equivalent_string(const char *s, void *data, int num)
384 {
385     chr_equiv_work *arg = (chr_equiv_work *) data;
386     
387     if (arg->no_eq == CHR_MAXEQUIV)
388         return;
389     arg->eq[arg->no_eq++] = nmem_strdup(arg->nmem, s);
390 }
391
392 /*
393  * Add a map to the string contained in the argument.
394  */
395 static void fun_add_map(const char *s, void *data, int num)
396 {
397     chrwork *arg = (chrwork *) data;
398
399     assert(arg->map->input);
400     yaz_log (YLOG_DEBUG, "set map %.*s", (int) strlen(s), s);
401     set_map_string(arg->map->input, arg->map->nmem, s, strlen(s), arg->string,
402                    0);
403     for (s = arg->string; *s; s++)
404         yaz_log (YLOG_DEBUG, " %3d", (unsigned char) *s);
405 }
406
407 static int scan_to_utf8 (yaz_iconv_t t, ucs4_t *from, size_t inlen,
408                         char *outbuf, size_t outbytesleft)
409 {
410     size_t inbytesleft = inlen * sizeof(ucs4_t);
411     char *inbuf = (char*) from;
412     size_t ret;
413    
414     if (t == 0)
415         *outbuf++ = *from;  /* ISO-8859-1 is OK here */
416     else
417     {
418         ret = yaz_iconv (t, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
419         if (ret == (size_t) (-1))
420         {
421             yaz_log(YLOG_LOG, "from: %2X %2X %2X %2X",
422                     from[0], from[1], from[2], from[3]);
423             yaz_log (YLOG_WARN|YLOG_ERRNO, "bad unicode sequence");
424             return -1;
425         }
426     }
427     *outbuf = '\0';
428     return 0;
429 }
430
431 static int scan_string(char *s_native,
432                        yaz_iconv_t t_unicode, yaz_iconv_t t_utf8,
433                        void (*fun)(const char *c, void *data, int num),
434                        void *data, int *num)
435 {
436     char str[1024];
437
438     ucs4_t arg[512];
439     ucs4_t arg_prim[512];
440     ucs4_t *s0, *s = arg;
441     ucs4_t c, begin, end;
442     size_t i;
443
444     if (t_unicode != 0)
445     {
446         char *outbuf = (char *) arg;
447         char *inbuf = s_native;
448         size_t outbytesleft = sizeof(arg)-4;
449         size_t inbytesleft = strlen(s_native);
450         size_t ret;             
451         ret = yaz_iconv(t_unicode, &inbuf, &inbytesleft,
452                         &outbuf, &outbytesleft);
453         if (ret == (size_t)(-1))
454             return -1;
455         i = (outbuf - (char*) arg)/sizeof(ucs4_t);
456     }
457     else
458     { 
459         for (i = 0; s_native[i]; i++)
460             arg[i] = s_native[i] & 255; /* ISO-8859-1 conversion */
461     }
462     arg[i] = 0;      /* terminate */
463     if (s[0] == 0xfeff || s[0] == 0xfeff)  /* skip byte Order Mark */
464         s++;
465     while (*s)
466     {
467         switch (*s)
468         {
469         case '{':
470             s++;
471             begin = zebra_prim_w(&s);
472             if (*s != '-')
473             {
474                 yaz_log(YLOG_FATAL, "Bad range in char-map");
475                 return -1;
476             }
477             s++;
478             end = zebra_prim_w(&s);
479             if (end <= begin)
480             {
481                 yaz_log(YLOG_FATAL, "Bad range in char-map");
482                 return -1;
483             }
484             s++;
485             for (c = begin; c <= end; c++)
486             {
487                 if (scan_to_utf8 (t_utf8, &c, 1, str, sizeof(str)-1))
488                     return -1;
489                 (*fun)(str, data, num ? (*num)++ : 0);
490             }
491             break;
492         case '(':
493             ++s;
494             s0 = s; i = 0;
495             while (*s != ')' || s[-1] == '\\')
496                 arg_prim[i++] = zebra_prim_w(&s);
497             arg_prim[i] = 0;
498             if (scan_to_utf8 (t_utf8, arg_prim, zebra_ucs4_strlen(arg_prim), str, sizeof(str)-1))
499                 return -1;
500             (*fun)(str, data, num ? (*num)++ : 0);
501             s++;
502             break;
503         default:
504             c = zebra_prim_w(&s);
505             if (scan_to_utf8 (t_utf8, &c, 1, str, sizeof(str)-1))
506                 return -1;
507             (*fun)(str, data, num ? (*num)++ : 0);
508         }
509     }
510     return 0;
511 }
512
513 chrmaptab chrmaptab_create(const char *tabpath, const char *name,
514                            const char *tabroot)
515 {
516     FILE *f;
517     char line[512], *argv[50];
518     chrmaptab res;
519     int lineno = 0;
520     int errors = 0;
521     int argc, num = (int) *CHR_BASE, i;
522     NMEM nmem;
523     yaz_iconv_t t_unicode = 0;
524     yaz_iconv_t t_utf8 = 0;
525     unsigned endian = 31;
526     const char *ucs4_native = "UCS-4";
527
528     yaz_log (YLOG_DEBUG, "maptab %s open", name);
529     if (!(f = yaz_fopen(tabpath, name, "r", tabroot)))
530     {
531         yaz_log(YLOG_WARN|YLOG_ERRNO, "%s", name);
532         return 0;
533     }
534
535     if (*(char*) &endian == 31)      /* little endian? */
536         ucs4_native = "UCS-4LE";
537
538     t_utf8 = yaz_iconv_open ("UTF-8", ucs4_native);
539
540     nmem = nmem_create ();
541     res = (chrmaptab) nmem_malloc(nmem, sizeof(*res));
542     res->nmem = nmem;
543     res->input = (chr_t_entry *) nmem_malloc(res->nmem, sizeof(*res->input));
544     res->input->target = (unsigned char **)
545         nmem_malloc(res->nmem, sizeof(*res->input->target) * 2);
546     res->input->target[0] = (unsigned char*) CHR_UNKNOWN;
547     res->input->target[1] = 0;
548     res->input->children = (chr_t_entry **)
549         nmem_malloc(res->nmem, sizeof(res->input) * 256);
550     for (i = 0; i < 256; i++)
551     {
552         res->input->children[i] = (chr_t_entry *)
553             nmem_malloc(res->nmem, sizeof(*res->input));
554         res->input->children[i]->children = 0;
555         res->input->children[i]->target = (unsigned char **)
556             nmem_malloc (res->nmem, 2 * sizeof(unsigned char *));
557         res->input->children[i]->target[1] = 0;
558         res->input->children[i]->target[0] = (unsigned char*) CHR_UNKNOWN;
559     }
560     res->q_input = (chr_t_entry *)
561         nmem_malloc(res->nmem, sizeof(*res->q_input));
562     res->q_input->target = 0;
563     res->q_input->children = 0;
564
565     for (i = *CHR_BASE; i < 256; i++)
566         res->output[i] = 0;
567     res->output[(int) *CHR_SPACE] = (unsigned char *) " ";
568     res->output[(int) *CHR_UNKNOWN] = (unsigned char*) "@";
569     res->base_uppercase = 0;
570
571     while (!errors && (argc = readconf_line(f, &lineno, line, 512, argv, 50)))
572         if (!yaz_matchstr(argv[0], "lowercase"))
573         {
574             if (argc != 2)
575             {
576                 yaz_log(YLOG_FATAL, "Syntax error in charmap");
577                 ++errors;
578             }
579             if (scan_string(argv[1], t_unicode, t_utf8, fun_addentry,
580                             res, &num) < 0)
581             {
582                 yaz_log(YLOG_FATAL, "Bad value-set specification");
583                 ++errors;
584             }
585             res->base_uppercase = num;
586             res->output[(int) *CHR_SPACE + num] = (unsigned char *) " ";
587             res->output[(int) *CHR_UNKNOWN + num] = (unsigned char*) "@";
588             num = (int) *CHR_BASE;
589         }
590         else if (!yaz_matchstr(argv[0], "uppercase"))
591         {
592             if (!res->base_uppercase)
593             {
594                 yaz_log(YLOG_FATAL, "Uppercase directive with no lowercase set");
595                 ++errors;
596             }
597             if (argc != 2)
598             {
599                 yaz_log(YLOG_FATAL, "Missing arg for uppercase directive");
600                 ++errors;
601             }
602             if (scan_string(argv[1], t_unicode, t_utf8, fun_addentry,
603                             res, &num) < 0)
604             {
605                 yaz_log(YLOG_FATAL, "Bad value-set specification");
606                 ++errors;
607             }
608         }
609         else if (!yaz_matchstr(argv[0], "space"))
610         {
611             if (argc != 2)
612             {
613                 yaz_log(YLOG_FATAL, "Syntax error in charmap for space");
614                 ++errors;
615             }
616             if (scan_string(argv[1], t_unicode, t_utf8,
617                             fun_addspace, res, 0) < 0)
618             {
619                 yaz_log(YLOG_FATAL, "Bad space specification");
620                 ++errors;
621             }
622         }
623         else if (!yaz_matchstr(argv[0], "cut"))
624         {
625             if (argc != 2)
626             {
627                 yaz_log(YLOG_FATAL, "Syntax error in charmap for cut");
628                 ++errors;
629             }
630             if (scan_string(argv[1], t_unicode, t_utf8,
631                             fun_addcut, res, 0) < 0)
632             {
633                 yaz_log(YLOG_FATAL, "Bad cut specification");
634                 ++errors;
635             }
636         }
637         else if (!yaz_matchstr(argv[0], "map"))
638         {
639             chrwork buf;
640
641             if (argc != 3)
642             {
643                 yaz_log(YLOG_FATAL, "charmap directive map requires 2 args");
644                 ++errors;
645             }
646             buf.map = res;
647             buf.string[0] = '\0';
648             if (scan_string(argv[2], t_unicode, t_utf8,
649                             fun_mkstring, &buf, 0) < 0)
650             {
651                 yaz_log(YLOG_FATAL, "Bad map target");
652                 ++errors;
653             }
654             if (scan_string(argv[1], t_unicode, t_utf8,
655                             fun_add_map, &buf, 0) < 0)
656             {
657                 yaz_log(YLOG_FATAL, "Bad map source");
658                 ++errors;
659             }
660         }
661         else if (!yaz_matchstr(argv[0], "equivalent"))
662         {
663             chr_equiv_work w;
664
665             if (argc != 2)
666             {
667                 yaz_log(YLOG_FATAL, "equivalent requires 1 argument");
668                 ++errors;
669             }
670             w.nmem = res->nmem;
671             w.no_eq = 0;
672             if (scan_string(argv[1], t_unicode, t_utf8, 
673                             fun_add_equivalent_string, &w, 0) < 0)
674             {
675                 yaz_log(YLOG_FATAL, "equivalent: invalid string");
676                 ++errors;
677             }
678             else if (w.no_eq == 0)
679             {
680                 yaz_log(YLOG_FATAL, "equivalent: no strings");
681                 ++errors;
682             }
683             else
684             {
685                 char *result_str;
686                 int i, slen = 5;
687
688                 /* determine length of regular expression */
689                 for (i = 0; i<w.no_eq; i++)
690                     slen += strlen(w.eq[i]) + 1;
691                 result_str = nmem_malloc(res->nmem, slen + 5);
692
693                 /* build the regular expression */
694                 *result_str = '\0';
695                 slen = 0;
696                 for (i = 0; i<w.no_eq; i++)
697                 {
698                     result_str[slen++]  = i ? '|' : '(';
699                     strcpy(result_str + slen, w.eq[i]);
700                     slen += strlen(w.eq[i]);
701                 }
702                 result_str[slen++] = ')';
703                 result_str[slen] = '\0';
704
705                 /* each eq will map to this regular expression */
706                 for (i = 0; i<w.no_eq; i++)
707                 {
708                     set_map_string(res->q_input, res->nmem,
709                                    w.eq[i], strlen(w.eq[i]),
710                                    result_str, 0);
711                 }
712             }
713         }
714         else if (!yaz_matchstr(argv[0], "encoding"))
715         {
716             /*
717              * Fix me. When t_unicode==0 and use encoding directive in *.chr file the beheviour of the
718              * zebra need to comment next part of code.
719              */
720
721             /* Original code */
722 #if 1
723             if (t_unicode != 0)
724                 yaz_iconv_close (t_unicode);
725             t_unicode = yaz_iconv_open (ucs4_native, argv[1]);
726 #endif
727             /*
728              * Fix me. It is additional staff for conversion of characters from local encoding
729              * of *.chr file to UTF-8 (internal encoding).
730              * NOTE: The derective encoding must be first directive in *.chr file.
731              */
732             /* For whatever reason Oleg enabled this.. */
733 #if 0
734             if (t_utf8 != 0)
735                 yaz_iconv_close(t_utf8);
736             t_utf8 = yaz_iconv_open ("UTF-8", argv[1]);
737 #endif
738         }
739         else
740         {
741             yaz_log(YLOG_WARN, "Syntax error at '%s' in %s", line, name);
742         }
743     
744     yaz_fclose(f);
745     if (errors)
746     {
747         chrmaptab_destroy(res);
748         res = 0;
749     }
750     yaz_log (YLOG_DEBUG, "maptab %s close %d errors", name, errors);
751     if (t_utf8 != 0)
752         yaz_iconv_close(t_utf8);
753     if (t_unicode != 0)
754         yaz_iconv_close(t_unicode);
755     return res;
756 }
757
758 void chrmaptab_destroy(chrmaptab tab)
759 {
760     if (tab)
761         nmem_destroy (tab->nmem);
762 }
763
764