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