source: main/trunk/greenstone2/common-src/src/gdbmedit/db2txt/db2txt.cpp@ 27624

Last change on this file since 27624 was 27624, checked in by ak19, 8 years ago

renamed winlock to filelock as it's not windows specific.

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 4.7 KB
Line 
1/**********************************************************************
2 *
3 * db2txt.cpp --
4 * Copyright (C) 1999 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#ifdef _MSC_VER
27#include "autoconf.h"
28#include "systems.h"
29#include "gdbmconst.h"
30#include "gdbm.h"
31
32#else
33
34/* header file for mkdir */
35#include <sys/stat.h>
36
37#include <gdbm.h>
38#endif
39
40#include "gsdlconf.h"
41#include "text_t.h"
42#include <stdlib.h>
43#include <cstring>
44
45// for sorting
46#if defined(GSDL_USE_OBJECTSPACE)
47# include <ospace\std\vector>
48# include <ospace\std\algorithm>
49#elif defined(GSDL_USE_STL_H)
50# include <vector.h>
51# if defined(GSDL_USE_ALGO_H)
52# include <algo.h>
53# else
54# include <algorithm.h>
55# endif
56#else
57# include <vector>
58# include <algorithm>
59#endif
60
61
62#if defined(GSDL_USE_OBJECTSPACE)
63#include <ospace\std\iostream>
64#elif defined(GSDL_USE_IOS_H)
65#include <iostream.h>
66#else
67#include <iostream>
68#endif
69
70// use the standard namespace
71#if !defined (GSDL_NAMESPACE_BROKEN)
72#if defined(GSDL_USE_OBJECTSPACE)
73using namespace ospace::std;
74#else
75using namespace std;
76#endif
77#endif
78
79// John Thompson's lock() and unlock() for windows
80// parked in separate file to help readability of main code
81#include "filelock.cpp"
82
83void print_usage (char *program_name) {
84 cerr << "usage: " << program_name << " [options] database-name" << endl << endl;
85 cerr << "options:" << endl;
86 cerr << " -sort sort the keys to the database before output" << endl << endl;
87}
88
89void print_entry(datum& key, datum& value)
90{
91 int i;
92 cout << "[";
93 for (i = 0; i < key.dsize; ++i) {
94 cout << key.dptr[i];
95 }
96 cout << "]" << endl;
97
98
99 for (i = 0; i < value.dsize; ++i) {
100 cout << value.dptr[i];
101 }
102 cout << endl << "----------------------------------------------------------------------" << endl;
103}
104
105
106int main (int argc, char *argv[]) {
107 int block_size = 0, i;
108 GDBM_FILE dbf;
109 datum key, value, nextkey;
110
111 // sanity check
112 if (argc != 2 && argc != 3) {
113 print_usage (argv[0]);
114 exit (0);
115 }
116
117 char *dbname;
118 int sort = 0;
119
120 if (argc == 3) {
121 if (strcmp (argv[1], "-sort") == 0) {
122 sort = 1;
123 dbname = argv[2];
124 } else {
125 cerr << argv[1] << " is not a valid option." << endl << endl;
126 print_usage (argv[0]);
127 exit (0);
128 }
129 } else dbname = argv[1];
130
131 vector<text_t> key_list;
132
133 // open the database
134#ifdef _MSC_VER
135 dbf = gdbm_open (dbname, block_size, GDBM_READER, 00664, NULL, 0);
136#else
137 dbf = gdbm_open (dbname, block_size, GDBM_READER, 00664, NULL);
138#endif
139 if (dbf == NULL) {
140 cerr << argv[0] << ": couldn't open " << dbname << endl;
141 exit (0);
142 }
143
144 key = gdbm_firstkey (dbf);
145 while (key.dptr != NULL) {
146
147 value = gdbm_fetch (dbf, key);
148
149 if(sort) { // store in vector, will sort and then print later
150 text_t key_str;
151 for (i = 0; i < key.dsize; ++i) {
152 key_str.push_back ((unsigned char)key.dptr[i]);
153 }
154 key_list.push_back(key_str); // STL makes a copy on push_back
155
156 } else {
157 print_entry(key, value);
158 }
159
160 free(value.dptr);
161
162 /* get next key */
163 nextkey = gdbm_nextkey (dbf, key);
164
165 /* free old key's dptr, otherwise causes memory leak */
166 free(key.dptr);
167
168 /* can now safely copy content of nextkey into key */
169 key = nextkey;
170 }
171
172 if(sort) {
173 std::sort(key_list.begin(), key_list.end());
174
175 vector<text_t>::const_iterator this_key = key_list.begin();
176 vector<text_t>::const_iterator end_key = key_list.end();
177 while (this_key != end_key) {
178 const text_t& key_str = *this_key;
179 char* key_cstr = key_str.getcstr();
180
181 key.dsize = strlen(key_cstr);
182 key.dptr = key_cstr;
183
184 value = gdbm_fetch (dbf, key);
185
186 print_entry(key,value); // print in sorted order now
187
188 free(value.dptr);
189
190 this_key++;
191 }
192 }
193
194
195 gdbm_close (dbf);
196 return 0;
197}
Note: See TracBrowser for help on using the repository browser.