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

Last change on this file was 28091, checked in by davidb, 11 years ago

Further header file needed

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 4.9 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#include <gdbm.h>
34#include <errno.h>
35#endif
36
37#include "gsdlconf.h"
38#include "text_t.h"
39#include <stdio.h>
40#include <stdlib.h>
41#include <cstring>
42
43// for sorting
44#if defined(GSDL_USE_OBJECTSPACE)
45# include <ospace\std\vector>
46# include <ospace\std\algorithm>
47#elif defined(GSDL_USE_STL_H)
48# include <vector.h>
49# if defined(GSDL_USE_ALGO_H)
50# include <algo.h>
51# else
52# include <algorithm.h>
53# endif
54#else
55# include <vector>
56# include <algorithm>
57#endif
58
59
60#if defined(GSDL_USE_OBJECTSPACE)
61#include <ospace\std\iostream>
62#elif defined(GSDL_USE_IOS_H)
63#include <iostream.h>
64#else
65#include <iostream>
66#endif
67
68// use the standard namespace
69#if !defined (GSDL_NAMESPACE_BROKEN)
70#if defined(GSDL_USE_OBJECTSPACE)
71using namespace ospace::std;
72#else
73using namespace std;
74#endif
75#endif
76
77#ifdef _GSDL_FLOCK_ENABLED
78// John Thompson's lock() and unlock() needed in parallel building
79// parked in separate file to help readability of main code
80#include "filelock.cpp"
81#endif
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 cerr << " " << gdbm_strerror(gdbm_errno) << endl;
142 cerr << " gdbm errno = " << gdbm_errno << endl;
143#ifndef _MSC_VER
144 cerr << " OS errno = " << errno << endl;
145 perror("gdbm_open failed: ");
146#endif
147
148 exit (-1);
149 }
150
151 key = gdbm_firstkey (dbf);
152 while (key.dptr != NULL) {
153
154 value = gdbm_fetch (dbf, key);
155
156 if(sort) { // store in vector, will sort and then print later
157 text_t key_str;
158 for (i = 0; i < key.dsize; ++i) {
159 key_str.push_back ((unsigned char)key.dptr[i]);
160 }
161 key_list.push_back(key_str); // STL makes a copy on push_back
162
163 } else {
164 print_entry(key, value);
165 }
166
167 free(value.dptr);
168
169 /* get next key */
170 nextkey = gdbm_nextkey (dbf, key);
171
172 /* free old key's dptr, otherwise causes memory leak */
173 free(key.dptr);
174
175 /* can now safely copy content of nextkey into key */
176 key = nextkey;
177 }
178
179 if(sort) {
180 std::sort(key_list.begin(), key_list.end());
181
182 vector<text_t>::const_iterator this_key = key_list.begin();
183 vector<text_t>::const_iterator end_key = key_list.end();
184 while (this_key != end_key) {
185 const text_t& key_str = *this_key;
186 char* key_cstr = key_str.getcstr();
187
188 key.dsize = strlen(key_cstr);
189 key.dptr = key_cstr;
190
191 value = gdbm_fetch (dbf, key);
192
193 print_entry(key,value); // print in sorted order now
194
195 free(value.dptr);
196
197 this_key++;
198 }
199 }
200
201
202 gdbm_close (dbf);
203 return 0;
204}
Note: See TracBrowser for help on using the repository browser.