1 /* PSPP - computes sample statistics.
2 Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 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, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25 #include <data/case.h>
26 #include <data/data-in.h>
27 #include <data/dictionary.h>
28 #include <data/procedure.h>
29 #include <data/transformations.h>
30 #include <data/variable.h>
31 #include <language/command.h>
32 #include <language/lexer/lexer.h>
33 #include <language/lexer/variable-parser.h>
34 #include <language/lexer/range-parser.h>
35 #include <libpspp/alloc.h>
36 #include <libpspp/assertion.h>
37 #include <libpspp/compiler.h>
38 #include <libpspp/magic.h>
39 #include <libpspp/message.h>
40 #include <libpspp/message.h>
41 #include <libpspp/pool.h>
42 #include <libpspp/str.h>
45 #define _(msgid) gettext (msgid)
49 /* Type of source value for RECODE. */
52 MAP_SINGLE, /* Specific value. */
53 MAP_RANGE, /* Range of values. */
54 MAP_SYSMIS, /* System missing value. */
55 MAP_MISSING, /* Any missing value. */
56 MAP_ELSE, /* Any value. */
57 MAP_CONVERT /* "123" => 123. */
60 /* A value involved in a RECODE mapping. */
63 double f; /* Numeric. */
64 char *c; /* Short or long string. */
67 /* Describes input values to be mapped. */
70 enum map_in_type type; /* One of MAP_*. */
71 union recode_value x, y; /* Source values. */
74 /* Describes the value used as output from a mapping. */
77 bool copy_input; /* If true, copy input to output. */
78 union recode_value value; /* If copy_input false, recoded value. */
79 int width; /* If copy_input false, output value width. */
82 /* Describes how to recode a single value or range of values into a
86 struct map_in in; /* Input values. */
87 struct map_out out; /* Output value. */
90 /* RECODE transformation. */
95 /* Variable types, for convenience. */
96 enum var_type src_type; /* src_vars[*]->type. */
97 enum var_type dst_type; /* dst_vars[*]->type. */
100 struct variable **src_vars; /* Source variables. */
101 struct variable **dst_vars; /* Destination variables. */
102 char **dst_names; /* Name of dest variables, if they're new. */
103 size_t var_cnt; /* Number of variables. */
106 struct mapping *mappings; /* Value mappings. */
107 size_t map_cnt; /* Number of mappings. */
110 static bool parse_src_vars (struct lexer *, struct recode_trns *, const struct dictionary *dict);
111 static bool parse_mappings (struct lexer *, struct recode_trns *);
112 static bool parse_dst_vars (struct lexer *, struct recode_trns *, const struct dictionary *dict);
114 static void add_mapping (struct recode_trns *,
115 size_t *map_allocated, const struct map_in *);
117 static bool parse_map_in (struct lexer *lexer, struct map_in *, struct pool *,
118 enum var_type src_type, size_t max_src_width);
119 static void set_map_in_generic (struct map_in *, enum map_in_type);
120 static void set_map_in_num (struct map_in *, enum map_in_type, double, double);
121 static void set_map_in_str (struct map_in *, struct pool *,
122 const struct string *, size_t width);
124 static bool parse_map_out (struct lexer *lexer, struct pool *, struct map_out *);
125 static void set_map_out_num (struct map_out *, double);
126 static void set_map_out_str (struct map_out *, struct pool *,
127 const struct string *);
129 static void enlarge_dst_widths (struct recode_trns *);
130 static void create_dst_vars (struct recode_trns *, struct dictionary *);
132 static trns_proc_func recode_trns_proc;
133 static trns_free_func recode_trns_free;
137 /* Parses the RECODE transformation. */
139 cmd_recode (struct lexer *lexer, struct dataset *ds)
143 struct recode_trns *trns
144 = pool_create_container (struct recode_trns, pool);
146 /* Parse source variable names,
147 then input to output mappings,
148 then destintation variable names. */
149 if (!parse_src_vars (lexer, trns, dataset_dict (ds) )
150 || !parse_mappings (lexer, trns)
151 || !parse_dst_vars (lexer, trns, dataset_dict (ds)))
153 recode_trns_free (trns);
157 /* Ensure that all the output strings are at least as wide
158 as the widest destination variable. */
159 if (trns->dst_type == VAR_STRING)
160 enlarge_dst_widths (trns);
162 /* Create destination variables, if needed.
163 This must be the final step; otherwise we'd have to
164 delete destination variables on failure. */
165 if (trns->src_vars != trns->dst_vars)
166 create_dst_vars (trns, dataset_dict (ds));
169 add_transformation (ds,
170 recode_trns_proc, recode_trns_free, trns);
172 while (lex_match (lexer, '/'));
174 return lex_end_of_command (lexer);
177 /* Parses a set of variables to recode into TRNS->src_vars and
178 TRNS->var_cnt. Sets TRNS->src_type. Returns true if
179 successful, false on parse error. */
181 parse_src_vars (struct lexer *lexer,
182 struct recode_trns *trns, const struct dictionary *dict)
184 if (!parse_variables (lexer, dict, &trns->src_vars, &trns->var_cnt,
187 pool_register (trns->pool, free, trns->src_vars);
188 trns->src_type = var_get_type (trns->src_vars[0]);
192 /* Parses a set of mappings, which take the form (input=output),
193 into TRNS->mappings and TRNS->map_cnt. Sets TRNS->dst_type.
194 Returns true if successful, false on parse error. */
196 parse_mappings (struct lexer *lexer, struct recode_trns *trns)
198 size_t max_src_width;
199 size_t map_allocated;
203 /* Find length of longest source variable. */
204 max_src_width = var_get_width (trns->src_vars[0]);
205 for (i = 1; i < trns->var_cnt; i++)
207 size_t var_width = var_get_width (trns->src_vars[i]);
208 if (var_width > max_src_width)
209 max_src_width = var_width;
212 /* Parse the mappings in parentheses. */
213 trns->mappings = NULL;
216 have_dst_type = false;
217 if (!lex_force_match (lexer, '('))
221 enum var_type dst_type;
223 if (!lex_match_id (lexer, "CONVERT"))
226 size_t first_map_idx;
229 first_map_idx = trns->map_cnt;
231 /* Parse source specifications. */
235 if (!parse_map_in (lexer, &in, trns->pool,
236 trns->src_type, max_src_width))
238 add_mapping (trns, &map_allocated, &in);
239 lex_match (lexer, ',');
241 while (!lex_match (lexer, '='));
243 if (!parse_map_out (lexer, trns->pool, &out))
245 dst_type = var_type_from_width (out.width);
246 if (have_dst_type && dst_type != trns->dst_type)
248 msg (SE, _("Inconsistent target variable types. "
250 "must be all numeric or all string."));
254 for (i = first_map_idx; i < trns->map_cnt; i++)
255 trns->mappings[i].out = out;
259 /* Parse CONVERT as a special case. */
261 set_map_in_generic (&in, MAP_CONVERT);
262 add_mapping (trns, &map_allocated, &in);
264 dst_type = VAR_NUMERIC;
265 if (trns->src_type != VAR_STRING
266 || (have_dst_type && trns->dst_type != VAR_NUMERIC))
268 msg (SE, _("CONVERT requires string input values and "
269 "numeric output values."));
273 trns->dst_type = dst_type;
274 have_dst_type = true;
276 if (!lex_force_match (lexer, ')'))
279 while (lex_match (lexer, '('));
284 /* Parses a mapping input value into IN, allocating memory from
285 POOL. The source value type must be provided as SRC_TYPE and,
286 if string, the maximum width of a string source variable must
287 be provided in MAX_SRC_WIDTH. Returns true if successful,
288 false on parse error. */
290 parse_map_in (struct lexer *lexer, struct map_in *in, struct pool *pool,
291 enum var_type src_type, size_t max_src_width)
293 if (lex_match_id (lexer, "ELSE"))
294 set_map_in_generic (in, MAP_ELSE);
295 else if (src_type == VAR_NUMERIC)
297 if (lex_match_id (lexer, "MISSING"))
298 set_map_in_generic (in, MAP_MISSING);
299 else if (lex_match_id (lexer, "SYSMIS"))
300 set_map_in_generic (in, MAP_SYSMIS);
304 if (!parse_num_range (lexer, &x, &y, NULL))
306 set_map_in_num (in, x == y ? MAP_SINGLE : MAP_RANGE, x, y);
311 if (!lex_force_string (lexer))
313 set_map_in_str (in, pool, lex_tokstr (lexer), max_src_width);
320 /* Adds IN to the list of mappings in TRNS.
321 MAP_ALLOCATED is the current number of allocated mappings,
322 which is updated as needed. */
324 add_mapping (struct recode_trns *trns,
325 size_t *map_allocated, const struct map_in *in)
328 if (trns->map_cnt >= *map_allocated)
329 trns->mappings = pool_2nrealloc (trns->pool, trns->mappings,
331 sizeof *trns->mappings);
332 m = &trns->mappings[trns->map_cnt++];
336 /* Sets IN as a mapping of the given TYPE. */
338 set_map_in_generic (struct map_in *in, enum map_in_type type)
343 /* Sets IN as a numeric mapping of the given TYPE,
344 with X and Y as the two numeric values. */
346 set_map_in_num (struct map_in *in, enum map_in_type type, double x, double y)
353 /* Sets IN as a string mapping, with STRING as the string,
354 allocated from POOL. The string is padded with spaces on the
355 right to WIDTH characters long. */
357 set_map_in_str (struct map_in *in, struct pool *pool,
358 const struct string *string, size_t width)
360 in->type = MAP_SINGLE;
361 in->x.c = pool_alloc_unaligned (pool, width);
362 buf_copy_rpad (in->x.c, width, ds_data (string), ds_length (string));
365 /* Parses a mapping output value into OUT, allocating memory from
366 POOL. Returns true if successful, false on parse error. */
368 parse_map_out (struct lexer *lexer, struct pool *pool, struct map_out *out)
370 if (lex_is_number (lexer))
372 set_map_out_num (out, lex_number (lexer));
375 else if (lex_match_id (lexer, "SYSMIS"))
376 set_map_out_num (out, SYSMIS);
377 else if (lex_token (lexer) == T_STRING)
379 set_map_out_str (out, pool, lex_tokstr (lexer));
382 else if (lex_match_id (lexer, "COPY"))
383 out->copy_input = true;
386 lex_error (lexer, _("expecting output value"));
392 /* Sets OUT as a numeric mapping output with the given VALUE. */
394 set_map_out_num (struct map_out *out, double value)
396 out->copy_input = false;
397 out->value.f = value;
401 /* Sets OUT as a string mapping output with the given VALUE. */
403 set_map_out_str (struct map_out *out, struct pool *pool,
404 const struct string *value)
406 const char *string = ds_data (value);
407 size_t length = ds_length (value);
409 out->copy_input = false;
410 out->value.c = pool_alloc_unaligned (pool, length);
411 memcpy (out->value.c, string, length);
415 /* Parses a set of target variables into TRNS->dst_vars and
418 parse_dst_vars (struct lexer *lexer, struct recode_trns *trns,
419 const struct dictionary *dict)
423 if (lex_match_id (lexer, "INTO"))
428 if (!parse_mixed_vars_pool (lexer, dict, trns->pool,
429 &trns->dst_names, &name_cnt,
433 if (name_cnt != trns->var_cnt)
435 msg (SE, _("%u variable(s) cannot be recoded into "
436 "%u variable(s). Specify the same number "
437 "of variables as source and target variables."),
438 (unsigned) trns->var_cnt, (unsigned) name_cnt);
442 trns->dst_vars = pool_nalloc (trns->pool,
443 trns->var_cnt, sizeof *trns->dst_vars);
444 for (i = 0; i < trns->var_cnt; i++)
447 v = trns->dst_vars[i] = dict_lookup_var (dict, trns->dst_names[i]);
448 if (v == NULL && trns->dst_type == VAR_STRING)
450 msg (SE, _("There is no variable named "
451 "%s. (All string variables specified "
452 "on INTO must already exist. Use the "
453 "STRING command to create a string "
462 trns->dst_vars = trns->src_vars;
463 if (trns->src_type != trns->dst_type)
465 msg (SE, _("INTO is required with %s input values "
466 "and %s output values."),
467 trns->src_type == VAR_NUMERIC ? _("numeric") : _("string"),
468 trns->dst_type == VAR_NUMERIC ? _("numeric") : _("string"));
473 for (i = 0; i < trns->var_cnt; i++)
475 struct variable *v = trns->dst_vars[i];
476 if (v != NULL && var_get_type (v) != trns->dst_type)
478 msg (SE, _("Type mismatch. Cannot store %s data in "
480 trns->dst_type == VAR_STRING ? _("string") : _("numeric"),
481 var_is_alpha (v) ? _("string") : _("numeric"),
490 /* Ensures that all the output values in TRNS are as wide as the
491 widest destination variable. */
493 enlarge_dst_widths (struct recode_trns *trns)
495 size_t max_dst_width;
499 for (i = 0; i < trns->var_cnt; i++)
501 struct variable *v = trns->dst_vars[i];
502 if (var_get_width (v) > max_dst_width)
503 max_dst_width = var_get_width (v);
506 for (i = 0; i < trns->map_cnt; i++)
508 struct map_out *out = &trns->mappings[i].out;
509 if (!out->copy_input && out->width < max_dst_width)
511 char *s = pool_alloc_unaligned (trns->pool, max_dst_width + 1);
512 str_copy_rpad (s, max_dst_width + 1, out->value.c);
518 /* Creates destination variables that don't already exist. */
520 create_dst_vars (struct recode_trns *trns, struct dictionary *dict)
524 for (i = 0; i < trns->var_cnt; i++)
526 struct variable **var = &trns->dst_vars[i];
527 const char *name = trns->dst_names[i];
529 *var = dict_lookup_var (dict, name);
531 *var = dict_create_var_assert (dict, name, 0);
532 assert (var_get_type (*var) == trns->dst_type);
536 /* Data transformation. */
538 /* Returns the output mapping in TRNS for an input of VALUE on
539 variable V, or a null pointer if there is no mapping. */
540 static const struct map_out *
541 find_src_numeric (struct recode_trns *trns, double value, struct variable *v)
545 for (m = trns->mappings; m < trns->mappings + trns->map_cnt; m++)
547 const struct map_in *in = &m->in;
548 const struct map_out *out = &m->out;
554 match = value == in->x.f;
557 match = var_is_num_missing (v, value, MV_USER);
560 match = value >= in->x.f && value <= in->y.f;
563 match = value == SYSMIS;
579 /* Returns the output mapping in TRNS for an input of VALUE with
580 the given WIDTH, or a null pointer if there is no mapping. */
581 static const struct map_out *
582 find_src_string (struct recode_trns *trns, const char *value, int width)
586 for (m = trns->mappings; m < trns->mappings + trns->map_cnt; m++)
588 const struct map_in *in = &m->in;
589 struct map_out *out = &m->out;
595 match = !memcmp (value, in->x.c, width);
605 match = data_in (ss_buffer (value, width), FMT_F, 0, 0, &uv, 0);
621 /* Performs RECODE transformation. */
623 recode_trns_proc (void *trns_, struct ccase *c, casenumber case_idx UNUSED)
625 struct recode_trns *trns = trns_;
628 for (i = 0; i < trns->var_cnt; i++)
630 struct variable *src_var = trns->src_vars[i];
631 struct variable *dst_var = trns->dst_vars[i];
633 const union value *src_data = case_data (c, src_var);
634 union value *dst_data = case_data_rw (c, dst_var);
636 const struct map_out *out;
638 if (trns->src_type == VAR_NUMERIC)
639 out = find_src_numeric (trns, src_data->f, src_var);
641 out = find_src_string (trns, src_data->s, var_get_width (src_var));
643 if (trns->dst_type == VAR_NUMERIC)
646 dst_data->f = !out->copy_input ? out->value.f : src_data->f;
647 else if (trns->src_vars != trns->dst_vars)
648 dst_data->f = SYSMIS;
654 if (!out->copy_input)
655 memcpy (dst_data->s, out->value.c, var_get_width (dst_var));
656 else if (trns->src_vars != trns->dst_vars)
657 buf_copy_rpad (dst_data->s, var_get_width (dst_var),
658 src_data->s, var_get_width (src_var));
660 else if (trns->src_vars != trns->dst_vars)
661 memset (dst_data->s, ' ', var_get_width (dst_var));
665 return TRNS_CONTINUE;
668 /* Frees a RECODE transformation. */
670 recode_trns_free (void *trns_)
672 struct recode_trns *trns = trns_;
673 pool_destroy (trns->pool);