1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2006 Free Software Foundation, Inc.
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.
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.
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/>. */
19 #include <language/stats/sort-criteria.h>
23 #include <data/case-ordering.h>
24 #include <data/dictionary.h>
25 #include <data/variable.h>
26 #include <language/lexer/lexer.h>
27 #include <language/lexer/variable-parser.h>
28 #include <libpspp/message.h>
31 #define _(msgid) gettext (msgid)
33 /* Parses a list of sort keys and returns a struct sort_criteria
34 based on it. Returns a null pointer on error.
35 If SAW_DIRECTION is nonnull, sets *SAW_DIRECTION to true if at
36 least one parenthesized sort direction was specified, false
38 struct case_ordering *
39 parse_case_ordering (struct lexer *lexer, const struct dictionary *dict,
42 struct case_ordering *ordering = case_ordering_create (dict);
43 const struct variable **vars = NULL;
46 if (saw_direction != NULL)
47 *saw_direction = false;
51 enum sort_direction direction;
57 if (!parse_variables_const (lexer, dict, &vars, &var_cnt, PV_NO_SCRATCH))
61 if (lex_match (lexer, '('))
63 if (lex_match_id (lexer, "D") || lex_match_id (lexer, "DOWN"))
64 direction = SRT_DESCEND;
65 else if (lex_match_id (lexer, "A") || lex_match_id (lexer, "UP"))
66 direction = SRT_ASCEND;
69 msg (SE, _("`A' or `D' expected inside parentheses."));
72 if (!lex_match (lexer, ')'))
74 msg (SE, _("`)' expected."));
77 if (saw_direction != NULL)
78 *saw_direction = true;
81 direction = SRT_ASCEND;
83 for (i = 0; i < var_cnt; i++)
84 if (!case_ordering_add_var (ordering, vars[i], direction))
85 msg (SW, _("Variable %s specified twice in sort criteria."),
86 var_get_name (vars[i]));
88 while (lex_token (lexer) == T_ID
89 && dict_lookup_var (dict, lex_tokid (lexer)) != NULL);
96 case_ordering_destroy (ordering);