source: main/trunk/greenstone2/common-src/src/lib/jdbmnaiveclass.cpp@ 31738

Last change on this file since 31738 was 31738, checked in by ak19, 7 years ago

Don't know how to stop the flashing DOS windows on GS2 when JDBM is used, since I don't know how to replace the popen calls in jdbmnaiveclass.cpp that get the process' output with variants that don't cause flashing DOS prompts. But replacing 2 occurrences of system() calls in jdbmnaiveclass.cpp with gsdltools.cpp's gsdl_system() calls, which hide any DOS prompts on windows. Compiles okay on Windows, and nothing appears to have broken. Doesn't resolve the other instances making calls to JDBM and which use popen calls instead of system().

File size: 6.8 KB
Line 
1/**********************************************************************
2 *
3 * jdbmnaiveclass.cpp --
4 * Copyright (C) 1999-2010 The New Zealand Digital Library Project
5 *
6 * A component of the Greenstone digital library software
7 * from the New Zealand Digital Library Project at the
8 * University of Waikato, New Zealand.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 *
24 *********************************************************************/
25
26#include "jdbmnaiveclass.h"
27#include "gsdltools.h"
28#include "gsdlunicode.h"
29#include "fileutil.h"
30#include "stdlib.h"
31#include <cstring>
32
33#ifdef __WIN32__
34//windows functions
35#define popen _popen
36#define pclose _pclose
37#endif
38
39jdbmnaiveclass::jdbmnaiveclass(const text_t& gsdlhome)
40 : dbclass(gsdlhome)
41{
42 gsdlhome_ = gsdlhome;
43
44 text_t jdbm_jar = filename_cat(gsdlhome_,"lib","java","jdbm.jar");
45
46 text_t jdbm_wrapper_jar = filename_cat(gsdlhome_,"bin","java","JDBMWrapper.jar");
47
48 classpath_ = pathname_cat(jdbm_wrapper_jar,jdbm_jar);
49 classpath_cstr_ = to_utf8(classpath_).getcstr();
50}
51
52jdbmnaiveclass::~jdbmnaiveclass()
53{
54 delete [] classpath_cstr_;
55}
56
57
58// returns true if opened
59bool jdbmnaiveclass::opendatabase (const text_t &filename, int mode, int num_retrys,
60#ifdef __WIN32__
61 bool need_filelock
62#else
63 bool
64#endif
65 )
66{
67 if (openfile_ == filename) {
68 return true;
69 }
70
71 if (logout == NULL) {
72 logout = &cerr;
73 }
74
75 // Map the DB mode value into equivalent JDBM 'must_exist' value
76 bool must_exist = true; // default
77
78 if (mode == DB_WRITER_CREATE) {
79 must_exist = false;
80 }
81
82 if (must_exist) {
83 // test that jdbm file exists
84
85 if (!file_exists(filename)) {
86 outconvertclass text_t2ascii;
87 (*logout) << text_t2ascii
88 << "Error: JDBM filename " << filename << "does not exist\n";
89 return false;
90 }
91 }
92
93 openfile_ = filename;
94
95 // In naive implementation, no connection to database to open, so
96 // nothing else to do at this stage
97
98 return true;
99}
100
101
102void jdbmnaiveclass::closedatabase ()
103{
104 // nothing to do in naive implementation
105}
106
107// returns file extension string
108text_t jdbmnaiveclass::getfileextension ()
109{
110 return ".jdb";
111}
112
113
114void jdbmnaiveclass::deletekey (const text_t &key)
115{
116 if (openfile_.empty()) return;
117
118 if (logout == NULL) {
119 logout = &cerr;
120 }
121
122 // get a utf-8 encoded c string of the unicode key
123 char* key_cstr = (to_utf8(key)).getcstr();
124 if (key_cstr == NULL) {
125 (*logout) << "jdbmnaiveclass: out of memory" << endl;
126 return;
127 }
128
129
130 // delete the key<
131 char* openfile_cstr = (to_utf8(openfile_)).getcstr();
132
133 char cmd[512];
134 sprintf(cmd,"java -cp \"%s\" JdbDel \"%s\" %s",classpath_cstr_,
135 openfile_cstr,key_cstr);
136
137 delete [] openfile_cstr;
138
139 //int status = system(cmd); // avoid flashing windows
140 int status = gsdl_system(cmd, true, *logout); // true for synchronous call: wait for process to end
141 if (status != 0 ) {
142 (*logout) << "jdbmnaiveclass: failed to run cmd: " << cmd << endl;
143 }
144
145 // free up the key memory
146 delete [] key_cstr;
147}
148
149
150
151// returns true on success
152bool jdbmnaiveclass::getkeydata (const text_t& key, text_t &data)
153{
154 if (openfile_.empty()) return false;
155
156 if (logout == NULL) {
157 logout = &cerr;
158 }
159
160 // get a utf-8 encoded c string of the unicode key
161 char* key_cstr = (to_utf8(key)).getcstr();
162 if (key_cstr == NULL) {
163 (*logout) << "jdbmnaiveclass: out of memory" << endl;
164
165 return false;
166 }
167
168 // fetch the result
169 char* openfile_cstr = (to_utf8(openfile_)).getcstr();
170
171 char cmd[512];
172 sprintf(cmd,"java -cp \"%s\" JdbGet \"%s\" %s",classpath_cstr_,
173 openfile_cstr,key_cstr);
174
175 delete [] openfile_cstr;
176
177 FILE* PIN = popen(cmd,"r");
178
179 if (PIN == NULL) {
180 (*logout) << "jdbmnaiveclass: failed to run cmd: " << cmd << endl;
181 }
182 else {
183 while (!feof(PIN)) {
184 char buf[256];
185 fgets(buf,256,PIN);
186
187 /*
188 * debuging code
189
190 cerr << "****## buf = " << buf << endl;
191
192 cerr << "****## buf: ";
193
194 int blen = strlen(buf);
195 for (int i=0; i< blen; i++) {
196 cerr << "(" << buf[i] << "," << (int)buf[i] << ") ";
197 }
198
199 cerr << endl;
200 */ // *****
201
202
203 data.append(buf);
204 }
205
206 }
207
208 delete [] key_cstr;
209
210 // cerr << "**** data before to_unicode = " << data << endl;
211
212 data = to_uni(data); // convert to unicode
213
214 pclose(PIN);
215
216 return true;
217}
218
219
220// returns array of keys
221text_tarray jdbmnaiveclass::getkeys ()
222{
223 text_tarray keys;
224
225 if (openfile_.empty()) return keys;
226
227 if (logout == NULL) {
228 logout = &cerr;
229 }
230
231 char* openfile_cstr = (to_utf8(openfile_)).getcstr();
232
233 char cmd[512];
234 sprintf(cmd,"java -cp \"%s\" JdbKeys \"%s\"",classpath_cstr_,
235 openfile_cstr);
236
237 delete [] openfile_cstr;
238
239 FILE* PIN = popen(cmd,"r");
240
241 if (PIN == NULL) {
242 (*logout) << "jdbmnaiveclass: failed to run cmd: " << cmd << endl;
243 }
244 else {
245 text_t key = "";
246
247 while (!feof(PIN)) {
248
249 char buf[256];
250 fgets(buf,256,PIN);
251 key.append(buf);
252
253 keys.push_back(key);
254
255 }
256 }
257
258 pclose(PIN);
259
260 return keys;
261}
262
263
264// returns true on success
265bool jdbmnaiveclass::setkeydata (const text_t &key, const text_t &data)
266{
267 if (openfile_.empty()) return false;
268
269 if (logout == NULL) {
270 logout = &cerr;
271 }
272
273 // get a utf-8 encoded c string of the unicode key
274 char* key_cstr = (to_utf8(key)).getcstr();
275
276 if (key_cstr == NULL) {
277 (*logout) << "jdbmnaiveclass: out of memory" << endl;
278
279 return false;
280 }
281
282 char* data_cstr = (to_utf8(data)).getcstr();
283 if (data_cstr == NULL) {
284 (*logout) << "jdbmnaiveclass: out of memory" << endl;
285
286 delete [] key_cstr;
287 return false;
288 }
289 int data_len = strlen(data_cstr);
290
291 char* openfile_cstr = (to_utf8(openfile_)).getcstr();
292
293 char cmd[512];
294 sprintf(cmd,"java -cp \"%s\" JdbSet \"%s\" %s %s",
295 classpath_cstr_,openfile_cstr,key_cstr,data_cstr);
296
297 delete [] openfile_cstr;
298
299 //int status = system(cmd); // causes flashing DOS prompt windows
300 // https://stackoverflow.com/questions/1597289/hide-console-in-c-system-function-win
301 int status = gsdl_system(cmd, true, *logout); // passing false makes it a synchronous call
302 if (status != 0) {
303 (*logout) << "jdbmnaiveclass: failed to run cmd:"
304 << cmd << endl;
305 }
306
307 delete [] key_cstr;
308 delete [] data_cstr;
309
310 return (status == 0);
311}
312
313
314
Note: See TracBrowser for help on using the repository browser.