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