source: gs2-extensions/tdb-edit/trunk/src/src/tdbget-src/tdbget.cpp@ 24047

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

I'd misunderstood where I needed to free the memory allocated to storing char*s in the TDB_DATA. Apparently I only have to free them when they are returned by tdb_fetch() or similar. Removed segfault causing calls to free()

File size: 2.5 KB
Line 
1/**********************************************************************
2 *
3 * tdbget -- 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 <fcntl.h>
36#include "tdb.h"
37
38// use the standard namespace
39#if !defined (GSDL_NAMESPACE_BROKEN)
40#if defined(GSDL_USE_OBJECTSPACE)
41using namespace ospace::std;
42#else
43using namespace std;
44#endif
45#endif
46
47/**
48 */
49void
50printUsage (char *program_name)
51{
52 cerr << "usage: " << program_name << " <database_path> <key>" << endl << endl;
53}
54/** printUsage() **/
55
56/**
57 */
58int
59main (int argc, char *argv[])
60{
61 // sanity check
62 if (argc != 3)
63 {
64 printUsage(argv[0]);
65 exit (-1);
66 }
67
68 char *dbname = argv[1];
69
70 int hash_size = 0;
71 int tdb_flags = TDB_DEFAULT; // Default = 0
72 int open_flags = O_RDONLY;
73 TDB_CONTEXT *tdb = tdb_open(dbname, hash_size, tdb_flags, open_flags, 0664);
74 if (!tdb)
75 {
76 cerr << "couldn't create " << dbname << endl;
77 exit (0);
78 }
79
80 TDB_DATA key;
81 key.dsize = strlen(argv[2]);
82 key.dptr = (unsigned char*)argv[2];
83
84 TDB_DATA value = tdb_fetch (tdb, key);
85
86 if (value.dsize > 0)
87 {
88 for (int i = 0; i < value.dsize; i++)
89 {
90 cout << value.dptr[i];
91 }
92 cout << endl; // used to be printf("\n");
93 // caller responsible for freeing this memory
94 free(value.dptr);
95 }
96 else
97 {
98 cout << endl; // used to be printf("\n");
99 }
100
101 tdb_close (tdb);
102 return 0;
103}
104/** main() **/
105
Note: See TracBrowser for help on using the repository browser.