Change implementation of Yaz_Z_Query::zquery2pquery to use YAZ'
[yazpp-moved-to-github.git] / src / gdu.cpp
1 /*
2  * Copyright (c) 1998-2005, Index Data.
3  * See the file LICENSE for details.
4  * 
5  * $Id: gdu.cpp,v 1.2 2005-06-25 15:53:19 adam Exp $
6  */
7
8 #include <yaz++/gdu.h>
9
10 using namespace yazpp_1;
11
12 GDU::GDU(Z_APDU *apdu)
13 {
14     ODR encode = odr_createmem(ODR_ENCODE);
15     Z_GDU *gdu = (Z_GDU *) odr_malloc(encode, sizeof(*gdu));
16     gdu->which = Z_GDU_Z3950;
17     gdu->u.z3950 = apdu;
18     base(gdu, encode);
19 }
20
21 GDU::GDU(Z_GDU *gdu)
22 {
23     base(gdu, odr_createmem(ODR_ENCODE));
24 }
25
26 void GDU::base(Z_GDU *gdu, ODR encode)
27 {
28     m_decode = odr_createmem(ODR_DECODE);
29     m_gdu = 0;
30     if (z_GDU(encode, &gdu, 0, "encode"))
31     {
32         int len;
33         char *buf = odr_getbuf(encode, &len, 0);
34         
35         odr_setbuf(m_decode, buf, len, 0);
36         z_GDU(m_decode, &m_gdu, 0, 0);
37     }
38     odr_destroy(encode);
39 }
40
41 GDU::~GDU()
42 {
43     odr_destroy(m_decode);
44 }
45
46 Z_GDU *GDU::get()
47 {
48     return m_gdu;
49 }
50
51 void GDU::extract_odr_to(ODR dst)
52 {
53     NMEM nmem = odr_extract_mem(m_decode);
54     if (!dst->mem)
55         dst->mem = nmem_create();
56     nmem_transfer(dst->mem, nmem);
57     nmem_destroy(nmem);
58 }
59
60
61 GDUQueue::GDUQueue()
62 {
63     m_list = 0;
64 }
65
66 int GDUQueue::size()
67 {
68     int no = 0;
69     GDUQueue_List *l;
70     for (l = m_list; l; l = l->m_next)
71         no++;
72     return no;
73 }
74
75 void GDUQueue::enqueue(GDU *gdu)
76 {
77     GDUQueue_List *l = new GDUQueue_List;
78     l->m_next = m_list;
79     l->m_item = gdu;
80     m_list = l;
81 }
82
83 GDU *GDUQueue::dequeue()
84 {
85     GDUQueue_List **l = &m_list;
86     if (!*l)
87         return 0;
88     while ((*l)->m_next)
89         l = &(*l)->m_next;
90     GDU *m = (*l)->m_item;
91     delete *l;
92     *l = 0;
93     return m;
94 }
95
96 void GDUQueue::clear()
97 {
98     GDU *g;
99     while ((g = dequeue()))
100         delete g;
101 }
102
103 GDUQueue::~GDUQueue()
104 {
105     clear();
106 }
107 /*
108  * Local variables:
109  * c-basic-offset: 4
110  * indent-tabs-mode: nil
111  * End:
112  * vim: shiftwidth=4 tabstop=8 expandtab
113  */
114