[535] | 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 |
|
---|
[26650] | 26 | #ifdef _MSC_VER
|
---|
[203] | 27 | #include "autoconf.h"
|
---|
| 28 | #include "systems.h"
|
---|
| 29 | #include "gdbmconst.h"
|
---|
| 30 | #include "gdbm.h"
|
---|
| 31 |
|
---|
| 32 | #else
|
---|
[27597] | 33 |
|
---|
| 34 | /* header file for mkdir */
|
---|
| 35 | #include <sys/stat.h>
|
---|
| 36 |
|
---|
[203] | 37 | #include <gdbm.h>
|
---|
| 38 | #endif
|
---|
| 39 |
|
---|
| 40 | #include "gsdlconf.h"
|
---|
[27600] | 41 | #include "text_t.h"
|
---|
[203] | 42 | #include <stdlib.h>
|
---|
[27600] | 43 | #include <cstring>
|
---|
[203] | 44 |
|
---|
[27600] | 45 | // for sorting
|
---|
[203] | 46 | #if defined(GSDL_USE_OBJECTSPACE)
|
---|
[27600] | 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)
|
---|
[203] | 63 | #include <ospace\std\iostream>
|
---|
| 64 | #elif defined(GSDL_USE_IOS_H)
|
---|
| 65 | #include <iostream.h>
|
---|
| 66 | #else
|
---|
| 67 | #include <iostream>
|
---|
| 68 | #endif
|
---|
| 69 |
|
---|
[2499] | 70 | // use the standard namespace
|
---|
| 71 | #if !defined (GSDL_NAMESPACE_BROKEN)
|
---|
| 72 | #if defined(GSDL_USE_OBJECTSPACE)
|
---|
| 73 | using namespace ospace::std;
|
---|
| 74 | #else
|
---|
| 75 | using namespace std;
|
---|
| 76 | #endif
|
---|
| 77 | #endif
|
---|
| 78 |
|
---|
[27625] | 79 | // John Thompson's lock() and unlock() needed in parallel building
|
---|
[27600] | 80 | // parked in separate file to help readability of main code
|
---|
[27624] | 81 | #include "filelock.cpp"
|
---|
[27600] | 82 |
|
---|
[203] | 83 | void print_usage (char *program_name) {
|
---|
[27600] | 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;
|
---|
[203] | 87 | }
|
---|
| 88 |
|
---|
[27600] | 89 | void 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;
|
---|
[203] | 97 |
|
---|
[27597] | 98 |
|
---|
[27600] | 99 | for (i = 0; i < value.dsize; ++i) {
|
---|
| 100 | cout << value.dptr[i];
|
---|
[27597] | 101 | }
|
---|
[27600] | 102 | cout << endl << "----------------------------------------------------------------------" << endl;
|
---|
[27597] | 103 | }
|
---|
| 104 |
|
---|
| 105 |
|
---|
[203] | 106 | int main (int argc, char *argv[]) {
|
---|
| 107 | int block_size = 0, i;
|
---|
| 108 | GDBM_FILE dbf;
|
---|
[15694] | 109 | datum key, value, nextkey;
|
---|
[203] | 110 |
|
---|
| 111 | // sanity check
|
---|
[27600] | 112 | if (argc != 2 && argc != 3) {
|
---|
[203] | 113 | print_usage (argv[0]);
|
---|
| 114 | exit (0);
|
---|
| 115 | }
|
---|
| 116 |
|
---|
[27600] | 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 |
|
---|
[203] | 133 | // open the database
|
---|
[26650] | 134 | #ifdef _MSC_VER
|
---|
[27600] | 135 | dbf = gdbm_open (dbname, block_size, GDBM_READER, 00664, NULL, 0);
|
---|
[982] | 136 | #else
|
---|
[27600] | 137 | dbf = gdbm_open (dbname, block_size, GDBM_READER, 00664, NULL);
|
---|
[982] | 138 | #endif
|
---|
[203] | 139 | if (dbf == NULL) {
|
---|
[27600] | 140 | cerr << argv[0] << ": couldn't open " << dbname << endl;
|
---|
[203] | 141 | exit (0);
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | key = gdbm_firstkey (dbf);
|
---|
| 145 | while (key.dptr != NULL) {
|
---|
[27600] | 146 |
|
---|
[203] | 147 | value = gdbm_fetch (dbf, key);
|
---|
[27600] | 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 |
|
---|
[15218] | 160 | free(value.dptr);
|
---|
| 161 |
|
---|
[15694] | 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;
|
---|
[203] | 170 | }
|
---|
[27600] | 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 | }
|
---|
[203] | 193 |
|
---|
[27600] | 194 |
|
---|
[203] | 195 | gdbm_close (dbf);
|
---|
| 196 | return 0;
|
---|
| 197 | }
|
---|