Avoid strcasecmp which does not exist on Windows
[yaz-moved-to-github.git] / src / sortspec.c
index 07e4e0f..7f272f5 100644 (file)
@@ -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.
  */
 /**
@@ -13,6 +13,7 @@
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
+#include <yaz/matchstr.h>
 
 #include <yaz/z-core.h>
 #include <yaz/sortspec.h>
@@ -25,10 +26,10 @@ Z_SortKeySpecList *yaz_sort_spec(ODR out, const char *arg)
     Z_SortKeySpecList *sksl = (Z_SortKeySpecList *)
         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 +38,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));
         sks->sortElement->which = Z_SortElement_generic;
         sks->sortElement->u.generic = sk;
-        
+
         if ((sort_string_sep = strchr(sort_string, '=')))
         {
             int i = 0;
@@ -55,7 +56,7 @@ 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)
             {
@@ -87,7 +88,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 +124,8 @@ 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;
+                break;
             }
         }
     }
@@ -181,6 +183,7 @@ int yaz_sort_spec_to_cql(Z_SortKeySpecList *sksl, WRBUF w)
             wrbuf_puts(w, "/missingValue=");
             wrbuf_write(w, (const char *) sks->u.missingValueData->buf,
                         sks->u.missingValueData->len);
+            break;
         }
     }
     return 0;
@@ -287,11 +290,48 @@ int yaz_sort_spec_to_srw_sortkeys(Z_SortKeySpecList *sksl, WRBUF w)
         case Z_SortKeySpec_missingValueData:
             wrbuf_write(w, (const char *) sks->u.missingValueData->buf,
                         sks->u.missingValueData->len);
+            break;
+        }
+    }
+    return 0;
+}
+
+int yaz_sort_spec_to_solr_sortkeys(Z_SortKeySpecList *sksl, WRBUF w)
+{
+    int i;
+    for (i = 0; i < sksl->num_specs; i++)
+    {
+        Z_SortKeySpec *sks = sksl->specs[i];
+        Z_SortKey *sk;
+
+        if (sks->sortElement->which != Z_SortElement_generic)
+            return -1;
+
+        sk = sks->sortElement->u.generic;
+
+        if (i)
+            wrbuf_puts(w, ",");
+
+        if (sk->which == Z_SortKey_sortAttributes)
+            return -1;
+        else if (sk->which == Z_SortKey_sortField)
+        {
+            wrbuf_puts(w, sk->u.sortField);
+        }
+        switch (*sks->sortRelation)
+        {
+        case Z_SortKeySpec_ascending:
+            wrbuf_puts(w, " asc");
+            break;
+        case Z_SortKeySpec_descending:
+            wrbuf_puts(w, " desc");
+            break;
         }
     }
     return 0;
 }
 
+
 int yaz_srw_sortkeys_to_sort_spec(const char *srw_sortkeys, WRBUF w)
 {
     /* sru sortkey layout: path,schema,ascending,caseSensitive,missingValue */
@@ -300,7 +340,7 @@ int yaz_srw_sortkeys_to_sort_spec(const char *srw_sortkeys, WRBUF w)
     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)
@@ -313,7 +353,7 @@ int yaz_srw_sortkeys_to_sort_spec(const char *srw_sortkeys, WRBUF w)
             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])
@@ -331,14 +371,17 @@ int yaz_srw_sortkeys_to_sort_spec(const char *srw_sortkeys, WRBUF w)
             wrbuf_puts(w, case_sensitive ? "s" : "i");
             if (missing)
             {
-                if (!strcmp(missing, "omit"))
+                if (!strcmp(missing, "omit")) {
                     ;
+                }
                 else if (!strcmp(missing, "abort"))
                     wrbuf_puts(w, "!");
-                else if (!strcmp(missing, "lowValue"))
+                else if (!strcmp(missing, "lowValue")) {
                     ;
-                else if (!strcmp(missing, "highValue"))
+                }
+                else if (!strcmp(missing, "highValue")) {
                     ;
+                }
                 else
                 {
                     wrbuf_puts(w, "=");
@@ -351,6 +394,53 @@ int yaz_srw_sortkeys_to_sort_spec(const char *srw_sortkeys, WRBUF w)
     return 0;
 }
 
+int yaz_solr_sortkeys_to_sort_spec(const char *solr_sortkeys, WRBUF w)
+{
+    /* Solr sortkey layout: field order[, field order] */
+    /* see cql_sortby_to_sortkeys of YAZ. */
+    char **sortspec;
+    int num_sortspec = 0;
+    int i;
+    NMEM nmem = nmem_create();
+
+    if (solr_sortkeys)
+        nmem_strsplit(nmem, ",", solr_sortkeys, &sortspec, &num_sortspec);
+    if (num_sortspec > 0)
+    {
+        for (i = 0; i < num_sortspec; i++)
+        {
+            char **arg;
+            int num_arg;
+            char order = 'a';
+            int case_sensitive = 0;
+            nmem_strsplitx(nmem, " ", sortspec[i], &arg, &num_arg, 0);
+
+            if (num_arg != 2)
+                return -1;
+
+            if (yaz_matchstr(arg[1], "asc") &&
+                yaz_matchstr(arg[1], "desc"))
+                return -1;
+
+            if (arg[1][0]) {
+                order = tolower(arg[1][0]);
+            }
+            if (i)
+                wrbuf_puts(w, " ");
+
+            wrbuf_puts(w, arg[0]); /* field */
+            wrbuf_puts(w, " ");
+
+            wrbuf_putc(w, order);
+            // Always in-sensitive
+            wrbuf_puts(w, case_sensitive ? "s" : "i");
+        }
+    }
+    nmem_destroy(nmem);
+    return 0;
+}
+
+
 /*
  * Local variables:
  * c-basic-offset: 4