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