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/alloc.h>
31 #include <libpspp/compiler.h>
32 #include <libpspp/message.h>
33 #include <libpspp/message.h>
34 #include <libpspp/pool.h>
35 #include <libpspp/str.h>
38 #define _(msgid) gettext (msgid)
43 CNT_SINGLE, /* Single value. */
44 CNT_RANGE /* a <= x <= b. */
47 /* Numeric count criteria. */
50 enum value_type type; /* How to interpret a, b. */
51 double a, b; /* Values to count. */
56 struct criteria *next;
58 /* Variables to count. */
59 const struct variable **vars;
62 /* Count special values? */
63 bool count_system_missing; /* Count system missing? */
64 bool count_user_missing; /* Count user missing? */
66 /* Criterion values. */
70 struct num_value *num;
79 struct variable *var; /* Destination variable. */
80 char *name; /* Name of dest var. */
81 struct criteria *crit; /* The criteria specifications. */
86 struct dst_var *dst_vars;
90 static trns_proc_func count_trns_proc;
91 static trns_free_func count_trns_free;
93 static bool parse_numeric_criteria (struct lexer *, struct pool *, struct criteria *);
94 static bool parse_string_criteria (struct lexer *, struct pool *, struct criteria *);
97 cmd_count (struct lexer *lexer, struct dataset *ds)
99 struct dst_var *dv; /* Destination var being parsed. */
100 struct count_trns *trns; /* Transformation. */
102 /* Parses each slash-delimited specification. */
103 trns = pool_create_container (struct count_trns, pool);
104 trns->dst_vars = dv = pool_alloc (trns->pool, sizeof *dv);
107 struct criteria *crit;
109 /* Initialize this struct dst_var to ensure proper cleanup. */
114 /* Get destination variable, or at least its name. */
115 if (!lex_force_id (lexer))
117 dv->var = dict_lookup_var (dataset_dict (ds), lex_tokid (lexer));
120 if (var_is_alpha (dv->var))
122 msg (SE, _("Destination cannot be a string variable."));
127 dv->name = pool_strdup (trns->pool, lex_tokid (lexer));
130 if (!lex_force_match (lexer, '='))
133 crit = dv->crit = pool_alloc (trns->pool, sizeof *crit);
140 if (!parse_variables_const (lexer, dataset_dict (ds), &crit->vars,
142 PV_DUPLICATE | PV_SAME_TYPE))
144 pool_register (trns->pool, free, crit->vars);
146 if (!lex_force_match (lexer, '('))
150 if (var_is_numeric (crit->vars[0]))
151 ok = parse_numeric_criteria (lexer, trns->pool, crit);
153 ok = parse_string_criteria (lexer, trns->pool, crit);
157 if (lex_token (lexer) == '/' || lex_token (lexer) == '.')
160 crit = crit->next = pool_alloc (trns->pool, sizeof *crit);
163 if (lex_token (lexer) == '.')
166 if (!lex_force_match (lexer, '/'))
168 dv = dv->next = pool_alloc (trns->pool, sizeof *dv);
171 /* Create all the nonexistent destination variables. */
172 for (dv = trns->dst_vars; dv; dv = dv->next)
175 /* It's valid, though motivationally questionable, to count to
176 the same dest var more than once. */
177 dv->var = dict_lookup_var (dataset_dict (ds), dv->name);
180 dv->var = dict_create_var_assert (dataset_dict (ds), dv->name, 0);
183 add_transformation (ds, count_trns_proc, count_trns_free, trns);
187 count_trns_free (trns);
191 /* Parses a set of numeric criterion values. Returns success. */
193 parse_numeric_criteria (struct lexer *lexer, struct pool *pool, struct criteria *crit)
195 size_t allocated = 0;
197 crit->values.num = NULL;
198 crit->count_system_missing = false;
199 crit->count_user_missing = false;
204 if (lex_match_id (lexer, "SYSMIS"))
205 crit->count_system_missing = true;
206 else if (lex_match_id (lexer, "MISSING"))
207 crit->count_user_missing = true;
208 else if (parse_num_range (lexer, &low, &high, NULL))
210 struct num_value *cur;
212 if (crit->value_cnt >= allocated)
213 crit->values.num = pool_2nrealloc (pool, crit->values.num,
215 sizeof *crit->values.num);
216 cur = &crit->values.num[crit->value_cnt++];
217 cur->type = low == high ? CNT_SINGLE : CNT_RANGE;
224 lex_match (lexer, ',');
225 if (lex_match (lexer, ')'))
231 /* Parses a set of string criteria values. Returns success. */
233 parse_string_criteria (struct lexer *lexer, struct pool *pool, struct criteria *crit)
236 size_t allocated = 0;
239 for (i = 0; i < crit->var_cnt; i++)
240 if (var_get_width (crit->vars[i]) > len)
241 len = var_get_width (crit->vars[i]);
243 crit->values.str = NULL;
247 if (crit->value_cnt >= allocated)
248 crit->values.str = pool_2nrealloc (pool, crit->values.str,
250 sizeof *crit->values.str);
252 if (!lex_force_string (lexer))
254 cur = &crit->values.str[crit->value_cnt++];
255 *cur = pool_alloc (pool, len + 1);
256 str_copy_rpad (*cur, len + 1, ds_cstr (lex_tokstr (lexer)));
259 lex_match (lexer, ',');
260 if (lex_match (lexer, ')'))
267 /* Transformation. */
269 /* Counts the number of values in case C matching CRIT. */
271 count_numeric (struct criteria *crit, struct ccase *c)
276 for (i = 0; i < crit->var_cnt; i++)
278 double x = case_num (c, crit->vars[i]);
279 if (var_is_num_missing (crit->vars[i], x, MV_ANY))
282 ? crit->count_system_missing
283 : crit->count_user_missing)
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]), *v,
315 var_get_width (crit->vars[i])))
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 (var_is_numeric (crit->vars[0]))
341 counter += count_numeric (crit, c);
343 counter += count_string (crit, c);
344 case_data_rw (c, dv->var)->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);