treewide: Replace <name>_cnt by n_<name>s and <name>_cap by allocated_<name>.
[pspp] / src / language / stats / sort-criteria.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2006, 2010, 2011 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 #include "language/stats/sort-criteria.h"
20
21 #include <stdlib.h>
22
23 #include "data/dictionary.h"
24 #include "data/subcase.h"
25 #include "data/variable.h"
26 #include "language/lexer/lexer.h"
27 #include "language/lexer/variable-parser.h"
28 #include "libpspp/message.h"
29
30 #include "gettext.h"
31 #define _(msgid) gettext (msgid)
32
33 /* Parses a list of sort fields and appends them to ORDERING,
34    which the caller must already have initialized.
35    Returns true if successful, false on error.
36    If SAW_DIRECTION is nonnull, sets *SAW_DIRECTION to true if at
37    least one parenthesized sort direction was specified, false
38    otherwise. */
39 bool
40 parse_sort_criteria (struct lexer *lexer, const struct dictionary *dict,
41                      struct subcase *ordering,
42                      const struct variable ***vars, bool *saw_direction)
43 {
44   const struct variable **local_vars = NULL;
45   size_t n_vars = 0;
46
47   if (vars == NULL)
48     vars = &local_vars;
49   *vars = NULL;
50
51   if (saw_direction != NULL)
52     *saw_direction = false;
53
54   do
55     {
56       size_t prev_n_vars = n_vars;
57       enum subcase_direction direction;
58       size_t i;
59
60       /* Variables. */
61       if (!parse_variables_const (lexer, dict, vars, &n_vars,
62                                   PV_APPEND | PV_NO_SCRATCH))
63         goto error;
64
65       /* Sort direction. */
66       if (lex_match (lexer, T_LPAREN))
67         {
68           if (lex_match_id (lexer, "D") || lex_match_id (lexer, "DOWN"))
69             direction = SC_DESCEND;
70           else if (lex_match_id (lexer, "A") || lex_match_id (lexer, "UP"))
71             direction = SC_ASCEND;
72           else
73             {
74               lex_error_expecting (lexer, "A", "D");
75               goto error;
76             }
77           if (!lex_force_match (lexer, T_RPAREN))
78             goto error;
79           if (saw_direction != NULL)
80             *saw_direction = true;
81         }
82       else
83         direction = SC_ASCEND;
84
85       for (i = prev_n_vars; i < n_vars; i++)
86         {
87           const struct variable *var = (*vars)[i];
88           if (!subcase_add_var (ordering, var, direction))
89             msg (SW, _("Variable %s specified twice in sort criteria."),
90                  var_get_name (var));
91         }
92     }
93   while (lex_token (lexer) == T_ID
94          && dict_lookup_var (dict, lex_tokcstr (lexer)) != NULL);
95
96   free (local_vars);
97   return true;
98
99 error:
100   free (local_vars);
101   if (vars)
102     *vars = NULL;
103   return false;
104 }