Remove unneeded #includes.
[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
45   while (lex_token (lexer) != T_ENDCMD)
46     {
47       struct missing_values mv = MV_INIT_EMPTY_NUMERIC;
48       struct variable **v = NULL;
49       size_t nv;
50
51       if (!parse_variables (lexer, dict, &v, &nv, PV_NONE))
52         goto error;
53
54       if (!lex_force_match (lexer, T_LPAREN))
55         goto error;
56
57       int values_start = lex_ofs (lexer);
58       int values_end;
59       for (values_end = values_start; ; values_end++)
60         {
61           enum token_type next = lex_ofs_token (lexer, values_end + 1)->type;
62           if (next == T_RPAREN || next == T_ENDCMD || next == T_STOP)
63             break;
64         }
65
66       if (!lex_match (lexer, T_RPAREN))
67         {
68           if (var_is_numeric (v[0]))
69             {
70               while (!lex_match (lexer, T_RPAREN))
71                 {
72                   enum fmt_type type = var_get_print_format (v[0])->type;
73                   double x, y;
74
75                   if (!parse_num_range (lexer, &x, &y, &type))
76                     goto error;
77
78                   if (!(x == y
79                         ? mv_add_num (&mv, x)
80                         : mv_add_range (&mv, x, y)))
81                     {
82                       lex_ofs_error (lexer, values_start, values_end,
83                                      _("Too many numeric missing values.  At "
84                                        "most three individual values or one "
85                                        "value and one range are allowed."));
86                       goto error;
87                     }
88
89                   lex_match (lexer, T_COMMA);
90                 }
91             }
92           else
93             {
94               const char *encoding = dict_get_encoding (dict);
95
96               mv_init (&mv, MV_MAX_STRING);
97               while (!lex_match (lexer, T_RPAREN))
98                 {
99                   if (!lex_force_string (lexer))
100                     goto error;
101
102                   /* Truncate the string to fit in 8 bytes in the dictionary
103                      encoding. */
104                   const char *utf8_s = lex_tokcstr (lexer);
105                   size_t utf8_len = ss_length (lex_tokss (lexer));
106                   size_t utf8_trunc_len = utf8_encoding_trunc_len (
107                     utf8_s, encoding, MV_MAX_STRING);
108                   if (utf8_trunc_len < utf8_len)
109                     lex_error (lexer, _("Truncating missing value to maximum "
110                                         "acceptable length (%d bytes)."),
111                                MV_MAX_STRING);
112
113                   /* Recode to dictionary encoding and add. */
114                   char *raw_s = recode_string (encoding, "UTF-8",
115                                                utf8_s, utf8_trunc_len);
116                   bool ok = mv_add_str (&mv, CHAR_CAST (const uint8_t *, raw_s),
117                                         strlen (raw_s));
118                   free (raw_s);
119                   if (!ok)
120                     {
121                       lex_ofs_error (lexer, values_start, values_end,
122                                      _("Too many string missing values.  "
123                                        "At most three individual values "
124                                        "are allowed."));
125                       goto error;
126                     }
127
128                   lex_get (lexer);
129                   lex_match (lexer, T_COMMA);
130                 }
131             }
132         }
133       lex_match (lexer, T_SLASH);
134
135       bool ok = true;
136       for (size_t i = 0; i < nv; i++)
137         {
138           int var_width = var_get_width (v[i]);
139
140           if (mv_is_resizable (&mv, var_width))
141             var_set_missing_values (v[i], &mv);
142           else
143             {
144               ok = false;
145               if (!var_width)
146                 lex_ofs_error (lexer, values_start, values_end,
147                                _("Cannot assign string missing values to "
148                                  "numeric variable %s."), var_get_name (v[i]));
149               else
150                 lex_ofs_error (lexer, values_start, values_end,
151                                _("Missing values are too long to assign "
152                                  "to variable %s with width %d."),
153                                var_get_name (v[i]), var_get_width (v[i]));
154             }
155         }
156       mv_destroy (&mv);
157       free (v);
158       if (!ok)
159         return CMD_FAILURE;
160       continue;
161
162     error:
163       mv_destroy (&mv);
164       free (v);
165       return CMD_FAILURE;
166     }
167
168   return CMD_SUCCESS;
169 }
170