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

Last change on this file since 27626 was 27626, checked in by ak19, 11 years ago

Dr Bainbridge hopes including sys/types.h may help compiling filelock.cpp along on Mac.

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