b97410a51bc234ffb826e06d6705f339ea56e6eb
[pspp-builds.git] / src / numeric.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 1997-9, 2000 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., 59 Temple Place - Suite 330, Boston, MA
18    02111-1307, USA. */
19
20 #include <config.h>
21 #include <assert.h>
22 #include <stdlib.h>
23 #include "command.h"
24 #include "error.h"
25 #include "lexer.h"
26 #include "str.h"
27 #include "var.h"
28
29 #include "debug-print.h"
30
31 /* Parses the NUMERIC command. */
32 int
33 cmd_numeric (void)
34 {
35   int i;
36
37   /* Names of variables to create. */
38   char **v;
39   int nv;
40
41   /* Format spec for variables to create.  f.type==-1 if default is to
42      be used. */
43   struct fmt_spec f;
44
45   lex_match_id ("NUMERIC");
46   do
47     {
48       if (!parse_DATA_LIST_vars (&v, &nv, PV_NONE))
49         return CMD_PART_SUCCESS_MAYBE;
50
51       /* Get the optional format specification. */
52       if (lex_match ('('))
53         {
54           if (!parse_format_specifier (&f, 0))
55             goto fail;
56           if (formats[f.type].cat & FCAT_STRING)
57             {
58               msg (SE, _("Format type %s may not be used with a numeric "
59                    "variable."), fmt_to_string (&f));
60               goto fail;
61             }
62
63           if (!lex_match (')'))
64             {
65               msg (SE, _("`)' expected after output format."));
66               goto fail;
67             }
68         }
69       else
70         f.type = -1;
71
72       /* Create each variable. */
73       for (i = 0; i < nv; i++)
74         {
75           struct variable *new_var = dict_create_var (default_dict, v[i], 0);
76           if (!new_var)
77             msg (SE, _("There is already a variable named %s."), v[i]);
78           else
79             {
80               if (f.type != -1)
81                 new_var->print = new_var->write = f;
82             }
83         }
84
85       /* Clean up. */
86       for (i = 0; i < nv; i++)
87         free (v[i]);
88       free (v);
89     }
90   while (lex_match ('/'));
91
92   return lex_end_of_command ();
93
94   /* If we have an error at a point where cleanup is required,
95      flow-of-control comes here. */
96 fail:
97   for (i = 0; i < nv; i++)
98     free (v[i]);
99   free (v);
100   return CMD_PART_SUCCESS_MAYBE;
101 }
102
103 /* Parses the STRING command. */
104 int
105 cmd_string (void)
106 {
107   int i;
108
109   /* Names of variables to create. */
110   char **v;
111   int nv;
112
113   /* Format spec for variables to create. */
114   struct fmt_spec f;
115
116   /* Width of variables to create. */
117   int width;
118
119   lex_match_id ("STRING");
120   do
121     {
122       if (!parse_DATA_LIST_vars (&v, &nv, PV_NONE))
123         return CMD_PART_SUCCESS_MAYBE;
124
125       if (!lex_force_match ('(')
126           || !parse_format_specifier (&f, 0))
127         goto fail;
128       if (!(formats[f.type].cat & FCAT_STRING))
129         {
130           msg (SE, _("Format type %s may not be used with a string "
131                "variable."), fmt_to_string (&f));
132           goto fail;
133         }
134
135       if (!lex_match (')'))
136         {
137           msg (SE, _("`)' expected after output format."));
138           goto fail;
139         }
140
141       switch (f.type)
142         {
143         case FMT_A:
144           width = f.w;
145           break;
146         case FMT_AHEX:
147           width = f.w / 2;
148           break;
149         default:
150           assert (0);
151         }
152
153       /* Create each variable. */
154       for (i = 0; i < nv; i++)
155         {
156           struct variable *new_var = dict_create_var (default_dict, v[i],
157                                                       width);
158           if (!new_var)
159             msg (SE, _("There is already a variable named %s."), v[i]);
160           else
161             new_var->print = new_var->write = f;
162         }
163
164       /* Clean up. */
165       for (i = 0; i < nv; i++)
166         free (v[i]);
167       free (v);
168     }
169   while (lex_match ('/'));
170
171   return lex_end_of_command ();
172
173   /* If we have an error at a point where cleanup is required,
174      flow-of-control comes here. */
175 fail:
176   for (i = 0; i < nv; i++)
177     free (v[i]);
178   free (v);
179   return CMD_PART_SUCCESS_MAYBE;
180 }
181
182 /* Parses the LEAVE command. */
183 int
184 cmd_leave (void)
185 {
186   struct variable **v;
187   int nv;
188
189   int i;
190
191   lex_match_id ("LEAVE");
192   if (!parse_variables (default_dict, &v, &nv, PV_NONE))
193     return CMD_FAILURE;
194   for (i = 0; i < nv; i++)
195     {
196       if (!v[i]->reinit)
197         continue;
198       v[i]->reinit = 0;
199       v[i]->init = 1;
200     }
201   free (v);
202
203   return lex_end_of_command ();
204 }