1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2009, 2010, 2011 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/dataset.h"
23 #include "data/dictionary.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/value-parser.h"
29 #include "language/lexer/variable-parser.h"
30 #include "libpspp/compiler.h"
31 #include "libpspp/i18n.h"
32 #include "libpspp/message.h"
33 #include "libpspp/pool.h"
34 #include "libpspp/str.h"
36 #include "gl/xalloc.h"
39 #define _(msgid) gettext (msgid)
44 CNT_SINGLE, /* Single value. */
45 CNT_RANGE /* a <= x <= b. */
48 /* Numeric count criteria. */
51 enum value_type type; /* How to interpret a, b. */
52 double a, b; /* Values to count. */
57 struct criteria *next;
59 /* Variables to count. */
60 const struct variable **vars;
63 /* Count special values? */
64 bool count_system_missing; /* Count system missing? */
65 bool count_user_missing; /* Count user missing? */
67 /* Criterion values. */
71 struct num_value *num;
80 struct variable *var; /* Destination variable. */
81 char *name; /* Name of dest var. */
82 struct criteria *crit; /* The criteria specifications. */
87 struct dst_var *dst_vars;
91 static trns_proc_func count_trns_proc;
92 static trns_free_func count_trns_free;
94 static bool parse_numeric_criteria (struct lexer *, struct pool *, struct criteria *);
95 static bool parse_string_criteria (struct lexer *, struct pool *,
97 const char *dict_encoding);
100 cmd_count (struct lexer *lexer, 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 (lexer))
120 dv->var = dict_lookup_var (dataset_dict (ds), lex_tokcstr (lexer));
123 if (var_is_alpha (dv->var))
125 msg (SE, _("Destination cannot be a string variable."));
130 dv->name = pool_strdup (trns->pool, lex_tokcstr (lexer));
133 if (!lex_force_match (lexer, T_EQUALS))
136 crit = dv->crit = pool_alloc (trns->pool, sizeof *crit);
139 struct dictionary *dict = dataset_dict (ds);
144 if (!parse_variables_const (lexer, dict, &crit->vars,
146 PV_DUPLICATE | PV_SAME_TYPE))
148 pool_register (trns->pool, free, crit->vars);
150 if (!lex_force_match (lexer, T_LPAREN))
154 if (var_is_numeric (crit->vars[0]))
155 ok = parse_numeric_criteria (lexer, trns->pool, crit);
157 ok = parse_string_criteria (lexer, trns->pool, crit,
158 dict_get_encoding (dict));
162 if (lex_token (lexer) == T_SLASH || lex_token (lexer) == T_ENDCMD)
165 crit = crit->next = pool_alloc (trns->pool, sizeof *crit);
168 if (lex_token (lexer) == T_ENDCMD)
171 if (!lex_force_match (lexer, T_SLASH))
173 dv = dv->next = pool_alloc (trns->pool, sizeof *dv);
176 /* Create all the nonexistent destination variables. */
177 for (dv = trns->dst_vars; dv; dv = dv->next)
180 /* It's valid, though motivationally questionable, to count to
181 the same dest var more than once. */
182 dv->var = dict_lookup_var (dataset_dict (ds), dv->name);
185 dv->var = dict_create_var_assert (dataset_dict (ds), dv->name, 0);
188 add_transformation (ds, count_trns_proc, count_trns_free, trns);
192 count_trns_free (trns);
196 /* Parses a set of numeric criterion values. Returns success. */
198 parse_numeric_criteria (struct lexer *lexer, struct pool *pool, struct criteria *crit)
200 size_t allocated = 0;
202 crit->values.num = NULL;
203 crit->count_system_missing = false;
204 crit->count_user_missing = false;
209 if (lex_match_id (lexer, "SYSMIS"))
210 crit->count_system_missing = true;
211 else if (lex_match_id (lexer, "MISSING"))
212 crit->count_user_missing = true;
213 else if (parse_num_range (lexer, &low, &high, NULL))
215 struct num_value *cur;
217 if (crit->value_cnt >= allocated)
218 crit->values.num = pool_2nrealloc (pool, crit->values.num,
220 sizeof *crit->values.num);
221 cur = &crit->values.num[crit->value_cnt++];
222 cur->type = low == high ? CNT_SINGLE : CNT_RANGE;
229 lex_match (lexer, T_COMMA);
230 if (lex_match (lexer, T_RPAREN))
236 /* Parses a set of string criteria values. Returns success. */
238 parse_string_criteria (struct lexer *lexer, struct pool *pool,
239 struct criteria *crit, const char *dict_encoding)
242 size_t allocated = 0;
245 for (i = 0; i < crit->var_cnt; i++)
246 if (var_get_width (crit->vars[i]) > len)
247 len = var_get_width (crit->vars[i]);
249 crit->values.str = NULL;
255 if (crit->value_cnt >= allocated)
256 crit->values.str = pool_2nrealloc (pool, crit->values.str,
258 sizeof *crit->values.str);
260 if (!lex_force_string (lexer))
263 s = recode_string (dict_encoding, "UTF-8", lex_tokcstr (lexer),
264 ss_length (lex_tokss (lexer)));
266 cur = &crit->values.str[crit->value_cnt++];
267 *cur = pool_alloc (pool, len + 1);
268 str_copy_rpad (*cur, len + 1, s);
273 lex_match (lexer, T_COMMA);
274 if (lex_match (lexer, T_RPAREN))
281 /* Transformation. */
283 /* Counts the number of values in case C matching CRIT. */
285 count_numeric (struct criteria *crit, const struct ccase *c)
290 for (i = 0; i < crit->var_cnt; i++)
292 double x = case_num (c, crit->vars[i]);
293 if (var_is_num_missing (crit->vars[i], x, MV_ANY))
296 ? crit->count_system_missing
297 : crit->count_user_missing)
304 for (v = crit->values.num; v < crit->values.num + crit->value_cnt;
306 if (v->type == CNT_SINGLE ? x == v->a : x >= v->a && x <= v->b)
317 /* Counts the number of values in case C matching CRIT. */
319 count_string (struct criteria *crit, const struct ccase *c)
324 for (i = 0; i < crit->var_cnt; i++)
327 for (v = crit->values.str; v < crit->values.str + crit->value_cnt; v++)
328 if (!memcmp (case_str (c, crit->vars[i]), *v,
329 var_get_width (crit->vars[i])))
339 /* Performs the COUNT transformation T on case C. */
341 count_trns_proc (void *trns_, struct ccase **c,
342 casenumber case_num UNUSED)
344 struct count_trns *trns = trns_;
347 *c = case_unshare (*c);
348 for (dv = trns->dst_vars; dv; dv = dv->next)
350 struct criteria *crit;
354 for (crit = dv->crit; crit; crit = crit->next)
355 if (var_is_numeric (crit->vars[0]))
356 counter += count_numeric (crit, *c);
358 counter += count_string (crit, *c);
359 case_data_rw (*c, dv->var)->f = counter;
361 return TRNS_CONTINUE;
364 /* Destroys all dynamic data structures associated with TRNS. */
366 count_trns_free (void *trns_)
368 struct count_trns *trns = trns_;
369 pool_destroy (trns->pool);