4bf1ef94a1be3be59375314ecab70f74aad04458
[pspp] / src / language / dictionary / numeric.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2006, 2010, 2011, 2014 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/dataset.h"
22 #include "data/dictionary.h"
23 #include "data/variable.h"
24 #include "data/format.h"
25 #include "language/command.h"
26 #include "language/lexer/format-parser.h"
27 #include "language/lexer/lexer.h"
28 #include "language/lexer/variable-parser.h"
29 #include "libpspp/assertion.h"
30 #include "libpspp/message.h"
31 #include "libpspp/str.h"
32
33 #include "gettext.h"
34 #define _(msgid) gettext (msgid)
35
36 /* Parses the NUMERIC command. */
37 int
38 cmd_numeric (struct lexer *lexer, struct dataset *ds)
39 {
40   do
41     {
42       char **v;
43       size_t nv;
44       int vars_start = lex_ofs (lexer);
45       if (!parse_DATA_LIST_vars (lexer, dataset_dict (ds),
46                                  &v, &nv, PV_NO_DUPLICATE))
47         return CMD_FAILURE;
48       int vars_end = lex_ofs (lexer) - 1;
49
50       bool ok = false;
51
52       /* Get the optional format specification. */
53       struct fmt_spec f = var_default_formats (0);
54       if (lex_match (lexer, T_LPAREN))
55         {
56           if (!parse_format_specifier (lexer, &f))
57             goto done;
58
59           char *error = fmt_check_output__ (&f);
60           if (error)
61             {
62               lex_next_error (lexer, -1, -1, "%s", error);
63               free (error);
64               goto done;
65             }
66
67           if (fmt_is_string (f.type))
68             {
69               char str[FMT_STRING_LEN_MAX + 1];
70               lex_next_error (lexer, -1, -1,
71                               _("Format type %s may not be used with a numeric "
72                                 "variable."), fmt_to_string (&f, str));
73               goto done;
74             }
75
76           if (!lex_match (lexer, T_RPAREN))
77             {
78               lex_error_expecting (lexer, "`)'");
79               goto done;
80             }
81         }
82
83       /* Create each variable. */
84       for (size_t i = 0; i < nv; i++)
85         {
86           struct variable *new_var = dict_create_var (dataset_dict (ds),
87                                                       v[i], 0);
88           if (!new_var)
89             lex_ofs_error (lexer, vars_start, vars_end,
90                            _("There is already a variable named %s."), v[i]);
91           else
92             var_set_both_formats (new_var, &f);
93         }
94       ok = true;
95
96     done:
97       for (size_t i = 0; i < nv; i++)
98         free (v[i]);
99       free (v);
100       if (!ok)
101         return CMD_FAILURE;
102     }
103   while (lex_match (lexer, T_SLASH));
104
105   return CMD_SUCCESS;
106 }
107
108 /* Parses the STRING command. */
109 int
110 cmd_string (struct lexer *lexer, struct dataset *ds)
111 {
112   do
113     {
114       char **v;
115       size_t nv;
116       int vars_start = lex_ofs (lexer);
117       if (!parse_DATA_LIST_vars (lexer, dataset_dict (ds),
118                                  &v, &nv, PV_NO_DUPLICATE))
119         return CMD_FAILURE;
120       int vars_end = lex_ofs (lexer) - 1;
121
122       bool ok = false;
123
124       struct fmt_spec f;
125       if (!lex_force_match (lexer, T_LPAREN)
126           || !parse_format_specifier (lexer, &f))
127         goto done;
128
129       char *error = fmt_check_type_compat__ (&f, NULL, VAL_STRING);
130       if (!error)
131         error = fmt_check_output__ (&f);
132       if (error)
133         {
134           lex_next_error (lexer, -1, -1, "%s", error);
135           free (error);
136           goto done;
137         }
138
139       if (!lex_force_match (lexer, T_RPAREN))
140         goto done;
141
142       /* Create each variable. */
143       int width = fmt_var_width (&f);
144       for (size_t i = 0; i < nv; i++)
145         {
146           struct variable *new_var = dict_create_var (dataset_dict (ds), v[i],
147                                                       width);
148           if (!new_var)
149             lex_ofs_error (lexer, vars_start, vars_end,
150                            _("There is already a variable named %s."), v[i]);
151           else
152             var_set_both_formats (new_var, &f);
153         }
154       ok = true;
155
156     done:
157       for (size_t i = 0; i < nv; i++)
158         free (v[i]);
159       free (v);
160       if (!ok)
161         return CMD_FAILURE;
162     }
163   while (lex_match (lexer, T_SLASH));
164
165   return CMD_SUCCESS;
166 }
167
168 /* Parses the LEAVE command. */
169 int
170 cmd_leave (struct lexer *lexer, struct dataset *ds)
171 {
172   struct variable **v;
173   size_t nv;
174
175   if (!parse_variables (lexer, dataset_dict (ds), &v, &nv, PV_NONE))
176     return CMD_CASCADING_FAILURE;
177   for (size_t i = 0; i < nv; i++)
178     var_set_leave (v[i], true);
179   free (v);
180
181   return CMD_SUCCESS;
182 }