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/>. */
23 #include <data/case.h>
24 #include <data/data-in.h>
25 #include <data/format.h>
26 #include <data/dictionary.h>
27 #include <data/procedure.h>
28 #include <data/transformations.h>
29 #include <data/variable.h>
30 #include <language/command.h>
31 #include <language/lexer/lexer.h>
32 #include <language/lexer/variable-parser.h>
33 #include <language/lexer/range-parser.h>
34 #include <libpspp/assertion.h>
35 #include <libpspp/compiler.h>
36 #include <libpspp/message.h>
37 #include <libpspp/pool.h>
38 #include <libpspp/str.h>
43 #define _(msgid) gettext (msgid)
47 /* Type of source value for RECODE. */
50 MAP_SINGLE, /* Specific value. */
51 MAP_RANGE, /* Range of values. */
52 MAP_SYSMIS, /* System missing value. */
53 MAP_MISSING, /* Any missing value. */
54 MAP_ELSE, /* Any value. */
55 MAP_CONVERT /* "123" => 123. */
58 /* A value involved in a RECODE mapping. */
61 double f; /* Numeric. */
62 char *c; /* Short or long string. */
65 /* Describes input values to be mapped. */
68 enum map_in_type type; /* One of MAP_*. */
69 union recode_value x, y; /* Source values. */
72 /* Describes the value used as output from a mapping. */
75 bool copy_input; /* If true, copy input to output. */
76 union recode_value value; /* If copy_input false, recoded value. */
77 int width; /* If copy_input false, output value width. */
80 /* Describes how to recode a single value or range of values into a
84 struct map_in in; /* Input values. */
85 struct map_out out; /* Output value. */
88 /* RECODE transformation. */
93 /* Variable types, for convenience. */
94 enum val_type src_type; /* src_vars[*] type. */
95 enum val_type dst_type; /* dst_vars[*] type. */
98 const struct variable **src_vars; /* Source variables. */
99 const struct variable **dst_vars; /* Destination variables. */
100 char **dst_names; /* Name of dest variables, if they're new. */
101 size_t var_cnt; /* Number of variables. */
104 struct mapping *mappings; /* Value mappings. */
105 size_t map_cnt; /* Number of mappings. */
108 static bool parse_src_vars (struct lexer *, struct recode_trns *, const struct dictionary *dict);
109 static bool parse_mappings (struct lexer *, struct recode_trns *);
110 static bool parse_dst_vars (struct lexer *, struct recode_trns *, const struct dictionary *dict);
112 static void add_mapping (struct recode_trns *,
113 size_t *map_allocated, const struct map_in *);
115 static bool parse_map_in (struct lexer *lexer, struct map_in *, struct pool *,
116 enum val_type src_type, size_t max_src_width);
117 static void set_map_in_generic (struct map_in *, enum map_in_type);
118 static void set_map_in_num (struct map_in *, enum map_in_type, double, double);
119 static void set_map_in_str (struct map_in *, struct pool *,
120 const struct string *, size_t width);
122 static bool parse_map_out (struct lexer *lexer, struct pool *, struct map_out *);
123 static void set_map_out_num (struct map_out *, double);
124 static void set_map_out_str (struct map_out *, struct pool *,
125 const struct string *);
127 static void enlarge_dst_widths (struct recode_trns *);
128 static void create_dst_vars (struct recode_trns *, struct dictionary *);
130 static trns_proc_func recode_trns_proc;
131 static trns_free_func recode_trns_free;
135 /* Parses the RECODE transformation. */
137 cmd_recode (struct lexer *lexer, struct dataset *ds)
141 struct recode_trns *trns
142 = pool_create_container (struct recode_trns, pool);
144 /* Parse source variable names,
145 then input to output mappings,
146 then destintation variable names. */
147 if (!parse_src_vars (lexer, trns, dataset_dict (ds) )
148 || !parse_mappings (lexer, trns)
149 || !parse_dst_vars (lexer, trns, dataset_dict (ds)))
151 recode_trns_free (trns);
155 /* Ensure that all the output strings are at least as wide
156 as the widest destination variable. */
157 if (trns->dst_type == VAL_STRING)
158 enlarge_dst_widths (trns);
160 /* Create destination variables, if needed.
161 This must be the final step; otherwise we'd have to
162 delete destination variables on failure. */
163 if (trns->src_vars != trns->dst_vars)
164 create_dst_vars (trns, dataset_dict (ds));
167 add_transformation (ds,
168 recode_trns_proc, recode_trns_free, trns);
170 while (lex_match (lexer, '/'));
172 return lex_end_of_command (lexer);
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 lexer *lexer,
180 struct recode_trns *trns, const struct dictionary *dict)
182 if (!parse_variables_const (lexer, dict, &trns->src_vars, &trns->var_cnt,
185 pool_register (trns->pool, free, trns->src_vars);
186 trns->src_type = var_get_type (trns->src_vars[0]);
190 /* Parses a set of mappings, which take the form (input=output),
191 into TRNS->mappings and TRNS->map_cnt. Sets TRNS->dst_type.
192 Returns true if successful, false on parse error. */
194 parse_mappings (struct lexer *lexer, struct recode_trns *trns)
196 size_t max_src_width;
197 size_t map_allocated;
201 /* Find length of longest source variable. */
202 max_src_width = var_get_width (trns->src_vars[0]);
203 for (i = 1; i < trns->var_cnt; i++)
205 size_t var_width = var_get_width (trns->src_vars[i]);
206 if (var_width > max_src_width)
207 max_src_width = var_width;
210 /* Parse the mappings in parentheses. */
211 trns->mappings = NULL;
214 have_dst_type = false;
215 if (!lex_force_match (lexer, '('))
219 enum val_type dst_type;
221 if (!lex_match_id (lexer, "CONVERT"))
224 size_t first_map_idx;
227 first_map_idx = trns->map_cnt;
229 /* Parse source specifications. */
233 if (!parse_map_in (lexer, &in, trns->pool,
234 trns->src_type, max_src_width))
236 add_mapping (trns, &map_allocated, &in);
237 lex_match (lexer, ',');
239 while (!lex_match (lexer, '='));
241 if (!parse_map_out (lexer, trns->pool, &out))
243 dst_type = val_type_from_width (out.width);
244 if (have_dst_type && dst_type != trns->dst_type)
246 msg (SE, _("Inconsistent target variable types. "
248 "must be all numeric or all string."));
252 for (i = first_map_idx; i < trns->map_cnt; i++)
253 trns->mappings[i].out = out;
257 /* Parse CONVERT as a special case. */
259 set_map_in_generic (&in, MAP_CONVERT);
260 add_mapping (trns, &map_allocated, &in);
261 set_map_out_num (&trns->mappings[trns->map_cnt - 1].out, 0.0);
263 dst_type = VAL_NUMERIC;
264 if (trns->src_type != VAL_STRING
265 || (have_dst_type && trns->dst_type != VAL_NUMERIC))
267 msg (SE, _("CONVERT requires string input values and "
268 "numeric output values."));
272 trns->dst_type = dst_type;
273 have_dst_type = true;
275 if (!lex_force_match (lexer, ')'))
278 while (lex_match (lexer, '('));
283 /* Parses a mapping input value into IN, allocating memory from
284 POOL. The source value type must be provided as SRC_TYPE and,
285 if string, the maximum width of a string source variable must
286 be provided in MAX_SRC_WIDTH. Returns true if successful,
287 false on parse error. */
289 parse_map_in (struct lexer *lexer, struct map_in *in, struct pool *pool,
290 enum val_type src_type, size_t max_src_width)
292 if (lex_match_id (lexer, "ELSE"))
293 set_map_in_generic (in, MAP_ELSE);
294 else if (src_type == VAL_NUMERIC)
296 if (lex_match_id (lexer, "MISSING"))
297 set_map_in_generic (in, MAP_MISSING);
298 else if (lex_match_id (lexer, "SYSMIS"))
299 set_map_in_generic (in, MAP_SYSMIS);
303 if (!parse_num_range (lexer, &x, &y, NULL))
305 set_map_in_num (in, x == y ? MAP_SINGLE : MAP_RANGE, x, y);
310 if (!lex_force_string (lexer))
312 set_map_in_str (in, pool, lex_tokstr (lexer), max_src_width);
314 if (lex_token (lexer) == T_ID
315 && lex_id_match (ss_cstr ("THRU"), ss_cstr (lex_tokid (lexer))))
317 msg (SE, _("THRU is not allowed with string variables."));
325 /* Adds IN to the list of mappings in TRNS.
326 MAP_ALLOCATED is the current number of allocated mappings,
327 which is updated as needed. */
329 add_mapping (struct recode_trns *trns,
330 size_t *map_allocated, const struct map_in *in)
333 if (trns->map_cnt >= *map_allocated)
334 trns->mappings = pool_2nrealloc (trns->pool, trns->mappings,
336 sizeof *trns->mappings);
337 m = &trns->mappings[trns->map_cnt++];
341 /* Sets IN as a mapping of the given TYPE. */
343 set_map_in_generic (struct map_in *in, enum map_in_type type)
348 /* Sets IN as a numeric mapping of the given TYPE,
349 with X and Y as the two numeric values. */
351 set_map_in_num (struct map_in *in, enum map_in_type type, double x, double y)
358 /* Sets IN as a string mapping, with STRING as the string,
359 allocated from POOL. The string is padded with spaces on the
360 right to WIDTH characters long. */
362 set_map_in_str (struct map_in *in, struct pool *pool,
363 const struct string *string, size_t width)
365 in->type = MAP_SINGLE;
366 in->x.c = pool_alloc_unaligned (pool, width);
367 buf_copy_rpad (in->x.c, width, ds_data (string), ds_length (string));
370 /* Parses a mapping output value into OUT, allocating memory from
371 POOL. Returns true if successful, false on parse error. */
373 parse_map_out (struct lexer *lexer, struct pool *pool, struct map_out *out)
375 if (lex_is_number (lexer))
377 set_map_out_num (out, lex_number (lexer));
380 else if (lex_match_id (lexer, "SYSMIS"))
381 set_map_out_num (out, SYSMIS);
382 else if (lex_token (lexer) == T_STRING)
384 set_map_out_str (out, pool, lex_tokstr (lexer));
387 else if (lex_match_id (lexer, "COPY"))
388 out->copy_input = true;
391 lex_error (lexer, _("expecting output value"));
397 /* Sets OUT as a numeric mapping output with the given VALUE. */
399 set_map_out_num (struct map_out *out, double value)
401 out->copy_input = false;
402 out->value.f = value;
406 /* Sets OUT as a string mapping output with the given VALUE. */
408 set_map_out_str (struct map_out *out, struct pool *pool,
409 const struct string *value)
411 const char *string = ds_data (value);
412 size_t length = ds_length (value);
414 out->copy_input = false;
415 out->value.c = pool_alloc_unaligned (pool, length);
416 memcpy (out->value.c, string, length);
420 /* Parses a set of target variables into TRNS->dst_vars and
423 parse_dst_vars (struct lexer *lexer, struct recode_trns *trns,
424 const struct dictionary *dict)
428 if (lex_match_id (lexer, "INTO"))
433 if (!parse_mixed_vars_pool (lexer, dict, trns->pool,
434 &trns->dst_names, &name_cnt,
438 if (name_cnt != trns->var_cnt)
440 msg (SE, _("%zu variable(s) cannot be recoded into "
441 "%zu variable(s). Specify the same number "
442 "of variables as source and target variables."),
443 trns->var_cnt, name_cnt);
447 trns->dst_vars = pool_nalloc (trns->pool,
448 trns->var_cnt, sizeof *trns->dst_vars);
449 for (i = 0; i < trns->var_cnt; i++)
451 const struct variable *v;
452 v = trns->dst_vars[i] = dict_lookup_var (dict, trns->dst_names[i]);
453 if (v == NULL && trns->dst_type == VAL_STRING)
455 msg (SE, _("There is no variable named "
456 "%s. (All string variables specified "
457 "on INTO must already exist. Use the "
458 "STRING command to create a string "
467 trns->dst_vars = trns->src_vars;
468 if (trns->src_type != trns->dst_type)
470 msg (SE, _("INTO is required with %s input values "
471 "and %s output values."),
472 trns->src_type == VAL_NUMERIC ? _("numeric") : _("string"),
473 trns->dst_type == VAL_NUMERIC ? _("numeric") : _("string"));
478 for (i = 0; i < trns->var_cnt; i++)
480 const struct variable *v = trns->dst_vars[i];
481 if (v != NULL && var_get_type (v) != trns->dst_type)
483 msg (SE, _("Type mismatch. Cannot store %s data in "
485 trns->dst_type == VAL_STRING ? _("string") : _("numeric"),
486 var_is_alpha (v) ? _("string") : _("numeric"),
495 /* Ensures that all the output values in TRNS are as wide as the
496 widest destination variable. */
498 enlarge_dst_widths (struct recode_trns *trns)
500 size_t max_dst_width;
504 for (i = 0; i < trns->var_cnt; i++)
506 const struct variable *v = trns->dst_vars[i];
507 if (var_get_width (v) > max_dst_width)
508 max_dst_width = var_get_width (v);
511 for (i = 0; i < trns->map_cnt; i++)
513 struct map_out *out = &trns->mappings[i].out;
514 if (!out->copy_input && out->width < max_dst_width)
516 char *s = pool_alloc_unaligned (trns->pool, max_dst_width + 1);
517 buf_copy_rpad (s, max_dst_width + 1, out->value.c, out->width);
523 /* Creates destination variables that don't already exist. */
525 create_dst_vars (struct recode_trns *trns, struct dictionary *dict)
529 for (i = 0; i < trns->var_cnt; i++)
531 const struct variable **var = &trns->dst_vars[i];
532 const char *name = trns->dst_names[i];
534 *var = dict_lookup_var (dict, name);
536 *var = dict_create_var_assert (dict, name, 0);
537 assert (var_get_type (*var) == trns->dst_type);
541 /* Data transformation. */
543 /* Returns the output mapping in TRNS for an input of VALUE on
544 variable V, or a null pointer if there is no mapping. */
545 static const struct map_out *
546 find_src_numeric (struct recode_trns *trns, double value, const struct variable *v)
550 for (m = trns->mappings; m < trns->mappings + trns->map_cnt; m++)
552 const struct map_in *in = &m->in;
553 const struct map_out *out = &m->out;
559 match = value == in->x.f;
562 match = var_is_num_missing (v, value, MV_ANY);
565 match = value >= in->x.f && value <= in->y.f;
568 match = value == SYSMIS;
584 /* Returns the output mapping in TRNS for an input of VALUE with
585 the given WIDTH, or a null pointer if there is no mapping. */
586 static const struct map_out *
587 find_src_string (struct recode_trns *trns, const char *value, int width)
591 for (m = trns->mappings; m < trns->mappings + trns->map_cnt; m++)
593 const struct map_in *in = &m->in;
594 struct map_out *out = &m->out;
600 match = !memcmp (value, in->x.c, width);
610 match = data_in (ss_buffer (value, width), LEGACY_NATIVE,
611 FMT_F, 0, 0, &uv, 0);
627 /* Performs RECODE transformation. */
629 recode_trns_proc (void *trns_, struct ccase *c, casenumber case_idx UNUSED)
631 struct recode_trns *trns = trns_;
634 for (i = 0; i < trns->var_cnt; i++)
636 const struct variable *src_var = trns->src_vars[i];
637 const struct variable *dst_var = trns->dst_vars[i];
639 const union value *src_data = case_data (c, src_var);
640 union value *dst_data = case_data_rw (c, dst_var);
642 const struct map_out *out;
644 if (trns->src_type == VAL_NUMERIC)
645 out = find_src_numeric (trns, src_data->f, src_var);
647 out = find_src_string (trns, src_data->s, var_get_width (src_var));
649 if (trns->dst_type == VAL_NUMERIC)
652 dst_data->f = !out->copy_input ? out->value.f : src_data->f;
653 else if (trns->src_vars != trns->dst_vars)
654 dst_data->f = SYSMIS;
660 if (!out->copy_input)
661 memcpy (dst_data->s, out->value.c, var_get_width (dst_var));
662 else if (trns->src_vars != trns->dst_vars)
663 buf_copy_rpad (dst_data->s, var_get_width (dst_var),
664 src_data->s, var_get_width (src_var));
666 else if (trns->src_vars != trns->dst_vars)
667 memset (dst_data->s, ' ', var_get_width (dst_var));
671 return TRNS_CONTINUE;
674 /* Frees a RECODE transformation. */
676 recode_trns_free (void *trns_)
678 struct recode_trns *trns = trns_;
679 pool_destroy (trns->pool);