Change union value type to contain uint8_t types instead of char.
[pspp-builds.git] / src / language / dictionary / missing-values.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2006, 2009 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 <stdlib.h>
20
21 #include <data/data-in.h>
22 #include <data/missing-values.h>
23 #include <data/procedure.h>
24 #include <data/value.h>
25 #include <data/variable.h>
26 #include <data/format.h>
27 #include <language/command.h>
28 #include <language/lexer/lexer.h>
29 #include <language/lexer/value-parser.h>
30 #include <language/lexer/variable-parser.h>
31 #include <libpspp/message.h>
32 #include <libpspp/message.h>
33 #include <libpspp/str.h>
34
35 #include "gettext.h"
36 #define _(msgid) gettext (msgid)
37
38 int
39 cmd_missing_values (struct lexer *lexer, struct dataset *ds)
40 {
41   struct variable **v;
42   size_t nv;
43
44   int retval = CMD_FAILURE;
45   bool deferred_errors = false;
46
47   while (lex_token (lexer) != '.')
48     {
49       size_t i;
50
51       if (!parse_variables (lexer, dataset_dict (ds), &v, &nv, PV_NONE))
52         goto done;
53
54       if (!lex_match (lexer, '('))
55         {
56           lex_error (lexer, _("expecting `('"));
57           goto done;
58         }
59
60       for (i = 0; i < nv; i++)
61         var_clear_missing_values (v[i]);
62
63       if (!lex_match (lexer, ')'))
64         {
65           struct missing_values mv;
66
67           for (i = 0; i < nv; i++)
68             if (var_get_type (v[i]) != var_get_type (v[0]))
69               {
70                 const struct variable *n = var_is_numeric (v[0]) ? v[0] : v[i];
71                 const struct variable *s = var_is_numeric (v[0]) ? v[i] : v[0];
72                 msg (SE, _("Cannot mix numeric variables (e.g. %s) and "
73                            "string variables (e.g. %s) within a single list."),
74                      var_get_name (n), var_get_name (s));
75                 goto done;
76               }
77
78           if (var_is_numeric (v[0]))
79             {
80               mv_init (&mv, 0);
81               while (!lex_match (lexer, ')'))
82                 {
83                   enum fmt_type type = var_get_print_format (v[0])->type;
84                   double x, y;
85                   bool ok;
86
87                   if (!parse_num_range (lexer, &x, &y, &type))
88                     goto done;
89
90                   ok = (x == y
91                         ? mv_add_num (&mv, x)
92                         : mv_add_range (&mv, x, y));
93                   if (!ok)
94                     deferred_errors = true;
95
96                   lex_match (lexer, ',');
97                 }
98             }
99           else
100             {
101               mv_init (&mv, MV_MAX_STRING);
102               while (!lex_match (lexer, ')'))
103                 {
104                   uint8_t value[MV_MAX_STRING];
105                   size_t length;
106
107                   if (!lex_force_string (lexer))
108                     {
109                       deferred_errors = true;
110                       break;
111                     }
112
113                   length = ds_length (lex_tokstr (lexer));
114                   if (length > MV_MAX_STRING)
115                     {
116                       msg (SE, _("Truncating missing value to maximum "
117                                  "acceptable length (%d bytes)."),
118                            MV_MAX_STRING);
119                       length = MV_MAX_STRING;
120                     }
121                   memset (value, ' ', MV_MAX_STRING);
122                   memcpy (value, ds_data (lex_tokstr (lexer)), length);
123
124                   if (!mv_add_str (&mv, value))
125                     deferred_errors = true;
126
127                   lex_get (lexer);
128                   lex_match (lexer, ',');
129                 }
130             }
131
132           for (i = 0; i < nv; i++)
133             {
134               if (mv_is_resizable (&mv, var_get_width (v[i])))
135                 var_set_missing_values (v[i], &mv);
136               else
137                 {
138                   msg (SE, _("Missing values provided are too long to assign "
139                              "to variable of width %d."),
140                        var_get_width (v[i]));
141                   deferred_errors = true;
142                 }
143             }
144
145           mv_destroy (&mv);
146         }
147
148       lex_match (lexer, '/');
149       free (v);
150       v = NULL;
151     }
152   retval = lex_end_of_command (lexer);
153
154  done:
155   free (v);
156   if (deferred_errors)
157     retval = CMD_FAILURE;
158   return retval;
159 }
160