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 <fcntl.h>
|
---|
36 | #include "tdb.h"
|
---|
37 |
|
---|
38 | // use the standard namespace
|
---|
39 | #if !defined (GSDL_NAMESPACE_BROKEN)
|
---|
40 | #if defined(GSDL_USE_OBJECTSPACE)
|
---|
41 | using namespace ospace::std;
|
---|
42 | #else
|
---|
43 | using namespace std;
|
---|
44 | #endif
|
---|
45 | #endif
|
---|
46 |
|
---|
47 | /**
|
---|
48 | */
|
---|
49 | void
|
---|
50 | printUsage (char *program_name)
|
---|
51 | {
|
---|
52 | cerr << "usage: " << program_name << " <database_path> <key> [<value>] [append]" << endl;
|
---|
53 | cerr << "\t- if no value is given then the pair indicated by the key is removed" << endl;
|
---|
54 | cerr << "\t- if a value is given followed by 'append' then the value is " << endl;
|
---|
55 | cerr << "\t added to the existing entry rather than overwriting it" << endl << endl;
|
---|
56 | }
|
---|
57 | /** printUsage() **/
|
---|
58 |
|
---|
59 | /**
|
---|
60 | */
|
---|
61 | int
|
---|
62 | main (int argc, char *argv[])
|
---|
63 | {
|
---|
64 | // sanity check
|
---|
65 | if ((argc < 3) || (argc>5))
|
---|
66 | {
|
---|
67 | printUsage(argv[0]);
|
---|
68 | exit (-1);
|
---|
69 | }
|
---|
70 |
|
---|
71 | // open the collection to the database - read write this time.
|
---|
72 | char *dbname = argv[1];
|
---|
73 | int hash_size = 0;
|
---|
74 | int tdb_flags = TDB_DEFAULT; // Default = 0
|
---|
75 | int tdb_store_flags = TDB_DEFAULT; // used later when storing
|
---|
76 | int open_flags = O_RDWR | O_CREAT;
|
---|
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 | // create the object to represent the key we are altering
|
---|
85 | TDB_DATA key_data;
|
---|
86 | key_data.dsize = strlen(argv[2]);
|
---|
87 | key_data.dptr = (unsigned char*)argv[2];
|
---|
88 |
|
---|
89 | // if there are a fistfull of arguments we may be doing a mystical append
|
---|
90 | if(argc > 4)
|
---|
91 | {
|
---|
92 | // if there are five arguments and the last one is append, then we
|
---|
93 | // concatenate the value onto whatever is already in this key
|
---|
94 | // value pair
|
---|
95 | if (strcmp(argv[4], "append") == 0)
|
---|
96 | {
|
---|
97 | // get old value, then top up
|
---|
98 | TDB_DATA original_value_data = tdb_fetch(tdb, key_data);
|
---|
99 |
|
---|
100 | // append orig_content with argv[3]
|
---|
101 | TDB_DATA concat_value_data;
|
---|
102 | concat_value_data.dsize = original_value_data.dsize + strlen(argv[3]);
|
---|
103 | // remember to leave room for \0 at end
|
---|
104 | char* concat_data = new char[concat_value_data.dsize + 1];
|
---|
105 | // if the value already has content, append to the end of it
|
---|
106 | if (original_value_data.dsize > 0)
|
---|
107 | {
|
---|
108 | strncpy(concat_data, (const char*)original_value_data.dptr, original_value_data.dsize);
|
---|
109 | // ensures \0 at end
|
---|
110 | strcpy(&concat_data[original_value_data.dsize], argv[3]);
|
---|
111 | }
|
---|
112 | // otherwise this is the first time key has been used
|
---|
113 | else
|
---|
114 | {
|
---|
115 | // ensures \0 at end
|
---|
116 | strcpy(concat_data, argv[3]);
|
---|
117 | }
|
---|
118 | concat_value_data.dptr = (unsigned char*)concat_data;
|
---|
119 |
|
---|
120 | if(tdb_store(tdb, key_data, concat_value_data, tdb_store_flags) != 0)
|
---|
121 | {
|
---|
122 | cerr << "failed to set [" << argv[2] << "] to '" << concat_data << "'" << endl;
|
---|
123 | }
|
---|
124 |
|
---|
125 | // I'm responsible for freeing some memory
|
---|
126 | delete [] concat_data;
|
---|
127 | free(original_value_data.dptr);
|
---|
128 | }
|
---|
129 | // five arguments but the last isn't append. No sir, I don't like it
|
---|
130 | else
|
---|
131 | {
|
---|
132 | printUsage(argv[0]);
|
---|
133 | }
|
---|
134 | }
|
---|
135 | // normal case - we've been given a key and a value to update it with
|
---|
136 | else if (argc == 4)
|
---|
137 | {
|
---|
138 | TDB_DATA value_data;
|
---|
139 | value_data.dsize = strlen(argv[3]);
|
---|
140 | value_data.dptr = (unsigned char*)argv[3];
|
---|
141 |
|
---|
142 | if(tdb_store(tdb, key_data, value_data, tdb_store_flags) != 0)
|
---|
143 | {
|
---|
144 | cerr << "failed to set [" << argv[2] << "] to '" << argv[3] << "'" << endl;
|
---|
145 | }
|
---|
146 | }
|
---|
147 | // only given a key? that's a deleting
|
---|
148 | else
|
---|
149 | {
|
---|
150 | if(tdb_delete(tdb, key_data) != 0)
|
---|
151 | {
|
---|
152 | cerr << "failed to remove [" << argv[2] << "]" << endl;
|
---|
153 | }
|
---|
154 | }
|
---|
155 |
|
---|
156 | tdb_close(tdb);
|
---|
157 |
|
---|
158 | return 0;
|
---|
159 | }
|
---|