1 /* PSPP - computes sample statistics.
2 Copyright (C) 1997-9, 2000, 2006 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 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, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 #include <language/stats/sort-criteria.h>
25 #include <data/case-ordering.h>
26 #include <data/dictionary.h>
27 #include <data/variable.h>
28 #include <language/lexer/lexer.h>
29 #include <language/lexer/variable-parser.h>
30 #include <libpspp/message.h>
33 #define _(msgid) gettext (msgid)
35 /* Parses a list of sort keys and returns a struct sort_criteria
36 based on it. Returns a null pointer on error.
37 If SAW_DIRECTION is nonnull, sets *SAW_DIRECTION to true if at
38 least one parenthesized sort direction was specified, false
40 struct case_ordering *
41 parse_case_ordering (struct lexer *lexer, const struct dictionary *dict,
44 struct case_ordering *ordering = case_ordering_create (dict);
45 struct variable **vars = NULL;
48 if (saw_direction != NULL)
49 *saw_direction = false;
53 enum sort_direction direction;
59 if (!parse_variables_const (lexer, dict, &vars, &var_cnt, PV_NO_SCRATCH))
63 if (lex_match (lexer, '('))
65 if (lex_match_id (lexer, "D") || lex_match_id (lexer, "DOWN"))
66 direction = SRT_DESCEND;
67 else if (lex_match_id (lexer, "A") || lex_match_id (lexer, "UP"))
68 direction = SRT_ASCEND;
71 msg (SE, _("`A' or `D' expected inside parentheses."));
74 if (!lex_match (lexer, ')'))
76 msg (SE, _("`)' expected."));
79 if (saw_direction != NULL)
80 *saw_direction = true;
83 direction = SRT_ASCEND;
85 for (i = 0; i < var_cnt; i++)
86 if (!case_ordering_add_var (ordering, vars[i], direction))
87 msg (SW, _("Variable %s specified twice in sort criteria."),
88 var_get_name (vars[i]));
90 while (lex_token (lexer) == T_ID
91 && dict_lookup_var (dict, lex_tokid (lexer)) != NULL);
98 case_ordering_destroy (ordering);