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

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

For the new archiveinf-doc.ldb and archiveinf-src.ldb to be generated on a Windows machine, the GDBM_WRCREAT flag needed to be set. This will now create the required database unless it already exists.

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 4.8 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_WRITER, 00664, NULL);
92#endif
93 if (dbf == NULL)
94 {
95 cerr << "couldn't open database connection to " << argv[1] << endl;
96 exit (-1);
97 }
98
99 key.dsize = strlen(argv[2]);
100 key.dptr = argv[2];
101
102 if(argc == 5) {
103
104 if (strcmp(argv[4],"append")==0) {
105 datum orig_content,concat_content;
106
107 //cerr << "Appending [" << argv[2] << "] to '" << argv[3] << "' (length " << strlen(argv[3]) << ")" << endl;
108 // get old value, then top up
109
110 orig_content = gdbm_fetch (dbf, key);
111
112 // append orig_content with argv[3]
113 concat_content.dsize = orig_content.dsize + strlen(argv[3]);
114 char* concat_data = new char[concat_content.dsize +1];// make room for \0 at end
115 if (orig_content.dsize>0) {
116 strncpy(concat_data,orig_content.dptr,orig_content.dsize);
117 strcpy(&concat_data[orig_content.dsize],argv[3]); // ensures \0 at end
118 }
119 else {
120 // first time key has been used
121 strcpy(concat_data,argv[3]); // ensures \0 at end
122 }
123
124 concat_content.dptr = concat_data;
125 if(gdbm_store(dbf, key, concat_content, GDBM_REPLACE) != 0)
126 {
127 cerr << "failed to set [" << argv[2] << "] to '" << concat_data << "'" << endl;
128 }
129 delete [] concat_data;
130 }
131 else {
132 print_usage (argv[0]);
133 gdbm_close (dbf);
134 exit(-1);
135 }
136 }
137 else if (argc == 4) {
138 datum content;
139
140 //cerr << "Setting [" << argv[2] << "] to '" << argv[3] << "' (length " << strlen(argv[3]) << ")" << endl;
141 content.dsize = strlen(argv[3]);
142 content.dptr = argv[3];
143 if(gdbm_store(dbf, key, content, GDBM_REPLACE) != 0)
144 {
145 cerr << "failed to set [" << argv[2] << "] to '" << argv[3] << "'" << endl;
146 }
147 }
148 else
149 {
150 if(gdbm_delete(dbf, key) != 0)
151 {
152 cerr << "failed to remove [" << argv[2] << "]" << endl;
153 }
154 }
155 gdbm_close (dbf);
156
157 return 0;
158}
Note: See TracBrowser for help on using the repository browser.