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