1 | /**********************************************************************
|
---|
2 | *
|
---|
3 | * gdbmset -- add or replace a single value from the GDBM database
|
---|
4 | * Copyright (C) 1999 The New Zealand Digital Library Project
|
---|
5 | *
|
---|
6 | * A component of the Greenstone digital library software
|
---|
7 | * from the New Zealand Digital Library Project at the
|
---|
8 | * University of Waikato, New Zealand.
|
---|
9 | *
|
---|
10 | * This program is free software; you can redistribute it and/or modify
|
---|
11 | * it under the terms of the GNU General Public License as published by
|
---|
12 | * the Free Software Foundation; either version 2 of the License, or
|
---|
13 | * (at your option) any later version.
|
---|
14 | *
|
---|
15 | * This program is distributed in the hope that it will be useful,
|
---|
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
18 | * GNU General Public License for more details.
|
---|
19 | *
|
---|
20 | * You should have received a copy of the GNU General Public License
|
---|
21 | * along with this program; if not, write to the Free Software
|
---|
22 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
23 | *
|
---|
24 | *********************************************************************/
|
---|
25 |
|
---|
26 | #ifdef __WIN32__
|
---|
27 | #include "autoconf.h"
|
---|
28 | #include "systems.h"
|
---|
29 | #include "gdbmconst.h"
|
---|
30 | #include "gdbm.h"
|
---|
31 |
|
---|
32 | #else
|
---|
33 | #include <gdbm.h>
|
---|
34 | #endif
|
---|
35 |
|
---|
36 | #include "gsdlconf.h"
|
---|
37 | #include <stdlib.h>
|
---|
38 |
|
---|
39 | #if defined(GSDL_USE_OBJECTSPACE)
|
---|
40 | #include <ospace\std\iostream>
|
---|
41 | #elif defined(GSDL_USE_IOS_H)
|
---|
42 | #include <iostream.h>
|
---|
43 | #else
|
---|
44 | #include <iostream>
|
---|
45 | #endif
|
---|
46 |
|
---|
47 | // use the standard namespace
|
---|
48 | #if !defined (GSDL_NAMESPACE_BROKEN)
|
---|
49 | #if defined(GSDL_USE_OBJECTSPACE)
|
---|
50 | using namespace ospace::std;
|
---|
51 | #else
|
---|
52 | using namespace std;
|
---|
53 | #endif
|
---|
54 | #endif
|
---|
55 |
|
---|
56 | void
|
---|
57 | print_usage (char *program_name)
|
---|
58 | {
|
---|
59 | cerr << "usage: " << program_name << " <database_path> <key> [<value>] [append]" << endl;
|
---|
60 | cerr << "\t- if no value is given then the lexicon indicated by the key is removed" << endl;
|
---|
61 | cerr << "\t- if a value is given followed by 'append' then the value is " << endl;
|
---|
62 | cerr << "\t added to the existing entry rather than overwriting it" << endl;
|
---|
63 | cerr << endl;
|
---|
64 | }
|
---|
65 |
|
---|
66 |
|
---|
67 | int main (int argc, char *argv[])
|
---|
68 | {
|
---|
69 | int block_size = 0;
|
---|
70 | GDBM_FILE dbf;
|
---|
71 | datum key;
|
---|
72 |
|
---|
73 | // sanity check
|
---|
74 | if ((argc < 3) || (argc>5)) {
|
---|
75 | print_usage (argv[0]);
|
---|
76 | exit (-1);
|
---|
77 | }
|
---|
78 |
|
---|
79 | // open the database
|
---|
80 | #ifdef __WIN32__
|
---|
81 | dbf = gdbm_open (argv[1], block_size, GDBM_WRITER, 00664, NULL, 0);
|
---|
82 | #else
|
---|
83 | dbf = gdbm_open (argv[1], block_size, GDBM_WRITER, 00664, NULL);
|
---|
84 | #endif
|
---|
85 | if (dbf == NULL)
|
---|
86 | {
|
---|
87 | cerr << "couldn't open database connection to " << argv[1] << endl;
|
---|
88 | exit (-1);
|
---|
89 | }
|
---|
90 |
|
---|
91 | key.dsize = strlen(argv[2]);
|
---|
92 | key.dptr = argv[2];
|
---|
93 |
|
---|
94 | if(argc == 5) {
|
---|
95 |
|
---|
96 | if (strcmp(argv[4],"append")==0) {
|
---|
97 | datum orig_content,concat_content;
|
---|
98 |
|
---|
99 | //cerr << "Appending [" << argv[2] << "] to '" << argv[3] << "' (length " << strlen(argv[3]) << ")" << endl;
|
---|
100 | // get old value, then top up
|
---|
101 |
|
---|
102 | orig_content = gdbm_fetch (dbf, key);
|
---|
103 |
|
---|
104 | // append orig_content with argv[3]
|
---|
105 | concat_content.dsize = orig_content.dsize + strlen(argv[3]);
|
---|
106 | char* concat_data = new char[concat_content.dsize +1];// make room for \0 at end
|
---|
107 | if (orig_content.dsize>0) {
|
---|
108 | strncpy(concat_data,orig_content.dptr,orig_content.dsize);
|
---|
109 | strcpy(&concat_data[orig_content.dsize],argv[3]); // ensures \0 at end
|
---|
110 | }
|
---|
111 | else {
|
---|
112 | // first time key has been used
|
---|
113 | strcpy(concat_data,argv[3]); // ensures \0 at end
|
---|
114 | }
|
---|
115 |
|
---|
116 | concat_content.dptr = concat_data;
|
---|
117 | if(gdbm_store(dbf, key, concat_content, GDBM_REPLACE) != 0)
|
---|
118 | {
|
---|
119 | cerr << "failed to set [" << argv[2] << "] to '" << concat_data << "'" << endl;
|
---|
120 | }
|
---|
121 | delete [] concat_data;
|
---|
122 | }
|
---|
123 | else {
|
---|
124 | print_usage (argv[0]);
|
---|
125 | gdbm_close (dbf);
|
---|
126 | exit(-1);
|
---|
127 | }
|
---|
128 | }
|
---|
129 | else if (argc == 4) {
|
---|
130 | datum content;
|
---|
131 |
|
---|
132 | //cerr << "Setting [" << argv[2] << "] to '" << argv[3] << "' (length " << strlen(argv[3]) << ")" << endl;
|
---|
133 | content.dsize = strlen(argv[3]);
|
---|
134 | content.dptr = argv[3];
|
---|
135 | if(gdbm_store(dbf, key, content, GDBM_REPLACE) != 0)
|
---|
136 | {
|
---|
137 | cerr << "failed to set [" << argv[2] << "] to '" << argv[3] << "'" << endl;
|
---|
138 | }
|
---|
139 | }
|
---|
140 | else
|
---|
141 | {
|
---|
142 | if(gdbm_delete(dbf, key) != 0)
|
---|
143 | {
|
---|
144 | cerr << "failed to remove [" << argv[2] << "]" << endl;
|
---|
145 | }
|
---|
146 | }
|
---|
147 | gdbm_close (dbf);
|
---|
148 |
|
---|
149 | return 0;
|
---|
150 | }
|
---|