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

Last change on this file since 28089 was 28089, checked in by davidb, 11 years ago

Additional error message details added

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