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
24 #include <data/case.h>
25 #include <data/dictionary.h>
26 #include <data/procedure.h>
27 #include <data/transformations.h>
28 #include <data/variable.h>
29 #include <language/command.h>
30 #include <language/lexer/lexer.h>
31 #include <language/lexer/range-parser.h>
32 #include <language/lexer/variable-parser.h>
33 #include <libpspp/alloc.h>
34 #include <libpspp/compiler.h>
35 #include <libpspp/message.h>
36 #include <libpspp/message.h>
37 #include <libpspp/pool.h>
38 #include <libpspp/str.h>
41 #define _(msgid) gettext (msgid)
46 CNT_SINGLE, /* Single value. */
47 CNT_RANGE /* a <= x <= b. */
50 /* Numeric count criteria. */
53 enum value_type type; /* How to interpret a, b. */
54 double a, b; /* Values to count. */
59 struct criteria *next;
61 /* Variables to count. */
62 struct variable **vars;
65 /* Count special values?. */
66 bool count_system_missing; /* Count system missing? */
67 bool count_user_missing; /* Count user missing? */
69 /* Criterion values. */
73 struct num_value *num;
82 struct variable *var; /* Destination variable. */
83 char *name; /* Name of dest var. */
84 struct criteria *crit; /* The criteria specifications. */
89 struct dst_var *dst_vars;
93 static trns_proc_func count_trns_proc;
94 static trns_free_func count_trns_free;
96 static bool parse_numeric_criteria (struct pool *, struct criteria *);
97 static bool parse_string_criteria (struct pool *, struct criteria *);
100 cmd_count (struct dataset *ds)
102 struct dst_var *dv; /* Destination var being parsed. */
103 struct count_trns *trns; /* Transformation. */
105 /* Parses each slash-delimited specification. */
106 trns = pool_create_container (struct count_trns, pool);
107 trns->dst_vars = dv = pool_alloc (trns->pool, sizeof *dv);
110 struct criteria *crit;
112 /* Initialize this struct dst_var to ensure proper cleanup. */
117 /* Get destination variable, or at least its name. */
118 if (!lex_force_id ())
120 dv->var = dict_lookup_var (dataset_dict (ds), tokid);
123 if (dv->var->type == ALPHA)
125 msg (SE, _("Destination cannot be a string variable."));
130 dv->name = pool_strdup (trns->pool, tokid);
133 if (!lex_force_match ('='))
136 crit = dv->crit = pool_alloc (trns->pool, sizeof *crit);
143 if (!parse_variables (dataset_dict (ds), &crit->vars, &crit->var_cnt,
144 PV_DUPLICATE | PV_SAME_TYPE))
146 pool_register (trns->pool, free, crit->vars);
148 if (!lex_force_match ('('))
152 if (crit->vars[0]->type == NUMERIC)
153 ok = parse_numeric_criteria (trns->pool, crit);
155 ok = parse_string_criteria (trns->pool, crit);
159 if (token == '/' || token == '.')
162 crit = crit->next = pool_alloc (trns->pool, sizeof *crit);
168 if (!lex_force_match ('/'))
170 dv = dv->next = pool_alloc (trns->pool, sizeof *dv);
173 /* Create all the nonexistent destination variables. */
174 for (dv = trns->dst_vars; dv; dv = dv->next)
177 /* It's valid, though motivationally questionable, to count to
178 the same dest var more than once. */
179 dv->var = dict_lookup_var (dataset_dict (ds), dv->name);
182 dv->var = dict_create_var_assert (dataset_dict (ds), dv->name, 0);
185 add_transformation (ds, count_trns_proc, count_trns_free, trns);
189 count_trns_free (trns);
193 /* Parses a set of numeric criterion values. Returns success. */
195 parse_numeric_criteria (struct pool *pool, struct criteria *crit)
197 size_t allocated = 0;
199 crit->values.num = NULL;
200 crit->count_system_missing = false;
201 crit->count_user_missing = false;
206 if (lex_match_id ("SYSMIS"))
207 crit->count_system_missing = true;
208 else if (lex_match_id ("MISSING"))
209 crit->count_user_missing = true;
210 else if (parse_num_range (&low, &high, NULL))
212 struct num_value *cur;
214 if (crit->value_cnt >= allocated)
215 crit->values.num = pool_2nrealloc (pool, crit->values.num,
217 sizeof *crit->values.num);
218 cur = &crit->values.num[crit->value_cnt++];
219 cur->type = low == high ? CNT_SINGLE : CNT_RANGE;
233 /* Parses a set of string criteria values. Returns success. */
235 parse_string_criteria (struct pool *pool, struct criteria *crit)
238 size_t allocated = 0;
241 for (i = 0; i < crit->var_cnt; i++)
242 if (crit->vars[i]->width > len)
243 len = crit->vars[i]->width;
245 crit->values.str = NULL;
249 if (crit->value_cnt >= allocated)
250 crit->values.str = pool_2nrealloc (pool, crit->values.str,
252 sizeof *crit->values.str);
254 if (!lex_force_string ())
256 cur = &crit->values.str[crit->value_cnt++];
257 *cur = pool_alloc (pool, len + 1);
258 str_copy_rpad (*cur, len + 1, ds_cstr (&tokstr));
269 /* Transformation. */
271 /* Counts the number of values in case C matching CRIT. */
273 count_numeric (struct criteria *crit, struct ccase *c)
278 for (i = 0; i < crit->var_cnt; i++)
280 double x = case_num (c, crit->vars[i]->fv);
282 counter += crit->count_system_missing;
283 else if (crit->count_user_missing
284 && mv_is_num_user_missing (&crit->vars[i]->miss, x))
290 for (v = crit->values.num; v < crit->values.num + crit->value_cnt;
292 if (v->type == CNT_SINGLE ? x == v->a : x >= v->a && x <= v->b)
303 /* Counts the number of values in case C matching CRIT. */
305 count_string (struct criteria *crit, struct ccase *c)
310 for (i = 0; i < crit->var_cnt; i++)
313 for (v = crit->values.str; v < crit->values.str + crit->value_cnt; v++)
314 if (!memcmp (case_str (c, crit->vars[i]->fv), *v,
315 crit->vars[i]->width))
325 /* Performs the COUNT transformation T on case C. */
327 count_trns_proc (void *trns_, struct ccase *c,
328 casenumber case_num UNUSED)
330 struct count_trns *trns = trns_;
333 for (dv = trns->dst_vars; dv; dv = dv->next)
335 struct criteria *crit;
339 for (crit = dv->crit; crit; crit = crit->next)
340 if (crit->vars[0]->type == NUMERIC)
341 counter += count_numeric (crit, c);
343 counter += count_string (crit, c);
344 case_data_rw (c, dv->var->fv)->f = counter;
346 return TRNS_CONTINUE;
349 /* Destroys all dynamic data structures associated with TRNS. */
351 count_trns_free (void *trns_)
353 struct count_trns *trns = (struct count_trns *) trns_;
354 pool_destroy (trns->pool);