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