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
21 #include <libpspp/message.h>
25 #include <libpspp/alloc.h>
26 #include <data/case.h>
27 #include <language/command.h>
28 #include <libpspp/compiler.h>
29 #include <data/data-in.h>
30 #include <data/dictionary.h>
31 #include <libpspp/message.h>
32 #include <language/lexer/lexer.h>
33 #include <libpspp/magic.h>
34 #include <libpspp/pool.h>
35 #include <language/lexer/range-parser.h>
36 #include <libpspp/str.h>
37 #include <data/variable.h>
40 #define _(msgid) gettext (msgid)
44 /* Type of source value for RECODE. */
47 MAP_SINGLE, /* Specific value. */
48 MAP_RANGE, /* Range of values. */
49 MAP_SYSMIS, /* System missing value. */
50 MAP_MISSING, /* Any missing value. */
51 MAP_ELSE, /* Any value. */
52 MAP_CONVERT /* "123" => 123. */
55 /* Describes input values to be mapped. */
58 enum map_in_type type; /* One of MAP_*. */
59 union value x, y; /* Source values. */
62 /* Describes the value used as output from a mapping. */
65 bool copy_input; /* If true, copy input to output. */
66 union value value; /* If copy_input false, recoded value. */
67 int width; /* If copy_input false, output value width. */
70 /* Describes how to recode a single value or range of values into a
74 struct map_in in; /* Input values. */
75 struct map_out out; /* Output value. */
78 /* RECODE transformation. */
83 /* Variable types, for convenience. */
84 enum var_type src_type; /* src_vars[*]->type. */
85 enum var_type dst_type; /* dst_vars[*]->type. */
88 struct variable **src_vars; /* Source variables. */
89 struct variable **dst_vars; /* Destination variables. */
90 char **dst_names; /* Name of dest variables, if they're new. */
91 size_t var_cnt; /* Number of variables. */
94 struct mapping *mappings; /* Value mappings. */
95 size_t map_cnt; /* Number of mappings. */
98 static bool parse_src_vars (struct recode_trns *);
99 static bool parse_mappings (struct recode_trns *);
100 static bool parse_dst_vars (struct recode_trns *);
102 static void add_mapping (struct recode_trns *,
103 size_t *map_allocated, const struct map_in *);
105 static bool parse_map_in (struct map_in *, struct pool *,
106 enum var_type src_type, size_t max_src_width);
107 static void set_map_in_generic (struct map_in *, enum map_in_type);
108 static void set_map_in_num (struct map_in *, enum map_in_type, double, double);
109 static void set_map_in_str (struct map_in *, struct pool *,
110 const struct string *, size_t width);
112 static bool parse_map_out (struct pool *, struct map_out *);
113 static void set_map_out_num (struct map_out *, double);
114 static void set_map_out_str (struct map_out *, struct pool *,
115 const struct string *);
117 static void enlarge_dst_widths (struct recode_trns *);
118 static void create_dst_vars (struct recode_trns *);
120 static trns_proc_func recode_trns_proc;
121 static trns_free_func recode_trns_free;
125 /* Parses the RECODE transformation. */
131 struct recode_trns *trns
132 = pool_create_container (struct recode_trns, pool);
134 /* Parse source variable names,
135 then input to output mappings,
136 then destintation variable names. */
137 if (!parse_src_vars (trns)
138 || !parse_mappings (trns)
139 || !parse_dst_vars (trns))
141 recode_trns_free (trns);
142 return CMD_PART_SUCCESS;
145 /* Ensure that all the output strings are at least as wide
146 as the widest destination variable. */
147 if (trns->dst_type == ALPHA)
148 enlarge_dst_widths (trns);
150 /* Create destination variables, if needed.
151 This must be the final step; otherwise we'd have to
152 delete destination variables on failure. */
153 if (trns->src_vars != trns->dst_vars)
154 create_dst_vars (trns);
157 add_transformation (recode_trns_proc, recode_trns_free, trns);
159 while (lex_match ('/'));
161 return lex_end_of_command ();
164 /* Parses a set of variables to recode into TRNS->src_vars and
165 TRNS->var_cnt. Sets TRNS->src_type. Returns true if
166 successful, false on parse error. */
168 parse_src_vars (struct recode_trns *trns)
170 if (!parse_variables (default_dict, &trns->src_vars, &trns->var_cnt,
173 pool_register (trns->pool, free, trns->src_vars);
174 trns->src_type = trns->src_vars[0]->type;
178 /* Parses a set of mappings, which take the form (input=output),
179 into TRNS->mappings and TRNS->map_cnt. Sets TRNS->dst_type.
180 Returns true if successful, false on parse error. */
182 parse_mappings (struct recode_trns *trns)
184 size_t max_src_width;
185 size_t map_allocated;
189 /* Find length of longest source variable. */
190 max_src_width = trns->src_vars[0]->width;
191 for (i = 1; i < trns->var_cnt; i++)
193 size_t var_width = trns->src_vars[i]->width;
194 if (var_width > max_src_width)
195 max_src_width = var_width;
198 /* Parse the mappings in parentheses. */
199 trns->mappings = NULL;
202 have_dst_type = false;
203 if (!lex_force_match ('('))
207 enum var_type dst_type;
209 if (!lex_match_id ("CONVERT"))
212 size_t first_map_idx;
215 first_map_idx = trns->map_cnt;
217 /* Parse source specifications. */
221 if (!parse_map_in (&in, trns->pool,
222 trns->src_type, max_src_width))
224 add_mapping (trns, &map_allocated, &in);
227 while (!lex_match ('='));
229 if (!parse_map_out (trns->pool, &out))
231 dst_type = out.width == 0 ? NUMERIC : ALPHA;
232 if (have_dst_type && dst_type != trns->dst_type)
234 msg (SE, _("Inconsistent target variable types. "
236 "must be all numeric or all string."));
240 for (i = first_map_idx; i < trns->map_cnt; i++)
241 trns->mappings[i].out = out;
245 /* Parse CONVERT as a special case. */
247 set_map_in_generic (&in, MAP_CONVERT);
248 add_mapping (trns, &map_allocated, &in);
251 if (trns->src_type != ALPHA
252 || (have_dst_type && trns->dst_type != NUMERIC))
254 msg (SE, _("CONVERT requires string input values and "
255 "numeric output values."));
259 trns->dst_type = dst_type;
260 have_dst_type = true;
262 if (!lex_force_match (')'))
265 while (lex_match ('('));
270 /* Parses a mapping input value into IN, allocating memory from
271 POOL. The source value type must be provided as SRC_TYPE and,
272 if string, the maximum width of a string source variable must
273 be provided in MAX_SRC_WIDTH. Returns true if successful,
274 false on parse error. */
276 parse_map_in (struct map_in *in, struct pool *pool,
277 enum var_type src_type, size_t max_src_width)
279 if (lex_match_id ("ELSE"))
280 set_map_in_generic (in, MAP_ELSE);
281 else if (src_type == NUMERIC)
283 if (lex_match_id ("MISSING"))
284 set_map_in_generic (in, MAP_MISSING);
285 else if (lex_match_id ("SYSMIS"))
286 set_map_in_generic (in, MAP_SYSMIS);
290 if (!parse_num_range (&x, &y, NULL))
292 set_map_in_num (in, x == y ? MAP_SINGLE : MAP_RANGE, x, y);
297 if (!lex_force_string ())
299 set_map_in_str (in, pool, &tokstr, max_src_width);
306 /* Adds IN to the list of mappings in TRNS.
307 MAP_ALLOCATED is the current number of allocated mappings,
308 which is updated as needed. */
310 add_mapping (struct recode_trns *trns,
311 size_t *map_allocated, const struct map_in *in)
314 if (trns->map_cnt >= *map_allocated)
315 trns->mappings = pool_2nrealloc (trns->pool, trns->mappings,
317 sizeof *trns->mappings);
318 m = &trns->mappings[trns->map_cnt++];
322 /* Sets IN as a mapping of the given TYPE. */
324 set_map_in_generic (struct map_in *in, enum map_in_type type)
329 /* Sets IN as a numeric mapping of the given TYPE,
330 with X and Y as the two numeric values. */
332 set_map_in_num (struct map_in *in, enum map_in_type type, double x, double y)
339 /* Sets IN as a string mapping, with STRING as the string,
340 allocated from POOL. The string is padded with spaces on the
341 right to WIDTH characters long. */
343 set_map_in_str (struct map_in *in, struct pool *pool,
344 const struct string *string, size_t width)
346 in->type = MAP_SINGLE;
347 in->x.c = pool_alloc_unaligned (pool, width);
348 buf_copy_rpad (in->x.c, width, ds_data (string), ds_length (string));
351 /* Parses a mapping output value into OUT, allocating memory from
352 POOL. Returns true if successful, false on parse error. */
354 parse_map_out (struct pool *pool, struct map_out *out)
356 if (lex_is_number ())
358 set_map_out_num (out, lex_number ());
361 else if (lex_match_id ("SYSMIS"))
362 set_map_out_num (out, SYSMIS);
363 else if (token == T_STRING)
365 set_map_out_str (out, pool, &tokstr);
368 else if (lex_match_id ("COPY"))
369 out->copy_input = true;
372 lex_error (_("expecting output value"));
378 /* Sets OUT as a numeric mapping output with the given VALUE. */
380 set_map_out_num (struct map_out *out, double value)
382 out->copy_input = false;
383 out->value.f = value;
387 /* Sets OUT as a string mapping output with the given VALUE. */
389 set_map_out_str (struct map_out *out, struct pool *pool,
390 const struct string *value)
392 const char *string = ds_data (value);
393 size_t length = ds_length (value);
395 out->copy_input = false;
396 out->value.c = pool_alloc_unaligned (pool, length);
397 memcpy (out->value.c, string, length);
401 /* Parses a set of target variables into TRNS->dst_vars and
404 parse_dst_vars (struct recode_trns *trns)
408 if (lex_match_id ("INTO"))
413 if (!parse_mixed_vars_pool (trns->pool, &trns->dst_names, &name_cnt,
417 if (name_cnt != trns->var_cnt)
419 msg (SE, _("%u variable(s) cannot be recoded into "
420 "%u variable(s). Specify the same number "
421 "of variables as source and target variables."),
422 (unsigned) trns->var_cnt, (unsigned) name_cnt);
426 trns->dst_vars = pool_nalloc (trns->pool,
427 trns->var_cnt, sizeof *trns->dst_vars);
428 for (i = 0; i < trns->var_cnt; i++)
431 v = trns->dst_vars[i] = dict_lookup_var (default_dict,
433 if (v == NULL && trns->dst_type == ALPHA)
435 msg (SE, _("There is no variable named "
436 "%s. (All string variables specified "
437 "on INTO must already exist. Use the "
438 "STRING command to create a string "
447 trns->dst_vars = trns->src_vars;
448 if (trns->src_type != trns->dst_type)
450 msg (SE, _("INTO is required with %s input values "
451 "and %s output values."),
452 var_type_adj (trns->src_type),
453 var_type_adj (trns->dst_type));
458 for (i = 0; i < trns->var_cnt; i++)
460 struct variable *v = trns->dst_vars[i];
461 if (v != NULL && v->type != trns->dst_type)
463 msg (SE, _("Type mismatch. Cannot store %s data in "
465 trns->dst_type == ALPHA ? _("string") : _("numeric"),
466 v->type == ALPHA ? _("string") : _("numeric"),
475 /* Ensures that all the output values in TRNS are as wide as the
476 widest destination variable. */
478 enlarge_dst_widths (struct recode_trns *trns)
480 size_t max_dst_width;
484 for (i = 0; i < trns->var_cnt; i++)
486 struct variable *v = trns->dst_vars[i];
487 if (v->width > max_dst_width)
488 max_dst_width = v->width;
491 for (i = 0; i < trns->map_cnt; i++)
493 struct map_out *out = &trns->mappings[i].out;
494 if (!out->copy_input && out->width < max_dst_width)
496 char *s = pool_alloc_unaligned (trns->pool, max_dst_width + 1);
497 str_copy_rpad (s, max_dst_width + 1, out->value.c);
503 /* Creates destination variables that don't already exist. */
505 create_dst_vars (struct recode_trns *trns)
509 for (i = 0; i < trns->var_cnt; i++)
511 struct variable **var = &trns->dst_vars[i];
512 const char *name = trns->dst_names[i];
514 *var = dict_lookup_var (default_dict, name);
516 *var = dict_create_var_assert (default_dict, name, 0);
517 assert ((*var)->type == trns->dst_type);
521 /* Data transformation. */
523 /* Returns the output mapping in TRNS for an input of VALUE on
524 variable V, or a null pointer if there is no mapping. */
525 static const struct map_out *
526 find_src_numeric (struct recode_trns *trns, double value, struct variable *v)
530 for (m = trns->mappings; m < trns->mappings + trns->map_cnt; m++)
532 const struct map_in *in = &m->in;
533 const struct map_out *out = &m->out;
539 match = value == in->x.f;
542 match = mv_is_num_user_missing (&v->miss, value);
545 match = value >= in->x.f && value <= in->y.f;
561 /* Returns the output mapping in TRNS for an input of VALUE with
562 the given WIDTH, or a null pointer if there is no mapping. */
563 static const struct map_out *
564 find_src_string (struct recode_trns *trns, const char *value, int width)
568 for (m = trns->mappings; m < trns->mappings + trns->map_cnt; m++)
570 const struct map_in *in = &m->in;
571 struct map_out *out = &m->out;
577 match = !memcmp (value, in->x.c, width);
587 di.e = value + width;
589 di.flags = DI_IGNORE_ERROR;
591 di.format.type = FMT_F;
594 match = data_in (&di);
608 /* Performs RECODE transformation. */
610 recode_trns_proc (void *trns_, struct ccase *c, int case_idx UNUSED)
612 struct recode_trns *trns = trns_;
615 for (i = 0; i < trns->var_cnt; i++)
617 struct variable *src_var = trns->src_vars[i];
618 struct variable *dst_var = trns->dst_vars[i];
620 const union value *src_data = case_data (c, src_var->fv);
621 union value *dst_data = case_data_rw (c, dst_var->fv);
623 const struct map_out *out;
625 if (trns->src_type == NUMERIC)
626 out = find_src_numeric (trns, src_data->f, src_var);
628 out = find_src_string (trns, src_data->s, src_var->width);
630 if (trns->dst_type == NUMERIC)
633 dst_data->f = !out->copy_input ? out->value.f : src_data->f;
634 else if (trns->src_vars != trns->dst_vars)
635 dst_data->f = SYSMIS;
641 if (!out->copy_input)
642 memcpy (dst_data->s, out->value.c, dst_var->width);
643 else if (trns->src_vars != trns->dst_vars)
644 buf_copy_rpad (dst_data->s, dst_var->width,
645 src_data->s, src_var->width);
647 else if (trns->src_vars != trns->dst_vars)
648 memset (dst_data->s, ' ', dst_var->width);
652 return TRNS_CONTINUE;
655 /* Frees a RECODE transformation. */
657 recode_trns_free (void *trns_)
659 struct recode_trns *trns = trns_;
660 pool_destroy (trns->pool);