source: main/trunk/greenstone2/common-src/src/gdbmedit/gdbmset/gdbmset.cpp@ 22039

Last change on this file since 22039 was 22039, checked in by davidb, 14 years ago

Improved error reporting to GDBMedit utils

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