1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2006, 2009, 2010, 2011 Free Software Foundation, Inc.
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.
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.
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/>. */
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"
37 #define _(msgid) gettext (msgid)
40 cmd_missing_values (struct lexer *lexer, struct dataset *ds)
42 struct dictionary *dict = dataset_dict (ds);
43 struct variable **v = NULL;
48 while (lex_token (lexer) != T_ENDCMD)
52 if (!parse_variables (lexer, dict, &v, &nv, PV_NONE))
55 if (!lex_force_match (lexer, T_LPAREN))
58 for (i = 0; i < nv; i++)
59 var_clear_missing_values (v[i]);
61 if (!lex_match (lexer, T_RPAREN))
63 struct missing_values mv;
65 for (i = 0; i < nv; i++)
66 if (var_get_type (v[i]) != var_get_type (v[0]))
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));
76 if (var_is_numeric (v[0]))
79 while (!lex_match (lexer, T_RPAREN))
81 enum fmt_type type = var_get_print_format (v[0])->type;
85 if (!parse_num_range (lexer, &x, &y, &type))
90 : mv_add_range (&mv, x, y));
94 lex_match (lexer, T_COMMA);
99 mv_init (&mv, MV_MAX_STRING);
100 while (!lex_match (lexer, T_RPAREN))
102 uint8_t value[MV_MAX_STRING];
106 if (!lex_force_string (lexer))
112 dict_mv = recode_string (dict_get_encoding (dict), "UTF-8",
114 ss_length (lex_tokss (lexer)));
115 length = strlen (dict_mv);
116 if (length > MV_MAX_STRING)
118 /* XXX truncate graphemes not bytes */
119 msg (SE, _("Truncating missing value to maximum "
120 "acceptable length (%d bytes)."),
122 length = MV_MAX_STRING;
124 memset (value, ' ', MV_MAX_STRING);
125 memcpy (value, dict_mv, length);
128 if (!mv_add_str (&mv, value))
132 lex_match (lexer, T_COMMA);
136 for (i = 0; i < nv; i++)
138 if (mv_is_resizable (&mv, var_get_width (v[i])))
139 var_set_missing_values (v[i], &mv);
142 msg (SE, _("Missing values provided are too long to assign "
143 "to variable of width %d."),
144 var_get_width (v[i]));
152 lex_match (lexer, T_SLASH);
158 return ok ? CMD_SUCCESS : CMD_FAILURE;