9d94cf3f7e45350ae49b3e4092d7e0c7f350d359
[pspp-builds.git] / src / language / dictionary / 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., 51 Franklin Street, Fifth Floor, Boston, MA
18    02110-1301, USA. */
19
20 #include <config.h>
21 #include <libpspp/message.h>
22 #include <stdlib.h>
23 #include <language/command.h>
24 #include <data/dictionary.h>
25 #include <libpspp/message.h>
26 #include <language/lexer/lexer.h>
27 #include <libpspp/str.h>
28 #include <data/variable.h>
29
30 #include "gettext.h"
31 #define _(msgid) gettext (msgid)
32
33 /* Parses the NUMERIC command. */
34 int
35 cmd_numeric (void)
36 {
37   size_t i;
38
39   /* Names of variables to create. */
40   char **v;
41   size_t nv;
42
43   /* Format spec for variables to create.  f.type==-1 if default is to
44      be used. */
45   struct fmt_spec f;
46
47   do
48     {
49       if (!parse_DATA_LIST_vars (&v, &nv, PV_NONE))
50         return CMD_FAILURE;
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             }
84         }
85
86       /* Clean up. */
87       for (i = 0; i < nv; i++)
88         free (v[i]);
89       free (v);
90     }
91   while (lex_match ('/'));
92
93   return lex_end_of_command ();
94
95   /* If we have an error at a point where cleanup is required,
96      flow-of-control comes here. */
97 fail:
98   for (i = 0; i < nv; i++)
99     free (v[i]);
100   free (v);
101   return CMD_FAILURE;
102 }
103
104 /* Parses the STRING command. */
105 int
106 cmd_string (void)
107 {
108   size_t i;
109
110   /* Names of variables to create. */
111   char **v;
112   size_t nv;
113
114   /* Format spec for variables to create. */
115   struct fmt_spec f;
116
117   /* Width of variables to create. */
118   int width;
119
120   do
121     {
122       if (!parse_DATA_LIST_vars (&v, &nv, PV_NONE))
123         return CMD_FAILURE;
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           abort ();
152         }
153
154       /* Create each variable. */
155       for (i = 0; i < nv; i++)
156         {
157           struct variable *new_var = dict_create_var (default_dict, v[i],
158                                                       width);
159           if (!new_var)
160             msg (SE, _("There is already a variable named %s."), v[i]);
161           else
162             new_var->print = new_var->write = f;
163         }
164
165       /* Clean up. */
166       for (i = 0; i < nv; i++)
167         free (v[i]);
168       free (v);
169     }
170   while (lex_match ('/'));
171
172   return lex_end_of_command ();
173
174   /* If we have an error at a point where cleanup is required,
175      flow-of-control comes here. */
176 fail:
177   for (i = 0; i < nv; i++)
178     free (v[i]);
179   free (v);
180   return CMD_FAILURE;
181 }
182
183 /* Parses the LEAVE command. */
184 int
185 cmd_leave (void)
186 {
187   struct variable **v;
188   size_t nv;
189
190   size_t i;
191
192   if (!parse_variables (default_dict, &v, &nv, PV_NONE))
193     return CMD_CASCADING_FAILURE;
194   for (i = 0; i < nv; i++)
195     v[i]->leave = true;
196   free (v);
197
198   return lex_end_of_command ();
199 }