Added new files resulting from directory restructuring.
[pspp-builds.git] / src / language / stats / sort-criteria.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3    Written by Ben Pfaff <blp@gnu.org>.
4
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.
9
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.
14
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
18    02110-1301, USA. */
19
20 #include <config.h>
21 #include <sys/types.h>
22 #include <assert.h>
23 #include <stdlib.h>
24 #include <limits.h>
25 #include "alloc.h"
26 #include "command.h"
27 #include "message.h"
28 #include "lexer.h"
29 #include "settings.h"
30 #include "variable.h"
31 #include "sort-criteria.h"
32 #include "sort.h"
33
34 #include "gettext.h"
35 #define _(msgid) gettext (msgid)
36
37 static bool  is_terminator(int tok, const int *terminators);
38
39
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
44    otherwise. 
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 '.'
49    
50 */
51 struct sort_criteria *
52 sort_parse_criteria (const struct dictionary *dict,
53                      struct variable ***vars, size_t *var_cnt,
54                      bool *saw_direction,
55                      const int *terminators
56                      )
57 {
58   struct sort_criteria *criteria;
59   struct variable **local_vars = NULL;
60   size_t local_var_cnt;
61
62   assert ((vars == NULL) == (var_cnt == NULL));
63   if (vars == NULL) 
64     {
65       vars = &local_vars;
66       var_cnt = &local_var_cnt;
67     }
68
69   criteria = xmalloc (sizeof *criteria);
70   criteria->crits = NULL;
71   criteria->crit_cnt = 0;
72
73   *vars = NULL;
74   *var_cnt = 0;
75   if (saw_direction != NULL)
76     *saw_direction = false;
77
78   do
79     {
80       size_t prev_var_cnt = *var_cnt;
81       enum sort_direction direction;
82
83       /* Variables. */
84       if (!parse_variables (dict, vars, var_cnt,
85                             PV_NO_DUPLICATE | PV_APPEND | PV_NO_SCRATCH))
86         goto error;
87
88       /* Sort direction. */
89       if (lex_match ('('))
90         {
91           if (lex_match_id ("D") || lex_match_id ("DOWN"))
92             direction = SRT_DESCEND;
93           else if (lex_match_id ("A") || lex_match_id ("UP"))
94             direction = SRT_ASCEND;
95           else
96             {
97               msg (SE, _("`A' or `D' expected inside parentheses."));
98               goto error;
99             }
100           if (!lex_match (')'))
101             {
102               msg (SE, _("`)' expected."));
103               goto error;
104             }
105           if (saw_direction != NULL)
106             *saw_direction = true;
107         }
108       else
109         direction = SRT_ASCEND;
110
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++) 
115         {
116           struct sort_criterion *c = &criteria->crits[prev_var_cnt];
117           c->fv = (*vars)[prev_var_cnt]->fv;
118           c->width = (*vars)[prev_var_cnt]->width;
119           c->dir = direction;
120         }
121     }
122   while (token != '.' && token != '/' && !is_terminator(token, terminators));
123
124   free (local_vars);
125   return criteria;
126
127  error:
128   free (local_vars);
129   sort_destroy_criteria (criteria);
130   return NULL;
131 }
132
133 /* Return TRUE if TOK is a member of the list of TERMINATORS.
134    FALSE otherwise */
135 static bool 
136 is_terminator(int tok, const int *terminators)
137 {
138   if (terminators == NULL ) 
139     return false;
140
141   while ( *terminators) 
142     {
143       if (tok == *terminators++)
144         return true;
145     }
146
147   return false;
148 }
149
150
151
152 /* Destroys a SORT CASES program. */
153 void
154 sort_destroy_criteria (struct sort_criteria *criteria) 
155 {
156   if (criteria != NULL) 
157     {
158       free (criteria->crits);
159       free (criteria);
160     }
161 }
162
163
164