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