e6c5af2bd5b275a7a0404834731bf2b608ee4264
[yaz-moved-to-github.git] / include / yaz / ccl.h
1 /*
2  * Copyright (c) 1995, the EUROPAGATE consortium (see below).
3  *
4  * The EUROPAGATE consortium members are:
5  *
6  *    University College Dublin
7  *    Danmarks Teknologiske Videnscenter
8  *    An Chomhairle Leabharlanna
9  *    Consejo Superior de Investigaciones Cientificas
10  *
11  * Permission to use, copy, modify, distribute, and sell this software and
12  * its documentation, in whole or in part, for any purpose, is hereby granted,
13  * provided that:
14  *
15  * 1. This copyright and permission notice appear in all copies of the
16  * software and its documentation. Notices of copyright or attribution
17  * which appear at the beginning of any file must remain unchanged.
18  *
19  * 2. The names of EUROPAGATE or the project partners may not be used to
20  * endorse or promote products derived from this software without specific
21  * prior written permission.
22  *
23  * 3. Users of this software (implementors and gateway operators) agree to
24  * inform the EUROPAGATE consortium of their use of the software. This
25  * information will be used to evaluate the EUROPAGATE project and the
26  * software, and to plan further developments. The consortium may use
27  * the information in later publications.
28  * 
29  * 4. Users of this software agree to make their best efforts, when
30  * documenting their use of the software, to acknowledge the EUROPAGATE
31  * consortium, and the role played by the software in their work.
32  *
33  * THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTY OF ANY KIND,
34  * EXPRESS, IMPLIED, OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
35  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
36  * IN NO EVENT SHALL THE EUROPAGATE CONSORTIUM OR ITS MEMBERS BE LIABLE
37  * FOR ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF
38  * ANY KIND, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA
39  * OR PROFITS, WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND
40  * ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
41  * USE OR PERFORMANCE OF THIS SOFTWARE.
42  *
43  */
44
45 /** \file ccl.h
46     \brief Header with public definitions for CCL.
47 */
48
49 /*
50  * CCL - header file
51  *
52  * $Id: ccl.h,v 1.29 2007-05-01 12:22:10 adam Exp $
53  *
54  * Old Europagate Log:
55  *
56  * Revision 1.10  1996/01/08  08:41:22  adam
57  * Minor changes.
58  *
59  * Revision 1.9  1995/07/20  08:15:16  adam
60  * Bug fix: Token value for comma and OR were the same!
61  *
62  * Revision 1.8  1995/07/11  12:28:34  adam
63  * New function: ccl_token_simple (split into simple tokens) and
64  *  ccl_token_del (delete tokens).
65  *
66  * Revision 1.7  1995/05/16  09:39:38  adam
67  * LICENSE.
68  *
69  * Revision 1.6  1995/05/11  14:04:03  adam
70  * Changes in the reading of qualifier(s). New function: ccl_qual_fitem.
71  * New variable ccl_case_sensitive, which controls whether reserved
72  * words and field names are case sensitive or not.
73  *
74  * Revision 1.5  1995/02/23  08:32:11  adam
75  * Changed header.
76  *
77  * Revision 1.3  1995/02/16  13:20:10  adam
78  * Spell fix.
79  *
80  * Revision 1.2  1995/02/15  17:43:08  adam
81  * Minor changes to the ccl interface. Bug fix in iso2709 module.
82  *
83  * Revision 1.1  1995/02/14  19:55:21  adam
84  * Header files ccl.h/cclp.h are gone! They have been merged an
85  * moved to ../include/ccl.h.
86  *
87  */
88
89 #ifndef CCL_H
90 #define CCL_H
91
92 #include <yaz/yconfig.h>
93 #include <stdio.h>
94 #include <yaz/xmalloc.h>
95 #include <yaz/wrbuf.h>
96
97 YAZ_BEGIN_CDECL
98     
99 #define CCL_ERR_OK                0
100 #define CCL_ERR_TERM_EXPECTED     1
101 #define CCL_ERR_RP_EXPECTED       2
102 #define CCL_ERR_SETNAME_EXPECTED  3
103 #define CCL_ERR_OP_EXPECTED       4
104 #define CCL_ERR_BAD_RP            5
105 #define CCL_ERR_UNKNOWN_QUAL      6
106 #define CCL_ERR_DOUBLE_QUAL       7
107 #define CCL_ERR_EQ_EXPECTED       8
108 #define CCL_ERR_BAD_RELATION      9
109 #define CCL_ERR_TRUNC_NOT_LEFT   10
110 #define CCL_ERR_TRUNC_NOT_BOTH   11
111 #define CCL_ERR_TRUNC_NOT_RIGHT  12
112     
113 /** \brief attribute node (type, value) pair as used in RPN */
114 struct ccl_rpn_attr {
115     /** \brief next attribute */
116     struct ccl_rpn_attr *next;
117     /** \brief attribute set */
118     char *set;
119     /** \brief attribute type, Bib-1: 1=use, 2=relation, 3=position, .. */
120     int type;
121     /** \brief attribute value type (numeric or string) */
122     int kind;
123 #define CCL_RPN_ATTR_NUMERIC 1
124 #define CCL_RPN_ATTR_STRING 2
125     union {
126         /** \brief numeric attribute value */
127         int numeric;
128         /** \brief string attribute value */
129         char *str;
130     } value;
131 };
132
133 /** \brief node type or RPN tree generated by the CCL parser */
134 enum ccl_rpn_kind {
135     CCL_RPN_AND,
136     CCL_RPN_OR,
137     CCL_RPN_NOT,
138     CCL_RPN_TERM,
139     CCL_RPN_SET,
140     CCL_RPN_PROX
141 };
142
143 /** \brief RPN tree structure node */
144 struct ccl_rpn_node {
145     /** \brief node type, one of CCL_RPN_AND, CCL_RPN_OR,.. */
146     enum ccl_rpn_kind kind;
147     union {
148         /** \brief Boolean including proximity 0=left, 1=right, 2=prox parms */
149         struct ccl_rpn_node *p[3];
150         /** \brief Attributes + Term */
151         struct {
152             char *term;
153             char *qual;
154             struct ccl_rpn_attr *attr_list;
155         } t;
156         /** Result set */
157         char *setname;
158     } u;
159 };
160
161 /** \brief CCL bibset, AKA profile */
162 typedef struct ccl_qualifiers *CCL_bibset;
163
164 /** \brief CCL parser */
165 typedef struct ccl_parser *CCL_parser;
166     
167 /**
168    \brief parse CCL find string using CCL profile return RPN tree
169    
170    Parses a CCL Find command in a simple C string. Returns CCL parse
171    tree node describing RPN if parsing is successful. If parsing is
172    unsuccesful, NULL is returned and error and pos is set accordingly.
173 */
174 YAZ_EXPORT
175 struct ccl_rpn_node *ccl_find_str(CCL_bibset bibset,
176                                   const char *str, int *error, int *pos);
177
178
179 /**
180    \brief parse CCL find string with parser and return RPN tree
181    
182    Parses a CCL Find command in a simple C string. Returns CCL parse
183    tree node describing RPN if parsing is successful. If parsing is
184    unsuccesful, NULL is returned and error and pos is set accordingly.
185 */
186 YAZ_EXPORT
187 struct ccl_rpn_node *ccl_parser_find_str(CCL_parser cclp, const char *str);
188
189 /** Set case sensitivity for parser */
190 YAZ_EXPORT
191 void ccl_parser_set_case(CCL_parser p, int case_sensitivity_flag);
192
193 /** Return english-readable error message for CCL parser error number */
194 YAZ_EXPORT
195 const char *ccl_err_msg(int ccl_errno);
196
197 /** Delete RPN tree returned by ccl_find */
198 YAZ_EXPORT
199 void ccl_rpn_delete(struct ccl_rpn_node *rpn);
200
201 /** Dump RPN tree in readable format to fd_out */
202 YAZ_EXPORT
203 void ccl_pr_tree(struct ccl_rpn_node *rpn, FILE *fd_out);
204
205 /** Add qualifier and supply attribute pairs for it */
206 YAZ_EXPORT
207 void ccl_qual_add(CCL_bibset b, const char *name, int no, int *attr);
208
209 /** Add qualifier and supply attributes pairs+attribute set for it */
210 YAZ_EXPORT
211 void ccl_qual_add_set(CCL_bibset b, const char *name, int no,
212                       int *type, int *value, char **svalue, char **attsets);
213
214 /** Add special qualifier */
215 YAZ_EXPORT
216 void ccl_qual_add_special(CCL_bibset bibset, const char *n, const char *cp);
217
218 /** Add combo qualifier */
219 YAZ_EXPORT
220 void ccl_qual_add_combi(CCL_bibset b, const char *n, const char **names);
221
222 /** Read CCL qualifier list spec from file inf */
223 YAZ_EXPORT
224 void ccl_qual_file(CCL_bibset bibset, FILE *inf);
225
226 /** Read CCL qualifier list spec from file inf */
227 YAZ_EXPORT
228 int ccl_qual_fname(CCL_bibset bibset, const char *fname);
229
230 /** Add CCL qualifier as buf spec(multiple lines). */
231 YAZ_EXPORT
232 void ccl_qual_buf(CCL_bibset bibset, const char *buf);
233
234 /** Add CCL qualifier as line spec. Note: line is _modified_ */
235 YAZ_EXPORT
236 void ccl_qual_line(CCL_bibset bibset, char *line);
237
238 /* Add CCL qualifier by using qual_name + value pair */
239 YAZ_EXPORT
240 void ccl_qual_fitem(CCL_bibset bibset, const char *value,
241                     const char *qual_name);
242
243 /** Make CCL qualifier set */
244 YAZ_EXPORT
245 CCL_bibset ccl_qual_mk(void);
246
247 /** Delete CCL qualifier set */
248 YAZ_EXPORT
249 void ccl_qual_rm(CCL_bibset *b);
250
251 /** Char-to-upper function */
252 extern int(*ccl_toupper)(int c);
253
254 /** CCL version of ccl_stricmp */
255 YAZ_EXPORT
256 int ccl_stricmp(const char *s1, const char *s2);
257
258 /** CCL version of ccl_memicmp */
259 YAZ_EXPORT
260 int ccl_memicmp(const char *s1, const char *s2, size_t n);
261
262 /** Create CCL parser */
263 YAZ_EXPORT
264 CCL_parser ccl_parser_create(CCL_bibset bibset);
265
266 /** Destroy CCL parser */
267 YAZ_EXPORT
268 void ccl_parser_destroy(CCL_parser p);
269
270 /** Search for special qualifier */
271 YAZ_EXPORT
272 const char **ccl_qual_search_special(CCL_bibset b, const char *name);
273 /** Pretty-print CCL RPN node tree to WRBUF */
274 YAZ_EXPORT
275 void ccl_pquery(WRBUF w, struct ccl_rpn_node *p);
276
277 YAZ_EXPORT
278 int ccl_parser_get_error(CCL_parser cclp, int *pos);
279
280 YAZ_EXPORT
281 struct ccl_rpn_node *ccl_rpn_node_create(enum ccl_rpn_kind kind);
282
283 YAZ_EXPORT
284 void ccl_add_attr_numeric(struct ccl_rpn_node *p, const char *set,
285                           int type, int value);
286
287 YAZ_EXPORT
288 void ccl_add_attr_string(struct ccl_rpn_node *p, const char *set,
289                          int type, char *value);
290
291 YAZ_EXPORT
292 int ccl_search_stop(CCL_bibset bibset, const char *qname,
293                     const char *src_str, size_t src_len);
294
295
296 /** \brief stop words handle (pimpl) */
297 typedef struct ccl_stop_words *ccl_stop_words_t;
298
299 /** \brief creates stop words handle */
300 YAZ_EXPORT
301 ccl_stop_words_t ccl_stop_words_create(void);
302
303 /** \brief destroys stop words handle */
304 YAZ_EXPORT
305 void ccl_stop_words_destroy(ccl_stop_words_t csw);
306
307 /** \brief removes stop words from RPN tree */
308 YAZ_EXPORT
309 int ccl_stop_words_tree(ccl_stop_words_t csw,
310                         CCL_bibset bibset, struct ccl_rpn_node **t);
311
312 /** \brief returns information about removed "stop" words */
313 YAZ_EXPORT
314 int ccl_stop_words_info(ccl_stop_words_t csw, int idx,
315                         const char **qualname, const char **term);
316
317 #ifndef ccl_assert
318 #define ccl_assert(x) ;
319 #endif
320
321
322 /** \brief common attributes
323
324    use (1)
325
326    relation (2)
327                             -1  none
328                              0  ordered
329                            1-6  relation (<, <=, =, >=, >, <>)
330
331    position (3)
332                             -1  none
333                              1  first in field
334                              2  first in sub field
335                              3  any position in field
336    structure (4)
337                             -1  none
338                              0  word/phrase auto select
339                              1  phrase
340                              2  word
341                              3  key
342                              4  year
343                              5  date (normalized)
344                              6  word list 
345                            100  date (un-normalized)
346                            101  name (normalized)
347                            102  name (un-normalized)
348    truncation (5)                            
349    completeness (6)
350 */
351
352 #define CCL_BIB1_USE 1
353 #define CCL_BIB1_REL 2
354 #define CCL_BIB1_POS 3
355 #define CCL_BIB1_STR 4
356 #define CCL_BIB1_TRU 5
357 #define CCL_BIB1_COM 6
358
359 #define CCL_BIB1_STR_WP (-1)
360 #define CCL_BIB1_STR_AND_LIST (-2)
361 #define CCL_BIB1_STR_OR_LIST (-3)
362 #define CCL_BIB1_REL_ORDER (-1)
363 #define CCL_BIB1_REL_PORDER (-2)
364
365 #define CCL_BIB1_TRU_CAN_LEFT (-1)
366 #define CCL_BIB1_TRU_CAN_RIGHT (-2)
367 #define CCL_BIB1_TRU_CAN_BOTH  (-3)
368 #define CCL_BIB1_TRU_CAN_NONE  (-4)
369
370
371
372 YAZ_END_CDECL
373
374 #endif
375
376 /*
377  * Local variables:
378  * c-basic-offset: 4
379  * indent-tabs-mode: nil
380  * End:
381  * vim: shiftwidth=4 tabstop=8 expandtab
382  */
383