87dc6b4fd5875ecb21e3684d09a77fbed88fdbac
[pspp-builds.git] / src / language / stats / sort-criteria.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3    Written by Ben Pfaff <blp@gnu.org>.
4
5    This program is free software; you can redistribute it and/or
6    modify it under the terms of the GNU General Public License as
7    published by the Free Software Foundation; either version 2 of the
8    License, or (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful, but
11    WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18    02110-1301, USA. */
19
20 #include <config.h>
21 #include <sys/types.h>
22 #include <assert.h>
23 #include <stdlib.h>
24 #include <limits.h>
25 #include <libpspp/alloc.h>
26 #include <language/command.h>
27 #include <libpspp/message.h>
28 #include <language/lexer/lexer.h>
29 #include <language/lexer/variable-parser.h>
30 #include <data/settings.h>
31 #include <data/variable.h>
32 #include "sort-criteria.h"
33 #include <math/sort.h>
34
35 #include "gettext.h"
36 #define _(msgid) gettext (msgid)
37
38 static bool  is_terminator(int tok, const int *terminators);
39
40
41 /* Parses a list of sort keys and returns a struct sort_criteria
42    based on it.  Returns a null pointer on error.
43    If SAW_DIRECTION is nonnull, sets *SAW_DIRECTION to true if at
44    least one parenthesized sort direction was specified, false
45    otherwise. 
46    If TERMINATORS is non-null, then it must be a pointer to a 
47    null terminated list of tokens, in addition to the defaults,
48    which are to be considered terminators of the clause being parsed.
49    The default terminators are '/' and '.'
50    
51 */
52 struct sort_criteria *
53 sort_parse_criteria (struct lexer *lexer, const struct dictionary *dict,
54                      struct variable ***vars, size_t *var_cnt,
55                      bool *saw_direction,
56                      const int *terminators
57                      )
58 {
59   struct sort_criteria *criteria;
60   struct variable **local_vars = NULL;
61   size_t local_var_cnt;
62
63   assert ((vars == NULL) == (var_cnt == NULL));
64   if (vars == NULL) 
65     {
66       vars = &local_vars;
67       var_cnt = &local_var_cnt;
68     }
69
70   criteria = xmalloc (sizeof *criteria);
71   criteria->crits = NULL;
72   criteria->crit_cnt = 0;
73
74   *vars = NULL;
75   *var_cnt = 0;
76   if (saw_direction != NULL)
77     *saw_direction = false;
78
79   do
80     {
81       size_t prev_var_cnt = *var_cnt;
82       enum sort_direction direction;
83
84       /* Variables. */
85       if (!parse_variables (lexer, dict, vars, var_cnt,
86                             PV_NO_DUPLICATE | PV_APPEND | PV_NO_SCRATCH))
87         goto error;
88
89       /* Sort direction. */
90       if (lex_match (lexer, '('))
91         {
92           if (lex_match_id (lexer, "D") || lex_match_id (lexer, "DOWN"))
93             direction = SRT_DESCEND;
94           else if (lex_match_id (lexer, "A") || lex_match_id (lexer, "UP"))
95             direction = SRT_ASCEND;
96           else
97             {
98               msg (SE, _("`A' or `D' expected inside parentheses."));
99               goto error;
100             }
101           if (!lex_match (lexer, ')'))
102             {
103               msg (SE, _("`)' expected."));
104               goto error;
105             }
106           if (saw_direction != NULL)
107             *saw_direction = true;
108         }
109       else
110         direction = SRT_ASCEND;
111
112       criteria->crits = xnrealloc (criteria->crits,
113                                    *var_cnt, sizeof *criteria->crits);
114       criteria->crit_cnt = *var_cnt;
115       for (; prev_var_cnt < criteria->crit_cnt; prev_var_cnt++) 
116         {
117           struct sort_criterion *c = &criteria->crits[prev_var_cnt];
118           c->fv = var_get_case_index ((*vars)[prev_var_cnt]);
119           c->width = var_get_width ((*vars)[prev_var_cnt]);
120           c->dir = direction;
121         }
122     }
123   while (lex_token (lexer) != '.' && lex_token (lexer) != '/' && !is_terminator(lex_token (lexer), terminators));
124
125   free (local_vars);
126   return criteria;
127
128  error:
129   free (local_vars);
130   sort_destroy_criteria (criteria);
131   return NULL;
132 }
133
134 /* Return TRUE if TOK is a member of the list of TERMINATORS.
135    FALSE otherwise */
136 static bool 
137 is_terminator(int tok, const int *terminators)
138 {
139   if (terminators == NULL ) 
140     return false;
141
142   while ( *terminators) 
143     {
144       if (tok == *terminators++)
145         return true;
146     }
147
148   return false;
149 }
150
151
152
153 /* Destroys a SORT CASES program. */
154 void
155 sort_destroy_criteria (struct sort_criteria *criteria) 
156 {
157   if (criteria != NULL) 
158     {
159       free (criteria->crits);
160       free (criteria);
161     }
162 }
163
164
165