source: gsdl/trunk/common-src/src/gdbmedit/gdbmset/gdbmset.cpp@ 17883

Last change on this file since 17883 was 17883, checked in by ak19, 15 years ago

For a MAC, also need to use the GDBM_WRCREAT flag to ensure the databases archiveinf-doc and archiveinf-src exist so that they can be written to.

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 4.9 KB
Line 
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)
50using namespace ospace::std;
51#else
52using namespace std;
53#endif
54#endif
55
56void
57print_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
67int main (int argc, char *argv[])
68{
69 // The block_size parameter is ignored unless the file is a new file.
70 // If it is less than 512, dbm will use the stat block size for the file system.
71 int block_size = 0;
72 GDBM_FILE dbf;
73 datum key;
74
75 // sanity check
76 if ((argc < 3) || (argc>5)) {
77 print_usage (argv[0]);
78 exit (-1);
79 }
80
81 // open the database
82#ifdef __WIN32__
83 // On Windows need to make sure the database is generated if it does not already exist
84 // GDBM_WRCREAT flag means: open database for read/write, create if necessary
85 // http://www.rt.com/man/gdbm.3.html is the Unix man page on gdbm_open
86 // http://trac.greenstone.org/changeset/928 - the 6th param of gdbm_open (only in the
87 // Windows version of the function) was set to 1 when using WRCREAT
88 // http://www.sfr-fresh.com/unix/misc/q-7.11.tar.gz:a/q-7.11/modules/gdbm/README-Gdbm
89 dbf = gdbm_open (argv[1], block_size, GDBM_WRCREAT, 00664, NULL, 1);
90#else
91 dbf = gdbm_open (argv[1], block_size, GDBM_WRCREAT, 00664, NULL);
92 // dbf = gdbm_open (argv[1], block_size, GDBM_WRITER, 00664, NULL);
93#endif
94 if (dbf == NULL)
95 {
96 cerr << "couldn't open database connection to " << argv[1] << endl;
97 exit (-1);
98 }
99
100 key.dsize = strlen(argv[2]);
101 key.dptr = argv[2];
102
103 if(argc == 5) {
104
105 if (strcmp(argv[4],"append")==0) {
106 datum orig_content,concat_content;
107
108 //cerr << "Appending [" << argv[2] << "] to '" << argv[3] << "' (length " << strlen(argv[3]) << ")" << endl;
109 // get old value, then top up
110
111 orig_content = gdbm_fetch (dbf, key);
112
113 // append orig_content with argv[3]
114 concat_content.dsize = orig_content.dsize + strlen(argv[3]);
115 char* concat_data = new char[concat_content.dsize +1];// make room for \0 at end
116 if (orig_content.dsize>0) {
117 strncpy(concat_data,orig_content.dptr,orig_content.dsize);
118 strcpy(&concat_data[orig_content.dsize],argv[3]); // ensures \0 at end
119 }
120 else {
121 // first time key has been used
122 strcpy(concat_data,argv[3]); // ensures \0 at end
123 }
124
125 concat_content.dptr = concat_data;
126 if(gdbm_store(dbf, key, concat_content, GDBM_REPLACE) != 0)
127 {
128 cerr << "failed to set [" << argv[2] << "] to '" << concat_data << "'" << endl;
129 }
130 delete [] concat_data;
131 }
132 else {
133 print_usage (argv[0]);
134 gdbm_close (dbf);
135 exit(-1);
136 }
137 }
138 else if (argc == 4) {
139 datum content;
140
141 //cerr << "Setting [" << argv[2] << "] to '" << argv[3] << "' (length " << strlen(argv[3]) << ")" << endl;
142 content.dsize = strlen(argv[3]);
143 content.dptr = argv[3];
144 if(gdbm_store(dbf, key, content, GDBM_REPLACE) != 0)
145 {
146 cerr << "failed to set [" << argv[2] << "] to '" << argv[3] << "'" << endl;
147 }
148 }
149 else
150 {
151 if(gdbm_delete(dbf, key) != 0)
152 {
153 cerr << "failed to remove [" << argv[2] << "]" << endl;
154 }
155 }
156 gdbm_close (dbf);
157
158 return 0;
159}
Note: See TracBrowser for help on using the repository browser.