MISSING VALUES: Report an error when too many missing values are specified.
[pspp] / src / language / dictionary / missing-values.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2006, 2009, 2010, 2011, 2013, 2016 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/dictionary.h"
23 #include "data/dataset.h"
24 #include "data/format.h"
25 #include "data/missing-values.h"
26 #include "data/value.h"
27 #include "data/variable.h"
28 #include "language/command.h"
29 #include "language/lexer/lexer.h"
30 #include "language/lexer/value-parser.h"
31 #include "language/lexer/variable-parser.h"
32 #include "libpspp/i18n.h"
33 #include "libpspp/message.h"
34 #include "libpspp/str.h"
35
36 #include "gettext.h"
37 #define _(msgid) gettext (msgid)
38
39 int
40 cmd_missing_values (struct lexer *lexer, struct dataset *ds)
41 {
42   struct dictionary *dict = dataset_dict (ds);
43   struct variable **v = NULL;
44   size_t nv;
45
46   bool ok = true;
47
48   while (lex_token (lexer) != T_ENDCMD)
49     {
50       size_t i;
51
52       if (!parse_variables (lexer, dict, &v, &nv, PV_NONE))
53         goto error;
54
55       if (!lex_force_match (lexer, T_LPAREN))
56         goto error;
57
58       for (i = 0; i < nv; i++)
59         var_clear_missing_values (v[i]);
60
61       if (!lex_match (lexer, T_RPAREN))
62         {
63           struct missing_values mv;
64
65           for (i = 0; i < nv; i++)
66             if (var_get_type (v[i]) != var_get_type (v[0]))
67               {
68                 const struct variable *n = var_is_numeric (v[0]) ? v[0] : v[i];
69                 const struct variable *s = var_is_numeric (v[0]) ? v[i] : v[0];
70                 msg (SE, _("Cannot mix numeric variables (e.g. %s) and "
71                            "string variables (e.g. %s) within a single list."),
72                      var_get_name (n), var_get_name (s));
73                 goto error;
74               }
75
76           if (var_is_numeric (v[0]))
77             {
78               mv_init (&mv, 0);
79               while (!lex_match (lexer, T_RPAREN))
80                 {
81                   enum fmt_type type = var_get_print_format (v[0])->type;
82                   double x, y;
83
84                   if (!parse_num_range (lexer, &x, &y, &type))
85                     goto error;
86
87                   if (!(x == y
88                         ? mv_add_num (&mv, x)
89                         : mv_add_range (&mv, x, y)))
90                     {
91                       msg (SE, _("Too many numeric missing values.  At most "
92                                  "three individual values or one value and "
93                                  "one range are allowed."));
94                       ok = false;
95                     }
96
97                   lex_match (lexer, T_COMMA);
98                 }
99             }
100           else
101             {
102               const char *encoding = dict_get_encoding (dict);
103
104               mv_init (&mv, MV_MAX_STRING);
105               while (!lex_match (lexer, T_RPAREN))
106                 {
107                   const char *utf8_s;
108                   size_t utf8_trunc_len;
109                   size_t utf8_len;
110
111                   char *raw_s;
112
113                   if (!lex_force_string (lexer))
114                     {
115                       ok = false;
116                       break;
117                     }
118
119                   /* Truncate the string to fit in 8 bytes in the dictionary
120                      encoding. */
121                   utf8_s = lex_tokcstr (lexer);
122                   utf8_len = ss_length (lex_tokss (lexer));
123                   utf8_trunc_len = utf8_encoding_trunc_len (utf8_s, encoding,
124                                                             MV_MAX_STRING);
125                   if (utf8_trunc_len < utf8_len)
126                     msg (SE, _("Truncating missing value to maximum "
127                                "acceptable length (%d bytes)."),
128                          MV_MAX_STRING);
129
130                   /* Recode to dictionary encoding and add. */
131                   raw_s = recode_string (encoding, "UTF-8",
132                                          utf8_s, utf8_trunc_len);
133                   if (!mv_add_str (&mv, CHAR_CAST (const uint8_t *, raw_s),
134                                    strlen (raw_s)))
135                     {
136                       msg (SE,
137                            _("Too many string missing values.  "
138                              "At most three individual values are allowed."));
139                       ok = false;
140                     }
141                   free (raw_s);
142
143                   lex_get (lexer);
144                   lex_match (lexer, T_COMMA);
145                 }
146             }
147
148           for (i = 0; i < nv; i++)
149             {
150               if (mv_is_resizable (&mv, var_get_width (v[i])))
151                 var_set_missing_values (v[i], &mv);
152               else
153                 {
154                   msg (SE, _("Missing values provided are too long to assign "
155                              "to variable of width %d."),
156                        var_get_width (v[i]));
157                   ok = false;
158                 }
159             }
160
161           mv_destroy (&mv);
162         }
163
164       lex_match (lexer, T_SLASH);
165       free (v);
166       v = NULL;
167     }
168
169   free (v);
170   return ok ? CMD_SUCCESS : CMD_FAILURE;
171
172 error:
173   free (v);
174   return CMD_FAILURE;
175 }
176