1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000 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/>. */
21 #include <data/case.h>
22 #include <data/dictionary.h>
23 #include <data/procedure.h>
24 #include <data/transformations.h>
25 #include <data/variable.h>
26 #include <language/command.h>
27 #include <language/lexer/lexer.h>
28 #include <language/lexer/range-parser.h>
29 #include <language/lexer/variable-parser.h>
30 #include <libpspp/compiler.h>
31 #include <libpspp/message.h>
32 #include <libpspp/message.h>
33 #include <libpspp/pool.h>
34 #include <libpspp/str.h>
39 #define _(msgid) gettext (msgid)
44 CNT_SINGLE, /* Single value. */
45 CNT_RANGE /* a <= x <= b. */
48 /* Numeric count criteria. */
51 enum value_type type; /* How to interpret a, b. */
52 double a, b; /* Values to count. */
57 struct criteria *next;
59 /* Variables to count. */
60 const struct variable **vars;
63 /* Count special values? */
64 bool count_system_missing; /* Count system missing? */
65 bool count_user_missing; /* Count user missing? */
67 /* Criterion values. */
71 struct num_value *num;
80 struct variable *var; /* Destination variable. */
81 char *name; /* Name of dest var. */
82 struct criteria *crit; /* The criteria specifications. */
87 struct dst_var *dst_vars;
91 static trns_proc_func count_trns_proc;
92 static trns_free_func count_trns_free;
94 static bool parse_numeric_criteria (struct lexer *, struct pool *, struct criteria *);
95 static bool parse_string_criteria (struct lexer *, struct pool *, struct criteria *);
98 cmd_count (struct lexer *lexer, struct dataset *ds)
100 struct dst_var *dv; /* Destination var being parsed. */
101 struct count_trns *trns; /* Transformation. */
103 /* Parses each slash-delimited specification. */
104 trns = pool_create_container (struct count_trns, pool);
105 trns->dst_vars = dv = pool_alloc (trns->pool, sizeof *dv);
108 struct criteria *crit;
110 /* Initialize this struct dst_var to ensure proper cleanup. */
115 /* Get destination variable, or at least its name. */
116 if (!lex_force_id (lexer))
118 dv->var = dict_lookup_var (dataset_dict (ds), lex_tokid (lexer));
121 if (var_is_alpha (dv->var))
123 msg (SE, _("Destination cannot be a string variable."));
128 dv->name = pool_strdup (trns->pool, lex_tokid (lexer));
131 if (!lex_force_match (lexer, '='))
134 crit = dv->crit = pool_alloc (trns->pool, sizeof *crit);
141 if (!parse_variables_const (lexer, dataset_dict (ds), &crit->vars,
143 PV_DUPLICATE | PV_SAME_TYPE))
145 pool_register (trns->pool, free, crit->vars);
147 if (!lex_force_match (lexer, '('))
151 if (var_is_numeric (crit->vars[0]))
152 ok = parse_numeric_criteria (lexer, trns->pool, crit);
154 ok = parse_string_criteria (lexer, trns->pool, crit);
158 if (lex_token (lexer) == '/' || lex_token (lexer) == '.')
161 crit = crit->next = pool_alloc (trns->pool, sizeof *crit);
164 if (lex_token (lexer) == '.')
167 if (!lex_force_match (lexer, '/'))
169 dv = dv->next = pool_alloc (trns->pool, sizeof *dv);
172 /* Create all the nonexistent destination variables. */
173 for (dv = trns->dst_vars; dv; dv = dv->next)
176 /* It's valid, though motivationally questionable, to count to
177 the same dest var more than once. */
178 dv->var = dict_lookup_var (dataset_dict (ds), dv->name);
181 dv->var = dict_create_var_assert (dataset_dict (ds), dv->name, 0);
184 add_transformation (ds, count_trns_proc, count_trns_free, trns);
188 count_trns_free (trns);
192 /* Parses a set of numeric criterion values. Returns success. */
194 parse_numeric_criteria (struct lexer *lexer, struct pool *pool, struct criteria *crit)
196 size_t allocated = 0;
198 crit->values.num = NULL;
199 crit->count_system_missing = false;
200 crit->count_user_missing = false;
205 if (lex_match_id (lexer, "SYSMIS"))
206 crit->count_system_missing = true;
207 else if (lex_match_id (lexer, "MISSING"))
208 crit->count_user_missing = true;
209 else if (parse_num_range (lexer, &low, &high, NULL))
211 struct num_value *cur;
213 if (crit->value_cnt >= allocated)
214 crit->values.num = pool_2nrealloc (pool, crit->values.num,
216 sizeof *crit->values.num);
217 cur = &crit->values.num[crit->value_cnt++];
218 cur->type = low == high ? CNT_SINGLE : CNT_RANGE;
225 lex_match (lexer, ',');
226 if (lex_match (lexer, ')'))
232 /* Parses a set of string criteria values. Returns success. */
234 parse_string_criteria (struct lexer *lexer, struct pool *pool, struct criteria *crit)
237 size_t allocated = 0;
240 for (i = 0; i < crit->var_cnt; i++)
241 if (var_get_width (crit->vars[i]) > len)
242 len = var_get_width (crit->vars[i]);
244 crit->values.str = NULL;
248 if (crit->value_cnt >= allocated)
249 crit->values.str = pool_2nrealloc (pool, crit->values.str,
251 sizeof *crit->values.str);
253 if (!lex_force_string (lexer))
255 cur = &crit->values.str[crit->value_cnt++];
256 *cur = pool_alloc (pool, len + 1);
257 str_copy_rpad (*cur, len + 1, ds_cstr (lex_tokstr (lexer)));
260 lex_match (lexer, ',');
261 if (lex_match (lexer, ')'))
268 /* Transformation. */
270 /* Counts the number of values in case C matching CRIT. */
272 count_numeric (struct criteria *crit, struct ccase *c)
277 for (i = 0; i < crit->var_cnt; i++)
279 double x = case_num (c, crit->vars[i]);
280 if (var_is_num_missing (crit->vars[i], x, MV_ANY))
283 ? crit->count_system_missing
284 : crit->count_user_missing)
291 for (v = crit->values.num; v < crit->values.num + crit->value_cnt;
293 if (v->type == CNT_SINGLE ? x == v->a : x >= v->a && x <= v->b)
304 /* Counts the number of values in case C matching CRIT. */
306 count_string (struct criteria *crit, struct ccase *c)
311 for (i = 0; i < crit->var_cnt; i++)
314 for (v = crit->values.str; v < crit->values.str + crit->value_cnt; v++)
315 if (!memcmp (case_str (c, crit->vars[i]), *v,
316 var_get_width (crit->vars[i])))
326 /* Performs the COUNT transformation T on case C. */
328 count_trns_proc (void *trns_, struct ccase *c,
329 casenumber case_num UNUSED)
331 struct count_trns *trns = trns_;
334 for (dv = trns->dst_vars; dv; dv = dv->next)
336 struct criteria *crit;
340 for (crit = dv->crit; crit; crit = crit->next)
341 if (var_is_numeric (crit->vars[0]))
342 counter += count_numeric (crit, c);
344 counter += count_string (crit, c);
345 case_data_rw (c, dv->var)->f = counter;
347 return TRNS_CONTINUE;
350 /* Destroys all dynamic data structures associated with TRNS. */
352 count_trns_free (void *trns_)
354 struct count_trns *trns = (struct count_trns *) trns_;
355 pool_destroy (trns->pool);