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