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

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

Explicitly include cstdlib as modern compilers don't add by default

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