42fa9ff2157a531671b5aa2f173b56a1c203a10f
[pspp] / src / language / xforms / recode.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2009, 2010, 2011 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>. */
16
17 #include <config.h>
18
19 #include <ctype.h>
20 #include <math.h>
21 #include <stdlib.h>
22
23 #include "data/case.h"
24 #include "data/data-in.h"
25 #include "data/dataset.h"
26 #include "data/dictionary.h"
27 #include "data/format.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/value-parser.h"
33 #include "language/lexer/variable-parser.h"
34 #include "libpspp/assertion.h"
35 #include "libpspp/cast.h"
36 #include "libpspp/compiler.h"
37 #include "libpspp/i18n.h"
38 #include "libpspp/message.h"
39 #include "libpspp/pool.h"
40 #include "libpspp/str.h"
41
42 #include "gl/xalloc.h"
43
44 #include "gettext.h"
45 #define _(msgid) gettext (msgid)
46 \f
47 /* Definitions. */
48
49 /* Type of source value for RECODE. */
50 enum map_in_type
51   {
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. */
58   };
59
60 /* Describes input values to be mapped. */
61 struct map_in
62   {
63     enum map_in_type type;      /* One of MAP_*. */
64     union value x, y;           /* Source values. */
65   };
66
67 /* Describes the value used as output from a mapping. */
68 struct map_out
69   {
70     bool copy_input;            /* If true, copy input to output. */
71     union value value;          /* If copy_input false, recoded value. */
72     int width;                  /* If copy_input false, output value width. */
73   };
74
75 /* Describes how to recode a single value or range of values into a
76    single value.  */
77 struct mapping
78   {
79     struct map_in in;           /* Input values. */
80     struct map_out out;         /* Output value. */
81   };
82
83 /* RECODE transformation. */
84 struct recode_trns
85   {
86     struct pool *pool;
87
88     /* Variable types, for convenience. */
89     enum val_type src_type;     /* src_vars[*] type. */
90     enum val_type dst_type;     /* dst_vars[*] type. */
91
92     /* Variables. */
93     const struct variable **src_vars;   /* Source variables. */
94     const struct variable **dst_vars;   /* Destination variables. */
95     const struct dictionary *dst_dict;  /* Dictionary of dst_vars */
96     char **dst_names;           /* Name of dest variables, if they're new. */
97     size_t n_vars;             /* Number of variables. */
98
99     /* Mappings. */
100     struct mapping *mappings;   /* Value mappings. */
101     size_t n_maps;              /* Number of mappings. */
102     int max_src_width;          /* Maximum width of src_vars[*]. */
103     int max_dst_width;          /* Maximum width of any map_out in mappings. */
104   };
105
106 static bool parse_src_vars (struct lexer *, struct recode_trns *, const struct dictionary *dict);
107 static bool parse_mappings (struct lexer *, struct recode_trns *,
108                             const char *dict_encoding);
109 static bool parse_dst_vars (struct lexer *, struct recode_trns *, const struct dictionary *dict);
110
111 static void add_mapping (struct recode_trns *,
112                          size_t *map_allocated, const struct map_in *);
113
114 static bool parse_map_in (struct lexer *lexer, struct map_in *, struct pool *,
115                           enum val_type src_type, size_t max_src_width,
116                           const char *dict_encoding);
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                             struct substring, size_t width,
121                             const char *dict_encoding);
122
123 static bool parse_map_out (struct lexer *lexer, 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                              struct substring);
127
128 static bool enlarge_dst_widths (struct recode_trns *);
129 static void create_dst_vars (struct recode_trns *, struct dictionary *);
130
131 static bool recode_trns_free (void *trns_);
132
133 static const struct trns_class recode_trns_class;
134 \f
135 /* Parser. */
136
137 /* Parses the RECODE transformation. */
138 int
139 cmd_recode (struct lexer *lexer, struct dataset *ds)
140 {
141   do
142     {
143       struct dictionary *dict = dataset_dict (ds);
144       struct recode_trns *trns
145         = pool_create_container (struct recode_trns, pool);
146
147       /* Parse source variable names,
148          then input to output mappings,
149          then destintation variable names. */
150       if (!parse_src_vars (lexer, trns, dict)
151           || !parse_mappings (lexer, trns, dict_get_encoding (dict))
152           || !parse_dst_vars (lexer, trns, dict))
153         {
154           recode_trns_free (trns);
155           return CMD_FAILURE;
156         }
157
158       /* Ensure that all the output strings are at least as wide
159          as the widest destination variable. */
160       if (trns->dst_type == VAL_STRING)
161         {
162           if (! enlarge_dst_widths (trns))
163             {
164               recode_trns_free (trns);
165               return CMD_FAILURE;
166             }
167         }
168
169       /* Create destination variables, if needed.
170          This must be the final step; otherwise we'd have to
171          delete destination variables on failure. */
172       trns->dst_dict = dict;
173       if (trns->src_vars != trns->dst_vars)
174         create_dst_vars (trns, dict);
175
176       /* Done. */
177       add_transformation (ds, &recode_trns_class, trns);
178     }
179   while (lex_match (lexer, T_SLASH));
180
181   return CMD_SUCCESS;
182 }
183
184 /* Parses a set of variables to recode into TRNS->src_vars and
185    TRNS->n_vars.  Sets TRNS->src_type.  Returns true if
186    successful, false on parse error. */
187 static bool
188 parse_src_vars (struct lexer *lexer,
189                 struct recode_trns *trns, const struct dictionary *dict)
190 {
191   if (!parse_variables_const (lexer, dict, &trns->src_vars, &trns->n_vars,
192                         PV_SAME_TYPE))
193     return false;
194   pool_register (trns->pool, free, trns->src_vars);
195   trns->src_type = var_get_type (trns->src_vars[0]);
196   return true;
197 }
198
199 /* Parses a set of mappings, which take the form (input=output),
200    into TRNS->mappings and TRNS->n_maps.  Sets TRNS->dst_type.
201    Returns true if successful, false on parse error. */
202 static bool
203 parse_mappings (struct lexer *lexer, struct recode_trns *trns,
204                 const char *dict_encoding)
205 {
206   size_t map_allocated;
207   bool have_dst_type;
208   size_t i;
209
210   /* Find length of longest source variable. */
211   trns->max_src_width = var_get_width (trns->src_vars[0]);
212   for (i = 1; i < trns->n_vars; i++)
213     {
214       size_t var_width = var_get_width (trns->src_vars[i]);
215       if (var_width > trns->max_src_width)
216         trns->max_src_width = var_width;
217     }
218
219   /* Parse the mappings in parentheses. */
220   trns->mappings = NULL;
221   trns->n_maps = 0;
222   map_allocated = 0;
223   have_dst_type = false;
224   if (!lex_force_match (lexer, T_LPAREN))
225     return false;
226   do
227     {
228       enum val_type dst_type;
229
230       if (!lex_match_id (lexer, "CONVERT"))
231         {
232           struct map_out out;
233           size_t first_map_idx;
234           size_t i;
235
236           first_map_idx = trns->n_maps;
237
238           /* Parse source specifications. */
239           do
240             {
241               struct map_in in;
242
243               if (!parse_map_in (lexer, &in, trns->pool,
244                                  trns->src_type, trns->max_src_width,
245                                  dict_encoding))
246                 return false;
247               add_mapping (trns, &map_allocated, &in);
248               lex_match (lexer, T_COMMA);
249             }
250           while (!lex_match (lexer, T_EQUALS));
251
252           if (!parse_map_out (lexer, trns->pool, &out))
253             return false;
254
255           if (out.copy_input)
256             dst_type = trns->src_type;
257           else
258             dst_type = val_type_from_width (out.width);
259           if (have_dst_type && dst_type != trns->dst_type)
260             {
261               msg (SE, _("Inconsistent target variable types.  "
262                          "Target variables "
263                          "must be all numeric or all string."));
264               return false;
265             }
266
267           for (i = first_map_idx; i < trns->n_maps; i++)
268             trns->mappings[i].out = out;
269         }
270       else
271         {
272           /* Parse CONVERT as a special case. */
273           struct map_in in;
274           set_map_in_generic (&in, MAP_CONVERT);
275           add_mapping (trns, &map_allocated, &in);
276           set_map_out_num (&trns->mappings[trns->n_maps - 1].out, 0.0);
277
278           dst_type = VAL_NUMERIC;
279           if (trns->src_type != VAL_STRING
280               || (have_dst_type && trns->dst_type != VAL_NUMERIC))
281             {
282               lex_next_error (lexer, -1, -1,
283                               _("CONVERT requires string input values and "
284                                 "numeric output values."));
285               return false;
286             }
287         }
288       trns->dst_type = dst_type;
289       have_dst_type = true;
290
291       if (!lex_force_match (lexer, T_RPAREN))
292         return false;
293     }
294   while (lex_match (lexer, T_LPAREN));
295
296   return true;
297 }
298
299 /* Parses a mapping input value into IN, allocating memory from
300    POOL.  The source value type must be provided as SRC_TYPE and,
301    if string, the maximum width of a string source variable must
302    be provided in MAX_SRC_WIDTH.  Returns true if successful,
303    false on parse error. */
304 static bool
305 parse_map_in (struct lexer *lexer, struct map_in *in, struct pool *pool,
306               enum val_type src_type, size_t max_src_width,
307               const char *dict_encoding)
308 {
309
310   if (lex_match_id (lexer, "ELSE"))
311     set_map_in_generic (in, MAP_ELSE);
312   else if (src_type == VAL_NUMERIC)
313     {
314       if (lex_match_id (lexer, "MISSING"))
315         set_map_in_generic (in, MAP_MISSING);
316       else if (lex_match_id (lexer, "SYSMIS"))
317         set_map_in_generic (in, MAP_SYSMIS);
318       else
319         {
320           double x, y;
321           if (!parse_num_range (lexer, &x, &y, NULL))
322             return false;
323           set_map_in_num (in, x == y ? MAP_SINGLE : MAP_RANGE, x, y);
324         }
325     }
326   else
327     {
328       if (lex_match_id (lexer, "MISSING"))
329         set_map_in_generic (in, MAP_MISSING);
330       else if (!lex_force_string (lexer))
331         return false;
332       else
333         {
334           set_map_in_str (in, pool, lex_tokss (lexer), max_src_width,
335                           dict_encoding);
336           lex_get (lexer);
337           if (lex_token (lexer) == T_ID
338               && lex_id_match (ss_cstr ("THRU"), lex_tokss (lexer)))
339             {
340               lex_error (lexer, _("%s is not allowed with string variables."),
341                          "THRU");
342               return false;
343             }
344         }
345     }
346
347   return true;
348 }
349
350 /* Adds IN to the list of mappings in TRNS.
351    MAP_ALLOCATED is the current number of allocated mappings,
352    which is updated as needed. */
353 static void
354 add_mapping (struct recode_trns *trns,
355              size_t *map_allocated, const struct map_in *in)
356 {
357   struct mapping *m;
358   if (trns->n_maps >= *map_allocated)
359     trns->mappings = pool_2nrealloc (trns->pool, trns->mappings,
360                                      map_allocated,
361                                      sizeof *trns->mappings);
362   m = &trns->mappings[trns->n_maps++];
363   m->in = *in;
364 }
365
366 /* Sets IN as a mapping of the given TYPE. */
367 static void
368 set_map_in_generic (struct map_in *in, enum map_in_type type)
369 {
370   in->type = type;
371 }
372
373 /* Sets IN as a numeric mapping of the given TYPE,
374    with X and Y as the two numeric values. */
375 static void
376 set_map_in_num (struct map_in *in, enum map_in_type type, double x, double y)
377 {
378   in->type = type;
379   in->x.f = x;
380   in->y.f = y;
381 }
382
383 /* Sets IN as a string mapping, with STRING as the string,
384    allocated from POOL.  The string is padded with spaces on the
385    right to WIDTH characters long. */
386 static void
387 set_map_in_str (struct map_in *in, struct pool *pool,
388                 struct substring string, size_t width,
389                 const char *dict_encoding)
390 {
391   char *s = recode_string (dict_encoding, "UTF-8",
392                            ss_data (string), ss_length (string));
393   in->type = MAP_SINGLE;
394   value_init_pool (pool, &in->x, width);
395   value_copy_buf_rpad (&in->x, width,
396                        CHAR_CAST (uint8_t *, s), strlen (s), ' ');
397   free (s);
398 }
399
400 /* Parses a mapping output value into OUT, allocating memory from
401    POOL.  Returns true if successful, false on parse error. */
402 static bool
403 parse_map_out (struct lexer *lexer, struct pool *pool, struct map_out *out)
404 {
405   if (lex_is_number (lexer))
406     {
407       set_map_out_num (out, lex_number (lexer));
408       lex_get (lexer);
409     }
410   else if (lex_match_id (lexer, "SYSMIS"))
411     set_map_out_num (out, SYSMIS);
412   else if (lex_is_string (lexer))
413     {
414       set_map_out_str (out, pool, lex_tokss (lexer));
415       lex_get (lexer);
416     }
417   else if (lex_match_id (lexer, "COPY"))
418     {
419       out->copy_input = true;
420       out->width = 0;
421     }
422   else
423     {
424       lex_error (lexer, _("Syntax error expecting output value."));
425       return false;
426     }
427   return true;
428 }
429
430 /* Sets OUT as a numeric mapping output with the given VALUE. */
431 static void
432 set_map_out_num (struct map_out *out, double value)
433 {
434   out->copy_input = false;
435   out->value.f = value;
436   out->width = 0;
437 }
438
439 /* Sets OUT as a string mapping output with the given VALUE. */
440 static void
441 set_map_out_str (struct map_out *out, struct pool *pool,
442                  const struct substring value)
443 {
444   const char *string = ss_data (value);
445   size_t length = ss_length (value);
446
447   if (length == 0)
448     {
449       /* A length of 0 will yield a numeric value, which is not
450          what we want. */
451       string = " ";
452       length = 1;
453     }
454
455   out->copy_input = false;
456   value_init_pool (pool, &out->value, length);
457   memcpy (out->value.s, string, length);
458   out->width = length;
459 }
460
461 /* Parses a set of target variables into TRNS->dst_vars and
462    TRNS->dst_names. */
463 static bool
464 parse_dst_vars (struct lexer *lexer, struct recode_trns *trns,
465                 const struct dictionary *dict)
466 {
467   size_t i;
468
469   if (lex_match_id (lexer, "INTO"))
470     {
471       size_t n_names;
472       size_t i;
473
474       if (!parse_mixed_vars_pool (lexer, dict, trns->pool,
475                                   &trns->dst_names, &n_names,
476                                   PV_NONE))
477         return false;
478
479       if (n_names != trns->n_vars)
480         {
481           msg (SE, _("%zu variable(s) cannot be recoded into "
482                      "%zu variable(s).  Specify the same number "
483                      "of variables as source and target variables."),
484                trns->n_vars, n_names);
485           return false;
486         }
487
488       trns->dst_vars = pool_nalloc (trns->pool,
489                                     trns->n_vars, sizeof *trns->dst_vars);
490       for (i = 0; i < trns->n_vars; i++)
491         {
492           const struct variable *v;
493           v = trns->dst_vars[i] = dict_lookup_var (dict, trns->dst_names[i]);
494           if (v == NULL && trns->dst_type == VAL_STRING)
495             {
496               msg (SE, _("There is no variable named "
497                          "%s.  (All string variables specified "
498                          "on INTO must already exist.  Use the "
499                          "STRING command to create a string "
500                          "variable.)"),
501                    trns->dst_names[i]);
502               return false;
503             }
504         }
505
506     }
507   else
508     {
509       trns->dst_vars = trns->src_vars;
510       if (trns->src_type != trns->dst_type)
511         {
512           msg (SE, _("INTO is required with %s input values "
513                      "and %s output values."),
514                trns->src_type == VAL_NUMERIC ? _("numeric") : _("string"),
515                trns->dst_type == VAL_NUMERIC ? _("numeric") : _("string"));
516           return false;
517         }
518     }
519
520   for (i = 0; i < trns->n_vars; i++)
521     {
522       const struct variable *v = trns->dst_vars[i];
523       if (v != NULL && var_get_type (v) != trns->dst_type)
524         {
525           if (trns->dst_type == VAL_STRING)
526             msg (SE, _("Type mismatch.  Cannot store string data in "
527                        "numeric variable %s."), var_get_name (v));
528           else
529             msg (SE, _("Type mismatch.  Cannot store numeric data in "
530                        "string variable %s."), var_get_name (v));
531           return false;
532         }
533     }
534
535   return true;
536 }
537
538 /* Ensures that all the output values in TRNS are as wide as the
539    widest destination variable. */
540 static bool
541 enlarge_dst_widths (struct recode_trns *trns)
542 {
543   size_t i;
544   const struct variable *narrow_var = NULL;
545   int min_dst_width = INT_MAX;
546   trns->max_dst_width = 0;
547
548   for (i = 0; i < trns->n_vars; i++)
549     {
550       const struct variable *v = trns->dst_vars[i];
551       if (var_get_width (v) > trns->max_dst_width)
552         trns->max_dst_width = var_get_width (v);
553
554       if (var_get_width (v) < min_dst_width)
555         {
556           min_dst_width = var_get_width (v);
557           narrow_var = v;
558         }
559     }
560
561   for (i = 0; i < trns->n_maps; i++)
562     {
563       struct map_out *out = &trns->mappings[i].out;
564       if (!out->copy_input)
565         {
566           if (out->width > min_dst_width)
567             {
568               msg (ME,
569                    _("Cannot recode because the variable %s would require a width of %d bytes or greater, but it has a width of only %d bytes."),
570                    var_get_name (narrow_var), out->width, min_dst_width);
571               return false;
572             }
573
574           value_resize_pool (trns->pool, &out->value,
575                              out->width, trns->max_dst_width);
576         }
577     }
578
579   return true;
580 }
581
582 /* Creates destination variables that don't already exist. */
583 static void
584 create_dst_vars (struct recode_trns *trns, struct dictionary *dict)
585 {
586   size_t i;
587
588   for (i = 0; i < trns->n_vars; i++)
589     {
590       const struct variable **var = &trns->dst_vars[i];
591       const char *name = trns->dst_names[i];
592
593       *var = dict_lookup_var (dict, name);
594       if (*var == NULL)
595         *var = dict_create_var_assert (dict, name, 0);
596       assert (var_get_type (*var) == trns->dst_type);
597     }
598 }
599 \f
600 /* Data transformation. */
601
602 /* Returns the output mapping in TRNS for an input of VALUE on
603    variable V, or a null pointer if there is no mapping. */
604 static const struct map_out *
605 find_src_numeric (struct recode_trns *trns, double value, const struct variable *v)
606 {
607   struct mapping *m;
608
609   for (m = trns->mappings; m < trns->mappings + trns->n_maps; m++)
610     {
611       const struct map_in *in = &m->in;
612       const struct map_out *out = &m->out;
613       bool match;
614
615       switch (in->type)
616         {
617         case MAP_SINGLE:
618           match = value == in->x.f;
619           break;
620         case MAP_MISSING:
621           match = var_is_num_missing (v, value) != 0;
622           break;
623         case MAP_RANGE:
624           match = value >= in->x.f && value <= in->y.f;
625           break;
626         case MAP_SYSMIS:
627           match = value == SYSMIS;
628           break;
629         case MAP_ELSE:
630           match = true;
631           break;
632         default:
633           NOT_REACHED ();
634         }
635
636       if (match)
637         return out;
638     }
639
640   return NULL;
641 }
642
643 /* Returns the output mapping in TRNS for an input of VALUE with
644    the given WIDTH, or a null pointer if there is no mapping. */
645 static const struct map_out *
646 find_src_string (struct recode_trns *trns, const uint8_t *value,
647                  const struct variable *src_var)
648 {
649   const char *encoding = dict_get_encoding (trns->dst_dict);
650   int width = var_get_width (src_var);
651   struct mapping *m;
652
653   for (m = trns->mappings; m < trns->mappings + trns->n_maps; m++)
654     {
655       const struct map_in *in = &m->in;
656       struct map_out *out = &m->out;
657       bool match;
658
659       switch (in->type)
660         {
661         case MAP_SINGLE:
662           match = !memcmp (value, in->x.s, width);
663           break;
664         case MAP_ELSE:
665           match = true;
666           break;
667         case MAP_CONVERT:
668           {
669             union value uv;
670             char *error;
671
672             error = data_in (ss_buffer (CHAR_CAST_BUG (char *, value), width),
673                              C_ENCODING, FMT_F, settings_get_fmt_settings (),
674                              &uv, 0, encoding);
675             match = error == NULL;
676             free (error);
677
678             out->value.f = uv.f;
679             break;
680           }
681         case MAP_MISSING:
682           match = var_is_str_missing (src_var, value) != 0;
683           break;
684         default:
685           NOT_REACHED ();
686         }
687
688       if (match)
689         return out;
690     }
691
692   return NULL;
693 }
694
695 /* Performs RECODE transformation. */
696 static enum trns_result
697 recode_trns_proc (void *trns_, struct ccase **c, casenumber case_idx UNUSED)
698 {
699   struct recode_trns *trns = trns_;
700   size_t i;
701
702   *c = case_unshare (*c);
703   for (i = 0; i < trns->n_vars; i++)
704     {
705       const struct variable *src_var = trns->src_vars[i];
706       const struct variable *dst_var = trns->dst_vars[i];
707       const struct map_out *out;
708
709       if (trns->src_type == VAL_NUMERIC)
710         out = find_src_numeric (trns, case_num (*c, src_var), src_var);
711       else
712         out = find_src_string (trns, case_str (*c, src_var), src_var);
713
714       if (trns->dst_type == VAL_NUMERIC)
715         {
716           double *dst = case_num_rw (*c, dst_var);
717           if (out != NULL)
718             *dst = !out->copy_input ? out->value.f : case_num (*c, src_var);
719           else if (trns->src_vars != trns->dst_vars)
720             *dst = SYSMIS;
721         }
722       else
723         {
724           char *dst = CHAR_CAST_BUG (char *, case_str_rw (*c, dst_var));
725           if (out != NULL)
726             {
727               if (!out->copy_input)
728                 memcpy (dst, out->value.s, var_get_width (dst_var));
729               else if (trns->src_vars != trns->dst_vars)
730                 {
731                   union value *dst_data = case_data_rw (*c, dst_var);
732                   const union value *src_data = case_data (*c, src_var);
733                   value_copy_rpad (dst_data, var_get_width (dst_var),
734                                    src_data, var_get_width (src_var), ' ');
735                 }
736             }
737           else if (trns->src_vars != trns->dst_vars)
738             memset (dst, ' ', var_get_width (dst_var));
739         }
740     }
741
742   return TRNS_CONTINUE;
743 }
744
745 /* Frees a RECODE transformation. */
746 static bool
747 recode_trns_free (void *trns_)
748 {
749   struct recode_trns *trns = trns_;
750   pool_destroy (trns->pool);
751   return true;
752 }
753
754 static const struct trns_class recode_trns_class = {
755   .name = "RECODE",
756   .execute = recode_trns_proc,
757   .destroy = recode_trns_free,
758 };