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