Update all #include directives to the currently preferred style.
[pspp-builds.git] / 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 var_cnt = 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_var_cnt = var_cnt;
57       enum subcase_direction direction;
58       size_t i;
59
60       /* Variables. */
61       if (!parse_variables_const (lexer, dict, vars, &var_cnt,
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               msg (SE, _("`A' or `D' expected inside parentheses."));
75               goto error;
76             }
77           if (!lex_match (lexer, T_RPAREN))
78             {
79               msg (SE, _("`)' expected."));
80               goto error;
81             }
82           if (saw_direction != NULL)
83             *saw_direction = true;
84         }
85       else
86         direction = SC_ASCEND;
87
88       for (i = prev_var_cnt; i < var_cnt; i++) 
89         {
90           const struct variable *var = (*vars)[i];
91           if (!subcase_add_var (ordering, var, direction))
92             msg (SW, _("Variable %s specified twice in sort criteria."),
93                  var_get_name (var)); 
94         }
95     }
96   while (lex_token (lexer) == T_ID
97          && dict_lookup_var (dict, lex_tokcstr (lexer)) != NULL);
98
99   free (local_vars);
100   return true;
101
102 error:
103   free (local_vars);
104   if (vars)
105     *vars = NULL;
106   return false;
107 }