autoheader generates config.h instead of cconfig.h.
[pazpar2-moved-to-github.git] / src / charsets.c
1 /* This file is part of Pazpar2.
2    Copyright (C) 2006-2008 Index Data
3
4 Pazpar2 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 Pazpar2 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 /** \file charsets.c
21     \brief Pazpar2 Character set facilities
22 */
23
24 #if HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <yaz/xmalloc.h>
29 #include <yaz/wrbuf.h>
30 #include <yaz/log.h>
31 #include <ctype.h>
32 #include <assert.h>
33
34 #include "charsets.h"
35 #include "normalize7bit.h"
36
37 #ifdef HAVE_ICU
38 #include "icu_I18N.h"
39 #endif // HAVE_ICU
40
41 /* charset handle */
42 struct pp2_charset_s {
43     const char *(*token_next_handler)(pp2_relevance_token_t prt);
44     const char *(*get_sort_handler)(pp2_relevance_token_t prt, int skip);
45 #ifdef HAVE_ICU
46     struct icu_chain * icu_chn;
47     UErrorCode icu_sts;
48 #endif // HAVE_ICU
49 };
50
51 static const char *pp2_relevance_token_a_to_z(pp2_relevance_token_t prt);
52 static const char *pp2_get_sort_ascii(pp2_relevance_token_t prt, int skip_article);
53
54 #ifdef HAVE_ICU
55 static const char *pp2_relevance_token_icu(pp2_relevance_token_t prt);
56 static const char *pp2_get_sort_icu(pp2_relevance_token_t prt, int skip_article);
57 #endif // HAVE_ICU
58
59 /* tokenzier handle */
60 struct pp2_relevance_token_s {
61     const char *cp;     /* unnormalized buffer we're tokenizing */
62     const char *last_cp;  /* pointer to last token we're dealing with */
63     pp2_charset_t pct;  /* our main charset handle (type+config) */
64     WRBUF norm_str;     /* normized string we return (temporarily) */
65     WRBUF sort_str;     /* sort string we return (temporarily) */
66 };
67
68
69 pp2_charset_t pp2_charset_create_xml(xmlNode *xml_node)
70 {
71 #ifdef HAVE_ICU
72     UErrorCode status = U_ZERO_ERROR;
73     while (xml_node && xml_node->type != XML_ELEMENT_NODE)
74         xml_node = xml_node->next;
75     struct icu_chain *chain = icu_chain_xml_config(xml_node, &status);
76     if (!chain || U_FAILURE(status)){
77         //xmlDocPtr icu_doc = 0;
78         //xmlChar *xmlstr = 0;
79                 //int size = 0;
80                 //xmlDocDumpMemory(icu_doc, size);
81         
82         yaz_log(YLOG_FATAL, "Could not parse ICU chain config:\n"
83                 "<%s>\n ... \n</%s>",
84                 xml_node->name, xml_node->name);
85         return 0;
86     }
87     return pp2_charset_create(chain);
88 #else // HAVE_ICU
89     yaz_log(YLOG_FATAL, "Error: ICU support requested with element:\n"
90             "<%s>\n ... \n</%s>",
91             xml_node->name, xml_node->name);
92     yaz_log(YLOG_FATAL, 
93             "But no ICU support compiled into pazpar2 server.");
94     yaz_log(YLOG_FATAL, 
95             "Please install libicu36-dev and icu-doc or similar, "
96             "re-configure and re-compile");            
97     return 0;
98 #endif // HAVE_ICU
99 }
100
101
102 pp2_charset_t pp2_charset_create(struct icu_chain * icu_chn)
103 {
104     pp2_charset_t pct = xmalloc(sizeof(*pct));
105
106     pct->token_next_handler = pp2_relevance_token_a_to_z;
107     pct->get_sort_handler  = pp2_get_sort_ascii;
108 #ifdef HAVE_ICU
109     pct->icu_chn = 0;
110     if (icu_chn)
111     {
112         pct->icu_chn = icu_chn;
113         pct->icu_sts = U_ZERO_ERROR;
114         pct->token_next_handler = pp2_relevance_token_icu;
115         pct->get_sort_handler = pp2_get_sort_icu;
116     }
117 #endif // HAVE_ICU
118     return pct;
119 }
120
121 void pp2_charset_destroy(pp2_charset_t pct)
122 {
123     xfree(pct);
124 }
125
126 pp2_relevance_token_t pp2_relevance_tokenize(pp2_charset_t pct,
127                                              const char *buf)
128 {
129     pp2_relevance_token_t prt = xmalloc(sizeof(*prt));
130
131     assert(pct);
132
133     prt->norm_str = wrbuf_alloc();
134     prt->sort_str = wrbuf_alloc();
135     prt->cp = buf;
136     prt->last_cp = 0;
137     prt->pct = pct;
138
139 #ifdef HAVE_ICU
140     if (pct->icu_chn)
141     {
142         pct->icu_sts = U_ZERO_ERROR;
143         int ok = 0;
144         ok = icu_chain_assign_cstr(pct->icu_chn, buf, &pct->icu_sts);
145         //printf("\nfield ok: %d '%s'\n", ok, buf);
146         prt->pct = pct;
147     }
148 #endif // HAVE_ICU
149     return prt;
150 }
151
152
153 void pp2_relevance_token_destroy(pp2_relevance_token_t prt)
154 {
155     assert(prt);
156     if(prt->norm_str) 
157         wrbuf_destroy(prt->norm_str);
158     if(prt->sort_str) 
159         wrbuf_destroy(prt->sort_str);
160     xfree(prt);
161 }
162
163 const char *pp2_relevance_token_next(pp2_relevance_token_t prt)
164 {
165     assert(prt);
166     return (prt->pct->token_next_handler)(prt);
167 }
168
169 const char *pp2_get_sort(pp2_relevance_token_t prt, int skip)
170 {
171     return prt->pct->get_sort_handler(prt, skip);
172 }
173
174 #define raw_char(c) (((c) >= 'a' && (c) <= 'z') ? (c) : -1)
175 /* original tokenizer with our tokenize interface, but we
176    add +1 to ensure no '\0' are in our string (except for EOF)
177 */
178 static const char *pp2_relevance_token_a_to_z(pp2_relevance_token_t prt)
179 {
180     const char *cp = prt->cp;
181     int c;
182
183     /* skip white space */
184     while (*cp && (c = raw_char(tolower(*cp))) < 0)
185         cp++;
186     if (*cp == '\0')
187     {
188         prt->cp = cp;
189         prt->last_cp = 0;
190         return 0;
191     }
192     /* now read the term itself */
193
194     prt->last_cp = cp;
195     wrbuf_rewind(prt->norm_str);
196     while (*cp && (c = raw_char(tolower(*cp))) >= 0)
197     {
198         wrbuf_putc(prt->norm_str, c);
199         cp++;
200     }
201     prt->cp = cp;
202     return wrbuf_cstr(prt->norm_str);
203 }
204
205 static const char *pp2_get_sort_ascii(pp2_relevance_token_t prt,
206                                     int skip_article)
207 {
208     if (prt->last_cp == 0)
209         return 0;
210     else
211     {
212         char *tmp = xstrdup(prt->last_cp);
213         char *result = 0;
214         result = normalize7bit_mergekey(tmp, skip_article);
215         
216         wrbuf_rewind(prt->sort_str);
217         wrbuf_puts(prt->sort_str, result);
218         xfree(tmp);
219         return wrbuf_cstr(prt->sort_str);
220     }
221 }
222
223
224 #ifdef HAVE_ICU
225 static const char *pp2_relevance_token_icu(pp2_relevance_token_t prt)
226 {
227     if (icu_chain_next_token(prt->pct->icu_chn, &prt->pct->icu_sts))
228     {
229         if (U_FAILURE(prt->pct->icu_sts))
230         {
231             return 0;
232         }
233         return icu_chain_get_norm(prt->pct->icu_chn);
234     }
235     return 0;
236 }
237
238 static const char *pp2_get_sort_icu(pp2_relevance_token_t prt,
239                                     int skip_article)
240 {
241     return icu_chain_get_sort(prt->pct->icu_chn);
242 }
243
244 #endif // HAVE_ICU
245
246
247
248 /*
249  * Local variables:
250  * c-basic-offset: 4
251  * indent-tabs-mode: nil
252  * End:
253  * vim: shiftwidth=4 tabstop=8 expandtab
254  */