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