source: gsdl/trunk/src/z3950/z3950server.cpp@ 15507

Last change on this file since 15507 was 15498, checked in by mdewsnip, 16 years ago

The "src/z3950" directory now contains all the z3950server files, and an updated Makefile to compile them.

File size: 26.7 KB
Line 
1
2
3// Standard headers
4
5#if defined(GSDL_USE_OBJECTSPACE)
6# include <ospace\std\iostream>
7# include <ospace\std\fstream>
8# include <ospace\std\sstream>
9#elif defined(GSDL_USE_IOS_H)
10# include <iostream.h>
11# include <fstream.h>
12# include <strstream.h>
13#else
14# include <iostream>
15# include <fstream>
16# include <sstream>
17#endif
18
19#include <string>
20#include <list>
21
22#include <stdio.h>
23#include <stdlib.h>
24#include <ctype.h>
25#include <time.h>
26
27// Greenstone headers
28#include "fileutil.h"
29#include "comtypes.h"
30#include "nullproto.h"
31
32// Z39.50 server headers
33#include "z3950parser.h"
34#include "z3950explain.h"
35
36#include "z3950server.h"
37
38// Dublin Core -> MARC (d2m) headers and source files
39#include "d2m4gs.h"
40
41// these globals are local to each thread,
42// one Z39.50 connection = one thread.
43text_t gsdlhome; // this is specified externally when Greenstone is installed
44list<FilterResponse_t> Response_tlist; // list of response sets
45list<text_t> Response_tlist_colnames; // collection names for the above
46list<int> Response_tlist_sizes; // sizes (number of records) for the above
47z3950Server *Server = NULL;
48collectset *Cservers = NULL;
49nullproto *Protocol = NULL;
50
51map<text_t, gsdlCollection> Collection_map; // info and tools for collections
52map<text_t, text_t> Resultsets; // mapping from ResultSetId to GSQuery
53
54int z3950_verbosity_ = 3;
55
56// *** initialize things here ***
57bend_initresult *bend_init(bend_initrequest *q)
58{
59 // *** called when client connects to the server ***
60 if (z3950_verbosity_ > 1) {
61 cerr << "Entering bend_init" << endl;
62 }
63
64 Protocol = new nullproto();
65 Cservers = new collectset(gsdlhome);
66 Protocol->set_collectset(Cservers);
67
68 cerr << "Starting Z39.50 Server ..." << endl;
69
70 Server = new z3950Server(Protocol, gsdlhome);
71 cerr << "Server constructed" << endl;
72
73 const bool status = Server->initialise();
74 cerr << "Initialised server: return status = " << status << endl;
75
76 text_tarray collist;
77 comerror_t err;
78 Protocol->get_collection_list( collist, err, cout );
79
80 // display the list of available Greenstone collections
81 text_tarray::iterator cols_here=collist.begin();
82 text_tarray::iterator cols_end=collist.end();
83 while (cols_here != cols_end) {
84 gsdlCollection tmpcol(*cols_here, gsdlhome);
85
86 Collection_map[*cols_here] = tmpcol;
87
88 if (tmpcol.z3950Capeable()) {
89 cerr << " " << tmpcol.getName() << "\tis Z39.50 capable" << endl;
90 }
91 cols_here++;
92 }
93
94
95 bend_initresult *r = (bend_initresult *) odr_malloc (q->stream, sizeof(*r));
96 //static char *dummy = "Hej fister"; // what's this for?
97
98 r->errcode = 0;
99 r->errstring = 0;
100 r->handle = Protocol;
101 q->bend_sort = ztest_sort; /* register sort handler */
102 q->bend_search = ztest_search; /* register search handler */
103 q->bend_present = ztest_present; /* register present handle */
104 q->bend_esrequest = ztest_esrequest;
105 q->bend_delete = ztest_delete;
106 q->bend_fetch = ztest_fetch;
107 q->bend_scan = ztest_scan;
108
109 return r;
110}
111
112// *** perform query, get number of hits ***
113int ztest_search (void *handle, bend_search_rr *rr)
114{
115 // *** called when client issues a "find" command
116 if (z3950_verbosity_>1) {
117 cerr << "Entering ztest_search" << endl;
118 }
119
120 Response_tlist.clear();
121 Response_tlist_colnames.clear();
122 Response_tlist_sizes.clear();
123
124 comerror_t err;
125 FilterRequest_t request;
126
127 request.filterName = "QueryFilter";
128 request.filterResultOptions = FROID | FRtermFreq | FRmetadata;
129
130 OptionValue_t option;
131
132 // how many results to get. grouping all results together into
133 // one set instead of breaking up into "pages".
134 // note: see if Z39.50 has a concept of pages.
135 option.name = "StartResults";
136 option.value = "1";
137 request.filterOptions.push_back (option);
138 option.name = "EndResults";
139 option.value = MAXHITS;
140 request.filterOptions.push_back (option);
141 option.name = "Maxdocs";
142 option.value = MAXHITS;
143 request.filterOptions.push_back (option);
144
145 text_t GSQuery = ZQueryToGSQuery(rr);
146 option.name = "Term";
147 option.value = GSQuery;
148 request.filterOptions.push_back (option);
149 Resultsets[Resultsets.size()+1] = GSQuery;
150
151 option.name = "QueryType";
152 option.value = "boolean";
153 request.filterOptions.push_back (option);
154
155 option.name = "MatchMode";
156 option.value = "all";
157 request.filterOptions.push_back (option);
158
159 option.name = "Casefold";
160 option.value = "true";
161 request.filterOptions.push_back (option);
162
163 option.name = "Stem";
164 option.value = "false";
165 request.filterOptions.push_back (option);
166
167 option.name = "AccentFold";
168 option.value = "false";
169 request.filterOptions.push_back (option);
170
171 // MG queries don't need this, MGPP queries do.
172 // doesn't do any harm to include it anyway.
173 // note: not all collections have document-level granularity...
174 option.name = "Level";
175 option.value = "Doc";
176 request.filterOptions.push_back (option);
177
178 // Z39.50 supports the searching of multiple databases
179 rr->hits = 0;
180 for (int i = 0; i < rr->num_bases; i++) {
181 FilterResponse_t response;
182 cerr << "Searching basename = " << rr->basenames[i] << endl;
183 // intercept Explain requests
184 if (strcmp(rr->basenames[i], "IR-Explain-1")==0) {
185 //ColInfoResponse_t collectinfo;
186 //Protocol->get_collectinfo("csbib", collectinfo, err, cerr);
187 //response.numDocs = 1;
188 //ResultDocInfo_t docinfo;
189 //docinfo.metadata["foo"].values.push_back("bar");
190 getExplainInfo(rr, response, err);
191 //response.docInfo.push_back(docinfo);
192 } else {
193 cerr << "calling filter..." << endl;
194 Protocol->filter(rr->basenames[i], request, response, err, cerr);
195 cerr << "returned from filter." << endl;
196 if (response.numDocs > MAXHITS) {
197 response.numDocs = MAXHITS;
198 }
199 }
200 rr->hits += response.numDocs;
201 Response_tlist.push_back(response);
202 text_t basename = rr->basenames[i];
203 Response_tlist_colnames.push_back(basename);
204 Response_tlist_sizes.push_back(response.numDocs);
205 cerr << "search complete." << endl;
206 }
207
208 if (rr->hits > MAXHITS) rr->hits = MAXHITS;
209 return 0;
210}
211
212// *** extra code for showing results, not needed ***
213int ztest_present (void *handle, bend_present_rr *rr)
214{
215 // *** called when client issues a "show" command, 1st of 5
216 if (z3950_verbosity_>1) {
217 cerr << "entering ztest_present" << endl;
218 }
219 return 0;
220}
221
222int ztest_esrequest (void *handle, bend_esrequest_rr *rr)
223{
224
225 if (z3950_verbosity_>1) {
226 cerr << "Entering ztest_esrequest" << endl;
227 }
228 yaz_log(LOG_LOG, "function: %d", *rr->esr->function);
229 if (rr->esr->packageName)
230 yaz_log(LOG_LOG, "packagename: %s", rr->esr->packageName);
231 yaz_log(LOG_LOG, "Waitaction: %d", *rr->esr->waitAction);
232
233 if (!rr->esr->taskSpecificParameters)
234 {
235 yaz_log (LOG_WARN, "No task specific parameters");
236 }
237 else if (rr->esr->taskSpecificParameters->which == Z_External_itemOrder)
238 {
239 Z_ItemOrder *it = rr->esr->taskSpecificParameters->u.itemOrder;
240 yaz_log (LOG_LOG, "Received ItemOrder");
241 switch (it->which)
242 {
243#ifdef ASN_COMPILED
244 case Z_IOItemOrder_esRequest:
245#else
246 case Z_ItemOrder_esRequest:
247#endif
248 {
249 Z_IORequest *ir = it->u.esRequest;
250 Z_IOOriginPartToKeep *k = ir->toKeep;
251 Z_IOOriginPartNotToKeep *n = ir->notToKeep;
252
253 if (k && k->contact)
254 {
255 if (k->contact->name)
256 yaz_log(LOG_LOG, "contact name %s", k->contact->name);
257 if (k->contact->phone)
258 yaz_log(LOG_LOG, "contact phone %s", k->contact->phone);
259 if (k->contact->email)
260 yaz_log(LOG_LOG, "contact email %s", k->contact->email);
261 }
262 if (k->addlBilling)
263 {
264 yaz_log(LOG_LOG, "Billing info (not shown)");
265 }
266
267 if (n->resultSetItem)
268 {
269 yaz_log(LOG_LOG, "resultsetItem");
270 yaz_log(LOG_LOG, "setId: %s", n->resultSetItem->resultSetId);
271 yaz_log(LOG_LOG, "item: %d", *n->resultSetItem->item);
272 }
273#ifdef ASN_COMPILED
274 if (n->itemRequest)
275 {
276 Z_External *r = (Z_External*) n->itemRequest;
277 ILL_ItemRequest *item_req = 0;
278 ILL_Request *ill_req = 0;
279 if (r->direct_reference)
280 {
281 oident *ent = oid_getentbyoid(r->direct_reference);
282 if (ent)
283 yaz_log(LOG_LOG, "OID %s", ent->desc);
284 if (ent && ent->value == VAL_ISO_ILL_1)
285 {
286 yaz_log (LOG_LOG, "ItemRequest");
287 if (r->which == ODR_EXTERNAL_single)
288 {
289 odr_setbuf(rr->decode,
290 (char*)(r->u.single_ASN1_type->buf),
291 r->u.single_ASN1_type->len, 0);
292
293 if (!ill_ItemRequest (rr->decode, &item_req, 0, 0))
294 {
295 yaz_log (LOG_LOG,
296 "Couldn't decode ItemRequest %s near %d",
297 odr_errmsg(odr_geterror(rr->decode)),
298 odr_offset(rr->decode));
299 yaz_log(LOG_LOG, "PDU dump:");
300 odr_dumpBER(yaz_log_file(),
301 (const char*)(r->u.single_ASN1_type->buf),
302 r->u.single_ASN1_type->len);
303 }
304 if (rr->print)
305 {
306 ill_ItemRequest (rr->print, &item_req, 0,
307 "ItemRequest");
308 odr_reset (rr->print);
309 }
310 }
311 if (!item_req && r->which == ODR_EXTERNAL_single)
312 {
313 yaz_log (LOG_LOG, "ILLRequest");
314 odr_setbuf(rr->decode,
315 (char*)(r->u.single_ASN1_type->buf),
316 r->u.single_ASN1_type->len, 0);
317
318 if (!ill_Request (rr->decode, &ill_req, 0, 0))
319 {
320 yaz_log (LOG_LOG,
321 "Couldn't decode ILLRequest %s near %d",
322 odr_errmsg(odr_geterror(rr->decode)),
323 odr_offset(rr->decode));
324 yaz_log(LOG_LOG, "PDU dump:");
325 odr_dumpBER(yaz_log_file(),
326 (const char*)(r->u.single_ASN1_type->buf),
327 r->u.single_ASN1_type->len);
328 }
329 if (rr->print)
330 {
331 ill_Request (rr->print, &ill_req, 0,
332 "ILLRequest");
333 odr_reset (rr->print);
334 }
335 }
336 }
337 }
338 if (item_req)
339 {
340 yaz_log (LOG_LOG, "ILL protocol version = %d",
341 *item_req->protocol_version_num);
342 }
343 }
344#endif
345 }
346 break;
347 }
348 }
349 else if (rr->esr->taskSpecificParameters->which == Z_External_update)
350 {
351 Z_IUUpdate *up = rr->esr->taskSpecificParameters->u.update;
352 yaz_log (LOG_LOG, "Received DB Update");
353 if (up->which == Z_IUUpdate_esRequest)
354 {
355 Z_IUUpdateEsRequest *esRequest = up->u.esRequest;
356 Z_IUOriginPartToKeep *toKeep = esRequest->toKeep;
357 Z_IUSuppliedRecords *notToKeep = esRequest->notToKeep;
358
359 yaz_log (LOG_LOG, "action");
360 if (toKeep->action)
361 {
362 switch (*toKeep->action)
363 {
364 case Z_IUOriginPartToKeep_recordInsert:
365 yaz_log (LOG_LOG, " recordInsert");
366 break;
367 case Z_IUOriginPartToKeep_recordReplace:
368 yaz_log (LOG_LOG, " recordUpdate");
369 break;
370 case Z_IUOriginPartToKeep_recordDelete:
371 yaz_log (LOG_LOG, " recordDelete");
372 break;
373 case Z_IUOriginPartToKeep_elementUpdate:
374 yaz_log (LOG_LOG, " elementUpdate");
375 break;
376 case Z_IUOriginPartToKeep_specialUpdate:
377 yaz_log (LOG_LOG, " specialUpdate");
378 break;
379 default:
380 yaz_log (LOG_LOG, " unknown (%d)", *toKeep->action);
381 }
382 }
383 if (toKeep->databaseName)
384 {
385 yaz_log (LOG_LOG, "database: %s", toKeep->databaseName);
386 if (!strcmp(toKeep->databaseName, "fault"))
387 {
388 rr->errcode = 109;
389 rr->errstring = toKeep->databaseName;
390 }
391 if (!strcmp(toKeep->databaseName, "accept"))
392 rr->errcode = -1;
393 }
394 if (notToKeep)
395 {
396 int i;
397 for (i = 0; i < notToKeep->num; i++)
398 {
399 Z_External *rec = notToKeep->elements[i]->record;
400
401 if (rec->direct_reference)
402 {
403 struct oident *oident;
404 oident = oid_getentbyoid(rec->direct_reference);
405 if (oident)
406 yaz_log (LOG_LOG, "record %d type %s", i,
407 oident->desc);
408 }
409 switch (rec->which)
410 {
411 case Z_External_sutrs:
412 if (rec->u.octet_aligned->len > 170)
413 yaz_log (LOG_LOG, "%d bytes:\n%.168s ...",
414 rec->u.sutrs->len,
415 rec->u.sutrs->buf);
416 else
417 yaz_log (LOG_LOG, "%d bytes:\n%s",
418 rec->u.sutrs->len,
419 rec->u.sutrs->buf);
420 break;
421 case Z_External_octet :
422 if (rec->u.octet_aligned->len > 170)
423 yaz_log (LOG_LOG, "%d bytes:\n%.168s ...",
424 rec->u.octet_aligned->len,
425 rec->u.octet_aligned->buf);
426 else
427 yaz_log (LOG_LOG, "%d bytes\n%s",
428 rec->u.octet_aligned->len,
429 rec->u.octet_aligned->buf);
430 }
431 }
432 }
433 }
434 }
435 else
436 {
437 yaz_log (LOG_WARN, "Unknown Extended Service(%d)",
438 rr->esr->taskSpecificParameters->which);
439
440 }
441 return 0;
442}
443
444int ztest_delete (void *handle, bend_delete_rr *rr)
445{
446 // *** called when client issues a "delete" command
447 if (z3950_verbosity_>1) {
448 cerr << "Entering ztest_delete" << endl;
449 }
450 if (rr->num_setnames == 1 && !strcmp (rr->setnames[0], "1"))
451 rr->delete_status = Z_DeleteStatus_success;
452 else
453 rr->delete_status = Z_DeleteStatus_resultSetDidNotExist;
454 return 0;
455}
456
457// *** do we want to sort the results? ***
458/* Our sort handler really doesn't sort... */
459int ztest_sort (void *handle, bend_sort_rr *rr)
460{
461 if (z3950_verbosity_>1) {
462 cerr << "entering ztest_sort" << endl;
463 }
464 rr->errcode = 0;
465 rr->sort_status = Z_SortStatus_success;
466 return 0;
467}
468
469static int atoin (const char *buf, int n)
470{
471 // *** called when client issues a "show" command, 5th of 5
472 // *** NOT called if the "show" command has an invalid argument
473 if (z3950_verbosity_>1) {
474 cerr << "Entering atoin" << endl;
475 }
476 int val = 0;
477 while (--n >= 0)
478 {
479 if (isdigit(*buf))
480 val = val*10 + (*buf - '0');
481 buf++;
482 }
483 return val;
484}
485
486
487bool get_sutrs_record (int recnum, text_t &MetadataResult, text_t &CollectionName)
488{
489 if (z3950_verbosity_>1) {
490 cerr << "Entering get_sutrs_record" << endl;
491 }
492 int total_records = 0;
493
494 list<FilterResponse_t>::iterator RESPONSE_here = Response_tlist.begin();
495 list<FilterResponse_t>::iterator RESPONSE_end = Response_tlist.end();
496 list<text_t>::iterator RESPONSE_COLNAMES_here = Response_tlist_colnames.begin();
497 list<text_t>::iterator RESPONSE_COLNAMES_end = Response_tlist_colnames.end();
498 list<int>::iterator RESPONSE_SIZES_here = Response_tlist_sizes.begin();
499 list<int>::iterator RESPONSE_SIZES_end = Response_tlist_sizes.end();
500
501 RESPONSE_here = Response_tlist.begin();
502 RESPONSE_end = Response_tlist.end();
503 while (RESPONSE_here != RESPONSE_end) {
504 total_records += (*RESPONSE_here).numDocs;
505 RESPONSE_here++;
506 }
507
508 // check that the index is within bounds
509 if (recnum < 1 || recnum > total_records) {
510 cerr << "get_sutrs_record failed" << endl;
511 return false; // failed
512 } else {
513 FilterResponse_t response;
514 // iterate through Response_tlist to find the right response
515 RESPONSE_here = Response_tlist.begin();
516 RESPONSE_end = Response_tlist.end();
517 RESPONSE_COLNAMES_here = Response_tlist_colnames.begin();
518 RESPONSE_COLNAMES_end = Response_tlist_colnames.end();
519 RESPONSE_SIZES_here = Response_tlist_sizes.begin();
520 RESPONSE_SIZES_end = Response_tlist_sizes.end();
521 int n = 0;
522
523 while (RESPONSE_here != RESPONSE_end) {
524 n += *RESPONSE_SIZES_here;
525 if (n >= recnum) {
526 response = *RESPONSE_here;
527 CollectionName = *RESPONSE_COLNAMES_here;
528 recnum = *RESPONSE_SIZES_here - (n - recnum);
529 break;
530 }
531 // these should all have the same number of elements,
532 // so only need to check that one is within bounds.
533 // may want to combine these into a larger data structure later.
534 RESPONSE_here++;
535 RESPONSE_COLNAMES_here++;
536 RESPONSE_SIZES_here++;
537 }
538
539 // locate the desired record in the response
540
541 MetadataInfo_tmap::iterator METAFIELD_here = response.docInfo[recnum-1].metadata.begin();
542 MetadataInfo_tmap::iterator METAFIELD_end = response.docInfo[recnum-1].metadata.end();
543
544 while (METAFIELD_here!=METAFIELD_end) {
545 text_tarray::iterator METAVALUE_here = METAFIELD_here->second.values.begin();
546 text_tarray::iterator METAVALUE_end = METAFIELD_here->second.values.end();
547 while (METAVALUE_here!=METAVALUE_end) {
548 // construct a text_t containing the record:
549 // there are multiple metadata fields,
550 // each field can have multiple values.
551 /*
552 // don't include fields starting with a lowercase letter,
553 // these are system fields.
554 // later: include these anyway, since Explain fields start with lowercase letters
555 if (METAFIELD_here->first[0] >= 'A' &&
556 METAFIELD_here->first[0] <= 'Z') {
557 */
558 MetadataResult += METAFIELD_here->first; // field name
559 MetadataResult += " ";
560 MetadataResult += *METAVALUE_here; // field contents
561 MetadataResult += "\n";
562 //}
563 METAVALUE_here++;
564 }
565 METAFIELD_here++;
566 }
567 return true; // succeeded
568 }
569}
570
571bool get_marc_record (int recnum, list<metatag*> &Metatag_list, text_t &CollectionName)
572{
573 if (z3950_verbosity_>1) {
574 cerr << "Entering get_marc_record" << endl;
575 }
576 int total_records = 0;
577
578 list<FilterResponse_t>::iterator RESPONSE_here = Response_tlist.begin();
579 list<FilterResponse_t>::iterator RESPONSE_end = Response_tlist.end();
580 list<text_t>::iterator RESPONSE_COLNAMES_here = Response_tlist_colnames.begin();
581 list<text_t>::iterator RESPONSE_COLNAMES_end = Response_tlist_colnames.end();
582 list<int>::iterator RESPONSE_SIZES_here = Response_tlist_sizes.begin();
583 list<int>::iterator RESPONSE_SIZES_end = Response_tlist_sizes.end();
584
585 RESPONSE_here = Response_tlist.begin();
586 RESPONSE_end = Response_tlist.end();
587 while (RESPONSE_here != RESPONSE_end) {
588 total_records += (*RESPONSE_here).numDocs;
589 RESPONSE_here++;
590 }
591
592 // check that the index is within bounds
593 if (recnum < 1 || recnum > total_records) {
594 cerr << "get_marc_record failed" << endl;
595 return false; // failed
596 } else {
597 FilterResponse_t response;
598 // iterate through Response_tlist to find the right response
599 RESPONSE_here = Response_tlist.begin();
600 RESPONSE_end = Response_tlist.end();
601 RESPONSE_COLNAMES_here = Response_tlist_colnames.begin();
602 RESPONSE_COLNAMES_end = Response_tlist_colnames.end();
603 RESPONSE_SIZES_here = Response_tlist_sizes.begin();
604 RESPONSE_SIZES_end = Response_tlist_sizes.end();
605 int n = 0;
606 while (RESPONSE_here != RESPONSE_end) {
607 n += *RESPONSE_SIZES_here;
608 if (n >= recnum) {
609 response = *RESPONSE_here;
610 CollectionName = *RESPONSE_COLNAMES_here;
611 recnum = *RESPONSE_SIZES_here - (n - recnum);
612 break;
613 }
614 // these should all have the same number of elements,
615 // so only need to check that one is within bounds.
616 // may want to combine these into a larger data structure later.
617 RESPONSE_here++;
618 RESPONSE_COLNAMES_here++;
619 RESPONSE_SIZES_here++;
620 }
621
622 // locate the desired record in the response
623
624 MetadataInfo_tmap::iterator METAFIELD_here = response.docInfo[recnum-1].metadata.begin();
625 MetadataInfo_tmap::iterator METAFIELD_end = response.docInfo[recnum-1].metadata.end();
626
627 while (METAFIELD_here!=METAFIELD_end) {
628 text_tarray::iterator METAVALUE_here = METAFIELD_here->second.values.begin();
629 text_tarray::iterator METAVALUE_end = METAFIELD_here->second.values.end();
630 while (METAVALUE_here!=METAVALUE_end) {
631 if (METAFIELD_here->first[0] >= 'A' &&
632 METAFIELD_here->first[0] <= 'Z') {
633 metatag *tmptag = new metatag();
634 tmptag->name = METAFIELD_here->first.getcstr();
635 tmptag->type = "";
636 tmptag->scheme = "";
637 tmptag->value = (*METAVALUE_here).getcstr();
638 Metatag_list.push_back(tmptag);
639 }
640 METAVALUE_here++;
641 }
642 METAFIELD_here++;
643 }
644 return true; // succeeded
645 }
646}
647
648// *** implement this ***
649int ztest_fetch(void *handle, bend_fetch_rr *r)
650{
651 // *** called when client issues a "show" command, 2nd of 5
652 if (z3950_verbosity_>1) {
653 cerr << "Entering ztest_fetch" << endl;
654 }
655 char *cp;
656 r->errstring = 0;
657 r->last_in_set = 0;
658 r->basename = "DUMMY"; // this is set below
659 r->output_format = r->request_format;
660
661 // *** use this until found a good way of mapping Greenstone
662 // *** metadata fields to USMARC fields
663 if (r->request_format == VAL_SUTRS)
664 {
665 // copies record into r, errcode will be 13 if fails
666 text_t MetadataResult;
667 text_t CollectionName;
668 if (!get_sutrs_record(r->number, MetadataResult, CollectionName)) {
669 r->errcode = 13;
670 return 0;
671 } else {
672 r->len = MetadataResult.size();
673 r->record = (char*) odr_malloc(r->stream, r->len+1);
674 char *tmptxt = MetadataResult.getcstr();
675 strcpy(r->record, tmptxt);
676 delete tmptxt;
677 r->basename = (char*) odr_malloc(r->stream, CollectionName.size()+1);
678 tmptxt = CollectionName.getcstr();
679 strcpy(r->basename, tmptxt);
680 delete tmptxt;
681 }
682 }
683 // *** do we want to use this format? ***
684 else if (r->request_format == VAL_GRS1)
685 {
686 // bail out, since we don't support grs (yet?)
687 r->errcode = 107; // "Query type not supported"
688 return 0;
689
690 }
691 // assume that here on in is some sort of MARC format
692 else {
693 marcrec *mrec = new marcrec;
694
695 mrec = (marcrec*) malloc(sizeof(*mrec));
696
697 mrec->marcline = (char*) malloc(100000);
698 mrec->partitle = (char*) malloc(10000);
699 mrec->subtitle = (char*) malloc(10000);
700 mrec->year = (char*) malloc(1000);
701 mrec->url = (char*) malloc(1000);
702 mrec->fmat = (char*) malloc(1000);
703 mrec->s008 = (char*) malloc(41);
704
705 mrec->ncreators = 0;
706 mrec->ntitles = 0;
707 *mrec->marcline = 0;
708 *mrec->subtitle = 0;
709 *mrec->partitle = 0;
710 *mrec->year = 0;
711 *mrec->url = 0;
712 *mrec->fmat = 0;
713 strcpy(mrec->s008, " ");
714
715 text_t CollectionName;
716 list<metatag*> Metatag_list;
717
718 if (!get_marc_record(r->number, Metatag_list, CollectionName)) {
719 r->errcode = 13;
720 return 0;
721 } else {
722 list<metatag*>::iterator METATAG_here = Metatag_list.begin();
723 list<metatag*>::iterator METATAG_end = Metatag_list.end();
724 while (METATAG_here != METATAG_end) {
725 usMARC(*METATAG_here, mrec);
726 METATAG_here++;
727 }
728
729 // set the date in the marc record to the current date
730 char today[32];
731 time_t d;
732 struct tm *time_struct;
733 //putenv("TZ=NFT-1DFT");
734 d = time(NULL);
735 time_struct = localtime(&d);
736 strftime(today, 16, "%y%m%d", time_struct);
737 put008(mrec->s008, today, F008_DATE_ENTERED);
738 put008(mrec->s008, "s", F008_TYPE_OF_DATE);
739
740 // to do: make this safer + more efficient
741 cp = new char[1000000];
742 char *cp2 = new char[1000000];
743
744 switch(r->request_format) {
745 case VAL_DANMARC: {
746 MARCtidy(mrec, cp, DANMARC);
747 r->output_format = VAL_DANMARC;
748 break; }
749 case VAL_FINMARC: {
750 MARCtidy(mrec, cp, FINMARC);
751 r->output_format = VAL_FINMARC;
752 break; }
753 // YAZ doesn't know about ISMARC
754 /*
755 case VAL_ISMARC: {
756 put008(mrec->s008, "k", F008_FORM_OF_PUBLICATION);
757 MARCtidy(mrec, cp, ISMARC);
758 r->output_format = VAL_ISMARC;
759 break; }
760 */
761 case VAL_NORMARC: {
762 MARCtidy(mrec, cp, NORMARC);
763 r->output_format = VAL_NORMARC;
764 break; }
765 case VAL_SWEMARC: {
766 MARCtidy(mrec, cp, SWEMARC);
767 r->output_format = VAL_SWEMARC;
768 break; }
769 default: {
770 MARCtidy(mrec, cp, USMARC);
771 r->output_format = VAL_USMARC;
772 break; }
773 }
774
775 sprintf(cp2, "%s", (char*) MARC2709(cp, 0, 6, 'n', 'm', ' '));
776 r->len = strlen(cp2);
777 r->record = (char*) odr_malloc(r->stream, r->len+1);
778 strcpy(r->record, cp2);
779 r->basename = (char*) odr_malloc(r->stream, CollectionName.size()+1);
780 char *tmptxt;
781 tmptxt = CollectionName.getcstr();
782 strcpy(r->basename, tmptxt);
783 delete tmptxt;
784 delete cp2;
785 cerr << "dumping record:`" << cp << "'" << endl;
786 delete cp;
787 }
788 }
789
790 r->errcode = 0;
791 return 0;
792}
793
794/*
795 * silly dummy-scan what reads words from a file.
796 */
797int ztest_scan(void *handle, bend_scan_rr *q)
798{
799 // *** called when client issues a "scan" command
800 if (z3950_verbosity_>1) {
801 cerr << "Entering ztest_scan" << endl;
802 }
803
804 // bail out, since we don't support scan (yet?)
805 q->errcode = 107; // "Query type not supported"
806 return 0;
807
808 static FILE *f = 0;
809 static struct scan_entry list[200];
810 static char entries[200][80];
811 int hits[200];
812 char term[80], *p;
813 int i, pos;
814 int term_position_req = q->term_position;
815 int num_entries_req = q->num_entries;
816
817 q->errcode = 0;
818 q->errstring = 0;
819 q->entries = list;
820 q->status = BEND_SCAN_SUCCESS;
821 if (!f && !(f = fopen("dummy-words", "r")))
822 {
823 perror("dummy-words");
824 exit(1);
825 }
826 if (q->term->term->which != Z_Term_general)
827 {
828 q->errcode = 229; /* unsupported term type */
829 return 0;
830 }
831 if (*q->step_size != 0)
832 {
833 q->errcode = 205; /*Only zero step size supported for Scan */
834 return 0;
835 }
836 if (q->term->term->u.general->len >= 80)
837 {
838 q->errcode = 11; /* term too long */
839 return 0;
840 }
841 if (q->num_entries > 200)
842 {
843 q->errcode = 31;
844 return 0;
845 }
846 memcpy(term, q->term->term->u.general->buf, q->term->term->u.general->len);
847 term[q->term->term->u.general->len] = '\0';
848 for (p = term; *p; p++)
849 if (islower(*p))
850 *p = toupper(*p);
851
852 fseek(f, 0, SEEK_SET);
853 q->num_entries = 0;
854
855 for (i = 0, pos = 0; fscanf(f, " %79[^:]:%d", entries[pos], &hits[pos]) == 2;
856 i++, pos < 199 ? pos++ : (pos = 0))
857 {
858 if (!q->num_entries && strcmp(entries[pos], term) >= 0) /* s-point fnd */
859 {
860 if ((q->term_position = term_position_req) > i + 1)
861 {
862 q->term_position = i + 1;
863 q->status = BEND_SCAN_PARTIAL;
864 }
865 for (; q->num_entries < q->term_position; q->num_entries++)
866 {
867 int po;
868
869 po = pos - q->term_position + q->num_entries+1; /* find pos */
870 if (po < 0)
871 po += 200;
872
873 if (!strcmp (term, "SD") && q->num_entries == 2)
874 {
875 list[q->num_entries].term = entries[pos];
876 list[q->num_entries].occurrences = -1;
877 list[q->num_entries].errcode = 233;
878 list[q->num_entries].errstring = "SD for Scan Term";
879 }
880 else
881 {
882 list[q->num_entries].term = entries[po];
883 list[q->num_entries].occurrences = hits[po];
884 }
885 }
886 }
887 else if (q->num_entries)
888 {
889 list[q->num_entries].term = entries[pos];
890 list[q->num_entries].occurrences = hits[pos];
891 q->num_entries++;
892 }
893 if (q->num_entries >= num_entries_req)
894 break;
895 }
896 if (feof(f))
897 q->status = BEND_SCAN_PARTIAL;
898 return 0;
899}
900
901
902
903void bend_close(void *handle)
904{
905 if (z3950_verbosity_>1) {
906 cerr << "Entering bend_close" << endl;
907 }
908 delete Server;
909 delete Cservers;
910 delete Protocol;
911 if (z3950_verbosity_>1) {
912 cerr << "heap cleaned up, exiting bend_close" << endl;
913 }
914 return;
915}
916
917int main (int argc, char *argv[])
918{
919
920 const int statserv_var = statserv_main(argc, argv, bend_init, bend_close);
921 cerr << "statserv_main returns: " << statserv_var << endl;
922 return statserv_var;
923
924 return 0;
925}
926
927
Note: See TracBrowser for help on using the repository browser.