aa6794d39f7308eb6cfda619c1af5eb8b7daf357
[pspp-builds.git] / src / language / dictionary / numeric.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 1997-9, 2000, 2006 Free Software Foundation, Inc.
3    Written by Ben Pfaff <blp@gnu.org>.
4
5    This program is free software; you can redistribute it and/or
6    modify it under the terms of the GNU General Public License as
7    published by the Free Software Foundation; either version 2 of the
8    License, or (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful, but
11    WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18    02110-1301, USA. */
19
20 #include <config.h>
21
22 #include <stdlib.h>
23
24 #include <data/dictionary.h>
25 #include <data/procedure.h>
26 #include <data/variable.h>
27 #include <language/command.h>
28 #include <language/lexer/format-parser.h>
29 #include <language/lexer/lexer.h>
30 #include <language/lexer/variable-parser.h>
31 #include <libpspp/assertion.h>
32 #include <libpspp/message.h>
33 #include <libpspp/str.h>
34
35 #include "gettext.h"
36 #define _(msgid) gettext (msgid)
37
38 /* Parses the NUMERIC command. */
39 int
40 cmd_numeric (struct lexer *lexer, struct dataset *ds)
41 {
42   size_t i;
43
44   /* Names of variables to create. */
45   char **v;
46   size_t nv;
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   do
53     {
54       if (!parse_DATA_LIST_vars (lexer, &v, &nv, PV_NONE))
55         return CMD_FAILURE;
56
57       /* Get the optional format specification. */
58       if (lex_match (lexer, '('))
59         {
60           if (!parse_format_specifier (lexer, &f))
61             goto fail;
62           if (fmt_is_string (f.type))
63             {
64               char str[FMT_STRING_LEN_MAX + 1];
65               msg (SE, _("Format type %s may not be used with a numeric "
66                          "variable."), fmt_to_string (&f, str));
67               goto fail;
68             }
69
70           if (!lex_match (lexer, ')'))
71             {
72               msg (SE, _("`)' expected after output format."));
73               goto fail;
74             }
75         }
76       else
77         f.type = -1;
78
79       /* Create each variable. */
80       for (i = 0; i < nv; i++)
81         {
82           struct variable *new_var = dict_create_var (dataset_dict (ds), v[i], 0);
83           if (!new_var)
84             msg (SE, _("There is already a variable named %s."), v[i]);
85           else
86             {
87               if (f.type != -1)
88                 var_set_both_formats (new_var, &f);
89             }
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, '/'));
98
99   return lex_end_of_command (lexer);
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, &v, &nv, PV_NONE))
129         return CMD_FAILURE;
130
131       if (!lex_force_match (lexer, '(')
132           || !parse_format_specifier (lexer, &f)
133           || !lex_force_match (lexer, ')'))
134         goto fail;
135       if (!fmt_is_string (f.type))
136         {
137           char str[FMT_STRING_LEN_MAX + 1];
138           msg (SE, _("Format type %s may not be used with a string "
139                      "variable."), fmt_to_string (&f, str));
140           goto fail;
141         }
142       if (!fmt_check_output (&f))
143         goto fail;
144
145       width = fmt_var_width (&f);
146
147       /* Create each variable. */
148       for (i = 0; i < nv; i++)
149         {
150           struct variable *new_var = dict_create_var (dataset_dict (ds), v[i],
151                                                       width);
152           if (!new_var)
153             msg (SE, _("There is already a variable named %s."), v[i]);
154           else
155             var_set_both_formats (new_var, &f);
156         }
157
158       /* Clean up. */
159       for (i = 0; i < nv; i++)
160         free (v[i]);
161       free (v);
162     }
163   while (lex_match (lexer, '/'));
164
165   return lex_end_of_command (lexer);
166
167   /* If we have an error at a point where cleanup is required,
168      flow-of-control comes here. */
169 fail:
170   for (i = 0; i < nv; i++)
171     free (v[i]);
172   free (v);
173   return CMD_FAILURE;
174 }
175
176 /* Parses the LEAVE command. */
177 int
178 cmd_leave (struct lexer *lexer, struct dataset *ds)
179 {
180   struct variable **v;
181   size_t nv;
182
183   size_t i;
184
185   if (!parse_variables (lexer, dataset_dict (ds), &v, &nv, PV_NONE))
186     return CMD_CASCADING_FAILURE;
187   for (i = 0; i < nv; i++)
188     var_set_leave (v[i], true);
189   free (v);
190
191   return lex_end_of_command (lexer);
192 }