120953188ac87a2872b386430f06d668aebd5edc
[pspp-builds.git] / src / language / dictionary / missing-values.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2006 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/missing-values.h>
23 #include <data/procedure.h>
24 #include <data/value.h>
25 #include <data/variable.h>
26 #include <language/command.h>
27 #include <language/lexer/lexer.h>
28 #include <language/lexer/variable-parser.h>
29 #include <language/lexer/range-parser.h>
30 #include <libpspp/message.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;
41   size_t nv;
42
43   int retval = CMD_FAILURE;
44   bool deferred_errors = false;
45
46   while (lex_token (lexer) != '.')
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_match (lexer, '('))
54         {
55           lex_error (lexer, _("expecting `('"));
56           goto done;
57         }
58
59       for (i = 0; i < nv; i++)
60         var_clear_missing_values (v[i]);
61
62       if (!lex_match (lexer, ')'))
63         {
64           struct missing_values mv;
65
66           for (i = 0; i < nv; i++)
67             if (var_get_type (v[i]) != var_get_type (v[0]))
68               {
69                 const struct variable *n = var_is_numeric (v[0]) ? v[0] : v[i];
70                 const struct variable *s = var_is_numeric (v[0]) ? v[i] : v[0];
71                 msg (SE, _("Cannot mix numeric variables (e.g. %s) and "
72                            "string variables (e.g. %s) within a single list."),
73                      var_get_name (n), var_get_name (s));
74                 goto done;
75               }
76
77           if (var_is_numeric (v[0]))
78             {
79               mv_init (&mv, 0);
80               while (!lex_match (lexer, ')'))
81                 {
82                   enum fmt_type type = var_get_print_format (v[0])->type;
83                   double x, y;
84                   bool ok;
85
86                   if (!parse_num_range (lexer, &x, &y, &type))
87                     goto done;
88
89                   ok = (x == y
90                         ? mv_add_num (&mv, x)
91                         : mv_add_num_range (&mv, x, y));
92                   if (!ok)
93                     deferred_errors = true;
94
95                   lex_match (lexer, ',');
96                 }
97             }
98           else
99             {
100               struct string value;
101
102               mv_init (&mv, MAX_SHORT_STRING);
103               while (!lex_match (lexer, ')'))
104                 {
105                   if (!lex_force_string (lexer))
106                     {
107                       deferred_errors = true;
108                       break;
109                     }
110
111                   ds_init_string (&value, lex_tokstr (lexer));
112
113                   if (ds_length (&value) > MAX_SHORT_STRING)
114                     {
115                       ds_truncate (&value, MAX_SHORT_STRING);
116                       msg (SE, _("Truncating missing value to short string "
117                                  "length (%d characters)."),
118                            MAX_SHORT_STRING);
119                     }
120                   else
121                     ds_rpad (&value, MAX_SHORT_STRING, ' ');
122
123                   if (!mv_add_str (&mv, ds_data (&value)))
124                     deferred_errors = true;
125                   ds_destroy (&value);
126
127                   lex_get (lexer);
128                   lex_match (lexer, ',');
129                 }
130             }
131
132           for (i = 0; i < nv; i++)
133             {
134               if (mv_is_resizable (&mv, var_get_width (v[i])))
135                 var_set_missing_values (v[i], &mv);
136               else
137                 {
138                   msg (SE, _("Missing values provided are too long to assign "
139                              "to variable of width %d."),
140                        var_get_width (v[i]));
141                   deferred_errors = true;
142                 }
143             }
144         }
145
146       lex_match (lexer, '/');
147       free (v);
148       v = NULL;
149     }
150   retval = lex_end_of_command (lexer);
151
152  done:
153   free (v);
154   if (deferred_errors)
155     retval = CMD_FAILURE;
156   return retval;
157 }
158