1 | /**********************************************************************
|
---|
2 | *
|
---|
3 | * tdb2txt.cpp -- A utility to retrieve the entire contents of a TDB
|
---|
4 | * file as text ala buildproc encoded output.
|
---|
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 | * Copyright (C) 2011 The New Zealand Digital Library Project
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or modify
|
---|
13 | * it under the terms of the GNU General Public License as published by
|
---|
14 | * the Free Software Foundation; either version 2 of the License, or
|
---|
15 | * (at your option) any later version.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful,
|
---|
18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
20 | * GNU General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, write to the Free Software
|
---|
24 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
25 | *
|
---|
26 | *********************************************************************/
|
---|
27 |
|
---|
28 | #if defined(GSDL_USE_OBJECTSPACE)
|
---|
29 | #include <ospace\std\iostream>
|
---|
30 | #elif defined(GSDL_USE_IOS_H)
|
---|
31 | #include <iostream.h>
|
---|
32 | #else
|
---|
33 | #include <iostream>
|
---|
34 | #endif
|
---|
35 |
|
---|
36 | #include <fcntl.h>
|
---|
37 | #include "tdb.h"
|
---|
38 |
|
---|
39 | // use the standard namespace
|
---|
40 | #if !defined (GSDL_NAMESPACE_BROKEN)
|
---|
41 | #if defined(GSDL_USE_OBJECTSPACE)
|
---|
42 | using namespace ospace::std;
|
---|
43 | #else
|
---|
44 | using namespace std;
|
---|
45 | #endif
|
---|
46 | #endif
|
---|
47 |
|
---|
48 | /**
|
---|
49 | */
|
---|
50 | void
|
---|
51 | printUsage (char *program_name)
|
---|
52 | {
|
---|
53 | cerr << "usage: " << program_name << " database-name" << endl << endl;
|
---|
54 | }
|
---|
55 | /** printUsage() **/
|
---|
56 |
|
---|
57 | /**
|
---|
58 | */
|
---|
59 | int
|
---|
60 | main (int argc, char *argv[])
|
---|
61 | {
|
---|
62 | // sanity check
|
---|
63 | if (argc != 2)
|
---|
64 | {
|
---|
65 | printUsage(argv[0]);
|
---|
66 | exit (0);
|
---|
67 | }
|
---|
68 |
|
---|
69 | char *dbname = argv[1];
|
---|
70 |
|
---|
71 | // open the database
|
---|
72 | int hash_size = 0;
|
---|
73 | int tdb_flags = TDB_DEFAULT;
|
---|
74 | int open_flags = O_RDONLY;
|
---|
75 | TDB_CONTEXT *tdb = tdb_open(dbname, hash_size, tdb_flags, open_flags, 0664);
|
---|
76 | if (!tdb)
|
---|
77 | {
|
---|
78 | cerr << "couldn't create " << dbname << endl;
|
---|
79 | exit (0);
|
---|
80 | }
|
---|
81 |
|
---|
82 | TDB_DATA key = tdb_firstkey(tdb);
|
---|
83 | while (key.dptr != NULL)
|
---|
84 | {
|
---|
85 | cout << "[";
|
---|
86 | for (int i = 0; i < key.dsize; ++i)
|
---|
87 | {
|
---|
88 | cout << key.dptr[i];
|
---|
89 | }
|
---|
90 | cout << "]" << endl;
|
---|
91 | TDB_DATA value = tdb_fetch (tdb, key);
|
---|
92 | for (int i = 0; i < value.dsize; ++i)
|
---|
93 | {
|
---|
94 | cout << value.dptr[i];
|
---|
95 | }
|
---|
96 | cout << endl << "----------------------------------------------------------------------" << endl;
|
---|
97 | free(value.dptr);
|
---|
98 |
|
---|
99 | /* get next key */
|
---|
100 | TDB_DATA nextkey = tdb_nextkey (tdb, key);
|
---|
101 |
|
---|
102 | /* free old key's dptr, otherwise causes memory leak */
|
---|
103 | free(key.dptr);
|
---|
104 |
|
---|
105 | /* can now safely copy content of nextkey into key */
|
---|
106 | key = nextkey;
|
---|
107 | }
|
---|
108 |
|
---|
109 | tdb_close(tdb);
|
---|
110 |
|
---|
111 | return 0;
|
---|
112 | }
|
---|
113 | /** main() **/
|
---|