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