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
23 #include <data/case.h>
24 #include <data/dictionary.h>
25 #include <data/procedure.h>
26 #include <data/transformations.h>
27 #include <data/variable.h>
28 #include <language/command.h>
29 #include <language/lexer/lexer.h>
30 #include <language/lexer/range-parser.h>
31 #include <language/lexer/variable-parser.h>
32 #include <libpspp/alloc.h>
33 #include <libpspp/compiler.h>
34 #include <libpspp/message.h>
35 #include <libpspp/message.h>
36 #include <libpspp/pool.h>
37 #include <libpspp/str.h>
40 #define _(msgid) gettext (msgid)
45 CNT_SINGLE, /* Single value. */
46 CNT_RANGE /* a <= x <= b. */
49 /* Numeric count criteria. */
52 enum value_type type; /* How to interpret a, b. */
53 double a, b; /* Values to count. */
58 struct criteria *next;
60 /* Variables to count. */
61 struct variable **vars;
64 /* Count special values?. */
65 bool count_system_missing; /* Count system missing? */
66 bool count_user_missing; /* Count user missing? */
68 /* Criterion values. */
72 struct num_value *num;
81 struct variable *var; /* Destination variable. */
82 char *name; /* Name of dest var. */
83 struct criteria *crit; /* The criteria specifications. */
88 struct dst_var *dst_vars;
92 static trns_proc_func count_trns_proc;
93 static trns_free_func count_trns_free;
95 static bool parse_numeric_criteria (struct lexer *, struct pool *, struct criteria *);
96 static bool parse_string_criteria (struct lexer *, struct pool *, struct criteria *);
99 cmd_count (struct lexer *lexer, struct dataset *ds)
101 struct dst_var *dv; /* Destination var being parsed. */
102 struct count_trns *trns; /* Transformation. */
104 /* Parses each slash-delimited specification. */
105 trns = pool_create_container (struct count_trns, pool);
106 trns->dst_vars = dv = pool_alloc (trns->pool, sizeof *dv);
109 struct criteria *crit;
111 /* Initialize this struct dst_var to ensure proper cleanup. */
116 /* Get destination variable, or at least its name. */
117 if (!lex_force_id (lexer))
119 dv->var = dict_lookup_var (dataset_dict (ds), lex_tokid (lexer));
122 if (var_is_alpha (dv->var))
124 msg (SE, _("Destination cannot be a string variable."));
129 dv->name = pool_strdup (trns->pool, lex_tokid (lexer));
132 if (!lex_force_match (lexer, '='))
135 crit = dv->crit = pool_alloc (trns->pool, sizeof *crit);
142 if (!parse_variables (lexer, dataset_dict (ds), &crit->vars, &crit->var_cnt,
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]);
281 counter += crit->count_system_missing;
282 else if (crit->count_user_missing
283 && var_is_num_user_missing (crit->vars[i], x))
289 for (v = crit->values.num; v < crit->values.num + crit->value_cnt;
291 if (v->type == CNT_SINGLE ? x == v->a : x >= v->a && x <= v->b)
302 /* Counts the number of values in case C matching CRIT. */
304 count_string (struct criteria *crit, struct ccase *c)
309 for (i = 0; i < crit->var_cnt; i++)
312 for (v = crit->values.str; v < crit->values.str + crit->value_cnt; v++)
313 if (!memcmp (case_str (c, crit->vars[i]), *v,
314 var_get_width (crit->vars[i])))
324 /* Performs the COUNT transformation T on case C. */
326 count_trns_proc (void *trns_, struct ccase *c,
327 casenumber case_num UNUSED)
329 struct count_trns *trns = trns_;
332 for (dv = trns->dst_vars; dv; dv = dv->next)
334 struct criteria *crit;
338 for (crit = dv->crit; crit; crit = crit->next)
339 if (var_is_numeric (crit->vars[0]))
340 counter += count_numeric (crit, c);
342 counter += count_string (crit, c);
343 case_data_rw (c, dv->var)->f = counter;
345 return TRNS_CONTINUE;
348 /* Destroys all dynamic data structures associated with TRNS. */
350 count_trns_free (void *trns_)
352 struct count_trns *trns = (struct count_trns *) trns_;
353 pool_destroy (trns->pool);