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
21 #include <libpspp/message.h>
23 #include <libpspp/alloc.h>
24 #include <data/case.h>
25 #include <language/command.h>
26 #include <libpspp/compiler.h>
27 #include <data/dictionary.h>
28 #include <libpspp/message.h>
29 #include <language/lexer/lexer.h>
30 #include <libpspp/pool.h>
31 #include <language/lexer/range-parser.h>
32 #include <libpspp/str.h>
33 #include <data/variable.h>
36 #define _(msgid) gettext (msgid)
41 CNT_SINGLE, /* Single value. */
42 CNT_RANGE /* a <= x <= b. */
45 /* Numeric count criteria. */
48 enum value_type type; /* How to interpret a, b. */
49 double a, b; /* Values to count. */
54 struct criteria *next;
56 /* Variables to count. */
57 struct variable **vars;
60 /* Count special values?. */
61 bool count_system_missing; /* Count system missing? */
62 bool count_user_missing; /* Count user missing? */
64 /* Criterion values. */
68 struct num_value *num;
77 struct variable *var; /* Destination variable. */
78 char *name; /* Name of dest var. */
79 struct criteria *crit; /* The criteria specifications. */
84 struct dst_var *dst_vars;
88 static trns_proc_func count_trns_proc;
89 static trns_free_func count_trns_free;
91 static bool parse_numeric_criteria (struct pool *, struct criteria *);
92 static bool parse_string_criteria (struct pool *, struct criteria *);
97 struct dst_var *dv; /* Destination var being parsed. */
98 struct count_trns *trns; /* Transformation. */
100 /* Parses each slash-delimited specification. */
101 trns = pool_create_container (struct count_trns, pool);
102 trns->dst_vars = dv = pool_alloc (trns->pool, sizeof *dv);
105 struct criteria *crit;
107 /* Initialize this struct dst_var to ensure proper cleanup. */
112 /* Get destination variable, or at least its name. */
113 if (!lex_force_id ())
115 dv->var = dict_lookup_var (default_dict, tokid);
118 if (dv->var->type == ALPHA)
120 msg (SE, _("Destination cannot be a string variable."));
125 dv->name = pool_strdup (trns->pool, tokid);
128 if (!lex_force_match ('='))
131 crit = dv->crit = pool_alloc (trns->pool, sizeof *crit);
138 if (!parse_variables (default_dict, &crit->vars, &crit->var_cnt,
139 PV_DUPLICATE | PV_SAME_TYPE))
141 pool_register (trns->pool, free, crit->vars);
143 if (!lex_force_match ('('))
147 if (crit->vars[0]->type == NUMERIC)
148 ok = parse_numeric_criteria (trns->pool, crit);
150 ok = parse_string_criteria (trns->pool, crit);
154 if (token == '/' || token == '.')
157 crit = crit->next = pool_alloc (trns->pool, sizeof *crit);
163 if (!lex_force_match ('/'))
165 dv = dv->next = pool_alloc (trns->pool, sizeof *dv);
168 /* Create all the nonexistent destination variables. */
169 for (dv = trns->dst_vars; dv; dv = dv->next)
172 /* It's valid, though motivationally questionable, to count to
173 the same dest var more than once. */
174 dv->var = dict_lookup_var (default_dict, dv->name);
177 dv->var = dict_create_var_assert (default_dict, dv->name, 0);
180 add_transformation (count_trns_proc, count_trns_free, trns);
184 count_trns_free (trns);
188 /* Parses a set of numeric criterion values. Returns success. */
190 parse_numeric_criteria (struct pool *pool, struct criteria *crit)
192 size_t allocated = 0;
194 crit->values.num = NULL;
195 crit->count_system_missing = false;
196 crit->count_user_missing = false;
201 if (lex_match_id ("SYSMIS"))
202 crit->count_system_missing = true;
203 else if (lex_match_id ("MISSING"))
204 crit->count_user_missing = true;
205 else if (parse_num_range (&low, &high, NULL))
207 struct num_value *cur;
209 if (crit->value_cnt >= allocated)
210 crit->values.num = pool_2nrealloc (pool, crit->values.num,
212 sizeof *crit->values.num);
213 cur = &crit->values.num[crit->value_cnt++];
214 cur->type = low == high ? CNT_SINGLE : CNT_RANGE;
228 /* Parses a set of string criteria values. Returns success. */
230 parse_string_criteria (struct pool *pool, struct criteria *crit)
233 size_t allocated = 0;
236 for (i = 0; i < crit->var_cnt; i++)
237 if (crit->vars[i]->width > len)
238 len = crit->vars[i]->width;
240 crit->values.str = NULL;
244 if (crit->value_cnt >= allocated)
245 crit->values.str = pool_2nrealloc (pool, crit->values.str,
247 sizeof *crit->values.str);
249 if (!lex_force_string ())
251 cur = &crit->values.str[crit->value_cnt++];
252 *cur = pool_alloc (pool, len + 1);
253 str_copy_rpad (*cur, len + 1, ds_c_str (&tokstr));
264 /* Transformation. */
266 /* Counts the number of values in case C matching CRIT. */
268 count_numeric (struct criteria *crit, struct ccase *c)
273 for (i = 0; i < crit->var_cnt; i++)
275 double x = case_num (c, crit->vars[i]->fv);
277 counter += crit->count_system_missing;
278 else if (crit->count_user_missing
279 && mv_is_num_user_missing (&crit->vars[i]->miss, x))
285 for (v = crit->values.num; v < crit->values.num + crit->value_cnt;
287 if (v->type == CNT_SINGLE ? x == v->a : x >= v->a && x <= v->b)
298 /* Counts the number of values in case C matching CRIT. */
300 count_string (struct criteria *crit, struct ccase *c)
305 for (i = 0; i < crit->var_cnt; i++)
308 for (v = crit->values.str; v < crit->values.str + crit->value_cnt; v++)
309 if (!memcmp (case_str (c, crit->vars[i]->fv), *v,
310 crit->vars[i]->width))
320 /* Performs the COUNT transformation T on case C. */
322 count_trns_proc (void *trns_, struct ccase *c,
325 struct count_trns *trns = trns_;
328 for (dv = trns->dst_vars; dv; dv = dv->next)
330 struct criteria *crit;
334 for (crit = dv->crit; crit; crit = crit->next)
335 if (crit->vars[0]->type == NUMERIC)
336 counter += count_numeric (crit, c);
338 counter += count_string (crit, c);
339 case_data_rw (c, dv->var->fv)->f = counter;
341 return TRNS_CONTINUE;
344 /* Destroys all dynamic data structures associated with TRNS. */
346 count_trns_free (void *trns_)
348 struct count_trns *trns = (struct count_trns *) trns_;
349 pool_destroy (trns->pool);