Changed include paths to be explicitly specified in the #include directive.
[pspp-builds.git] / src / language / dictionary / missing-values.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 1997-9, 2000 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 #include <libpspp/message.h>
22 #include <stdlib.h>
23 #include <language/command.h>
24 #include <data/data-in.h>
25 #include <libpspp/message.h>
26 #include <language/lexer/lexer.h>
27 #include <libpspp/magic.h>
28 #include <language/lexer/range-parser.h>
29 #include <libpspp/str.h>
30 #include <data/variable.h>
31
32 #include "gettext.h"
33 #define _(msgid) gettext (msgid)
34
35 #include <libpspp/debug-print.h>
36
37 int
38 cmd_missing_values (void)
39 {
40   struct variable **v;
41   size_t nv;
42
43   int retval = CMD_PART_SUCCESS_MAYBE;
44   bool deferred_errors = false;
45
46   while (token != '.')
47     {
48       size_t i;
49
50       if (!parse_variables (default_dict, &v, &nv, PV_NONE)) 
51         goto done;
52
53       if (!lex_match ('('))
54         {
55           lex_error (_("expecting `('"));
56           goto done;
57         }
58
59       for (i = 0; i < nv; i++)
60         mv_init (&v[i]->miss, v[i]->width);
61
62       if (!lex_match (')')) 
63         {
64           struct missing_values mv;
65
66           for (i = 0; i < nv; i++)
67             if (v[i]->type != v[0]->type)
68               {
69                 const struct variable *n = v[0]->type == NUMERIC ? v[0] : v[i];
70                 const struct variable *s = v[0]->type == NUMERIC ? 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                      n->name, s->name);
74                 goto done;
75               }
76
77           if (v[0]->type == NUMERIC) 
78             {
79               mv_init (&mv, 0);
80               while (!lex_match (')'))
81                 {
82                   double x, y;
83                   bool ok;
84
85                   if (!parse_num_range (&x, &y, &v[0]->print))
86                     goto done;
87                   
88                   ok = (x == y
89                         ? mv_add_num (&mv, x)
90                         : mv_add_num_range (&mv, x, y));
91                   if (!ok)
92                     deferred_errors = true;
93
94                   lex_match (',');
95                 }
96             }
97           else 
98             {
99               mv_init (&mv, MAX_SHORT_STRING);
100               while (!lex_match (')')) 
101                 {
102                   if (!lex_force_string ())
103                     {
104                       deferred_errors = true;
105                       break;
106                     }
107
108                   if (ds_length (&tokstr) > MAX_SHORT_STRING) 
109                     {
110                       ds_truncate (&tokstr, MAX_SHORT_STRING);
111                       msg (SE, _("Truncating missing value to short string "
112                                  "length (%d characters)."),
113                            MAX_SHORT_STRING);
114                     }
115                   else
116                     ds_rpad (&tokstr, MAX_SHORT_STRING, ' ');
117
118                   if (!mv_add_str (&mv, ds_data (&tokstr)))
119                     deferred_errors = true;
120
121                   lex_get ();
122                   lex_match (',');
123                 }
124             }
125           
126           for (i = 0; i < nv; i++) 
127             {
128               if (!mv_is_resizable (&mv, v[i]->width)) 
129                 {
130                   msg (SE, _("Missing values provided are too long to assign "
131                              "to variable of width %d."),
132                        v[i]->width);
133                   deferred_errors = true;
134                 }
135               else 
136                 {
137                   mv_copy (&v[i]->miss, &mv);
138                   mv_resize (&v[i]->miss, v[i]->width);
139                 }
140             }
141         }
142
143       lex_match ('/');
144       free (v);
145       v = NULL;
146     }
147   retval = lex_end_of_command ();
148   
149  done:
150   free (v);
151   if (deferred_errors)
152     retval = CMD_PART_SUCCESS_MAYBE;
153   return retval;
154 }
155