Encapsulated lexer and updated calling functions accordingly.
[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/procedure.h>
26 #include <data/variable.h>
27 #include <language/command.h>
28 #include <language/lexer/lexer.h>
29 #include <language/lexer/variable-parser.h>
30 #include <language/lexer/range-parser.h>
31 #include <libpspp/magic.h>
32 #include <libpspp/message.h>
33 #include <libpspp/message.h>
34 #include <libpspp/str.h>
35
36 #include "gettext.h"
37 #define _(msgid) gettext (msgid)
38
39 int
40 cmd_missing_values (struct lexer *lexer, struct dataset *ds)
41 {
42   struct variable **v;
43   size_t nv;
44
45   int retval = CMD_FAILURE;
46   bool deferred_errors = false;
47
48   while (lex_token (lexer) != '.')
49     {
50       size_t i;
51
52       if (!parse_variables (lexer, dataset_dict (ds), &v, &nv, PV_NONE)) 
53         goto done;
54
55       if (!lex_match (lexer, '('))
56         {
57           lex_error (lexer, _("expecting `('"));
58           goto done;
59         }
60
61       for (i = 0; i < nv; i++)
62         mv_init (&v[i]->miss, v[i]->width);
63
64       if (!lex_match (lexer, ')')) 
65         {
66           struct missing_values mv;
67
68           for (i = 0; i < nv; i++)
69             if (v[i]->type != v[0]->type)
70               {
71                 const struct variable *n = v[0]->type == NUMERIC ? v[0] : v[i];
72                 const struct variable *s = v[0]->type == NUMERIC ? v[i] : v[0];
73                 msg (SE, _("Cannot mix numeric variables (e.g. %s) and "
74                            "string variables (e.g. %s) within a single list."),
75                      n->name, s->name);
76                 goto done;
77               }
78
79           if (v[0]->type == NUMERIC) 
80             {
81               mv_init (&mv, 0);
82               while (!lex_match (lexer, ')'))
83                 {
84                   double x, y;
85                   bool ok;
86
87                   if (!parse_num_range (lexer, &x, &y, &v[0]->print))
88                     goto done;
89                   
90                   ok = (x == y
91                         ? mv_add_num (&mv, x)
92                         : mv_add_num_range (&mv, x, y));
93                   if (!ok)
94                     deferred_errors = true;
95
96                   lex_match (lexer, ',');
97                 }
98             }
99           else 
100             {
101               struct string value;
102
103               mv_init (&mv, MAX_SHORT_STRING);
104               while (!lex_match (lexer, ')')) 
105                 {
106                   if (!lex_force_string (lexer))
107                     {
108                       deferred_errors = true;
109                       break;
110                     }
111                   
112                   ds_init_string (&value, lex_tokstr (lexer));
113
114                   if (ds_length (&value) > MAX_SHORT_STRING) 
115                     {
116                       ds_truncate (&value, MAX_SHORT_STRING);
117                       msg (SE, _("Truncating missing value to short string "
118                                  "length (%d characters)."),
119                            MAX_SHORT_STRING);
120                     }
121                   else
122                     ds_rpad (&value, MAX_SHORT_STRING, ' ');
123
124                   if (!mv_add_str (&mv, ds_data (&value)))
125                     deferred_errors = true;
126                   ds_destroy (&value);
127
128                   lex_get (lexer);
129                   lex_match (lexer, ',');
130                 }
131             }
132           
133           for (i = 0; i < nv; i++) 
134             {
135               if (!mv_is_resizable (&mv, v[i]->width)) 
136                 {
137                   msg (SE, _("Missing values provided are too long to assign "
138                              "to variable of width %d."),
139                        v[i]->width);
140                   deferred_errors = true;
141                 }
142               else 
143                 {
144                   mv_copy (&v[i]->miss, &mv);
145                   mv_resize (&v[i]->miss, v[i]->width);
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