source: gs2-extensions/tdb-edit/trunk/src/src/tdb2txt-src/tdb2txt.cpp@ 23998

Last change on this file since 23998 was 23998, checked in by jmt12, 13 years ago

Removed text_t.h header as not needed, and gsdlib.a when linking too - smaller exe for the WIN

File size: 2.8 KB
Line 
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
38#include "tdb.h"
39
40// use the standard namespace
41#if !defined (GSDL_NAMESPACE_BROKEN)
42#if defined(GSDL_USE_OBJECTSPACE)
43using namespace ospace::std;
44#else
45using namespace std;
46#endif
47#endif
48
49/**
50 */
51void
52printUsage (char *program_name)
53{
54 cerr << "usage: " << program_name << " database-name" << endl << endl;
55}
56/** printUsage() **/
57
58/**
59 */
60int
61main (int argc, char *argv[])
62{
63 // sanity check
64 if (argc != 2)
65 {
66 printUsage(argv[0]);
67 exit (0);
68 }
69
70 char *dbname = argv[1];
71
72 // open the database
73 int hash_size = 0;
74 int tdb_flags = TDB_DEFAULT;
75 int open_flags = O_RDONLY;
76 TDB_CONTEXT *tdb = tdb_open(dbname, hash_size, tdb_flags, open_flags, 0664);
77 if (!tdb)
78 {
79 cerr << "couldn't create " << dbname << endl;
80 exit (0);
81 }
82
83 TDB_DATA key = tdb_firstkey (tdb);
84 while (key.dptr != NULL)
85 {
86 cout << "[";
87 for (int i = 0; i < key.dsize; ++i)
88 {
89 cout << key.dptr[i];
90 }
91 cout << "]" << endl;
92 TDB_DATA value = tdb_fetch (tdb, key);
93 for (int i = 0; i < value.dsize; ++i)
94 {
95 cout << value.dptr[i];
96 }
97 cout << endl << "----------------------------------------------------------------------" << endl;
98 free(value.dptr);
99
100 /* get next key */
101 TDB_DATA nextkey = tdb_nextkey (tdb, key);
102
103 /* free old key's dptr, otherwise causes memory leak */
104 free(key.dptr);
105
106 /* can now safely copy content of nextkey into key */
107 key = nextkey;
108 }
109
110 tdb_close(tdb);
111
112 return 0;
113}
114/** main() **/
Note: See TracBrowser for help on using the repository browser.