Fix uninit variable (hits_limit)
[idzebra-moved-to-github.git] / index / zaptterm.c
1 /* This file is part of the Zebra server.
2    Copyright (C) 1994-2011 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 /* convert APT search term to UTF8 */
32 ZEBRA_RES zapt_term_to_utf8(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
33                             char *termz)
34 {
35     size_t sizez;
36     Z_Term *term = zapt->term;
37
38     switch (term->which)
39     {
40     case Z_Term_general:
41         if (zh->iconv_to_utf8 != 0)
42         {
43             char *inbuf = (char *) term->u.general->buf;
44             size_t inleft = term->u.general->len;
45             char *outbuf = termz;
46             size_t outleft = IT_MAX_WORD-1;
47             size_t ret;
48
49             ret = yaz_iconv(zh->iconv_to_utf8, &inbuf, &inleft,
50                         &outbuf, &outleft);
51             if (ret == (size_t)(-1))
52             {
53                 ret = yaz_iconv(zh->iconv_to_utf8, 0, 0, 0, 0);
54                 zebra_setError(
55                     zh, 
56                     YAZ_BIB1_QUERY_TERM_INCLUDES_CHARS_THAT_DO_NOT_TRANSLATE_INTO_,
57                     0);
58                 return ZEBRA_FAIL;
59             }
60             yaz_iconv(zh->iconv_to_utf8, 0, 0, &outbuf, &outleft);
61             *outbuf = 0;
62         }
63         else
64         {
65             sizez = term->u.general->len;
66             if (sizez > IT_MAX_WORD-1)
67                 sizez = IT_MAX_WORD-1;
68             memcpy (termz, term->u.general->buf, sizez);
69             termz[sizez] = '\0';
70         }
71         break;
72     case Z_Term_characterString:
73         sizez = strlen(term->u.characterString);
74         if (sizez > IT_MAX_WORD-1)
75             sizez = IT_MAX_WORD-1;
76         memcpy (termz, term->u.characterString, sizez);
77         termz[sizez] = '\0';
78         break;
79     default:
80         zebra_setError(zh, YAZ_BIB1_UNSUPP_CODED_VALUE_FOR_TERM, 0);
81         return ZEBRA_FAIL;
82     }
83     return ZEBRA_OK;
84 }
85 /*
86  * Local variables:
87  * c-basic-offset: 4
88  * c-file-style: "Stroustrup"
89  * indent-tabs-mode: nil
90  * End:
91  * vim: shiftwidth=4 tabstop=8 expandtab
92  */
93