Remove "Written by Ben Pfaff <blp@gnu.org>" lines everywhere.
[pspp-builds.git] / src / language / dictionary / numeric.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 1997-9, 2000, 2006 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or
5    modify it under the terms of the GNU General Public License as
6    published by the Free Software Foundation; either version 2 of the
7    License, or (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful, but
10    WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software
16    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17    02110-1301, USA. */
18
19 #include <config.h>
20
21 #include <stdlib.h>
22
23 #include <data/dictionary.h>
24 #include <data/procedure.h>
25 #include <data/variable.h>
26 #include <language/command.h>
27 #include <language/lexer/format-parser.h>
28 #include <language/lexer/lexer.h>
29 #include <language/lexer/variable-parser.h>
30 #include <libpspp/assertion.h>
31 #include <libpspp/message.h>
32 #include <libpspp/str.h>
33
34 #include "gettext.h"
35 #define _(msgid) gettext (msgid)
36
37 /* Parses the NUMERIC command. */
38 int
39 cmd_numeric (struct lexer *lexer, struct dataset *ds)
40 {
41   size_t i;
42
43   /* Names of variables to create. */
44   char **v;
45   size_t nv;
46
47   /* Format spec for variables to create.  f.type==-1 if default is to
48      be used. */
49   struct fmt_spec f;
50
51   do
52     {
53       if (!parse_DATA_LIST_vars (lexer, &v, &nv, PV_NONE))
54         return CMD_FAILURE;
55
56       /* Get the optional format specification. */
57       if (lex_match (lexer, '('))
58         {
59           if (!parse_format_specifier (lexer, &f))
60             goto fail;
61           if (fmt_is_string (f.type))
62             {
63               char str[FMT_STRING_LEN_MAX + 1];
64               msg (SE, _("Format type %s may not be used with a numeric "
65                          "variable."), fmt_to_string (&f, str));
66               goto fail;
67             }
68
69           if (!lex_match (lexer, ')'))
70             {
71               msg (SE, _("`)' expected after output format."));
72               goto fail;
73             }
74         }
75       else
76         f.type = -1;
77
78       /* Create each variable. */
79       for (i = 0; i < nv; i++)
80         {
81           struct variable *new_var = dict_create_var (dataset_dict (ds), v[i], 0);
82           if (!new_var)
83             msg (SE, _("There is already a variable named %s."), v[i]);
84           else
85             {
86               if (f.type != -1)
87                 var_set_both_formats (new_var, &f);
88             }
89         }
90
91       /* Clean up. */
92       for (i = 0; i < nv; i++)
93         free (v[i]);
94       free (v);
95     }
96   while (lex_match (lexer, '/'));
97
98   return lex_end_of_command (lexer);
99
100   /* If we have an error at a point where cleanup is required,
101      flow-of-control comes here. */
102 fail:
103   for (i = 0; i < nv; i++)
104     free (v[i]);
105   free (v);
106   return CMD_FAILURE;
107 }
108
109 /* Parses the STRING command. */
110 int
111 cmd_string (struct lexer *lexer, struct dataset *ds)
112 {
113   size_t i;
114
115   /* Names of variables to create. */
116   char **v;
117   size_t nv;
118
119   /* Format spec for variables to create. */
120   struct fmt_spec f;
121
122   /* Width of variables to create. */
123   int width;
124
125   do
126     {
127       if (!parse_DATA_LIST_vars (lexer, &v, &nv, PV_NONE))
128         return CMD_FAILURE;
129
130       if (!lex_force_match (lexer, '(')
131           || !parse_format_specifier (lexer, &f)
132           || !lex_force_match (lexer, ')'))
133         goto fail;
134       if (!fmt_is_string (f.type))
135         {
136           char str[FMT_STRING_LEN_MAX + 1];
137           msg (SE, _("Format type %s may not be used with a string "
138                      "variable."), fmt_to_string (&f, str));
139           goto fail;
140         }
141       if (!fmt_check_output (&f))
142         goto fail;
143
144       width = fmt_var_width (&f);
145
146       /* Create each variable. */
147       for (i = 0; i < nv; i++)
148         {
149           struct variable *new_var = dict_create_var (dataset_dict (ds), v[i],
150                                                       width);
151           if (!new_var)
152             msg (SE, _("There is already a variable named %s."), v[i]);
153           else
154             var_set_both_formats (new_var, &f);
155         }
156
157       /* Clean up. */
158       for (i = 0; i < nv; i++)
159         free (v[i]);
160       free (v);
161     }
162   while (lex_match (lexer, '/'));
163
164   return lex_end_of_command (lexer);
165
166   /* If we have an error at a point where cleanup is required,
167      flow-of-control comes here. */
168 fail:
169   for (i = 0; i < nv; i++)
170     free (v[i]);
171   free (v);
172   return CMD_FAILURE;
173 }
174
175 /* Parses the LEAVE command. */
176 int
177 cmd_leave (struct lexer *lexer, struct dataset *ds)
178 {
179   struct variable **v;
180   size_t nv;
181
182   size_t i;
183
184   if (!parse_variables (lexer, dataset_dict (ds), &v, &nv, PV_NONE))
185     return CMD_CASCADING_FAILURE;
186   for (i = 0; i < nv; i++)
187     var_set_leave (v[i], true);
188   free (v);
189
190   return lex_end_of_command (lexer);
191 }