+Z_Records *yf::Zoom::Frontend::get_records(Odr_int start,
+ Odr_int number_to_present,
+ int *error,
+ const char **addinfo,
+ Odr_int *number_of_records_returned,
+ ODR odr,
+ BackendPtr b,
+ Odr_oid *preferredRecordSyntax,
+ const char *element_set_name)
+{
+ *number_of_records_returned = 0;
+ Z_Records *records = 0;
+ bool enable_pz2_transform = false;
+
+ if (start < 0 || number_to_present <= 0)
+ return records;
+
+ if (number_to_present > 10000)
+ number_to_present = 10000;
+
+ ZOOM_record *recs = (ZOOM_record *)
+ odr_malloc(odr, number_to_present * sizeof(*recs));
+
+ char oid_name_str[OID_STR_MAX];
+ const char *syntax_name = 0;
+
+ if (preferredRecordSyntax)
+ {
+ if (!oid_oidcmp(preferredRecordSyntax, yaz_oid_recsyn_xml)
+ && !strcmp(element_set_name, "pz2"))
+ {
+ if (b->sptr->request_syntax.length())
+ {
+ syntax_name = b->sptr->request_syntax.c_str();
+ enable_pz2_transform = true;
+ }
+ }
+ else
+ {
+ syntax_name =
+ yaz_oid_to_string_buf(preferredRecordSyntax, 0, oid_name_str);
+ }
+ }
+
+ yaz_log(YLOG_LOG, "enable_pz2_transform %s", enable_pz2_transform ?
+ "enabled" : "disabled");
+
+ b->set_option("preferredRecordSyntax", syntax_name);
+
+ if (enable_pz2_transform)
+ {
+ element_set_name = "F";
+ if (b->sptr->element_set.length())
+ element_set_name = b->sptr->element_set.c_str();
+ }
+
+ b->set_option("elementSetName", element_set_name);
+
+ b->present(start, number_to_present, recs, error, addinfo);
+
+ Odr_int i = 0;
+ if (!*error)
+ {
+ for (i = 0; i < number_to_present; i++)
+ if (!recs[i])
+ break;
+ }
+ if (i > 0)
+ { // only return records if no error and at least one record
+ char *odr_database = odr_strdup(odr,
+ b->m_frontend_database.c_str());
+ Z_NamePlusRecordList *npl = (Z_NamePlusRecordList *)
+ odr_malloc(odr, sizeof(*npl));
+ *number_of_records_returned = i;
+ npl->num_records = i;
+ npl->records = (Z_NamePlusRecord **)
+ odr_malloc(odr, i * sizeof(*npl->records));
+ for (i = 0; i < number_to_present; i++)
+ {
+ Z_NamePlusRecord *npr = 0;
+ const char *addinfo;
+ int sur_error = ZOOM_record_error(recs[i], 0 /* msg */,
+ &addinfo, 0 /* diagset */);
+
+ if (sur_error)
+ {
+ npr = zget_surrogateDiagRec(odr, odr_database, sur_error,
+ addinfo);
+ }
+ else if (enable_pz2_transform)
+ {
+ char rec_type_str[100];
+
+ strcpy(rec_type_str, b->sptr->use_turbomarc ?
+ "txml" : "xml");
+
+ // prevent buffer overflow ...
+ if (b->sptr->record_encoding.length() > 0 &&
+ b->sptr->record_encoding.length() <
+ (sizeof(rec_type_str)-20))
+ {
+ strcat(rec_type_str, "; charset=");
+ strcat(rec_type_str, b->sptr->record_encoding.c_str());
+ }
+
+ int rec_len;
+ const char *rec_buf = ZOOM_record_get(recs[i], rec_type_str,
+ &rec_len);
+ if (rec_buf && b->xsp)
+ {
+ xmlDoc *rec_doc = xmlParseMemory(rec_buf, rec_len);
+ if (rec_doc)
+ {
+ xmlDoc *rec_res;
+ rec_res = xsltApplyStylesheet(b->xsp, rec_doc, 0);
+
+ if (rec_res)
+ xsltSaveResultToString((xmlChar **) &rec_buf, &rec_len,
+ rec_res, b->xsp);
+ }
+ }
+
+ if (rec_buf)
+ {
+ npr = (Z_NamePlusRecord *) odr_malloc(odr, sizeof(*npr));
+ npr->databaseName = odr_database;
+ npr->which = Z_NamePlusRecord_databaseRecord;
+ npr->u.databaseRecord =
+ z_ext_record_xml(odr, rec_buf, rec_len);
+ }
+ else
+ {
+ npr = zget_surrogateDiagRec(
+ odr, odr_database,
+ YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS,
+ rec_type_str);
+ }
+ }
+ else
+ {
+ Z_External *ext =
+ (Z_External *) ZOOM_record_get(recs[i], "ext", 0);
+ if (ext)
+ {
+ npr = (Z_NamePlusRecord *) odr_malloc(odr, sizeof(*npr));
+ npr->databaseName = odr_database;
+ npr->which = Z_NamePlusRecord_databaseRecord;
+ npr->u.databaseRecord = ext;
+ }
+ else
+ {
+ npr = zget_surrogateDiagRec(
+ odr, odr_database,
+ YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS,
+ "ZOOM_record, type ext");
+ }
+ }
+ npl->records[i] = npr;
+ }
+ records = (Z_Records*) odr_malloc(odr, sizeof(*records));
+ records->which = Z_Records_DBOSD;
+ records->u.databaseOrSurDiagnostics = npl;
+ }
+ return records;
+}
+
+