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