946c35c167cd7f7f51834575d1b9e574a83fe617
[pspp-builds.git] / src / language / dictionary / numeric.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2006, 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/dictionary.h>
22 #include <data/procedure.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, &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               msg (SE, _("Format type %s may not be used with a numeric "
68                          "variable."), fmt_to_string (&f, str));
69               goto fail;
70             }
71
72           if (!lex_match (lexer, T_RPAREN))
73             {
74               msg (SE, _("`)' expected after output format."));
75               goto fail;
76             }
77         }
78       else
79         f.type = -1;
80
81       /* Create each variable. */
82       for (i = 0; i < nv; i++)
83         {
84           struct variable *new_var = dict_create_var (dataset_dict (ds), v[i], 0);
85           if (!new_var)
86             msg (SE, _("There is already a variable named %s."), v[i]);
87           else
88             {
89               if (f.type != -1)
90                 var_set_both_formats (new_var, &f);
91             }
92         }
93
94       /* Clean up. */
95       for (i = 0; i < nv; i++)
96         free (v[i]);
97       free (v);
98     }
99   while (lex_match (lexer, T_SLASH));
100
101   return lex_end_of_command (lexer);
102
103   /* If we have an error at a point where cleanup is required,
104      flow-of-control comes here. */
105 fail:
106   for (i = 0; i < nv; i++)
107     free (v[i]);
108   free (v);
109   return CMD_FAILURE;
110 }
111
112 /* Parses the STRING command. */
113 int
114 cmd_string (struct lexer *lexer, struct dataset *ds)
115 {
116   size_t i;
117
118   /* Names of variables to create. */
119   char **v;
120   size_t nv;
121
122   /* Format spec for variables to create. */
123   struct fmt_spec f;
124
125   /* Width of variables to create. */
126   int width;
127
128   do
129     {
130       if (!parse_DATA_LIST_vars (lexer, &v, &nv, PV_NO_DUPLICATE))
131         return CMD_FAILURE;
132
133       if (!lex_force_match (lexer, T_LPAREN)
134           || !parse_format_specifier (lexer, &f)
135           || !lex_force_match (lexer, T_RPAREN))
136         goto fail;
137       if (!fmt_is_string (f.type))
138         {
139           char str[FMT_STRING_LEN_MAX + 1];
140           msg (SE, _("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 lex_end_of_command (lexer);
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 lex_end_of_command (lexer);
194 }