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