af7eeec9afdc95a55899ff5439b416e8f5835784
[pspp-builds.git] / src / language / dictionary / missing-values.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 1997-9, 2000, 2006 Free Software Foundation, Inc.
3    Written by Ben Pfaff <blp@gnu.org>.
4
5    This program is free software; you can redistribute it and/or
6    modify it under the terms of the GNU General Public License as
7    published by the Free Software Foundation; either version 2 of the
8    License, or (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful, but
11    WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18    02110-1301, USA. */
19
20 #include <config.h>
21
22 #include <stdlib.h>
23
24 #include <data/data-in.h>
25 #include <data/missing-values.h>
26 #include <data/procedure.h>
27 #include <data/value.h>
28 #include <data/variable.h>
29 #include <language/command.h>
30 #include <language/lexer/lexer.h>
31 #include <language/lexer/variable-parser.h>
32 #include <language/lexer/range-parser.h>
33 #include <libpspp/magic.h>
34 #include <libpspp/message.h>
35 #include <libpspp/message.h>
36 #include <libpspp/str.h>
37
38 #include "gettext.h"
39 #define _(msgid) gettext (msgid)
40
41 int
42 cmd_missing_values (struct lexer *lexer, struct dataset *ds)
43 {
44   struct variable **v;
45   size_t nv;
46
47   int retval = CMD_FAILURE;
48   bool deferred_errors = false;
49
50   while (lex_token (lexer) != '.')
51     {
52       size_t i;
53
54       if (!parse_variables (lexer, dataset_dict (ds), &v, &nv, PV_NONE)) 
55         goto done;
56
57       if (!lex_match (lexer, '('))
58         {
59           lex_error (lexer, _("expecting `('"));
60           goto done;
61         }
62
63       for (i = 0; i < nv; i++)
64         var_clear_missing_values (v[i]);
65
66       if (!lex_match (lexer, ')')) 
67         {
68           struct missing_values mv;
69
70           for (i = 0; i < nv; i++)
71             if (var_get_type (v[i]) != var_get_type (v[0]))
72               {
73                 const struct variable *n = var_is_numeric (v[0]) ? v[0] : v[i];
74                 const struct variable *s = var_is_numeric (v[0]) ? v[i] : v[0];
75                 msg (SE, _("Cannot mix numeric variables (e.g. %s) and "
76                            "string variables (e.g. %s) within a single list."),
77                      var_get_name (n), var_get_name (s));
78                 goto done;
79               }
80
81           if (var_is_numeric (v[0])) 
82             {
83               mv_init (&mv, 0);
84               while (!lex_match (lexer, ')'))
85                 {
86                   enum fmt_type type = var_get_print_format (v[0])->type;
87                   double x, y;
88                   bool ok;
89
90                   if (!parse_num_range (lexer, &x, &y, &type))
91                     goto done;
92                   
93                   ok = (x == y
94                         ? mv_add_num (&mv, x)
95                         : mv_add_num_range (&mv, x, y));
96                   if (!ok)
97                     deferred_errors = true;
98
99                   lex_match (lexer, ',');
100                 }
101             }
102           else 
103             {
104               struct string value;
105
106               mv_init (&mv, MAX_SHORT_STRING);
107               while (!lex_match (lexer, ')')) 
108                 {
109                   if (!lex_force_string (lexer))
110                     {
111                       deferred_errors = true;
112                       break;
113                     }
114                   
115                   ds_init_string (&value, lex_tokstr (lexer));
116
117                   if (ds_length (&value) > MAX_SHORT_STRING) 
118                     {
119                       ds_truncate (&value, MAX_SHORT_STRING);
120                       msg (SE, _("Truncating missing value to short string "
121                                  "length (%d characters)."),
122                            MAX_SHORT_STRING);
123                     }
124                   else
125                     ds_rpad (&value, MAX_SHORT_STRING, ' ');
126
127                   if (!mv_add_str (&mv, ds_data (&value)))
128                     deferred_errors = true;
129                   ds_destroy (&value);
130
131                   lex_get (lexer);
132                   lex_match (lexer, ',');
133                 }
134             }
135           
136           for (i = 0; i < nv; i++) 
137             {
138               if (mv_is_resizable (&mv, var_get_width (v[i]))) 
139                 var_set_missing_values (v[i], &mv);
140               else 
141                 {
142                   msg (SE, _("Missing values provided are too long to assign "
143                              "to variable of width %d."),
144                        var_get_width (v[i]));
145                   deferred_errors = true;
146                 }
147             }
148         }
149
150       lex_match (lexer, '/');
151       free (v);
152       v = NULL;
153     }
154   retval = lex_end_of_command (lexer);
155   
156  done:
157   free (v);
158   if (deferred_errors)
159     retval = CMD_FAILURE;
160   return retval;
161 }
162