IDZEBRA  2.1.2
untrans.c
Go to the documentation of this file.
1 /* This file is part of the Zebra server.
2  Copyright (C) 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 #if HAVE_CONFIG_H
21 #include <config.h>
22 #endif
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 int 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  if (!zm)
36  {
37  return -2;
38  }
39  if (zebra_maps_is_icu(zm))
40  {
41  return -1;
42  }
43  else
44  {
45  int len = 0;
46  while (*src)
47  {
48  const char *cp = zebra_maps_output(zm, &src);
49  if (!cp)
50  {
51  if (len < IT_MAX_WORD-1)
52  dst[len++] = *src;
53  src++;
54  }
55  else
56  while (*cp && len < IT_MAX_WORD-1)
57  dst[len++] = *cp++;
58  }
59  dst[len] = '\0';
60  }
61  return 0;
62 }
63 
65  const char *index_type,
66  char **dst, const char *src)
67 {
68  char term_src[IT_MAX_WORD];
69  char term_dst[IT_MAX_WORD];
70  int r;
71 
72  r = zebra_term_untrans (zh, index_type, term_src, src);
73  if (r)
74  return r;
75 
76  if (zh->iconv_from_utf8 != 0)
77  {
78  int len;
79  char *inbuf = term_src;
80  size_t inleft = strlen(term_src);
81  char *outbuf = term_dst;
82  size_t outleft = sizeof(term_dst)-1;
83  size_t ret;
84 
85  ret = yaz_iconv (zh->iconv_from_utf8, &inbuf, &inleft,
86  &outbuf, &outleft);
87  if (ret == (size_t)(-1))
88  len = 0;
89  else
90  {
91  yaz_iconv (zh->iconv_from_utf8, 0, 0, &outbuf, &outleft);
92  len = outbuf - term_dst;
93  }
94  *dst = nmem_malloc(stream, len + 1);
95  if (len > 0)
96  memcpy (*dst, term_dst, len);
97  (*dst)[len] = '\0';
98  }
99  else
100  *dst = nmem_strdup(stream, term_src);
101  return 0;
102 }
103 
104 
105 
106 /*
107  * Local variables:
108  * c-basic-offset: 4
109  * c-file-style: "Stroustrup"
110  * indent-tabs-mode: nil
111  * End:
112  * vim: shiftwidth=4 tabstop=8 expandtab
113  */
114 
yaz_iconv_t iconv_from_utf8
Definition: index.h:216
struct zebra_register * reg
Definition: index.h:174
ZebraHandle zh
Definition: zebrash.c:64
int zebra_term_untrans(ZebraHandle zh, const char *index_type, char *dst, const char *src)
Definition: untrans.c:31
#define IT_MAX_WORD
Definition: it_key.h:27
int zebra_maps_is_icu(zebra_map_t zm)
Definition: zebramap.c:740
int zebra_term_untrans_iconv(ZebraHandle zh, NMEM stream, const char *index_type, char **dst, const char *src)
Definition: untrans.c:64
zebra_maps_t zebra_maps
Definition: index.h:143
zebra_map_t zebra_map_get(zebra_maps_t zms, const char *id)
Definition: zebramap.c:354
const char * zebra_maps_output(zebra_map_t zm, const char **from)
Definition: zebramap.c:437