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