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 <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 <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 pool *, struct criteria *);
96 static bool parse_string_criteria (struct pool *, struct criteria *);
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 ())
119 dv->var = dict_lookup_var (default_dict, tokid);
122 if (dv->var->type == ALPHA)
124 msg (SE, _("Destination cannot be a string variable."));
129 dv->name = pool_strdup (trns->pool, tokid);
132 if (!lex_force_match ('='))
135 crit = dv->crit = pool_alloc (trns->pool, sizeof *crit);
142 if (!parse_variables (default_dict, &crit->vars, &crit->var_cnt,
143 PV_DUPLICATE | PV_SAME_TYPE))
145 pool_register (trns->pool, free, crit->vars);
147 if (!lex_force_match ('('))
151 if (crit->vars[0]->type == NUMERIC)
152 ok = parse_numeric_criteria (trns->pool, crit);
154 ok = parse_string_criteria (trns->pool, crit);
158 if (token == '/' || token == '.')
161 crit = crit->next = pool_alloc (trns->pool, sizeof *crit);
167 if (!lex_force_match ('/'))
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 (default_dict, dv->name);
181 dv->var = dict_create_var_assert (default_dict, dv->name, 0);
184 add_transformation (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 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 ("SYSMIS"))
206 crit->count_system_missing = true;
207 else if (lex_match_id ("MISSING"))
208 crit->count_user_missing = true;
209 else if (parse_num_range (&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;
232 /* Parses a set of string criteria values. Returns success. */
234 parse_string_criteria (struct pool *pool, struct criteria *crit)
237 size_t allocated = 0;
240 for (i = 0; i < crit->var_cnt; i++)
241 if (crit->vars[i]->width > len)
242 len = crit->vars[i]->width;
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 ())
255 cur = &crit->values.str[crit->value_cnt++];
256 *cur = pool_alloc (pool, len + 1);
257 str_copy_rpad (*cur, len + 1, ds_c_str (&tokstr));
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]->fv);
281 counter += crit->count_system_missing;
282 else if (crit->count_user_missing
283 && mv_is_num_user_missing (&crit->vars[i]->miss, 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]->fv), *v,
314 crit->vars[i]->width))
324 /* Performs the COUNT transformation T on case C. */
326 count_trns_proc (void *trns_, struct ccase *c,
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 (crit->vars[0]->type == NUMERIC)
340 counter += count_numeric (crit, c);
342 counter += count_string (crit, c);
343 case_data_rw (c, dv->var->fv)->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);