[9926] | 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
|
---|
| 33 | void toUpperString(char *&text)
|
---|
| 34 | {
|
---|
| 35 | for (int i = 0; text[i] != '\0'; i++) {
|
---|
| 36 | text[i] = toupper(text[i]);
|
---|
| 37 | }
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 |
|
---|
| 41 | text_t gsdlCollection::getName()
|
---|
| 42 | {
|
---|
| 43 | return name;
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | bool gsdlCollection::z3950Capeable() const
|
---|
| 47 | {
|
---|
| 48 | return z3950ready;
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 |
|
---|
| 52 | bool 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 |
|
---|
| 100 | bool 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
|
---|
| 180 | bool 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 |
|
---|
| 188 | gsdlCollection::~gsdlCollection(void)
|
---|
| 189 | {
|
---|
| 190 | //cerr << "gsdlCollection destructor called" << endl;
|
---|
| 191 | }
|
---|
| 192 |
|
---|
| 193 | gsdlCollection::gsdlCollection(void)
|
---|
| 194 | {
|
---|
| 195 | //cerr << "gsdlCollection void constructor called" << endl;
|
---|
| 196 | }
|
---|
| 197 |
|
---|
| 198 | gsdlCollection::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.appendcstr("/collect/");
|
---|
| 215 | col_filename.append(collectionName);
|
---|
| 216 | col_filename.appendcstr("/etc/bib1_to_dces.cfg");
|
---|
| 217 |
|
---|
| 218 | if (!bib1_to_dc_mapping(col_filename)) {
|
---|
| 219 | text_t site_filename = gsdlhome;
|
---|
| 220 | site_filename.appendcstr("/etc/bib1_to_dces.cfg");
|
---|
| 221 |
|
---|
| 222 | if (!bib1_to_dc_mapping(site_filename)) {
|
---|
| 223 | cerr << "Unable to find bib1->dublin core mapping for "
|
---|
| 224 | << collectionName << endl;
|
---|
| 225 | cerr << "Looked for:\n " << col_filename << "\n "
|
---|
| 226 | << site_filename << endl;
|
---|
| 227 | z3950ready = false;
|
---|
| 228 | return;
|
---|
| 229 | }
|
---|
| 230 | }
|
---|
| 231 |
|
---|
| 232 | // read in the cfg file for mapping Dublin Core to Short (internal name)
|
---|
| 233 | text_t build_filename = gsdlhome;
|
---|
| 234 | build_filename.appendcstr("/collect/");
|
---|
| 235 | build_filename.append(collectionName);
|
---|
| 236 | build_filename.appendcstr("/index/build.cfg");
|
---|
| 237 |
|
---|
| 238 | if (!dc_to_short_mapping(build_filename,collectionName)) {
|
---|
| 239 | // explicitly check for error (in case in future more work is done after
|
---|
| 240 | // this
|
---|
| 241 | z3950ready = false;
|
---|
| 242 | return;
|
---|
| 243 | }
|
---|
| 244 |
|
---|
| 245 | }
|
---|
| 246 |
|
---|
| 247 |
|
---|
| 248 | void z3950Server::openLogfile(text_t extension, ofstream &out)
|
---|
| 249 | {
|
---|
| 250 | char *cfilename;
|
---|
| 251 | text_t filename;
|
---|
| 252 |
|
---|
| 253 | filename = filename_cat(gsdlhome, extension);
|
---|
| 254 | cfilename = filename.getcstr();
|
---|
| 255 |
|
---|
| 256 | out.open(cfilename);
|
---|
| 257 | delete cfilename;
|
---|
| 258 | }
|
---|
| 259 |
|
---|
| 260 | z3950Server::z3950Server(nullproto *protocol, text_t home)
|
---|
| 261 | {
|
---|
| 262 |
|
---|
| 263 | text_t dirname;
|
---|
| 264 | char * cdirname;
|
---|
[15404] | 265 | if (home.empty()) {
|
---|
[12496] | 266 | cerr << "Z3950Server Error: gsdlhome not set. \nPlease make sure"
|
---|
| 267 | << " you have a valid gsdlsite.cfg file in the directory \n"
|
---|
| 268 | << "you are running z3950server\n";
|
---|
| 269 | cerr << "Exiting..."<<endl;
|
---|
| 270 | exit(1);
|
---|
| 271 | }
|
---|
[9926] | 272 | cerr << "Z3950Server: gsdlhome = " << home << endl;
|
---|
| 273 |
|
---|
| 274 | this->gsdlhome = home;
|
---|
[12496] | 275 | //cerr << gsdlhome.getcstr() << endl;
|
---|
[9926] | 276 | dirname = filename_cat(gsdlhome, "/collect");
|
---|
| 277 | cdirname = dirname.getcstr();
|
---|
[12496] | 278 | if (!read_dir (cdirname, collections)) {
|
---|
| 279 | cerr << "Z3950Server Error: could not read directory "<<cdirname<<endl;
|
---|
| 280 | cerr << "Exiting..."<<endl;
|
---|
| 281 | exit (1);
|
---|
| 282 | }
|
---|
[9926] | 283 | delete cdirname;
|
---|
| 284 |
|
---|
| 285 | cerr << "Constructing set list server" << endl;
|
---|
| 286 | this->protocol = protocol;
|
---|
| 287 |
|
---|
| 288 | // initialise Yaz stuff for z39.50 server
|
---|
| 289 | }
|
---|
| 290 |
|
---|
| 291 | bool z3950Server::initialise()
|
---|
| 292 | {
|
---|
| 293 | ofstream logout;
|
---|
| 294 | int reply;
|
---|
| 295 | comerror_t error = noError;
|
---|
| 296 |
|
---|
| 297 | this->openLogfile("/etc/z3950out.txt", logout);
|
---|
| 298 | reply = protocol->init(error, logout);
|
---|
| 299 | logout.close();
|
---|
| 300 | return (reply != 0 ? 1 : 0);
|
---|
| 301 | }
|
---|
| 302 |
|
---|
| 303 | void z3950Server::configure(const text_t &key, const text_tarray &cfgline,
|
---|
| 304 | comerror_t &err)
|
---|
| 305 | {
|
---|
| 306 | err = noError;
|
---|
| 307 |
|
---|
| 308 | // ****
|
---|
| 309 | cerr << "Recieved " << key << " = ";
|
---|
| 310 | for (int unsigned i=0; i<cfgline.size(); i++)
|
---|
| 311 | {
|
---|
| 312 | cout << cfgline[i] << " ";
|
---|
| 313 | }
|
---|
| 314 | cout << endl;
|
---|
| 315 |
|
---|
| 316 | // DB // ****
|
---|
| 317 | if (key=="gsdlhome")
|
---|
| 318 | {
|
---|
| 319 | text_tarray cfgline;
|
---|
| 320 | cfgline.push_back (gsdlhome);
|
---|
| 321 | cerr << "Changing gsdlhome to " << gsdlhome << endl;
|
---|
| 322 | protocol->configure(key, cfgline, err);
|
---|
| 323 | }
|
---|
| 324 | else if (key=="httpdomain")
|
---|
| 325 | {
|
---|
| 326 | // Only let gsdlhome through !!!! // ****
|
---|
| 327 | cerr << "Supressing httpdomain" << endl;
|
---|
| 328 | }
|
---|
| 329 | else if (key=="httpprefix")
|
---|
| 330 | {
|
---|
| 331 | cerr << "Supressing httpprefix" << endl;
|
---|
| 332 | }
|
---|
| 333 | else
|
---|
| 334 | {
|
---|
| 335 | protocol->configure(key, cfgline,err);
|
---|
| 336 | }
|
---|
| 337 | }
|
---|
| 338 |
|
---|
| 339 | int z3950Server::run_z3950( int argc, char **argv )
|
---|
| 340 | {
|
---|
| 341 |
|
---|
| 342 | const int statserv_var = statserv_main(argc, argv, bend_init, bend_close);
|
---|
| 343 | cerr << "statserv_main returns: " << statserv_var << endl;
|
---|
| 344 | return statserv_var;
|
---|
| 345 | }
|
---|