8ad65690b0380c0c63f5a2d14c34dcf94efdc76a
[pspp-builds.git] / src / language / dictionary / vector.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 1997-9, 2000 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/format.h>
24 #include <data/procedure.h>
25 #include <data/dictionary.h>
26 #include <data/variable.h>
27 #include <language/command.h>
28 #include <language/lexer/format-parser.h>
29 #include <language/lexer/lexer.h>
30 #include <language/lexer/variable-parser.h>
31 #include <libpspp/alloc.h>
32 #include <libpspp/assertion.h>
33 #include <libpspp/message.h>
34 #include <libpspp/misc.h>
35 #include <libpspp/pool.h>
36 #include <libpspp/str.h>
37
38 #include "intprops.h"
39
40 #include "gettext.h"
41 #define _(msgid) gettext (msgid)
42
43 int
44 cmd_vector (struct lexer *lexer, struct dataset *ds)
45 {
46   struct dictionary *dict = dataset_dict (ds);
47   struct pool *pool = pool_create ();
48
49   do
50     {
51       char **vectors;
52       size_t vector_cnt, vector_cap;
53
54       /* Get the name(s) of the new vector(s). */
55       if (!lex_force_id (lexer))
56         return CMD_CASCADING_FAILURE;
57
58       vectors = NULL;
59       vector_cnt = vector_cap = 0;
60       while (lex_token (lexer) == T_ID)
61         {
62           size_t i;
63
64           if (dict_lookup_vector (dict, lex_tokid (lexer)))
65             {
66               msg (SE, _("A vector named %s already exists."),
67                    lex_tokid (lexer));
68               goto fail;
69             }
70
71           for (i = 0; i < vector_cnt; i++)
72             if (!strcasecmp (vectors[i], lex_tokid (lexer)))
73               {
74                 msg (SE, _("Vector name %s is given twice."),
75                      lex_tokid (lexer));
76                 goto fail;
77               }
78
79           if (vector_cnt == vector_cap)
80             vectors = pool_2nrealloc (pool,
81                                        vectors, &vector_cap, sizeof *vectors);
82           vectors[vector_cnt++] = xstrdup (lex_tokid (lexer));
83
84           lex_get (lexer);
85           lex_match (lexer, ',');
86         }
87
88       /* Now that we have the names it's time to check for the short
89          or long forms. */
90       if (lex_match (lexer, '='))
91         {
92           /* Long form. */
93           struct variable **v;
94           size_t nv;
95
96           if (vector_cnt > 1)
97             {
98               msg (SE, _("A slash must separate each vector "
99                          "specification in VECTOR's long form."));
100               goto fail;
101             }
102
103           if (!parse_variables_pool (lexer, pool, dict, &v, &nv,
104                                      PV_SAME_WIDTH | PV_DUPLICATE))
105             goto fail;
106
107           dict_create_vector (dict, vectors[0], v, nv);
108         }
109       else if (lex_match (lexer, '('))
110         {
111           /* Short form. */
112           struct fmt_spec format;
113           bool seen_format = false;
114
115           struct variable **vars;
116           int var_cnt;
117
118           size_t i;
119
120           var_cnt = 0;
121           format = fmt_for_output (FMT_F, 8, 2);
122           seen_format = false;
123           while (!lex_match (lexer, ')'))
124             {
125               if (lex_is_integer (lexer) && var_cnt == 0)
126                 {
127                   var_cnt = lex_integer (lexer);
128                   lex_get (lexer);
129                   if (var_cnt <= 0)
130                     {
131                       msg (SE, _("Vectors must have at least one element."));
132                       goto fail;
133                     }
134                 }
135               else if (lex_token (lexer) == T_ID && !seen_format)
136                 {
137                   seen_format = true;
138                   if (!parse_format_specifier (lexer, &format)
139                       || !fmt_check_output (&format)
140                       || !fmt_check_type_compat (&format, VAR_NUMERIC))
141                     goto fail;
142                 }
143               else
144                 {
145                   lex_error (lexer, NULL);
146                   goto fail;
147                 }
148               lex_match (lexer, ',');
149             }
150           if (var_cnt == 0)
151             {
152               lex_error (lexer, _("expecting vector length"));
153               goto fail;
154             }
155
156           /* Check that none of the variables exist and that
157              their names are no more than LONG_NAME_LEN bytes
158              long. */
159           for (i = 0; i < vector_cnt; i++)
160             {
161               int j;
162               for (j = 0; j < var_cnt; j++)
163                 {
164                   char name[LONG_NAME_LEN + INT_STRLEN_BOUND (int) + 1];
165                   sprintf (name, "%s%d", vectors[i], j + 1);
166                   if (strlen (name) > LONG_NAME_LEN)
167                     {
168                       msg (SE, _("%s is too long for a variable name."), name);
169                       goto fail;
170                     }
171                   if (dict_lookup_var (dict, name))
172                     {
173                       msg (SE, _("%s is an existing variable name."), name);
174                       goto fail;
175                     }
176                 }
177             }
178
179           /* Finally create the variables and vectors. */
180           vars = pool_nmalloc (pool, var_cnt, sizeof *vars);
181           for (i = 0; i < vector_cnt; i++)
182             {
183               int j;
184               for (j = 0; j < var_cnt; j++)
185                 {
186                   char name[LONG_NAME_LEN + 1];
187                   sprintf (name, "%s%d", vectors[i], j + 1);
188                   vars[j] = dict_create_var_assert (dict, name, 0);
189                   var_set_both_formats (vars[j], &format);
190                 }
191               dict_create_vector_assert (dict, vectors[i], vars, var_cnt);
192             }
193         }
194       else
195         {
196           lex_error (lexer, NULL);
197           goto fail;
198         }
199     }
200   while (lex_match (lexer, '/'));
201
202   pool_destroy (pool);
203   return lex_end_of_command (lexer);
204
205 fail:
206   pool_destroy (pool);
207   return CMD_FAILURE;
208 }