CQL: start work on cql_sortby_to_sortkeys
[yaz-moved-to-github.git] / src / cql_sortkeys.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2011 Index Data
3  * See the file LICENSE for details.
4  */
5 /**
6  * \file cql_sortkeys.c
7  * \brief CQL sortkeys utilities
8  *
9  */
10 #if HAVE_CONFIG_H
11 #include <config.h>
12 #endif
13
14 #include <assert.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <yaz/wrbuf.h>
18 #include <yaz/cql.h>
19
20 static void pr_n(void (*pr)(const char *buf, void *client_data),
21                  const char *buf, int len, void *client_data)
22 {
23     char tmp[4];
24     int left = len;
25
26     while (left > 0)
27     {
28         if (left >= sizeof(tmp))
29         {
30             memcpy(tmp, buf, sizeof(tmp)-1);
31             tmp[sizeof(tmp)-1] = '\0';
32             left = left - (sizeof(tmp)-1);
33         }
34         else
35         {
36             strcpy(tmp, buf);
37             left = 0;
38         }
39         pr(client_data, tmp);
40     }
41 }
42
43 int cql_sortby_to_sortkeys(struct cql_node *cn,
44                            void (*pr)(const char *buf, void *client_data),
45                            void *client_data)
46 {
47     if (cn && cn->which == CQL_NODE_SORT)
48     {
49         for (; cn; cn = cn->u.sort.next)
50         {
51             int ascending = 1;
52             int caseSensitive = 0;
53             const char *missingValue = 0;
54             const char *indx = cn->u.sort.index;
55
56             if (indx)
57             {
58                 const char *s = strchr(indx, '.');
59                 if (s)
60                 {
61                     pr(s+1, client_data);
62                     pr(",", client_data);
63                     pr_n(pr, indx, s - indx, client_data);
64                 }
65                 else
66                 {
67                     pr(indx, client_data);
68                     pr(",", client_data);
69                 }
70                 pr(",", client_data);
71             }
72             if (cn->u.sort.next)
73                 pr(" ", client_data);
74         }
75     }
76 }
77
78 /*
79  * Local variables:
80  * c-basic-offset: 4
81  * c-file-style: "Stroustrup"
82  * indent-tabs-mode: nil
83  * End:
84  * vim: shiftwidth=4 tabstop=8 expandtab
85  */
86