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

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

Initial checkin of refactoring of gdbmkeys to TDB

  • 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// use the standard namespace
36#if !defined (GSDL_NAMESPACE_BROKEN)
37#if defined(GSDL_USE_OBJECTSPACE)
38using namespace ospace::std;
39#else
40using namespace std;
41#endif
42#endif
43
44/**
45 */
46void
47printUsage(char *program_name)
48{
49 cerr << "usage: " << program_name << " <database_path>" << endl << endl;
50}
51/** printUsage() **/
52
53/** Open a connection to the TDB database then return a listing of all of the
54 * keys, one per line, to STDOUT.
55 * @param argc - must be 2
56 * @param argv - and char * array containing first the executable name, then
57 * the name of/path to the database
58 * @return 0
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 for (int i = 0; i < key.dsize; ++i)
87 {
88 cout << key.dptr[i];
89 }
90 cout << endl;
91
92 /* get next key */
93 TDB_DATA nextkey = tdb_nextkey(tdb, key);
94
95 /* free old key's dptr, otherwise causes memory leak */
96 free(key.dptr);
97
98 /* can now safely copy content of nextkey into key */
99 key = nextkey;
100 }
101
102 tdb_close(tdb);
103
104 return 0;
105}
106/** main() **/
107
Note: See TracBrowser for help on using the repository browser.