Source restructure. yaz-marcdump part of installation
[yaz-moved-to-github.git] / src / query.c
1 /*
2  * Copyright (c) 1995-2001, Index Data
3  * See the file LICENSE for details.
4  *
5  * $Id: query.c,v 1.1 2003-10-27 12:21:35 adam Exp $
6  */
7
8 #include <stdio.h>
9 #include <ctype.h>
10 #include <string.h>
11
12 #include <yaz/odr.h>
13 #include <yaz/proto.h>
14
15 static Z_Complex *makecomplex(ODR o, char **buf);
16 static Z_Operand *makesimple(ODR o, char **buf);
17 Z_RPNStructure *makerpn(ODR o, char **buf);
18
19 void skip_spaces(char**p)
20 {
21     while (**p && isspace(**p))
22         (*p)++;
23 }
24
25 static Z_Operand *makesimple(ODR o, char **buf)
26 {
27     Z_Operand *r;
28     Z_AttributesPlusTerm *t;
29     char *b;
30
31     r = odr_malloc(o, sizeof(*r));
32     if (**buf == 's' && *((*buf) + 1) == '=')
33     {
34         char *b = odr_malloc(o, 100);
35
36         r->which = Z_Operand_resultSetId;
37         r->u.resultSetId = b;
38         (*buf)++;
39         (*buf)++;
40         while (**buf && !isspace(**buf))
41             *(b++) = *((*buf)++);
42         *b = 0;
43         return r;
44     }
45     else if (**buf != '"')
46         return 0;
47     (*buf)++;
48     r->which = Z_Operand_APT;
49     r->u.attributesPlusTerm = t = odr_malloc(o, sizeof(*t));
50     t->attributes = odr_malloc(o, sizeof(*t));
51     t->attributes->num_attributes = 0;
52     t->attributes->attributes = 0;
53     t->term = odr_malloc(o, sizeof(*t->term));
54     t->term->which = Z_Term_general;
55     t->term->u.general = odr_malloc(o, sizeof(Odr_oct));
56     t->term->u.general->buf = odr_malloc(o, 100);
57     t->term->u.general->size = 100;
58     t->term->u.general->len = 0;
59     b = (char*) t->term->u.general->buf;
60     while (**buf && **buf != '"')
61     {
62         *(b++) = *((*buf)++);
63         t->term->u.general->len++;
64     }
65     if (**buf != '"')
66         return 0;
67     (*buf)++;
68     return r;
69 }
70
71 static Z_Complex *makecomplex(ODR o, char **buf)
72 {
73     Z_Complex *r;
74     char op[100], *b;
75
76     r = odr_malloc(o, sizeof(*r));
77     r->roperator = odr_malloc(o, sizeof(*r->roperator));
78
79     b = op;
80     while (**buf && !isspace(**buf))
81         *(b++) = *((*buf)++);
82     *b = 0;
83     if (!strcmp(op, "and"))
84         r->roperator->which = Z_Operator_and;
85     else if (!strcmp(op, "or"))
86         r->roperator->which = Z_Operator_or;
87     else if (!strcmp(op, "not"))
88         r->roperator->which = Z_Operator_and_not;
89     r->roperator->u.and = "";
90     while (**buf && !isspace(**buf))
91         (*buf)++;
92     if (!(r->s1 = makerpn(o, buf)))
93         return 0;
94     if (!(r->s2 = makerpn(o, buf)))
95         return 0;
96     return r;
97 }
98     
99 Z_RPNStructure *makerpn(ODR o, char **buf)
100 {
101     Z_RPNStructure *r;
102
103     r = odr_malloc(o, sizeof(*r));
104     skip_spaces(buf);
105     if (**buf == '"' || **buf == 's')
106     {
107         r->which = Z_RPNStructure_simple;
108         if (!(r->u.simple = makesimple(o, buf)))
109             return 0;
110         return r;
111     }
112     r->which = Z_RPNStructure_complex;
113     if (!(r->u.complex = makecomplex(o, buf)))
114         return 0;
115     return r;
116 }