1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2011 Index Data
3 * See the file LICENSE for details.
7 * \brief Implements SortSpec parsing.
17 #include <yaz/proto.h>
18 #include <yaz/sortspec.h>
19 #include <yaz/oid_db.h>
21 Z_SortKeySpecList *yaz_sort_spec (ODR out, const char *arg)
23 char sort_string_buf[64], sort_flags[64];
24 Z_SortKeySpecList *sksl = (Z_SortKeySpecList *)
25 odr_malloc (out, sizeof(*sksl));
29 sksl->specs = (Z_SortKeySpec **)odr_malloc (out, sizeof(sksl->specs) * 20);
31 while ((sscanf (arg, "%63s %63s%n", sort_string_buf,
32 sort_flags, &off)) == 2 && off > 1)
35 char *sort_string_sep;
36 char *sort_string = sort_string_buf;
37 Z_SortKeySpec *sks = (Z_SortKeySpec *)odr_malloc (out, sizeof(*sks));
38 Z_SortKey *sk = (Z_SortKey *)odr_malloc (out, sizeof(*sk));
41 sksl->specs[sksl->num_specs++] = sks;
42 sks->sortElement = (Z_SortElement *)
43 odr_malloc (out, sizeof(*sks->sortElement));
44 sks->sortElement->which = Z_SortElement_generic;
45 sks->sortElement->u.generic = sk;
47 if ((sort_string_sep = strchr (sort_string, '=')))
50 sk->which = Z_SortKey_sortAttributes;
51 sk->u.sortAttributes = (Z_SortAttributes *)
52 odr_malloc (out, sizeof(*sk->u.sortAttributes));
53 sk->u.sortAttributes->id = odr_oiddup(out, yaz_oid_attset_bib_1);
54 sk->u.sortAttributes->list = (Z_AttributeList *)
55 odr_malloc (out, sizeof(*sk->u.sortAttributes->list));
56 sk->u.sortAttributes->list->attributes = (Z_AttributeElement **)
58 sizeof(*sk->u.sortAttributes->list->attributes));
59 while (i < 10 && sort_string && sort_string_sep)
61 Z_AttributeElement *el = (Z_AttributeElement *)
62 odr_malloc (out, sizeof(*el));
63 sk->u.sortAttributes->list->attributes[i] = el;
65 el->attributeType = odr_intdup (out, atoi (sort_string));
66 el->which = Z_AttributeValue_numeric;
68 odr_intdup (out, atoi (sort_string_sep + 1));
70 sort_string = strchr(sort_string, ',');
74 sort_string_sep = strchr (sort_string, '=');
77 sk->u.sortAttributes->list->num_attributes = i;
81 sk->which = Z_SortKey_sortField;
82 sk->u.sortField = odr_strdup (out, sort_string);
84 sks->sortRelation = odr_intdup (out, Z_SortKeySpec_ascending);
85 sks->caseSensitivity = odr_intdup (out, Z_SortKeySpec_caseSensitive);
87 sks->which = Z_SortKeySpec_null;
88 sks->u.null = odr_nullval ();
90 for (i = 0; sort_flags[i]; i++)
92 switch (sort_flags[i])
97 *sks->sortRelation = Z_SortKeySpec_descending;
102 *sks->sortRelation = Z_SortKeySpec_ascending;
106 *sks->caseSensitivity = Z_SortKeySpec_caseInsensitive;
110 *sks->caseSensitivity = Z_SortKeySpec_caseSensitive;
113 sks->which = Z_SortKeySpec_abort;
114 sks->u.abort = odr_nullval();
117 sks->which = Z_SortKeySpec_missingValueData;
118 sks->u.missingValueData = (Odr_oct*)
119 odr_malloc(out, sizeof(Odr_oct));
121 sks->u.missingValueData->len = strlen(sort_flags+i);
122 sks->u.missingValueData->size = sks->u.missingValueData->len;
123 sks->u.missingValueData->buf = (unsigned char*)
124 odr_strdup(out, sort_flags+i);
125 i += strlen(sort_flags+i);
129 if (!sksl->num_specs)
136 * c-file-style: "Stroustrup"
137 * indent-tabs-mode: nil
139 * vim: shiftwidth=4 tabstop=8 expandtab