source: gs2-extensions/tdb-edit/trunk/src/src/tdbset-src/tdbset.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: 4.7 KB
Line 
1/**********************************************************************
2 *
3 * tdbset -- add or replace 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) 1999 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> <key> [<value>] [append]" << endl;
55 cerr << "\t- if no value is given then the pair indicated by the key is removed" << endl;
56 cerr << "\t- if a value is given followed by 'append' then the value is " << endl;
57 cerr << "\t added to the existing entry rather than overwriting it" << endl << endl;
58}
59/** printUsage() **/
60
61/**
62 */
63int
64main (int argc, char *argv[])
65{
66 // sanity check
67 if ((argc < 3) || (argc>5))
68 {
69 printUsage(argv[0]);
70 exit (-1);
71 }
72
73 // open the collection to the database - read write this time.
74 char *dbname = argv[1];
75 int hash_size = 0;
76 int tdb_flags = TDB_DEFAULT; // Default = 0
77 int tdb_store_flags = TDB_DEFAULT; // used later when storing
78 int open_flags = O_RDWR | O_CREAT;
79 TDB_CONTEXT *tdb = tdb_open(dbname, hash_size, tdb_flags, open_flags, 0664);
80 if (!tdb)
81 {
82 cerr << "couldn't create " << dbname << endl;
83 exit (0);
84 }
85
86 // create the object to represent the key we are altering
87 TDB_DATA key_data;
88 key_data.dsize = strlen(argv[2]);
89 key_data.dptr = (unsigned char*)argv[2];
90
91 // if there are a fistfull of arguments we may be doing a mystical append
92 if(argc > 4)
93 {
94 // if there are five arguments and the last one is append, then we
95 // concatenate the value onto whatever is already in this key
96 // value pair
97 if (strcmp(argv[4], "append") == 0)
98 {
99 // get old value, then top up
100 TDB_DATA original_value_data = tdb_fetch(tdb, key_data);
101
102 // append orig_content with argv[3]
103 TDB_DATA concat_value_data;
104 concat_value_data.dsize = original_value_data.dsize + strlen(argv[3]);
105 // remember to leave room for \0 at end
106 char* concat_data = new char[concat_value_data.dsize + 1];
107 // if the value already has content, append to the end of it
108 if (original_value_data.dsize > 0)
109 {
110 strncpy(concat_data, (const char*)original_value_data.dptr, original_value_data.dsize);
111 // ensures \0 at end
112 strcpy(&concat_data[original_value_data.dsize], argv[3]);
113 }
114 // otherwise this is the first time key has been used
115 else
116 {
117 // ensures \0 at end
118 strcpy(concat_data, argv[3]);
119 }
120 concat_value_data.dptr = (unsigned char*)concat_data;
121
122 if(tdb_store(tdb, key_data, concat_value_data, tdb_store_flags) != 0)
123 {
124 cerr << "failed to set [" << argv[2] << "] to '" << concat_data << "'" << endl;
125 }
126
127 // I'm responsible for freeing some memory
128 delete [] concat_data;
129 free(original_value_data.dptr);
130 }
131 // five arguments but the last isn't append. No sir, I don't like it
132 else
133 {
134 printUsage(argv[0]);
135 }
136 }
137 // normal case - we've been given a key and a value to update it with
138 else if (argc == 4)
139 {
140 TDB_DATA value_data;
141 value_data.dsize = strlen(argv[3]);
142 value_data.dptr = (unsigned char*)argv[3];
143
144 if(tdb_store(tdb, key_data, value_data, tdb_store_flags) != 0)
145 {
146 cerr << "failed to set [" << argv[2] << "] to '" << argv[3] << "'" << endl;
147 }
148 }
149 // only given a key? that's a deleting
150 else
151 {
152 if(tdb_delete(tdb, key_data) != 0)
153 {
154 cerr << "failed to remove [" << argv[2] << "]" << endl;
155 }
156 }
157
158 tdb_close(tdb);
159
160 return 0;
161}
Note: See TracBrowser for help on using the repository browser.