1 /* PSPP - computes sample statistics.
2 Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3 Written by Ben Pfaff <blp@gnu.org>.
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 #include <sys/types.h>
32 #define _(msgid) gettext (msgid)
34 static bool is_terminator(int tok, const int *terminators);
37 /* Parses a list of sort keys and returns a struct sort_criteria
38 based on it. Returns a null pointer on error.
39 If SAW_DIRECTION is nonnull, sets *SAW_DIRECTION to true if at
40 least one parenthesized sort direction was specified, false
42 If TERMINATORS is non-null, then it must be a pointer to a
43 null terminated list of tokens, in addition to the defaults,
44 which are to be considered terminators of the clause being parsed.
45 The default terminators are '/' and '.'
48 struct sort_criteria *
49 sort_parse_criteria (const struct dictionary *dict,
50 struct variable ***vars, int *var_cnt,
52 const int *terminators
55 struct sort_criteria *criteria;
56 struct variable **local_vars = NULL;
59 assert ((vars == NULL) == (var_cnt == NULL));
63 var_cnt = &local_var_cnt;
66 criteria = xmalloc (sizeof *criteria);
67 criteria->crits = NULL;
68 criteria->crit_cnt = 0;
72 if (saw_direction != NULL)
73 *saw_direction = false;
77 int prev_var_cnt = *var_cnt;
78 enum sort_direction direction;
81 if (!parse_variables (dict, vars, var_cnt,
82 PV_NO_DUPLICATE | PV_APPEND | PV_NO_SCRATCH))
88 if (lex_match_id ("D") || lex_match_id ("DOWN"))
89 direction = SRT_DESCEND;
90 else if (lex_match_id ("A") || lex_match_id ("UP"))
91 direction = SRT_ASCEND;
94 msg (SE, _("`A' or `D' expected inside parentheses."));
99 msg (SE, _("`)' expected."));
102 if (saw_direction != NULL)
103 *saw_direction = true;
106 direction = SRT_ASCEND;
108 criteria->crits = xrealloc (criteria->crits,
109 sizeof *criteria->crits * *var_cnt);
110 criteria->crit_cnt = *var_cnt;
111 for (; prev_var_cnt < criteria->crit_cnt; prev_var_cnt++)
113 struct sort_criterion *c = &criteria->crits[prev_var_cnt];
114 c->fv = (*vars)[prev_var_cnt]->fv;
115 c->width = (*vars)[prev_var_cnt]->width;
119 while (token != '.' && token != '/' && !is_terminator(token, terminators));
126 sort_destroy_criteria (criteria);
130 /* Return TRUE if TOK is a member of the list of TERMINATORS.
133 is_terminator(int tok, const int *terminators)
135 if (terminators == NULL )
138 while ( *terminators)
140 if (tok == *terminators++)
149 /* Destroys a SORT CASES program. */
151 sort_destroy_criteria (struct sort_criteria *criteria)
153 if (criteria != NULL)
155 free (criteria->crits);