1 /* $Id: util.cpp,v 1.9 2006-01-17 17:55:40 adam Exp $
2 Copyright (c) 2005, Index Data.
10 #include <yaz/pquery.h>
11 #include <yaz/otherinfo.h>
14 void yp2::util::piggyback(int smallSetUpperBound,
15 int largeSetLowerBound,
16 int mediumSetPresentNumber,
18 int &number_to_present)
20 // deal with piggyback
22 if (result_set_size < smallSetUpperBound)
24 // small set . Return all records in set
25 number_to_present = result_set_size;
27 else if (result_set_size > largeSetLowerBound)
29 // large set . Return no records
30 number_to_present = 0;
34 // medium set . Return mediumSetPresentNumber records
35 number_to_present = mediumSetPresentNumber;
36 if (number_to_present > result_set_size)
37 number_to_present = result_set_size;
41 bool yp2::util::pqf(ODR odr, Z_APDU *apdu, const std::string &q) {
42 YAZ_PQF_Parser pqf_parser = yaz_pqf_create();
44 Z_RPNQuery *rpn = yaz_pqf_parse(pqf_parser, odr, q.c_str());
47 yaz_pqf_destroy(pqf_parser);
50 yaz_pqf_destroy(pqf_parser);
51 Z_Query *query = (Z_Query *) odr_malloc(odr, sizeof(Z_Query));
52 query->which = Z_Query_type_1;
53 query->u.type_1 = rpn;
55 apdu->u.searchRequest->query = query;
59 void yp2::util::get_default_diag(Z_DefaultDiagFormat *r,
60 int &error_code, std::string &addinfo)
62 error_code = *r->condition;
65 case Z_DefaultDiagFormat_v2Addinfo:
66 addinfo = std::string(r->u.v2Addinfo);
68 case Z_DefaultDiagFormat_v3Addinfo:
69 addinfo = r->u.v3Addinfo;
74 void yp2::util::get_init_diagnostics(Z_InitResponse *initrs,
75 int &error_code, std::string &addinfo)
77 Z_External *uif = initrs->userInformationField;
79 if (uif && uif->which == Z_External_userInfo1)
81 Z_OtherInformation *ui = uif->u.userInfo1;
83 for (i = 0; i < ui->num_elements; i++)
85 Z_OtherInformationUnit *unit = ui->list[i];
86 if (unit->which == Z_OtherInfo_externallyDefinedInfo &&
87 unit->information.externallyDefinedInfo &&
88 unit->information.externallyDefinedInfo->which ==
91 Z_DiagnosticFormat *diag =
92 unit->information.externallyDefinedInfo->u.diag1;
96 Z_DiagnosticFormat_s *ds = diag->elements[0];
97 if (ds->which == Z_DiagnosticFormat_s_defaultDiagRec)
98 yp2::util::get_default_diag(ds->u.defaultDiagRec,
106 int yp2::util::get_vhost_otherinfo(Z_OtherInformation **otherInformation,
108 std::list<std::string> &vhosts)
111 for (cat = 1; ; cat++)
113 // check virtual host
115 yaz_oi_get_string_oidval(otherInformation,
117 cat /* categoryValue */,
118 remove_flag /* delete flag */);
121 vhosts.push_back(std::string(vhost));
127 void yp2::util::split_zurl(std::string zurl, std::string &host,
128 std::list<std::string> &db)
130 const char *zurl_cstr = zurl.c_str();
131 const char *sep = strchr(zurl_cstr, '/');
135 host = std::string(zurl_cstr, sep - zurl_cstr);
137 const char *cp1 = sep+1;
140 const char *cp2 = strchr(cp1, '+');
142 db.push_back(std::string(cp1, cp2 - cp1));
145 db.push_back(std::string(cp1));
157 bool yp2::util::set_databases_from_zurl(ODR odr, std::string zurl,
158 int *db_num, char ***db_strings)
161 std::list<std::string> dblist;
163 split_zurl(zurl, host, dblist);
165 if (dblist.size() == 0)
167 *db_num = dblist.size();
168 *db_strings = (char **) odr_malloc(odr, sizeof(char*) * (*db_num));
170 std::list<std::string>::const_iterator it = dblist.begin();
171 for (int i = 0; it != dblist.end(); it++, i++)
172 (*db_strings)[i] = odr_strdup(odr, it->c_str());
176 yp2::odr::odr(int type)
178 m_odr = odr_createmem(type);
183 m_odr = odr_createmem(ODR_ENCODE);
191 yp2::odr::operator ODR() const
196 Z_APDU *yp2::odr::create_close(Z_APDU *in_apdu,
197 int reason, const char *addinfo)
199 Z_APDU *apdu = create_APDU(Z_APDU_close, in_apdu);
201 *apdu->u.close->closeReason = reason;
203 apdu->u.close->diagnosticInformation = odr_strdup(m_odr, addinfo);
207 Z_APDU *yp2::odr::create_APDU(int type, Z_APDU *in_apdu)
209 return yp2::util::create_APDU(m_odr, type, in_apdu);
212 Z_APDU *yp2::util::create_APDU(ODR odr, int type, Z_APDU *in_apdu)
214 Z_APDU *out_apdu = zget_APDU(odr, type);
216 Z_ReferenceId **id_to = yp2::util::get_referenceId(out_apdu);
220 Z_ReferenceId **id_from = yp2::util::get_referenceId(in_apdu);
221 if (id_from && *id_from && id_to)
223 *id_to = (Z_ReferenceId*) odr_malloc (odr, sizeof(**id_to));
224 (*id_to)->size = (*id_to)->len = (*id_from)->len;
225 (*id_to)->buf = (unsigned char*) odr_malloc(odr, (*id_to)->len);
226 memcpy((*id_to)->buf, (*id_from)->buf, (*id_to)->len);
234 Z_APDU *yp2::odr::create_initResponse(Z_APDU *in_apdu,
235 int error, const char *addinfo)
237 Z_APDU *apdu = create_APDU(Z_APDU_initResponse, in_apdu);
240 apdu->u.initResponse->userInformationField =
241 zget_init_diagnostics(m_odr, error, addinfo);
242 *apdu->u.initResponse->result = 0;
247 Z_APDU *yp2::odr::create_searchResponse(Z_APDU *in_apdu,
248 int error, const char *addinfo)
250 Z_APDU *apdu = create_APDU(Z_APDU_searchResponse, in_apdu);
253 Z_Records *rec = (Z_Records *) odr_malloc(m_odr, sizeof(Z_Records));
254 *apdu->u.searchResponse->searchStatus = 0;
255 apdu->u.searchResponse->records = rec;
256 rec->which = Z_Records_NSD;
257 rec->u.nonSurrogateDiagnostic =
258 zget_DefaultDiagFormat(m_odr, error, addinfo);
264 Z_APDU *yp2::odr::create_presentResponse(Z_APDU *in_apdu,
265 int error, const char *addinfo)
267 Z_APDU *apdu = create_APDU(Z_APDU_presentResponse, in_apdu);
270 Z_Records *rec = (Z_Records *) odr_malloc(m_odr, sizeof(Z_Records));
271 apdu->u.presentResponse->records = rec;
272 rec->which = Z_Records_NSD;
273 rec->u.nonSurrogateDiagnostic =
274 zget_DefaultDiagFormat(m_odr, error, addinfo);
275 *apdu->u.presentResponse->presentStatus = Z_PresentStatus_failure;
280 Z_APDU *yp2::odr::create_scanResponse(Z_APDU *in_apdu,
281 int error, const char *addinfo)
283 Z_APDU *apdu = create_APDU(Z_APDU_scanResponse, in_apdu);
286 Z_ScanResponse *res = apdu->u.scanResponse;
287 res->entries = (Z_ListEntries *) odr_malloc(m_odr, sizeof(*res->entries));
288 *res->scanStatus = Z_Scan_failure;
290 res->entries->num_entries = 0;
291 res->entries->entries = 0;
292 res->entries->num_nonsurrogateDiagnostics = 1;
293 res->entries->nonsurrogateDiagnostics = (Z_DiagRec **)
294 odr_malloc(m_odr, sizeof(Z_DiagRec *));
295 res->entries->nonsurrogateDiagnostics[0] =
296 zget_DiagRec(m_odr, error, addinfo);
301 Z_ReferenceId **yp2::util::get_referenceId(Z_APDU *apdu)
305 case Z_APDU_initRequest:
306 return &apdu->u.initRequest->referenceId;
307 case Z_APDU_initResponse:
308 return &apdu->u.initResponse->referenceId;
309 case Z_APDU_searchRequest:
310 return &apdu->u.searchRequest->referenceId;
311 case Z_APDU_searchResponse:
312 return &apdu->u.searchResponse->referenceId;
313 case Z_APDU_presentRequest:
314 return &apdu->u.presentRequest->referenceId;
315 case Z_APDU_presentResponse:
316 return &apdu->u.presentResponse->referenceId;
317 case Z_APDU_deleteResultSetRequest:
318 return &apdu->u.deleteResultSetRequest->referenceId;
319 case Z_APDU_deleteResultSetResponse:
320 return &apdu->u.deleteResultSetResponse->referenceId;
321 case Z_APDU_accessControlRequest:
322 return &apdu->u.accessControlRequest->referenceId;
323 case Z_APDU_accessControlResponse:
324 return &apdu->u.accessControlResponse->referenceId;
325 case Z_APDU_resourceControlRequest:
326 return &apdu->u.resourceControlRequest->referenceId;
327 case Z_APDU_resourceControlResponse:
328 return &apdu->u.resourceControlResponse->referenceId;
329 case Z_APDU_triggerResourceControlRequest:
330 return &apdu->u.triggerResourceControlRequest->referenceId;
331 case Z_APDU_resourceReportRequest:
332 return &apdu->u.resourceReportRequest->referenceId;
333 case Z_APDU_resourceReportResponse:
334 return &apdu->u.resourceReportResponse->referenceId;
335 case Z_APDU_scanRequest:
336 return &apdu->u.scanRequest->referenceId;
337 case Z_APDU_scanResponse:
338 return &apdu->u.scanResponse->referenceId;
339 case Z_APDU_sortRequest:
340 return &apdu->u.sortRequest->referenceId;
341 case Z_APDU_sortResponse:
342 return &apdu->u.sortResponse->referenceId;
343 case Z_APDU_segmentRequest:
344 return &apdu->u.segmentRequest->referenceId;
345 case Z_APDU_extendedServicesRequest:
346 return &apdu->u.extendedServicesRequest->referenceId;
347 case Z_APDU_extendedServicesResponse:
348 return &apdu->u.extendedServicesResponse->referenceId;
350 return &apdu->u.close->referenceId;
359 * indent-tabs-mode: nil
360 * c-file-style: "stroustrup"
362 * vim: shiftwidth=4 tabstop=8 expandtab