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