Removed replace_tokens code that has not been used for quite some
[idzebra-moved-to-github.git] / util / charmap.c
1 /* $Id: charmap.c,v 1.37 2005-06-14 12:42:49 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, int map_only,
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     if (*(char*) &endian == 31)      /* little endian? */
529         ucs4_native = "UCS-4LE";
530
531     t_utf8 = yaz_iconv_open ("UTF-8", ucs4_native);
532
533     yaz_log (YLOG_DEBUG, "maptab %s open", name);
534     if (!(f = yaz_fopen(tabpath, name, "r", tabroot)))
535     {
536         yaz_log(YLOG_WARN|YLOG_ERRNO, "%s", name);
537         return 0;
538     }
539     nmem = nmem_create ();
540     res = (chrmaptab) nmem_malloc(nmem, sizeof(*res));
541     res->nmem = nmem;
542     res->input = (chr_t_entry *) nmem_malloc(res->nmem, sizeof(*res->input));
543     res->input->target = (unsigned char **)
544         nmem_malloc(res->nmem, sizeof(*res->input->target) * 2);
545     res->input->target[0] = (unsigned char*) CHR_UNKNOWN;
546     res->input->target[1] = 0;
547     res->input->children = (chr_t_entry **)
548         nmem_malloc(res->nmem, sizeof(res->input) * 256);
549     for (i = 0; i < 256; i++)
550     {
551         res->input->children[i] = (chr_t_entry *)
552             nmem_malloc(res->nmem, sizeof(*res->input));
553         res->input->children[i]->children = 0;
554         res->input->children[i]->target = (unsigned char **)
555             nmem_malloc (res->nmem, 2 * sizeof(unsigned char *));
556         res->input->children[i]->target[1] = 0;
557         if (map_only)
558         {
559             res->input->children[i]->target[0] = (unsigned char *)
560                 nmem_malloc (res->nmem, 2 * sizeof(unsigned char));
561             res->input->children[i]->target[0][0] = i;
562             res->input->children[i]->target[0][1] = 0;
563         }
564         else
565             res->input->children[i]->target[0] = (unsigned char*) CHR_UNKNOWN;
566     }
567     res->q_input = (chr_t_entry *)
568         nmem_malloc(res->nmem, sizeof(*res->q_input));
569     res->q_input->target = 0;
570     res->q_input->children = 0;
571
572     for (i = *CHR_BASE; i < 256; i++)
573         res->output[i] = 0;
574     res->output[(int) *CHR_SPACE] = (unsigned char *) " ";
575     res->output[(int) *CHR_UNKNOWN] = (unsigned char*) "@";
576     res->base_uppercase = 0;
577
578     while (!errors && (argc = readconf_line(f, &lineno, line, 512, argv, 50)))
579         if (!map_only && !yaz_matchstr(argv[0], "lowercase"))
580         {
581             if (argc != 2)
582             {
583                 yaz_log(YLOG_FATAL, "Syntax error in charmap");
584                 ++errors;
585             }
586             if (scan_string(argv[1], t_unicode, t_utf8, fun_addentry,
587                             res, &num) < 0)
588             {
589                 yaz_log(YLOG_FATAL, "Bad value-set specification");
590                 ++errors;
591             }
592             res->base_uppercase = num;
593             res->output[(int) *CHR_SPACE + num] = (unsigned char *) " ";
594             res->output[(int) *CHR_UNKNOWN + num] = (unsigned char*) "@";
595             num = (int) *CHR_BASE;
596         }
597         else if (!map_only && !yaz_matchstr(argv[0], "uppercase"))
598         {
599             if (!res->base_uppercase)
600             {
601                 yaz_log(YLOG_FATAL, "Uppercase directive with no lowercase set");
602                 ++errors;
603             }
604             if (argc != 2)
605             {
606                 yaz_log(YLOG_FATAL, "Missing arg for uppercase directive");
607                 ++errors;
608             }
609             if (scan_string(argv[1], t_unicode, t_utf8, fun_addentry,
610                             res, &num) < 0)
611             {
612                 yaz_log(YLOG_FATAL, "Bad value-set specification");
613                 ++errors;
614             }
615         }
616         else if (!map_only && !yaz_matchstr(argv[0], "space"))
617         {
618             if (argc != 2)
619             {
620                 yaz_log(YLOG_FATAL, "Syntax error in charmap for space");
621                 ++errors;
622             }
623             if (scan_string(argv[1], t_unicode, t_utf8,
624                             fun_addspace, res, 0) < 0)
625             {
626                 yaz_log(YLOG_FATAL, "Bad space specification");
627                 ++errors;
628             }
629         }
630         else if (!map_only && !yaz_matchstr(argv[0], "cut"))
631         {
632             if (argc != 2)
633             {
634                 yaz_log(YLOG_FATAL, "Syntax error in charmap for cut");
635                 ++errors;
636             }
637             if (scan_string(argv[1], t_unicode, t_utf8,
638                             fun_addcut, res, 0) < 0)
639             {
640                 yaz_log(YLOG_FATAL, "Bad cut specification");
641                 ++errors;
642             }
643         }
644         else if (!yaz_matchstr(argv[0], "map"))
645         {
646             chrwork buf;
647
648             if (argc != 3)
649             {
650                 yaz_log(YLOG_FATAL, "charmap directive map requires 2 args");
651                 ++errors;
652             }
653             buf.map = res;
654             buf.string[0] = '\0';
655             if (scan_string(argv[2], t_unicode, t_utf8,
656                             fun_mkstring, &buf, 0) < 0)
657             {
658                 yaz_log(YLOG_FATAL, "Bad map target");
659                 ++errors;
660             }
661             if (scan_string(argv[1], t_unicode, t_utf8,
662                             fun_add_map, &buf, 0) < 0)
663             {
664                 yaz_log(YLOG_FATAL, "Bad map source");
665                 ++errors;
666             }
667         }
668         else if (!yaz_matchstr(argv[0], "equivalent"))
669         {
670             chr_equiv_work w;
671
672             if (argc != 2)
673             {
674                 yaz_log(YLOG_FATAL, "equivalent requires 1 argument");
675                 ++errors;
676             }
677             w.nmem = res->nmem;
678             w.no_eq = 0;
679             if (scan_string(argv[1], t_unicode, t_utf8, 
680                             fun_add_equivalent_string, &w, 0) < 0)
681             {
682                 yaz_log(YLOG_FATAL, "equivalent: invalid string");
683                 ++errors;
684             }
685             else if (w.no_eq == 0)
686             {
687                 yaz_log(YLOG_FATAL, "equivalent: no strings");
688                 ++errors;
689             }
690             else
691             {
692                 char *result_str;
693                 int i, slen = 5;
694
695                 /* determine length of regular expression */
696                 for (i = 0; i<w.no_eq; i++)
697                     slen += strlen(w.eq[i]) + 1;
698                 result_str = nmem_malloc(res->nmem, slen + 5);
699
700                 /* build the regular expression */
701                 *result_str = '\0';
702                 slen = 0;
703                 for (i = 0; i<w.no_eq; i++)
704                 {
705                     result_str[slen++]  = i ? '|' : '(';
706                     strcpy(result_str + slen, w.eq[i]);
707                     slen += strlen(w.eq[i]);
708                 }
709                 result_str[slen++] = ')';
710                 result_str[slen] = '\0';
711
712                 /* each eq will map to this regular expression */
713                 for (i = 0; i<w.no_eq; i++)
714                 {
715                     set_map_string(res->q_input, res->nmem,
716                                    w.eq[i], strlen(w.eq[i]),
717                                    result_str, 0);
718                 }
719             }
720         }
721         else if (!yaz_matchstr(argv[0], "encoding"))
722         {
723             /*
724              * Fix me. When t_unicode==0 and use encoding directive in *.chr file the beheviour of the
725              * zebra need to comment next part of code.
726              */
727
728             /* Original code */
729 #if 1
730             if (t_unicode != 0)
731                 yaz_iconv_close (t_unicode);
732             t_unicode = yaz_iconv_open (ucs4_native, argv[1]);
733 #endif
734             /*
735              * Fix me. It is additional staff for conversion of characters from local encoding
736              * of *.chr file to UTF-8 (internal encoding).
737              * NOTE: The derective encoding must be first directive in *.chr file.
738              */
739             /* For whatever reason Oleg enabled this.. */
740 #if 0
741             if (t_utf8 != 0)
742                 yaz_iconv_close(t_utf8);
743             t_utf8 = yaz_iconv_open ("UTF-8", argv[1]);
744 #endif
745         }
746         else
747         {
748             yaz_log(YLOG_WARN, "Syntax error at '%s' in %s", line, name);
749         }
750     
751     yaz_fclose(f);
752     if (errors)
753     {
754         chrmaptab_destroy(res);
755         res = 0;
756     }
757     yaz_log (YLOG_DEBUG, "maptab %s close %d errors", name, errors);
758     if (t_utf8 != 0)
759         yaz_iconv_close(t_utf8);
760     if (t_unicode != 0)
761         yaz_iconv_close(t_unicode);
762     return res;
763 }
764
765 void chrmaptab_destroy(chrmaptab tab)
766 {
767     if (tab)
768         nmem_destroy (tab->nmem);
769 }
770
771