Disable all code related to libXp
[oweals/cde.git] / cde / programs / dtinfo / DtMmdb / btree / mmdb_btree.C
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /*
24  * $XConsortium: mmdb_btree.cc /main/3 1996/06/11 17:14:32 cde-hal $
25  *
26  * Copyright (c) 1993 HAL Computer Systems International, Ltd.
27  * All rights reserved.  Unpublished -- rights reserved under
28  * the Copyright Laws of the United States.  USE OF A COPYRIGHT
29  * NOTICE IS PRECAUTIONARY ONLY AND DOES NOT IMPLY PUBLICATION
30  * OR DISCLOSURE.
31  * 
32  * THIS SOFTWARE CONTAINS CONFIDENTIAL INFORMATION AND TRADE
33  * SECRETS OF HAL COMPUTER SYSTEMS INTERNATIONAL, LTD.  USE,
34  * DISCLOSURE, OR REPRODUCTION IS PROHIBITED WITHOUT THE
35  * PRIOR EXPRESS WRITTEN PERMISSION OF HAL COMPUTER SYSTEMS
36  * INTERNATIONAL, LTD.
37  * 
38  *                         RESTRICTED RIGHTS LEGEND
39  * Use, duplication, or disclosure by the Government is subject
40  * to the restrictions as set forth in subparagraph (c)(l)(ii)
41  * of the Rights in Technical Data and Computer Software clause
42  * at DFARS 252.227-7013.
43  *
44  *          HAL COMPUTER SYSTEMS INTERNATIONAL, LTD.
45  *                  1315 Dell Avenue
46  *                  Campbell, CA  95008
47  * 
48  */
49
50
51 #include "btree/mmdb_btree.h"
52
53
54 btree::btree(const char* store_name)
55 {
56 // let the package figure out all these parameters
57    btree_info.flags = 0;
58    btree_info.cachesize = 0;
59    btree_info.maxkeypage = 0;
60    btree_info.minkeypage = 0;
61    btree_info.psize = 0;
62    btree_info.compare = NULL;
63    btree_info.prefix = NULL;
64    btree_info.lorder = 0;
65
66
67    int mode = O_CREAT|O_RDWR;
68
69    //btree_DB = dbopen(store_name, mode, 0640, DB_BTREE, &btree_info);
70    btree_DB = dbopen(store_name, mode, 0640, DB_BTREE, NULL);
71
72    if ( btree_DB == 0 )
73       throw(stringException("btree dbopen failed"));
74 }
75
76 btree::~btree()
77 {
78    if ( btree_DB->sync(btree_DB, 0) == RET_ERROR )
79       throw(stringException("btree sync failed"));
80
81    if ( btree_DB->close(btree_DB) == RET_ERROR )
82       throw(stringException("btree close failed"));
83 }
84
85 void btree::clean()
86 {
87    throw(stringException("void btree::clean(): not implemented yet"));
88 }
89
90 void btree::data_t_2_DBT(data_t& w)
91 {
92    switch (w.flag) {
93      case data_t::INT:
94       key_DBT.data = &w.key.int_key;
95       key_DBT.size = sizeof(w.key.int_key);
96       break;
97
98      case data_t::STRING:
99       key_DBT.data = w.key.str_key;
100       key_DBT.size = strlen(w.key.str_key);
101       break;
102
103      case data_t::VOID:
104         throw(stringException("btree data_t_2_DBT: unknow key type"));
105    }
106 }
107
108 Boolean btree::insert(data_t& w)
109 {
110    data_t_2_DBT(w);
111
112    DBT data_DBT;
113    data_DBT.data = &w.dt;
114    data_DBT.size = sizeof(w.dt);
115
116    //int status = btree_DB->put(btree_DB, &key_DBT, &data_DBT, R_NOOVERWRITE);
117    int status = btree_DB->put(btree_DB, &key_DBT, &data_DBT, 0);
118
119    switch (status) {
120      case RET_ERROR:
121         throw(stringException("btree put failed"));
122         break;
123
124      case RET_SPECIAL:
125         throw(stringException("btree put: dup key"));
126         break;
127
128      case RET_SUCCESS:
129         return true;
130    }
131
132    return false;
133 }
134
135 Boolean btree::remove(data_t& w)
136 {
137    data_t_2_DBT(w);
138
139    int status = btree_DB->del(btree_DB, &key_DBT, 0);
140
141    switch (status) {
142      case RET_ERROR:
143         throw(stringException("btree delete failed"));
144         break;
145
146      case RET_SPECIAL:
147      case RET_SUCCESS:
148         return true;
149    }
150
151    return false;
152 }
153
154 Boolean btree::member(data_t& w) 
155 {
156    data_t_2_DBT(w);
157    DBT data_DBT;
158
159    int status = btree_DB->get(btree_DB, &key_DBT, &data_DBT, 0);
160
161    switch (status) {
162      case RET_ERROR:
163         throw(stringException("btree get failed"));
164         break;
165
166      case RET_SPECIAL:
167         return false;
168
169      case RET_SUCCESS:
170         if ( data_DBT.size != sizeof(w.dt) )
171            throw(stringException("btree get: tree corrupted"));
172
173         memcpy((char*)&w.dt, data_DBT.data, data_DBT.size);
174         return true;
175    }
176
177    return false;
178 }
179
180 ostream& btree::asciiOut(ostream& out)
181 {
182    return out;
183 }
184
185 istream& btree::asciiIn(istream& in)
186 {
187    return in;
188 }
189