Removed ccl_pr_tree_as_qrpn . Implement ccl_pr_tree in terms of
[yaz-moved-to-github.git] / src / cclptree.c
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 /** 
46  * \file cclptree.c
47  * \brief Implements CCL parse tree printing
48  *
49  * This source file implements functions to parse and print
50  * a CCL node tree (as a result of parsing).
51  */
52
53 /* CCL print rpn tree - infix notation
54  * Europagate, 1995
55  *
56  * $Id: cclptree.c,v 1.9 2007-04-25 20:50:56 adam Exp $
57  *
58  * Old Europagate Log:
59  *
60  * Revision 1.6  1995/05/16  09:39:26  adam
61  * LICENSE.
62  *
63  * Revision 1.5  1995/02/23  08:31:59  adam
64  * Changed header.
65  *
66  * Revision 1.3  1995/02/15  17:42:16  adam
67  * Minor changes of the api of this module. FILE* argument added
68  * to ccl_pr_tree.
69  *
70  * Revision 1.2  1995/02/14  19:55:11  adam
71  * Header files ccl.h/cclp.h are gone! They have been merged an
72  * moved to ../include/ccl.h.
73  * Node kind(s) in ccl_rpn_node have changed names.
74  *
75  * Revision 1.1  1995/02/14  10:25:56  adam
76  * The constructions 'qualifier rel term ...' implemented.
77  *
78  */
79
80 #include <stdio.h>
81 #include <string.h>
82 #include <ctype.h>
83
84 #include <yaz/ccl.h>
85
86 static void ccl_pquery_indent(WRBUF w, struct ccl_rpn_node *p, int indent);
87
88 static void ccl_pquery_complex(WRBUF w, struct ccl_rpn_node *p, int indent)
89 {
90     int sep_char = indent == -1 ? ' ' : '\n';
91     int next_indent = indent == -1 ? indent : indent+1;
92     switch (p->kind)
93     {
94     case CCL_RPN_AND:
95         wrbuf_puts(w, "@and");
96         break;
97     case CCL_RPN_OR:
98         wrbuf_puts(w, "@or");
99         break;
100     case CCL_RPN_NOT:
101         wrbuf_puts(w, "@not");
102         break;
103     case CCL_RPN_PROX:
104         if (p->u.p[2] && p->u.p[2]->kind == CCL_RPN_TERM)
105         {
106             const char *cp = p->u.p[2]->u.t.term;
107             /* exlusion distance ordered relation which-code unit-code */
108             if (*cp == '!')
109             {   
110                 /* word order specified */
111                 if (isdigit(((const unsigned char *) cp)[1]))
112                     wrbuf_printf(w, "@prox 0 %s 1 2 k 2", cp+1);
113                 else
114                     wrbuf_printf(w, "@prox 0 1 1 2 k 2");
115             } 
116             else if (*cp == '%')
117             {
118                 /* word order not specified */
119                 if (isdigit(((const unsigned char *) cp)[1]))
120                     wrbuf_printf(w, "@prox 0 %s 0 2 k 2", cp+1);
121                 else
122                     wrbuf_printf(w, "@prox 0 1 0 2 k 2");
123             }
124         }
125         else
126             wrbuf_puts(w, "@prox 0 2 0 1 k 2");
127         break;
128     default:
129         wrbuf_puts(w, "@ bad op (unknown)");
130     }
131     wrbuf_putc(w, sep_char);
132     ccl_pquery_indent(w, p->u.p[0], next_indent);
133     ccl_pquery_indent(w, p->u.p[1], next_indent);
134 }
135
136 static void ccl_prterm(WRBUF w, const char *term)
137 {
138     const char *cp = term;
139     for (; *cp; cp++)
140     {
141         if (*cp == ' ' || *cp == '\\')
142             wrbuf_putc(w, '\\');
143         wrbuf_putc(w, *cp);
144     }
145     wrbuf_puts(w, " ");
146 }
147
148 static void ccl_pquery_indent(WRBUF w, struct ccl_rpn_node *p, int indent)
149 {
150     struct ccl_rpn_attr *att;
151
152     if (indent != -1)
153     {
154         int i;
155         for (i = 0; i < indent; i++)
156             wrbuf_putc(w, ' ');
157     }
158     switch (p->kind)
159     {
160     case CCL_RPN_AND:
161     case CCL_RPN_OR:
162     case CCL_RPN_NOT:
163     case CCL_RPN_PROX:
164         ccl_pquery_complex(w, p, indent);
165         break;
166     case CCL_RPN_SET:
167         wrbuf_puts(w, "@set ");
168         ccl_prterm(w, p->u.setname);
169         if (indent != -1)
170             wrbuf_putc(w, '\n');
171         break;
172     case CCL_RPN_TERM:
173         for (att = p->u.t.attr_list; att; att = att->next)
174         {
175             char tmpattr[128];
176             wrbuf_puts(w, "@attr ");
177             if (att->set)
178             {
179                 wrbuf_puts(w, att->set);
180                 wrbuf_puts(w, " ");
181             }
182             switch(att->kind)
183             {
184             case CCL_RPN_ATTR_NUMERIC:
185                 sprintf(tmpattr, "%d=%d ", att->type, att->value.numeric);
186                 wrbuf_puts(w, tmpattr);
187                 break;
188             case CCL_RPN_ATTR_STRING:
189                 sprintf(tmpattr, "%d=", att->type);
190                 wrbuf_puts(w, tmpattr);
191                 wrbuf_puts(w, att->value.str);
192                 wrbuf_puts(w, " ");
193                 break;
194             }
195         }
196         ccl_prterm(w, p->u.t.term);
197         if (indent != -1)
198             wrbuf_putc(w, '\n');
199         break;
200     }
201 }
202
203 void ccl_pquery(WRBUF w, struct ccl_rpn_node *p)
204 {
205     ccl_pquery_indent(w, p, -1);
206 }
207
208 void ccl_pr_tree(struct ccl_rpn_node *rpn, FILE *fd_out)
209 {
210     WRBUF w = wrbuf_alloc();
211
212     ccl_pquery_indent(w, rpn, 0);
213     
214     fputs(wrbuf_cstr(w), fd_out);
215     wrbuf_destroy(w);
216 }
217
218 /*
219  * Local variables:
220  * c-basic-offset: 4
221  * indent-tabs-mode: nil
222  * End:
223  * vim: shiftwidth=4 tabstop=8 expandtab
224  */
225