source: main/trunk/greenstone2/common-src/src/gdbmedit/txt2db/txt2db.cpp@ 23128

Last change on this file since 23128 was 23128, checked in by kjdon, 14 years ago

made the gdbm_delete error a bit more informative

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 5.2 KB
Line 
1/**********************************************************************
2 *
3 * txt2db.cpp --
4 * A component of the Greenstone digital library software
5 * from the New Zealand Digital Library Project at the
6 * University of Waikato, New Zealand.
7 *
8 * Copyright (C) 1999 The New Zealand Digital Library Project
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
27#ifdef __WIN32__
28#include "autoconf.h"
29#include "systems.h"
30#include "gdbmconst.h"
31#include "gdbm.h"
32
33#else
34#include <gdbm.h>
35#endif
36
37#include "gsdlconf.h"
38#include "text_t.h"
39#include <stdlib.h>
40#include <cstring>
41
42#if defined(GSDL_USE_OBJECTSPACE)
43# include <ospace\std\iostream>
44#elif defined(GSDL_USE_IOS_H)
45# include <iostream.h>
46#else
47# include <iostream>
48#endif
49
50
51void print_usage (char *program_name) {
52 cerr << "usage: " << program_name << " [options] database-name" << endl << endl;
53 cerr << "options:" << endl;
54 cerr << " -append append to existing database" << endl << endl;
55}
56
57
58int main (int argc, char *argv[]) {
59 int block_size = 0;
60 GDBM_FILE dbf;
61 char c;
62 text_t key;
63 text_t value;
64 text_t tmp;
65 int num_dashes = 0;
66
67 // sanity check
68 if (argc != 2 && argc != 3) {
69 print_usage (argv[0]);
70 exit (0);
71 }
72
73 char *dbname;
74 int append = 0;
75 int delkey = 0;
76
77 if (argc == 3) {
78 if (strcmp (argv[1], "-append") == 0) {
79 append = 1;
80 dbname = argv[2];
81 } else {
82 cerr << argv[1] << " is not a valid option." << endl << endl;
83 print_usage (argv[0]);
84 exit (0);
85 }
86 } else dbname = argv[1];
87
88
89 // open the database
90 // note that GDBM_FAST is obsolete on newer versions of gdbm
91 int read_write = GDBM_NEWDB | GDBM_FAST;
92 if (append) read_write = GDBM_WRCREAT | GDBM_FAST;
93
94#ifdef __WIN32__
95 dbf = gdbm_open (dbname, block_size, read_write, 00664, NULL, 1);
96#else
97 dbf = gdbm_open (dbname, block_size, read_write, 00664, NULL);
98#endif
99 if (dbf == NULL) {
100 cerr << "couldn't create " << dbname << endl;
101 exit (0);
102 }
103
104 cin.get(c);
105 while (!cin.eof()) {
106 num_dashes = 0;
107 key = "";
108 value = "";
109
110 // Parse out 'key' from [key]\n
111
112 // scan for first occurrence of [
113 while (!cin.eof() && c != '[') cin.get(c);
114
115 if (!cin.eof()) cin.get(c); // skip [
116
117 // now look for closing ], building up 'key' as we go
118 while (!cin.eof() && c != ']') {
119 key.push_back ((unsigned char)c);
120 cin.get(c);
121 }
122
123 if (!cin.eof()) {
124 // most likely an eol char, but if '-', then signifies record
125 // is to be deleted, not added
126 cin.get(c);
127 if (c == '-') {
128 delkey = 1;
129 }
130 else {
131 delkey = 0;
132 }
133 }
134 while (!cin.eof() && (c == '\n' || c == '\r')) cin.get(c);
135
136 // look for 70 dashes
137 tmp = "";
138 while (!cin.eof() && (num_dashes < 70)) {
139 if (c == '\n') {
140 tmp.push_back ((unsigned char)c);
141 num_dashes = 0;
142
143 } else if (c == '\r') {
144 // Here we are able to process both Windows-specific text files
145 // (containing carriage-return, newline) and Linux text files
146 // (containing only newline characters) by ignoring the Windows'
147 // carriage-return altogether so that we produce a uniform database
148 // file from either system's type of text file.
149 // If we don't ignore the carriage return here, txt.gz files
150 // produced on Windows cause a GS library running on Linux to break.
151 num_dashes = 0;
152
153 } else if (c == '-') {
154 tmp.push_back ((unsigned char)c);
155 ++num_dashes;
156
157 } else {
158 value += tmp;
159 value.push_back ((unsigned char)c);
160 tmp = "";
161 num_dashes = 0;
162 }
163 cin.get(c);
164 }
165
166 // if the key is not an empty string store this key-value pair
167 if (!key.empty()) {
168 // convert key to a datum datatype
169 datum key_data;
170 key_data.dptr = key.getcstr();
171 if (key_data.dptr == NULL) {
172 cerr << "NULL key_data.dptr" << endl;
173 exit (0);
174 }
175 key_data.dsize = strlen(key_data.dptr);
176
177 if (delkey) {
178 // delete the given key
179 if (gdbm_delete(dbf, key_data) < 0) {
180 cerr << "gdbm_delete returned an error trying to delete key " << key << ": " << gdbm_strerror (gdbm_errno) <<endl;
181 }
182 }
183 else {
184
185 // add/append
186
187 // convert value to a datum datatype
188 datum value_data;
189 value_data.dptr = value.getcstr();
190 if (value_data.dptr == NULL) {
191 cerr << "NULL value_data.dptr" << endl;
192 exit (0);
193 }
194 value_data.dsize = strlen(value_data.dptr);
195
196 // store the value
197 if (gdbm_store (dbf, key_data, value_data, GDBM_REPLACE) < 0) {
198 cerr << "gdbm_store returned an error" << endl;
199 exit (0);
200 }
201
202
203 free(value_data.dptr);
204 }
205
206 free(key_data.dptr);
207 }
208 }
209
210 gdbm_close (dbf);
211
212 return 0;
213}
Note: See TracBrowser for help on using the repository browser.