0551e3c3ec2d8513f5ce9ca5fdac8720f6104d11
[idzebra-moved-to-github.git] / index / untrans.c
1 /* $Id: untrans.c,v 1.5 2007-10-31 16:56:14 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 #include <stdio.h>
24 #include <assert.h>
25 #include <ctype.h>
26
27 #include <yaz/diagbib1.h>
28 #include "index.h"
29 #include <charmap.h>
30
31 void zebra_term_untrans(ZebraHandle zh, const char *index_type,
32                         char *dst, const char *src)
33 {
34     zebra_map_t zm = zebra_map_get(zh->reg->zebra_maps, index_type);
35     int len = 0;
36     while (*src)
37     {
38         const char *cp = zebra_maps_output(zm, &src);
39         if (!cp)
40         {
41             if (len < IT_MAX_WORD-1)
42                 dst[len++] = *src;
43             src++;
44         }
45         else
46             while (*cp && len < IT_MAX_WORD-1)
47                 dst[len++] = *cp++;
48     }
49     dst[len] = '\0';
50 }
51
52 void zebra_term_untrans_iconv(ZebraHandle zh, NMEM stream, 
53                               const char *index_type,
54                               char **dst, const char *src)
55 {
56     char term_src[IT_MAX_WORD];
57     char term_dst[IT_MAX_WORD];
58     
59     zebra_term_untrans (zh, index_type, term_src, src);
60
61     if (zh->iconv_from_utf8 != 0)
62     {
63         int len;
64         char *inbuf = term_src;
65         size_t inleft = strlen(term_src);
66         char *outbuf = term_dst;
67         size_t outleft = sizeof(term_dst)-1;
68         size_t ret;
69         
70         ret = yaz_iconv (zh->iconv_from_utf8, &inbuf, &inleft,
71                          &outbuf, &outleft);
72         if (ret == (size_t)(-1))
73             len = 0;
74         else
75         {
76             yaz_iconv (zh->iconv_from_utf8, 0, 0, &outbuf, &outleft);
77             len = outbuf - term_dst;
78         }
79         *dst = nmem_malloc(stream, len + 1);
80         if (len > 0)
81             memcpy (*dst, term_dst, len);
82         (*dst)[len] = '\0';
83     }
84     else
85         *dst = nmem_strdup(stream, term_src);
86 }
87
88
89
90 /*
91  * Local variables:
92  * c-basic-offset: 4
93  * indent-tabs-mode: nil
94  * End:
95  * vim: shiftwidth=4 tabstop=8 expandtab
96  */