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