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
26 #include <data/case.h>
27 #include <data/data-in.h>
28 #include <data/dictionary.h>
29 #include <data/procedure.h>
30 #include <data/transformations.h>
31 #include <data/variable.h>
32 #include <language/command.h>
33 #include <language/lexer/lexer.h>
34 #include <language/lexer/range-parser.h>
35 #include <libpspp/alloc.h>
36 #include <libpspp/compiler.h>
37 #include <libpspp/magic.h>
38 #include <libpspp/message.h>
39 #include <libpspp/message.h>
40 #include <libpspp/pool.h>
41 #include <libpspp/str.h>
44 #define _(msgid) gettext (msgid)
48 /* Type of source value for RECODE. */
51 MAP_SINGLE, /* Specific value. */
52 MAP_RANGE, /* Range of values. */
53 MAP_SYSMIS, /* System missing value. */
54 MAP_MISSING, /* Any missing value. */
55 MAP_ELSE, /* Any value. */
56 MAP_CONVERT /* "123" => 123. */
59 /* A value involved in a RECODE mapping. */
62 double f; /* Numeric. */
63 char *c; /* Short or long string. */
66 /* Describes input values to be mapped. */
69 enum map_in_type type; /* One of MAP_*. */
70 union recode_value x, y; /* Source values. */
73 /* Describes the value used as output from a mapping. */
76 bool copy_input; /* If true, copy input to output. */
77 union recode_value value; /* If copy_input false, recoded value. */
78 int width; /* If copy_input false, output value width. */
81 /* Describes how to recode a single value or range of values into a
85 struct map_in in; /* Input values. */
86 struct map_out out; /* Output value. */
89 /* RECODE transformation. */
94 /* Variable types, for convenience. */
95 enum var_type src_type; /* src_vars[*]->type. */
96 enum var_type dst_type; /* dst_vars[*]->type. */
99 struct variable **src_vars; /* Source variables. */
100 struct variable **dst_vars; /* Destination variables. */
101 char **dst_names; /* Name of dest variables, if they're new. */
102 size_t var_cnt; /* Number of variables. */
105 struct mapping *mappings; /* Value mappings. */
106 size_t map_cnt; /* Number of mappings. */
109 static bool parse_src_vars (struct recode_trns *);
110 static bool parse_mappings (struct recode_trns *);
111 static bool parse_dst_vars (struct recode_trns *);
113 static void add_mapping (struct recode_trns *,
114 size_t *map_allocated, const struct map_in *);
116 static bool parse_map_in (struct map_in *, struct pool *,
117 enum var_type src_type, size_t max_src_width);
118 static void set_map_in_generic (struct map_in *, enum map_in_type);
119 static void set_map_in_num (struct map_in *, enum map_in_type, double, double);
120 static void set_map_in_str (struct map_in *, struct pool *,
121 const struct string *, size_t width);
123 static bool parse_map_out (struct pool *, struct map_out *);
124 static void set_map_out_num (struct map_out *, double);
125 static void set_map_out_str (struct map_out *, struct pool *,
126 const struct string *);
128 static void enlarge_dst_widths (struct recode_trns *);
129 static void create_dst_vars (struct recode_trns *);
131 static trns_proc_func recode_trns_proc;
132 static trns_free_func recode_trns_free;
136 /* Parses the RECODE transformation. */
142 struct recode_trns *trns
143 = pool_create_container (struct recode_trns, pool);
145 /* Parse source variable names,
146 then input to output mappings,
147 then destintation variable names. */
148 if (!parse_src_vars (trns)
149 || !parse_mappings (trns)
150 || !parse_dst_vars (trns))
152 recode_trns_free (trns);
156 /* Ensure that all the output strings are at least as wide
157 as the widest destination variable. */
158 if (trns->dst_type == ALPHA)
159 enlarge_dst_widths (trns);
161 /* Create destination variables, if needed.
162 This must be the final step; otherwise we'd have to
163 delete destination variables on failure. */
164 if (trns->src_vars != trns->dst_vars)
165 create_dst_vars (trns);
168 add_transformation (recode_trns_proc, recode_trns_free, trns);
170 while (lex_match ('/'));
172 return lex_end_of_command ();
175 /* Parses a set of variables to recode into TRNS->src_vars and
176 TRNS->var_cnt. Sets TRNS->src_type. Returns true if
177 successful, false on parse error. */
179 parse_src_vars (struct recode_trns *trns)
181 if (!parse_variables (default_dict, &trns->src_vars, &trns->var_cnt,
184 pool_register (trns->pool, free, trns->src_vars);
185 trns->src_type = trns->src_vars[0]->type;
189 /* Parses a set of mappings, which take the form (input=output),
190 into TRNS->mappings and TRNS->map_cnt. Sets TRNS->dst_type.
191 Returns true if successful, false on parse error. */
193 parse_mappings (struct recode_trns *trns)
195 size_t max_src_width;
196 size_t map_allocated;
200 /* Find length of longest source variable. */
201 max_src_width = trns->src_vars[0]->width;
202 for (i = 1; i < trns->var_cnt; i++)
204 size_t var_width = trns->src_vars[i]->width;
205 if (var_width > max_src_width)
206 max_src_width = var_width;
209 /* Parse the mappings in parentheses. */
210 trns->mappings = NULL;
213 have_dst_type = false;
214 if (!lex_force_match ('('))
218 enum var_type dst_type;
220 if (!lex_match_id ("CONVERT"))
223 size_t first_map_idx;
226 first_map_idx = trns->map_cnt;
228 /* Parse source specifications. */
232 if (!parse_map_in (&in, trns->pool,
233 trns->src_type, max_src_width))
235 add_mapping (trns, &map_allocated, &in);
238 while (!lex_match ('='));
240 if (!parse_map_out (trns->pool, &out))
242 dst_type = out.width == 0 ? NUMERIC : ALPHA;
243 if (have_dst_type && dst_type != trns->dst_type)
245 msg (SE, _("Inconsistent target variable types. "
247 "must be all numeric or all string."));
251 for (i = first_map_idx; i < trns->map_cnt; i++)
252 trns->mappings[i].out = out;
256 /* Parse CONVERT as a special case. */
258 set_map_in_generic (&in, MAP_CONVERT);
259 add_mapping (trns, &map_allocated, &in);
262 if (trns->src_type != ALPHA
263 || (have_dst_type && trns->dst_type != NUMERIC))
265 msg (SE, _("CONVERT requires string input values and "
266 "numeric output values."));
270 trns->dst_type = dst_type;
271 have_dst_type = true;
273 if (!lex_force_match (')'))
276 while (lex_match ('('));
281 /* Parses a mapping input value into IN, allocating memory from
282 POOL. The source value type must be provided as SRC_TYPE and,
283 if string, the maximum width of a string source variable must
284 be provided in MAX_SRC_WIDTH. Returns true if successful,
285 false on parse error. */
287 parse_map_in (struct map_in *in, struct pool *pool,
288 enum var_type src_type, size_t max_src_width)
290 if (lex_match_id ("ELSE"))
291 set_map_in_generic (in, MAP_ELSE);
292 else if (src_type == NUMERIC)
294 if (lex_match_id ("MISSING"))
295 set_map_in_generic (in, MAP_MISSING);
296 else if (lex_match_id ("SYSMIS"))
297 set_map_in_generic (in, MAP_SYSMIS);
301 if (!parse_num_range (&x, &y, NULL))
303 set_map_in_num (in, x == y ? MAP_SINGLE : MAP_RANGE, x, y);
308 if (!lex_force_string ())
310 set_map_in_str (in, pool, &tokstr, max_src_width);
317 /* Adds IN to the list of mappings in TRNS.
318 MAP_ALLOCATED is the current number of allocated mappings,
319 which is updated as needed. */
321 add_mapping (struct recode_trns *trns,
322 size_t *map_allocated, const struct map_in *in)
325 if (trns->map_cnt >= *map_allocated)
326 trns->mappings = pool_2nrealloc (trns->pool, trns->mappings,
328 sizeof *trns->mappings);
329 m = &trns->mappings[trns->map_cnt++];
333 /* Sets IN as a mapping of the given TYPE. */
335 set_map_in_generic (struct map_in *in, enum map_in_type type)
340 /* Sets IN as a numeric mapping of the given TYPE,
341 with X and Y as the two numeric values. */
343 set_map_in_num (struct map_in *in, enum map_in_type type, double x, double y)
350 /* Sets IN as a string mapping, with STRING as the string,
351 allocated from POOL. The string is padded with spaces on the
352 right to WIDTH characters long. */
354 set_map_in_str (struct map_in *in, struct pool *pool,
355 const struct string *string, size_t width)
357 in->type = MAP_SINGLE;
358 in->x.c = pool_alloc_unaligned (pool, width);
359 buf_copy_rpad (in->x.c, width, ds_data (string), ds_length (string));
362 /* Parses a mapping output value into OUT, allocating memory from
363 POOL. Returns true if successful, false on parse error. */
365 parse_map_out (struct pool *pool, struct map_out *out)
367 if (lex_is_number ())
369 set_map_out_num (out, lex_number ());
372 else if (lex_match_id ("SYSMIS"))
373 set_map_out_num (out, SYSMIS);
374 else if (token == T_STRING)
376 set_map_out_str (out, pool, &tokstr);
379 else if (lex_match_id ("COPY"))
380 out->copy_input = true;
383 lex_error (_("expecting output value"));
389 /* Sets OUT as a numeric mapping output with the given VALUE. */
391 set_map_out_num (struct map_out *out, double value)
393 out->copy_input = false;
394 out->value.f = value;
398 /* Sets OUT as a string mapping output with the given VALUE. */
400 set_map_out_str (struct map_out *out, struct pool *pool,
401 const struct string *value)
403 const char *string = ds_data (value);
404 size_t length = ds_length (value);
406 out->copy_input = false;
407 out->value.c = pool_alloc_unaligned (pool, length);
408 memcpy (out->value.c, string, length);
412 /* Parses a set of target variables into TRNS->dst_vars and
415 parse_dst_vars (struct recode_trns *trns)
419 if (lex_match_id ("INTO"))
424 if (!parse_mixed_vars_pool (trns->pool, &trns->dst_names, &name_cnt,
428 if (name_cnt != trns->var_cnt)
430 msg (SE, _("%u variable(s) cannot be recoded into "
431 "%u variable(s). Specify the same number "
432 "of variables as source and target variables."),
433 (unsigned) trns->var_cnt, (unsigned) name_cnt);
437 trns->dst_vars = pool_nalloc (trns->pool,
438 trns->var_cnt, sizeof *trns->dst_vars);
439 for (i = 0; i < trns->var_cnt; i++)
442 v = trns->dst_vars[i] = dict_lookup_var (default_dict,
444 if (v == NULL && trns->dst_type == ALPHA)
446 msg (SE, _("There is no variable named "
447 "%s. (All string variables specified "
448 "on INTO must already exist. Use the "
449 "STRING command to create a string "
458 trns->dst_vars = trns->src_vars;
459 if (trns->src_type != trns->dst_type)
461 msg (SE, _("INTO is required with %s input values "
462 "and %s output values."),
463 var_type_adj (trns->src_type),
464 var_type_adj (trns->dst_type));
469 for (i = 0; i < trns->var_cnt; i++)
471 struct variable *v = trns->dst_vars[i];
472 if (v != NULL && v->type != trns->dst_type)
474 msg (SE, _("Type mismatch. Cannot store %s data in "
476 trns->dst_type == ALPHA ? _("string") : _("numeric"),
477 v->type == ALPHA ? _("string") : _("numeric"),
486 /* Ensures that all the output values in TRNS are as wide as the
487 widest destination variable. */
489 enlarge_dst_widths (struct recode_trns *trns)
491 size_t max_dst_width;
495 for (i = 0; i < trns->var_cnt; i++)
497 struct variable *v = trns->dst_vars[i];
498 if (v->width > max_dst_width)
499 max_dst_width = v->width;
502 for (i = 0; i < trns->map_cnt; i++)
504 struct map_out *out = &trns->mappings[i].out;
505 if (!out->copy_input && out->width < max_dst_width)
507 char *s = pool_alloc_unaligned (trns->pool, max_dst_width + 1);
508 str_copy_rpad (s, max_dst_width + 1, out->value.c);
514 /* Creates destination variables that don't already exist. */
516 create_dst_vars (struct recode_trns *trns)
520 for (i = 0; i < trns->var_cnt; i++)
522 struct variable **var = &trns->dst_vars[i];
523 const char *name = trns->dst_names[i];
525 *var = dict_lookup_var (default_dict, name);
527 *var = dict_create_var_assert (default_dict, name, 0);
528 assert ((*var)->type == trns->dst_type);
532 /* Data transformation. */
534 /* Returns the output mapping in TRNS for an input of VALUE on
535 variable V, or a null pointer if there is no mapping. */
536 static const struct map_out *
537 find_src_numeric (struct recode_trns *trns, double value, struct variable *v)
541 for (m = trns->mappings; m < trns->mappings + trns->map_cnt; m++)
543 const struct map_in *in = &m->in;
544 const struct map_out *out = &m->out;
550 match = value == in->x.f;
553 match = mv_is_num_user_missing (&v->miss, value);
556 match = value >= in->x.f && value <= in->y.f;
572 /* Returns the output mapping in TRNS for an input of VALUE with
573 the given WIDTH, or a null pointer if there is no mapping. */
574 static const struct map_out *
575 find_src_string (struct recode_trns *trns, const char *value, int width)
579 for (m = trns->mappings; m < trns->mappings + trns->map_cnt; m++)
581 const struct map_in *in = &m->in;
582 struct map_out *out = &m->out;
588 match = !memcmp (value, in->x.c, width);
599 di.e = value + width;
601 di.flags = DI_IGNORE_ERROR;
603 di.format.type = FMT_F;
606 match = data_in (&di);
621 /* Performs RECODE transformation. */
623 recode_trns_proc (void *trns_, struct ccase *c, int 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->fv);
634 union value *dst_data = case_data_rw (c, dst_var->fv);
636 const struct map_out *out;
638 if (trns->src_type == NUMERIC)
639 out = find_src_numeric (trns, src_data->f, src_var);
641 out = find_src_string (trns, src_data->s, src_var->width);
643 if (trns->dst_type == 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, dst_var->width);
656 else if (trns->src_vars != trns->dst_vars)
657 buf_copy_rpad (dst_data->s, dst_var->width,
658 src_data->s, src_var->width);
660 else if (trns->src_vars != trns->dst_vars)
661 memset (dst_data->s, ' ', dst_var->width);
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);