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