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