6aa3f64fb2ec699036f3a903ddd4bd7c5e711f67
[idzebra-moved-to-github.git] / index / untrans.c
1 /* This file is part of the Zebra server.
2    Copyright (C) 1994-2009 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 #include <stdio.h>
21 #include <assert.h>
22 #include <ctype.h>
23
24 #include <yaz/diagbib1.h>
25 #include "index.h"
26 #include <charmap.h>
27
28 int zebra_term_untrans(ZebraHandle zh, const char *index_type,
29                        char *dst, const char *src)
30 {
31     zebra_map_t zm = zebra_map_get(zh->reg->zebra_maps, index_type);
32     if (!zm)
33     {
34         return -2;
35     }
36     if (zebra_maps_is_icu(zm))
37     {
38         return -1;
39     }
40     else
41     {
42         int len = 0;
43         while (*src)
44         {
45             const char *cp = zebra_maps_output(zm, &src);
46             if (!cp)
47             {
48                 if (len < IT_MAX_WORD-1)
49                     dst[len++] = *src;
50                 src++;
51             }
52             else
53                 while (*cp && len < IT_MAX_WORD-1)
54                     dst[len++] = *cp++;
55         }
56         dst[len] = '\0';
57     }
58     return 0;
59 }
60
61 int zebra_term_untrans_iconv(ZebraHandle zh, NMEM stream, 
62                              const char *index_type,
63                              char **dst, const char *src)
64 {
65     char term_src[IT_MAX_WORD];
66     char term_dst[IT_MAX_WORD];
67     int r;
68     
69     r = zebra_term_untrans (zh, index_type, term_src, src);
70     if (r)
71         return r;
72
73     if (zh->iconv_from_utf8 != 0)
74     {
75         int len;
76         char *inbuf = term_src;
77         size_t inleft = strlen(term_src);
78         char *outbuf = term_dst;
79         size_t outleft = sizeof(term_dst)-1;
80         size_t ret;
81         
82         ret = yaz_iconv (zh->iconv_from_utf8, &inbuf, &inleft,
83                          &outbuf, &outleft);
84         if (ret == (size_t)(-1))
85             len = 0;
86         else
87         {
88             yaz_iconv (zh->iconv_from_utf8, 0, 0, &outbuf, &outleft);
89             len = outbuf - term_dst;
90         }
91         *dst = nmem_malloc(stream, len + 1);
92         if (len > 0)
93             memcpy (*dst, term_dst, len);
94         (*dst)[len] = '\0';
95     }
96     else
97         *dst = nmem_strdup(stream, term_src);
98     return 0;
99 }
100
101
102
103 /*
104  * Local variables:
105  * c-basic-offset: 4
106  * c-file-style: "Stroustrup"
107  * indent-tabs-mode: nil
108  * End:
109  * vim: shiftwidth=4 tabstop=8 expandtab
110  */
111