source: main/trunk/greenstone2/runtime-src/src/z3950/z3950_to_gsdl.cpp@ 27220

Last change on this file since 27220 was 19611, checked in by kjdon, 15 years ago

changed path composition to use filename_cat instead of append, and changed location of z3950 config files to etc/packages/z3950

File size: 8.5 KB
Line 
1/**********************************************************************
2 *
3 * z3950_to_gsdl.cpp --
4 * Copyright (C) 2000 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#include "z3950parser.h"
27#include "z3950server.h"
28#include "z3950_to_gsdl.h"
29
30// converts a char* to uppercase, I wouldn't be
31// writing this if I could find an existing
32// function to do it
33void toUpperString(char *&text)
34{
35 for (int i = 0; text[i] != '\0'; i++) {
36 text[i] = toupper(text[i]);
37 }
38}
39
40
41text_t gsdlCollection::getName()
42{
43 return name;
44}
45
46bool gsdlCollection::z3950Capeable() const
47{
48 return z3950ready;
49}
50
51
52bool gsdlCollection::bib1_to_dc_mapping(text_t& filename)
53{
54 ifstream inFile;
55 int input_Bib1;
56 text_t input_DCES;
57 text_t input_Short;
58
59 char* filename_cstr = filename.getcstr();
60 inFile.open(filename_cstr, ios::in);
61 delete [] filename_cstr;
62
63 if (!inFile) {
64 return false;
65 }
66
67 const int BUFFERSIZE = 1024;
68 char buffer[BUFFERSIZE];
69
70 // read in pairs of values (mappings from Bib-1 to DCES)
71 while (!inFile.eof()) {
72 char input_DCES_tmp[BUFFERSIZE];
73
74 inFile.getline(buffer, BUFFERSIZE, '\n');
75 if (buffer[0] == '#') {
76 // ignore comments (# ...)
77 continue;
78 }
79
80 sscanf(buffer,"%d %s",&input_Bib1, input_DCES_tmp);
81
82 input_DCES = input_DCES_tmp;
83
84 Bib1_to_DCES[input_Bib1] = input_DCES;
85
86 if (z3950_verbosity_>2) {
87 cerr << "(" << input_Bib1 << "/" << input_DCES << ") ";
88 }
89 }
90
91 if (z3950_verbosity_>2) {
92 cerr << endl;
93 }
94 inFile.close();
95
96 return true;
97}
98
99
100bool gsdlCollection::dc_to_short_mapping(text_t& filename,
101 text_t& collectionName)
102{
103 ifstream inFileBuild;
104 text_t input_DCES;
105 text_t input_Short;
106
107 char* filename_cstr = filename.getcstr();
108 inFileBuild.open(filename_cstr, ios::in);
109 delete [] filename_cstr;
110
111 if (!inFileBuild) {
112 if (z3950_verbosity_>2) {
113 cerr << "Warning: Unable to open: " << filename << endl;
114 cerr << " Collection " << collectionName << " might not be built" << endl;
115 }
116 return false;
117 }
118
119 const int BUFFERSIZE = 1024;
120 char buffer[BUFFERSIZE];
121
122 // find the indexfieldmap line
123 do {
124 inFileBuild >> buffer;
125 if (inFileBuild.eof()) {
126 if (z3950_verbosity_>1) {
127 cerr << "Warning: Did not find indexfieldmap in build.cfg for "
128 << collectionName << endl;
129 }
130 return false;
131 }
132 } while (strcmp(buffer, "indexfieldmap") != 0);
133
134 // at this stage, buffer contains "indexfieldmap",
135 // if the file didn't contain it, would have returned above
136 inFileBuild >> buffer;
137 while (strstr(buffer, "->") != NULL) {
138 // buffer contains a DCES->Short mapping
139 input_DCES.clear();
140 input_Short.clear();
141
142 // copy in DCES
143 int i;
144 for (i = 0; buffer[i] != '-'; i++) {
145 input_DCES.appendcarr(&(buffer[i]), 1);
146 }
147
148 // skip over "->"
149 i+=2;
150
151 // copy in Short
152 for (; isalpha(buffer[i]); i++) {
153 input_Short.appendcarr(&(buffer[i]), 1);
154 }
155
156 // add to map
157 DCES_to_Short[input_DCES] = input_Short;
158
159 if (z3950_verbosity_>1) {
160 cerr << "<" << input_DCES << "/" << input_Short << "> ";
161 }
162
163 inFileBuild >> buffer;
164 }
165
166 inFileBuild.close();
167
168 if (z3950_verbosity_>1) {
169 cerr << endl;
170 }
171
172 return true;
173}
174
175
176
177
178 // converts a Bib1 field number to a short index name,
179 // returns true if a mapping existed, false otherwise
180bool gsdlCollection::getFieldArg(int Bib1, text_t &Short)
181{
182 if (Bib1_to_DCES.count(Bib1) < 1) return false;
183 if (DCES_to_Short.count(Bib1_to_DCES[Bib1]) < 1) return false;
184 Short = DCES_to_Short[Bib1_to_DCES[Bib1]];
185 return true;
186}
187
188gsdlCollection::~gsdlCollection(void)
189{
190 //cerr << "gsdlCollection destructor called" << endl;
191}
192
193gsdlCollection::gsdlCollection(void)
194{
195 //cerr << "gsdlCollection void constructor called" << endl;
196}
197
198gsdlCollection::gsdlCollection(text_t collectionName, text_t gsdlhome)
199{
200
201 //char *filename_cstr;
202 //ifstream inFile;
203 //int inFileSize = 0;
204 z3950ready = true;
205
206 // int input_Bib1;
207 //text_t input_DCES;
208 //text_t input_Short;
209
210 name = collectionName;
211
212 // read in the cfg file for Bib1_to_DCES
213 text_t col_filename = gsdlhome;
214 col_filename = filename_cat(gsdlhome, "collect", collectionName, "etc", "bib1-mapping.txt");
215
216 if (!bib1_to_dc_mapping(col_filename)) {
217 text_t site_filename = filename_cat(gsdlhome, "etc", "packages", "z3950", "bib1-mapping.txt");
218
219 if (!bib1_to_dc_mapping(site_filename)) {
220 cerr << "Unable to find bib1 attribute mapping for "
221 << collectionName << endl;
222 cerr << "Looked for:\n " << col_filename << "\n "
223 << site_filename << endl;
224 z3950ready = false;
225 return;
226 }
227 }
228
229 // read in the cfg file for mapping Dublin Core to Short (internal name)
230 text_t build_filename = filename_cat(gsdlhome, "collect", collectionName, "index", "build.cfg");
231
232 if (!dc_to_short_mapping(build_filename,collectionName)) {
233 // explicitly check for error (in case in future more work is done after
234 // this
235 z3950ready = false;
236 return;
237 }
238
239}
240
241
242void z3950Server::openLogfile(text_t extension, ofstream &out)
243{
244 char *cfilename;
245 text_t filename;
246
247 filename = filename_cat(gsdlhome, extension);
248 cfilename = filename.getcstr();
249
250 out.open(cfilename);
251 delete cfilename;
252}
253
254z3950Server::z3950Server(nullproto *protocol, text_t home)
255{
256
257 text_t dirname;
258 char * cdirname;
259 if (home.empty()) {
260 cerr << "Z3950Server Error: gsdlhome not set. \nPlease make sure"
261 << " you have a valid gsdlsite.cfg file in the directory \n"
262 << "you are running z3950server\n";
263 cerr << "Exiting..."<<endl;
264 exit(1);
265 }
266 cerr << "Z3950Server: gsdlhome = " << home << endl;
267
268 this->gsdlhome = home;
269 //cerr << gsdlhome.getcstr() << endl;
270 dirname = filename_cat(gsdlhome, "collect");
271 cdirname = dirname.getcstr();
272 if (!read_dir (cdirname, collections)) {
273 cerr << "Z3950Server Error: could not read directory "<<cdirname<<endl;
274 cerr << "Exiting..."<<endl;
275 exit (1);
276 }
277 delete cdirname;
278
279 cerr << "Constructing set list server" << endl;
280 this->protocol = protocol;
281
282 // initialise Yaz stuff for z39.50 server
283}
284
285bool z3950Server::initialise()
286{
287 ofstream logout;
288 int reply;
289 comerror_t error = noError;
290
291 // openLogfile does a filename_cat with this path, changing
292 // the / appropriately.
293 this->openLogfile("/etc/packages/z3950/z3950out.txt", logout);
294 reply = protocol->init(error, logout);
295 logout.close();
296 return (reply != 0 ? 1 : 0);
297}
298
299void z3950Server::configure(const text_t &key, const text_tarray &cfgline,
300 comerror_t &err)
301{
302 err = noError;
303
304 // ****
305 cerr << "Recieved " << key << " = ";
306 for (int unsigned i=0; i<cfgline.size(); i++)
307 {
308 cout << cfgline[i] << " ";
309 }
310 cout << endl;
311
312 // DB // ****
313 if (key=="gsdlhome")
314 {
315 text_tarray cfgline;
316 cfgline.push_back (gsdlhome);
317 cerr << "Changing gsdlhome to " << gsdlhome << endl;
318 protocol->configure(key, cfgline, err);
319 }
320 else if (key=="httpdomain")
321 {
322 // Only let gsdlhome through !!!! // ****
323 cerr << "Supressing httpdomain" << endl;
324 }
325 else if (key=="httpprefix")
326 {
327 cerr << "Supressing httpprefix" << endl;
328 }
329 else
330 {
331 protocol->configure(key, cfgline,err);
332 }
333}
334
335int z3950Server::run_z3950( int argc, char **argv )
336{
337
338 const int statserv_var = statserv_main(argc, argv, bend_init, bend_close);
339 cerr << "statserv_main returns: " << statserv_var << endl;
340 return statserv_var;
341}
Note: See TracBrowser for help on using the repository browser.