Rename procedure.[ch] to dataset.[ch].
[pspp-builds.git] / src / language / dictionary / numeric.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2006, 2010, 2011 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   size_t i;
41
42   /* Names of variables to create. */
43   char **v;
44   size_t nv;
45
46   do
47     {
48       /* Format spec for variables to create.  f.type==-1 if default is to
49          be used. */
50       struct fmt_spec f;
51
52       if (!parse_DATA_LIST_vars (lexer, dataset_dict (ds),
53                                  &v, &nv, PV_NO_DUPLICATE))
54         return CMD_FAILURE;
55
56       /* Get the optional format specification. */
57       if (lex_match (lexer, T_LPAREN))
58         {
59           if (!parse_format_specifier (lexer, &f))
60             goto fail;
61
62           if ( ! fmt_check_output (&f))
63             goto fail;
64
65           if (fmt_is_string (f.type))
66             {
67               char str[FMT_STRING_LEN_MAX + 1];
68               msg (SE, _("Format type %s may not be used with a numeric "
69                          "variable."), fmt_to_string (&f, str));
70               goto fail;
71             }
72
73           if (!lex_match (lexer, T_RPAREN))
74             {
75               msg (SE, _("`)' expected after output format."));
76               goto fail;
77             }
78         }
79       else
80         f.type = -1;
81
82       /* Create each variable. */
83       for (i = 0; i < nv; i++)
84         {
85           struct variable *new_var = dict_create_var (dataset_dict (ds), v[i], 0);
86           if (!new_var)
87             msg (SE, _("There is already a variable named %s."), v[i]);
88           else
89             {
90               if (f.type != -1)
91                 var_set_both_formats (new_var, &f);
92             }
93         }
94
95       /* Clean up. */
96       for (i = 0; i < nv; i++)
97         free (v[i]);
98       free (v);
99     }
100   while (lex_match (lexer, T_SLASH));
101
102   return CMD_SUCCESS;
103
104   /* If we have an error at a point where cleanup is required,
105      flow-of-control comes here. */
106 fail:
107   for (i = 0; i < nv; i++)
108     free (v[i]);
109   free (v);
110   return CMD_FAILURE;
111 }
112
113 /* Parses the STRING command. */
114 int
115 cmd_string (struct lexer *lexer, struct dataset *ds)
116 {
117   size_t i;
118
119   /* Names of variables to create. */
120   char **v;
121   size_t nv;
122
123   /* Format spec for variables to create. */
124   struct fmt_spec f;
125
126   /* Width of variables to create. */
127   int width;
128
129   do
130     {
131       if (!parse_DATA_LIST_vars (lexer, dataset_dict (ds),
132                                  &v, &nv, PV_NO_DUPLICATE))
133         return CMD_FAILURE;
134
135       if (!lex_force_match (lexer, T_LPAREN)
136           || !parse_format_specifier (lexer, &f)
137           || !lex_force_match (lexer, T_RPAREN))
138         goto fail;
139       if (!fmt_is_string (f.type))
140         {
141           char str[FMT_STRING_LEN_MAX + 1];
142           msg (SE, _("Format type %s may not be used with a string "
143                      "variable."), fmt_to_string (&f, str));
144           goto fail;
145         }
146       if (!fmt_check_output (&f))
147         goto fail;
148
149       width = fmt_var_width (&f);
150
151       /* Create each variable. */
152       for (i = 0; i < nv; i++)
153         {
154           struct variable *new_var = dict_create_var (dataset_dict (ds), v[i],
155                                                       width);
156           if (!new_var)
157             msg (SE, _("There is already a variable named %s."), v[i]);
158           else
159             var_set_both_formats (new_var, &f);
160         }
161
162       /* Clean up. */
163       for (i = 0; i < nv; i++)
164         free (v[i]);
165       free (v);
166     }
167   while (lex_match (lexer, T_SLASH));
168
169   return CMD_SUCCESS;
170
171   /* If we have an error at a point where cleanup is required,
172      flow-of-control comes here. */
173 fail:
174   for (i = 0; i < nv; i++)
175     free (v[i]);
176   free (v);
177   return CMD_FAILURE;
178 }
179
180 /* Parses the LEAVE command. */
181 int
182 cmd_leave (struct lexer *lexer, struct dataset *ds)
183 {
184   struct variable **v;
185   size_t nv;
186
187   size_t i;
188
189   if (!parse_variables (lexer, dataset_dict (ds), &v, &nv, PV_NONE))
190     return CMD_CASCADING_FAILURE;
191   for (i = 0; i < nv; i++)
192     var_set_leave (v[i], true);
193   free (v);
194
195   return CMD_SUCCESS;
196 }