1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) Index Data
3 * See the file LICENSE for details.
7 * \brief CQL sortkeys utilities
17 #include <yaz/wrbuf.h>
20 static void pr_n(void (*pr)(const char *buf, void *client_data),
21 const char *buf, int len, void *client_data)
28 if (left >= sizeof(tmp))
30 memcpy(tmp, buf, sizeof(tmp)-1);
31 tmp[sizeof(tmp)-1] = '\0';
32 left = left - (sizeof(tmp)-1);
36 memcpy(tmp, buf, left);
44 static int cql_sort_modifiers(struct cql_node *cn,
45 void (*pr)(const char *buf, void *client_data),
49 int caseSensitive = 0;
50 const char *missingValue = "highValue";
51 for (; cn; cn = cn->u.st.modifiers)
53 const char *indx = cn->u.st.index;
54 if (!strncmp(indx, "sort.", 5))
56 if (!strcmp(indx, "ignoreCase"))
58 else if (!strcmp(indx, "respectCase"))
60 else if (!strcmp(indx, "ascending"))
62 else if (!strcmp(indx, "descending"))
64 else if (!strcmp(indx, "missingOmit"))
65 missingValue = "omit";
66 else if (!strcmp(indx, "missingFail"))
67 missingValue = "abort";
68 else if (!strcmp(indx, "missingLow"))
69 missingValue = "lowValue";
70 else if (!strcmp(indx, "missingHigh"))
71 missingValue = "highValue";
75 pr(ascending ? "1" : "0", client_data);
77 pr(caseSensitive ? "1" : "0", client_data);
79 pr(missingValue, client_data);
83 int cql_sortby_to_sortkeys(struct cql_node *cn,
84 void (*pr)(const char *buf, void *client_data),
88 if (cn && cn->which == CQL_NODE_SORT)
90 for (; cn; cn = cn->u.sort.next)
92 const char *indx = cn->u.sort.index;
96 const char *s = strchr(indx, '.');
100 pr(",", client_data);
101 pr_n(pr, indx, s - indx, client_data);
105 pr(indx, client_data);
106 pr(",", client_data);
108 pr(",", client_data);
109 r = cql_sort_modifiers(cn->u.sort.modifiers, pr, client_data);
113 pr(" ", client_data);
120 int cql_sortby_to_sortkeys_buf(struct cql_node *cn, char *out, int max)
122 struct cql_buf_write_info info;
128 r = cql_sortby_to_sortkeys(cn, cql_buf_write_handler, &info);
130 info.buf[info.off] = '\0';
137 * c-file-style: "Stroustrup"
138 * indent-tabs-mode: nil
140 * vim: shiftwidth=4 tabstop=8 expandtab