0f341bc5935509b3eff839341787ec7254f2610a
[yazpp-moved-to-github.git] / src / yaz-z-query.cpp
1 /*
2  * Copyright (c) 1998-2000, Index Data.
3  * See the file LICENSE for details.
4  * 
5  * $Log: yaz-z-query.cpp,v $
6  * Revision 1.8  2000-09-08 10:23:42  adam
7  * Added skeleton of yaz-z-server.
8  *
9  * Revision 1.7  2000/05/10 11:36:58  ian
10  * Added default parameters for refid to request functions.
11  * Added default parameter for result set name to search and present request.
12  * Commented out forced logging of PDU contents.
13  * Added send_deleteResultSetRequest
14  *
15  * Revision 1.6  1999/12/06 13:52:45  adam
16  * Modified for new location of YAZ header files. Experimental threaded
17  * operation.
18  *
19  * Revision 1.5  1999/04/27 07:52:13  adam
20  * Improved proxy; added query match for result set re-use.
21  *
22  * Revision 1.4  1999/04/21 12:09:01  adam
23  * Many improvements. Modified to proxy server to work with "sessions"
24  * based on cookies.
25  *
26  * Revision 1.3  1999/04/20 10:30:05  adam
27  * Implemented various stuff for client and proxy. Updated calls
28  * to ODR to reflect new name parameter.
29  *
30  * Revision 1.2  1999/04/09 11:46:57  adam
31  * Added object Yaz_Z_Assoc. Much more functional client.
32  *
33  * Revision 1.1  1999/03/23 14:17:57  adam
34  * More work on timeout handling. Work on yaz-client.
35  *
36  */
37
38 #include <yaz-z-query.h>
39 #include <yaz/pquery.h>
40
41 Yaz_Z_Query::Yaz_Z_Query()
42 {
43     odr_encode = odr_createmem (ODR_ENCODE);
44     odr_decode = odr_createmem (ODR_DECODE);
45     odr_print = odr_createmem (ODR_PRINT);
46 }
47
48 int Yaz_Z_Query::set_rpn (const char *rpn)
49 {
50     buf = 0;
51     odr_reset (odr_encode);
52     Z_Query *query = (Z_Query*) odr_malloc (odr_encode, sizeof(*query));
53     query->which = Z_Query_type_1;
54     query->u.type_1 = p_query_rpn (odr_encode, PROTO_Z3950, rpn);
55     if (!query->u.type_1)
56         return -1;
57     if (!z_Query (odr_encode, &query, 0, 0))
58         return -1;
59     // z_Query(odr_print, &query, 0, 0);
60     buf = odr_getbuf (odr_encode, &len, 0);
61     return len;
62 }
63
64 void Yaz_Z_Query::set_Z_Query(Z_Query *z_query)
65 {
66     buf = 0;
67     odr_reset (odr_encode);
68     if (!z_Query (odr_encode, &z_query, 0, 0))
69         return;
70     buf = odr_getbuf (odr_encode, &len, 0);
71 }
72
73 Yaz_Z_Query::~Yaz_Z_Query()
74 {
75     odr_destroy (odr_encode);
76     odr_destroy (odr_decode);
77     odr_destroy (odr_print);
78 }
79
80 Z_Query *Yaz_Z_Query::get_Z_Query ()
81 {
82     Z_Query *query;
83     if (!buf)
84         return 0;
85     odr_setbuf (odr_decode, buf, len, 0);
86     if (!z_Query(odr_decode, &query, 0, 0))
87         return 0;
88     return query;
89 }
90
91 void Yaz_Z_Query::print(char *str, int len)
92 {
93
94 }
95
96 int Yaz_Z_Query::match(Yaz_Z_Query *other)
97 {
98     if (len != other->len)
99         return 0;
100     if (!buf || !other->buf)
101         return 0;
102     if (memcmp(buf, other->buf, len))
103         return 0;
104     return 1;
105 }