CQL: start work on cql_sortby_to_sortkeys
authorAdam Dickmeiss <adam@indexdata.dk>
Wed, 4 May 2011 15:22:23 +0000 (17:22 +0200)
committerAdam Dickmeiss <adam@indexdata.dk>
Wed, 4 May 2011 15:22:23 +0000 (17:22 +0200)
include/yaz/cql.h
src/Makefile.am
src/cql_sortkeys.c [new file with mode: 0644]
win/makefile

index c9df2ac..83b6217 100644 (file)
@@ -386,6 +386,37 @@ int cql_strcmp(const char *s1, const char *s2);
 YAZ_EXPORT
 int cql_strncmp(const char *s1, const char *s2, size_t n);
 
+/** \brief converts CQL sortby to sortkeys (ala versions 1.1)
+    \param cn CQL tree
+    \param pr print function
+    \param client_data data to be passed to pr function
+    
+    This will take CQL_NODE_SORT entries and conver them to
+
+    path,schema,ascending,caseSensitive,missingValue
+    items..
+
+    One for each sort keys. Where
+
+    path is string index for sorting
+
+    schema is schema for sort index
+
+    ascending is a boolean (0=false, 1=true). Default is true.
+
+    caseSensitive is a boolean. Default is false.
+
+    missingValue is a string and one of 'abort', 'highValue', 'lowValue',
+    or 'omit'. Default is 'highValue'.
+
+    See also
+    http://www.loc.gov/standards/sru/sru1-1archive/search-retrieve-operation.html#sort
+*/
+YAZ_EXPORT
+int cql_sortby_to_sortkeys(struct cql_node *cn,
+                           void (*pr)(const char *buf, void *client_data),
+                           void *client_data);
+
 YAZ_END_CDECL
 
 #endif
index 5aeb22c..31343c5 100644 (file)
@@ -94,7 +94,8 @@ libyaz_la_SOURCES=version.c options.c log.c \
   grs1disp.c zgdu.c soap.c srw.c srwutil.c uri.c solr.c \
   opacdisp.c cclfind.c ccltoken.c cclerrms.c cclqual.c cclptree.c cclp.h \
   cclqfile.c cclstr.c cclxmlconfig.c ccl_stop_words.c \
-  cql.y cqlstdio.c cqltransform.c cqlutil.c xcqlutil.c cqlstring.c rpn2cql.c \
+  cql.y cqlstdio.c cqltransform.c cqlutil.c xcqlutil.c cqlstring.c \
+  cql_sortkeys.c rpn2cql.c \
   rpn2solr.c solrtransform.c \
   cqlstrer.c querytowrbuf.c \
   tcpdchk.c \
diff --git a/src/cql_sortkeys.c b/src/cql_sortkeys.c
new file mode 100644 (file)
index 0000000..cdbdf7e
--- /dev/null
@@ -0,0 +1,86 @@
+/* This file is part of the YAZ toolkit.
+ * Copyright (C) 1995-2011 Index Data
+ * See the file LICENSE for details.
+ */
+/**
+ * \file cql_sortkeys.c
+ * \brief CQL sortkeys utilities
+ *
+ */
+#if HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <assert.h>
+#include <stdlib.h>
+#include <string.h>
+#include <yaz/wrbuf.h>
+#include <yaz/cql.h>
+
+static void pr_n(void (*pr)(const char *buf, void *client_data),
+                 const char *buf, int len, void *client_data)
+{
+    char tmp[4];
+    int left = len;
+
+    while (left > 0)
+    {
+        if (left >= sizeof(tmp))
+        {
+            memcpy(tmp, buf, sizeof(tmp)-1);
+            tmp[sizeof(tmp)-1] = '\0';
+            left = left - (sizeof(tmp)-1);
+        }
+        else
+        {
+            strcpy(tmp, buf);
+            left = 0;
+        }
+        pr(client_data, tmp);
+    }
+}
+
+int cql_sortby_to_sortkeys(struct cql_node *cn,
+                           void (*pr)(const char *buf, void *client_data),
+                           void *client_data)
+{
+    if (cn && cn->which == CQL_NODE_SORT)
+    {
+        for (; cn; cn = cn->u.sort.next)
+        {
+            int ascending = 1;
+            int caseSensitive = 0;
+            const char *missingValue = 0;
+            const char *indx = cn->u.sort.index;
+
+            if (indx)
+            {
+                const char *s = strchr(indx, '.');
+                if (s)
+                {
+                    pr(s+1, client_data);
+                    pr(",", client_data);
+                    pr_n(pr, indx, s - indx, client_data);
+                }
+                else
+                {
+                    pr(indx, client_data);
+                    pr(",", client_data);
+                }
+                pr(",", client_data);
+            }
+            if (cn->u.sort.next)
+                pr(" ", client_data);
+        }
+    }
+}
+
+/*
+ * Local variables:
+ * c-basic-offset: 4
+ * c-file-style: "Stroustrup"
+ * indent-tabs-mode: nil
+ * End:
+ * vim: shiftwidth=4 tabstop=8 expandtab
+ */
+
index 05269e8..89827cb 100644 (file)
@@ -503,6 +503,7 @@ MISC_OBJS= \
    $(OBJDIR)\xmlerror.obj \
    $(OBJDIR)\mime.obj \
    $(OBJDIR)\cql.obj \
+   $(OBJDIR)\cql_sortkeys.obj \
    $(OBJDIR)\cqlstdio.obj \
    $(OBJDIR)\cqlstring.obj \
    $(OBJDIR)\cqltransform.obj \