1 /* PSPP - computes sample statistics.
2 Copyright (C) 1997-9, 2000 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
20 #include <sys/types.h>
24 #include <libpspp/alloc.h>
25 #include <language/command.h>
26 #include <libpspp/message.h>
27 #include <language/lexer/lexer.h>
28 #include <language/lexer/variable-parser.h>
29 #include <data/settings.h>
30 #include <data/variable.h>
31 #include "sort-criteria.h"
32 #include <math/sort.h>
35 #define _(msgid) gettext (msgid)
37 static bool is_terminator(int tok, const int *terminators);
40 /* Parses a list of sort keys and returns a struct sort_criteria
41 based on it. Returns a null pointer on error.
42 If SAW_DIRECTION is nonnull, sets *SAW_DIRECTION to true if at
43 least one parenthesized sort direction was specified, false
45 If TERMINATORS is non-null, then it must be a pointer to a
46 null terminated list of tokens, in addition to the defaults,
47 which are to be considered terminators of the clause being parsed.
48 The default terminators are '/' and '.'
51 struct sort_criteria *
52 sort_parse_criteria (struct lexer *lexer, const struct dictionary *dict,
53 struct variable ***vars, size_t *var_cnt,
55 const int *terminators
58 struct sort_criteria *criteria;
59 struct variable **local_vars = NULL;
62 assert ((vars == NULL) == (var_cnt == NULL));
66 var_cnt = &local_var_cnt;
69 criteria = xmalloc (sizeof *criteria);
70 criteria->crits = NULL;
71 criteria->crit_cnt = 0;
75 if (saw_direction != NULL)
76 *saw_direction = false;
80 size_t prev_var_cnt = *var_cnt;
81 enum sort_direction direction;
84 if (!parse_variables (lexer, dict, vars, var_cnt,
85 PV_NO_DUPLICATE | PV_APPEND | PV_NO_SCRATCH))
89 if (lex_match (lexer, '('))
91 if (lex_match_id (lexer, "D") || lex_match_id (lexer, "DOWN"))
92 direction = SRT_DESCEND;
93 else if (lex_match_id (lexer, "A") || lex_match_id (lexer, "UP"))
94 direction = SRT_ASCEND;
97 msg (SE, _("`A' or `D' expected inside parentheses."));
100 if (!lex_match (lexer, ')'))
102 msg (SE, _("`)' expected."));
105 if (saw_direction != NULL)
106 *saw_direction = true;
109 direction = SRT_ASCEND;
111 criteria->crits = xnrealloc (criteria->crits,
112 *var_cnt, sizeof *criteria->crits);
113 criteria->crit_cnt = *var_cnt;
114 for (; prev_var_cnt < criteria->crit_cnt; prev_var_cnt++)
116 struct sort_criterion *c = &criteria->crits[prev_var_cnt];
117 c->fv = var_get_case_index ((*vars)[prev_var_cnt]);
118 c->width = var_get_width ((*vars)[prev_var_cnt]);
122 while (lex_token (lexer) != '.' && lex_token (lexer) != '/' && !is_terminator(lex_token (lexer), terminators));
129 sort_destroy_criteria (criteria);
133 /* Return TRUE if TOK is a member of the list of TERMINATORS.
136 is_terminator(int tok, const int *terminators)
138 if (terminators == NULL )
141 while ( *terminators)
143 if (tok == *terminators++)
152 /* Destroys a SORT CASES program. */
154 sort_destroy_criteria (struct sort_criteria *criteria)
156 if (criteria != NULL)
158 free (criteria->crits);