Fixed bug where sort flags a and d was reversed.
[yaz-moved-to-github.git] / zutil / sortspec.c
1 /*
2  * Copyright (c) 1995-2001, Index Data.
3  * See the file LICENSE for details.
4  *
5  * $Id: sortspec.c,v 1.2 2001-08-10 12:50:40 adam Exp $
6  */
7
8 #include <stdio.h>
9 #include <string.h>
10 #include <stdlib.h>
11
12 #include <yaz/proto.h>
13 #include <yaz/oid.h>
14 #include <yaz/sortspec.h>
15
16 Z_SortKeySpecList *yaz_sort_spec (ODR out, const char *arg)
17 {
18     int oid[OID_SIZE];
19     oident bib1;
20     char sort_string_buf[32], sort_flags[32];
21     Z_SortKeySpecList *sksl = (Z_SortKeySpecList *)
22         odr_malloc (out, sizeof(*sksl));
23     int off;
24     
25     sksl->num_specs = 0;
26     sksl->specs = (Z_SortKeySpec **)odr_malloc (out, sizeof(sksl->specs) * 20);
27     
28     bib1.proto = PROTO_Z3950;
29     bib1.oclass = CLASS_ATTSET;
30     bib1.value = VAL_BIB1;
31     while ((sscanf (arg, "%31s %31s%n", sort_string_buf,
32                     sort_flags, &off)) == 2  && off > 1)
33     {
34         int i;
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));
39         
40         arg += off;
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;
46         
47         if ((sort_string_sep = strchr (sort_string, '=')))
48         {
49             int i = 0;
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 = oid_ent_to_oid(&bib1, oid);
54             sk->u.sortAttributes->list = (Z_AttributeList *)
55                 odr_malloc (out, sizeof(*sk->u.sortAttributes->list));
56             sk->u.sortAttributes->list->attributes = (Z_AttributeElement **)
57                 odr_malloc (out, 10 * 
58                             sizeof(*sk->u.sortAttributes->list->attributes));
59             while (i < 10 && sort_string && sort_string_sep)
60             {
61                 Z_AttributeElement *el = (Z_AttributeElement *)
62                     odr_malloc (out, sizeof(*el));
63                 sk->u.sortAttributes->list->attributes[i] = el;
64                 el->attributeSet = 0;
65                 el->attributeType = odr_intdup (out, atoi (sort_string));
66                 el->which = Z_AttributeValue_numeric;
67                 el->value.numeric =
68                     odr_intdup (out, atoi (sort_string_sep + 1));
69                 i++;
70                 sort_string = strchr(sort_string, ',');
71                 if (sort_string)
72                 {
73                     sort_string++;
74                     sort_string_sep = strchr (sort_string, '=');
75                 }
76             }
77             sk->u.sortAttributes->list->num_attributes = i;
78         }
79         else
80         {
81             sk->which = Z_SortKey_sortField;
82             sk->u.sortField = odr_strdup (out, sort_string);
83         }
84         sks->sortRelation = odr_intdup (out, Z_SortRelation_ascending);
85         sks->caseSensitivity = odr_intdup (out, Z_SortCase_caseSensitive);
86
87 #ifdef ASN_COMPILED
88         sks->which = Z_SortKeySpec_null;
89         sks->u.null = odr_nullval ();
90 #else
91         sks->missingValueAction = NULL;
92 #endif
93         
94         for (i = 0; sort_flags[i]; i++)
95         {
96             switch (sort_flags[i])
97             {
98             case 'd':
99             case 'D':
100             case '>':
101                 *sks->sortRelation = Z_SortRelation_descending;
102                 break;
103             case 'a':
104             case 'A':
105             case '<':
106                 *sks->sortRelation = Z_SortRelation_ascending;
107                 break;
108             case 'i':
109             case 'I':
110                 *sks->caseSensitivity = Z_SortCase_caseInsensitive;
111                 break;
112             case 'S':
113             case 's':
114                 *sks->caseSensitivity = Z_SortCase_caseSensitive;
115                 break;
116             }
117         }
118     }
119     if (!sksl->num_specs)
120         return 0;
121     return sksl;
122 }