Merge branch 'master' of ssh://git.indexdata.com/home/git/pub/idzebra
[idzebra-moved-to-github.git] / index / zaptterm.c
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 /* 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                 int iconv_error = yaz_iconv_error(zh->iconv_to_utf8);
54
55                 ret = yaz_iconv(zh->iconv_to_utf8, 0, 0, 0, 0);
56                 zebra_setError(
57                     zh,
58                     iconv_error == YAZ_ICONV_E2BIG ?
59                     YAZ_BIB1_TOO_MANY_CHARS_IN_SEARCH_STATEMENT :
60                     YAZ_BIB1_QUERY_TERM_INCLUDES_CHARS_THAT_DO_NOT_TRANSLATE_INTO_,
61                     0);
62                 return ZEBRA_FAIL;
63             }
64             yaz_iconv(zh->iconv_to_utf8, 0, 0, &outbuf, &outleft);
65             *outbuf = 0;
66         }
67         else
68         {
69             sizez = term->u.general->len;
70             if (sizez > IT_MAX_WORD-1)
71                 sizez = IT_MAX_WORD-1;
72             memcpy (termz, term->u.general->buf, sizez);
73             termz[sizez] = '\0';
74         }
75         break;
76     case Z_Term_characterString:
77         sizez = strlen(term->u.characterString);
78         if (sizez > IT_MAX_WORD-1)
79             sizez = IT_MAX_WORD-1;
80         memcpy (termz, term->u.characterString, sizez);
81         termz[sizez] = '\0';
82         break;
83     default:
84         zebra_setError(zh, YAZ_BIB1_UNSUPP_CODED_VALUE_FOR_TERM, 0);
85         return ZEBRA_FAIL;
86     }
87     return ZEBRA_OK;
88 }
89 /*
90  * Local variables:
91  * c-basic-offset: 4
92  * c-file-style: "Stroustrup"
93  * indent-tabs-mode: nil
94  * End:
95  * vim: shiftwidth=4 tabstop=8 expandtab
96  */
97