X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=src%2Fsortspec.c;h=e6c032d20a64bd85124cc9e33bc1179e8d6b4f2e;hb=c9b2e9553e1e59d74556a83f38d39e16a389a286;hp=ed795ea096282863540758a6c0842c0fa549ebd3;hpb=77aaeaf4801a896d3eaa93b8d76a81be82d95bad;p=yaz-moved-to-github.git diff --git a/src/sortspec.c b/src/sortspec.c index ed795ea..e6c032d 100644 --- a/src/sortspec.c +++ b/src/sortspec.c @@ -1,5 +1,5 @@ /* This file is part of the YAZ toolkit. - * Copyright (C) 1995-2011 Index Data + * Copyright (C) 1995-2012 Index Data * See the file LICENSE for details. */ /** @@ -23,12 +23,12 @@ Z_SortKeySpecList *yaz_sort_spec(ODR out, const char *arg) { char sort_string_buf[64], sort_flags[64]; Z_SortKeySpecList *sksl = (Z_SortKeySpecList *) - odr_malloc (out, sizeof(*sksl)); + odr_malloc(out, sizeof(*sksl)); int off; - + sksl->num_specs = 0; sksl->specs = (Z_SortKeySpec **)odr_malloc(out, sizeof(sksl->specs) * 20); - + while ((sscanf(arg, "%63s %63s%n", sort_string_buf, sort_flags, &off)) == 2 && off > 1) { @@ -37,14 +37,14 @@ Z_SortKeySpecList *yaz_sort_spec(ODR out, const char *arg) char *sort_string = sort_string_buf; Z_SortKeySpec *sks = (Z_SortKeySpec *) odr_malloc(out, sizeof(*sks)); Z_SortKey *sk = (Z_SortKey *) odr_malloc(out, sizeof(*sk)); - + arg += off; sksl->specs[sksl->num_specs++] = sks; sks->sortElement = (Z_SortElement *) - odr_malloc (out, sizeof(*sks->sortElement)); + odr_malloc(out, sizeof(*sks->sortElement)); sks->sortElement->which = Z_SortElement_generic; sks->sortElement->u.generic = sk; - + if ((sort_string_sep = strchr(sort_string, '='))) { int i = 0; @@ -55,12 +55,12 @@ Z_SortKeySpecList *yaz_sort_spec(ODR out, const char *arg) sk->u.sortAttributes->list = (Z_AttributeList *) odr_malloc(out, sizeof(*sk->u.sortAttributes->list)); sk->u.sortAttributes->list->attributes = (Z_AttributeElement **) - odr_malloc(out, 10 * + odr_malloc(out, 10 * sizeof(*sk->u.sortAttributes->list->attributes)); while (i < 10 && sort_string && sort_string_sep) { Z_AttributeElement *el = (Z_AttributeElement *) - odr_malloc (out, sizeof(*el)); + odr_malloc(out, sizeof(*el)); sk->u.sortAttributes->list->attributes[i] = el; el->attributeSet = 0; el->attributeType = odr_intdup(out, atoi(sort_string)); @@ -87,7 +87,7 @@ Z_SortKeySpecList *yaz_sort_spec(ODR out, const char *arg) sks->which = Z_SortKeySpec_null; sks->u.null = odr_nullval (); - + for (i = 0; sort_flags[i]; i++) { switch (sort_flags[i]) @@ -123,7 +123,7 @@ Z_SortKeySpecList *yaz_sort_spec(ODR out, const char *arg) sks->u.missingValueData->size = sks->u.missingValueData->len; sks->u.missingValueData->buf = (unsigned char*) odr_strdup(out, sort_flags+i); - i += strlen(sort_flags+i); + i += strlen(sort_flags+i) - 1; } } } @@ -292,6 +292,64 @@ int yaz_sort_spec_to_srw_sortkeys(Z_SortKeySpecList *sksl, WRBUF w) 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: