*/
YAZ_EXPORT int yaz_sort_spec_to_srw_sortkeys(Z_SortKeySpecList *sksl, WRBUF w);
+/* \brief converts SRU/SRW 1.1 sortkeys to Z39.50 sortspec (WRBUF)
+ \param srw_sortkeys SRU sortkeys string
+ \param w resulting Z39.50 sortspec, WRBUF string
+ \retval 0 successful
+ \retval -1 failure
+*/
+int yaz_srw_sortkeys_to_sort_spec(const char *srw_sortkeys, WRBUF w);
YAZ_END_CDECL
#endif
return 0;
}
+int yaz_srw_sortkeys_to_sort_spec(const char *srw_sortkeys, WRBUF w)
+{
+ /* sru sortkey layout: path,schema,ascending,caseSensitive,missingValue */
+ /* see cql_sortby_to_sortkeys of YAZ. */
+ char **sortspec;
+ int num_sortspec = 0;
+ int i;
+ NMEM nmem = nmem_create();
+
+ if (srw_sortkeys)
+ nmem_strsplit_blank(nmem, srw_sortkeys, &sortspec, &num_sortspec);
+ if (num_sortspec > 0)
+ {
+ for (i = 0; i < num_sortspec; i++)
+ {
+ char **arg;
+ int num_arg;
+ int ascending = 1;
+ int case_sensitive = 0;
+ const char *missing = 0;
+ nmem_strsplitx(nmem, ",", sortspec[i], &arg, &num_arg, 0);
+
+ if (num_arg > 2 && arg[2][0])
+ ascending = atoi(arg[2]);
+ if (num_arg > 3 && arg[3][0])
+ case_sensitive = atoi(arg[3]);
+ if (num_arg > 4 && arg[4][0])
+ missing = arg[4];
+
+ if (i)
+ wrbuf_puts(w, " ");
+
+ wrbuf_puts(w, arg[0]); /* field */
+ wrbuf_puts(w, " ");
+
+ wrbuf_puts(w, ascending ? "a" : "d");
+ wrbuf_puts(w, case_sensitive ? "s" : "i");
+ if (missing)
+ {
+ if (!strcmp(missing, "omit"))
+ ;
+ else if (!strcmp(missing, "abort"))
+ wrbuf_puts(w, "!");
+ else if (!strcmp(missing, "lowValue"))
+ ;
+ else if (!strcmp(missing, "highValue"))
+ ;
+ else
+ {
+ wrbuf_puts(w, "=");
+ wrbuf_puts(w, missing);
+ }
+ }
+ }
+ }
+ nmem_destroy(nmem);
+ return 0;
+}
/*
* Local variables:
return ret;
}
+static int check_srw_sortkeys_to_sort_spec(const char *arg,
+ const char *expected_result)
+{
+ WRBUF w = wrbuf_alloc();
+ int ret = 0;
+ int r = yaz_srw_sortkeys_to_sort_spec(arg, w);
+
+ if (!expected_result && r)
+ ret = 1;
+ else if (expected_result && r == 0)
+ {
+ if (strcmp(wrbuf_cstr(w), expected_result) == 0)
+ ret = 1;
+ else
+ {
+ yaz_log(YLOG_WARN, "sort: diff: %s", arg);
+ yaz_log(YLOG_WARN, " expected %s", expected_result);
+ yaz_log(YLOG_WARN, " got %s", wrbuf_cstr(w));
+ }
+ }
+ else if (r)
+ {
+ yaz_log(YLOG_WARN, "sort: diff %s", arg);
+ yaz_log(YLOG_WARN, " expected %s", expected_result);
+ yaz_log(YLOG_WARN, " got error %d", r);
+ }
+ else if (r == 0)
+ {
+ yaz_log(YLOG_WARN, "sort: diff %s", arg);
+ yaz_log(YLOG_WARN, " expected error");
+ yaz_log(YLOG_WARN, " got %s", wrbuf_cstr(w));
+ }
+ wrbuf_destroy(w);
+ return ret;
+}
+
+
static void tst(void)
{
YAZ_CHECK(cql("title a",
YAZ_CHECK(srw_sortkeys("1=4,2=3 a", 0));
YAZ_CHECK(srw_sortkeys("date a=1900",
"date,,1,0,1900"));
+ YAZ_CHECK(check_srw_sortkeys_to_sort_spec(
+ "date,,1,0,1900",
+ "date ai=1900"));
}
int main(int argc, char **argv)