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