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