source: gs2-extensions/tdb-edit/trunk/src/src/tdbkeys-src/tdbkeys.cpp@ 24365

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

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

  • Property svn:executable set to *
File size: 2.7 KB
Line 
1/**********************************************************************
2 *
3 * tdbkeys -- retrieve a single value from the TDB database
4 *
5 * A component of the Greenstone digital library software
6 * from the New Zealand Digital Library Project at the
7 * University of Waikato, New Zealand.
8 *
9 * Copyright (C) 2011 The New Zealand Digital Library Project
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 *
25 *********************************************************************/
26
27#if defined(GSDL_USE_OBJECTSPACE)
28#include <ospace\std\iostream>
29#elif defined(GSDL_USE_IOS_H)
30#include <iostream.h>
31#else
32#include <iostream>
33#endif
34
35#include <cstdlib>
36
37#include <fcntl.h>
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_path>" << endl << endl;
55}
56/** printUsage() **/
57
58/** Open a connection to the TDB database then return a listing of all of the
59 * keys, one per line, to STDOUT.
60 * @param argc - must be 2
61 * @param argv - and char * array containing first the executable name, then
62 * the name of/path to the database
63 * @return 0
64 */
65int
66main (int argc, char *argv[])
67{
68 // sanity check
69 if (argc != 2)
70 {
71 printUsage(argv[0]);
72 exit (0);
73 }
74
75 char *dbname = argv[1];
76
77 // open the database
78 int hash_size = 0;
79 int tdb_flags = TDB_DEFAULT;
80 int open_flags = O_RDONLY;
81 TDB_CONTEXT *tdb = tdb_open(dbname, hash_size, tdb_flags, open_flags, 0664);
82 if (!tdb)
83 {
84 cerr << "couldn't create " << dbname << endl;
85 exit (0);
86 }
87
88 TDB_DATA key = tdb_firstkey(tdb);
89 while (key.dptr != NULL)
90 {
91 for (int i = 0; i < key.dsize; ++i)
92 {
93 cout << key.dptr[i];
94 }
95 cout << endl;
96
97 /* get next key */
98 TDB_DATA nextkey = tdb_nextkey(tdb, key);
99
100 /* free old key's dptr, otherwise causes memory leak */
101 free(key.dptr);
102
103 /* can now safely copy content of nextkey into key */
104 key = nextkey;
105 }
106
107 tdb_close(tdb);
108
109 return 0;
110}
111/** main() **/
112
Note: See TracBrowser for help on using the repository browser.