treewide: Replace <name>_cnt by n_<name>s and <name>_cap by allocated_<name>.
[pspp] / src / language / dictionary / vector.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2010, 2011, 2012, 2016 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU 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, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 #include <stdlib.h>
20
21 #include "data/dataset.h"
22 #include "data/format.h"
23 #include "data/dictionary.h"
24 #include "data/variable.h"
25 #include "language/command.h"
26 #include "language/lexer/format-parser.h"
27 #include "language/lexer/lexer.h"
28 #include "language/lexer/variable-parser.h"
29 #include "libpspp/assertion.h"
30 #include "libpspp/i18n.h"
31 #include "libpspp/message.h"
32 #include "libpspp/misc.h"
33 #include "libpspp/pool.h"
34 #include "libpspp/str.h"
35
36 #include "gl/intprops.h"
37 #include "gl/xalloc.h"
38
39 #include "gettext.h"
40 #define _(msgid) gettext (msgid)
41
42 int
43 cmd_vector (struct lexer *lexer, struct dataset *ds)
44 {
45   struct dictionary *dict = dataset_dict (ds);
46   struct pool *pool = pool_create ();
47
48   do
49     {
50       char **vectors;
51       size_t n_vectors, allocated_vectors;
52
53       /* Get the name(s) of the new vector(s). */
54       if (!lex_force_id (lexer)
55           || !dict_id_is_valid (dict, lex_tokcstr (lexer), true))
56         return CMD_CASCADING_FAILURE;
57
58       vectors = NULL;
59       n_vectors = allocated_vectors = 0;
60       while (lex_token (lexer) == T_ID)
61         {
62           size_t i;
63
64           if (dict_lookup_vector (dict, lex_tokcstr (lexer)))
65             {
66               msg (SE, _("A vector named %s already exists."),
67                    lex_tokcstr (lexer));
68               goto fail;
69             }
70
71           for (i = 0; i < n_vectors; i++)
72             if (!utf8_strcasecmp (vectors[i], lex_tokcstr (lexer)))
73               {
74                 msg (SE, _("Vector name %s is given twice."),
75                      lex_tokcstr (lexer));
76                 goto fail;
77               }
78
79           if (n_vectors == allocated_vectors)
80             vectors = pool_2nrealloc (pool, vectors, &allocated_vectors,
81                                       sizeof *vectors);
82           vectors[n_vectors++] = pool_strdup (pool, lex_tokcstr (lexer));
83
84           lex_get (lexer);
85           lex_match (lexer, T_COMMA);
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, T_EQUALS))
91         {
92           /* Long form. */
93           struct variable **v;
94           size_t nv;
95
96           if (n_vectors > 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, T_LPAREN))
110         {
111           /* Short form. */
112           struct fmt_spec format;
113           bool seen_format = false;
114
115           struct variable **vars;
116           int n_vars;
117
118           size_t i;
119
120           n_vars = 0;
121           format = fmt_for_output (FMT_F, 8, 2);
122           seen_format = false;
123           while (!lex_match (lexer, T_RPAREN))
124             {
125               if (lex_is_integer (lexer) && n_vars == 0)
126                 {
127                   if (!lex_force_int_range (lexer, NULL, 1, INT_MAX))
128                     goto fail;
129                   n_vars = lex_integer (lexer);
130                   lex_get (lexer);
131                 }
132               else if (lex_token (lexer) == T_ID && !seen_format)
133                 {
134                   seen_format = true;
135                   if (!parse_format_specifier (lexer, &format)
136                       || !fmt_check_output (&format))
137                     goto fail;
138                 }
139               else
140                 {
141                   lex_error (lexer, NULL);
142                   goto fail;
143                 }
144               lex_match (lexer, T_COMMA);
145             }
146           if (n_vars == 0)
147             {
148               lex_error (lexer, _("expecting vector length"));
149               goto fail;
150             }
151
152           /* Check that none of the variables exist and that their names are
153              not excessively long. */
154           for (i = 0; i < n_vectors; i++)
155             {
156               int j;
157               for (j = 0; j < n_vars; j++)
158                 {
159                   char *name = xasprintf ("%s%d", vectors[i], j + 1);
160                   if (!dict_id_is_valid (dict, name, true))
161                     {
162                       free (name);
163                       goto fail;
164                     }
165                   if (dict_lookup_var (dict, name))
166                     {
167                       msg (SE, _("%s is an existing variable name."), name);
168                       free (name);
169                       goto fail;
170                     }
171                   free (name);
172                 }
173             }
174
175           /* Finally create the variables and vectors. */
176           vars = pool_nmalloc (pool, n_vars, sizeof *vars);
177           for (i = 0; i < n_vectors; i++)
178             {
179               int j;
180               for (j = 0; j < n_vars; j++)
181                 {
182                   char *name = xasprintf ("%s%d", vectors[i], j + 1);
183                   vars[j] = dict_create_var_assert (dict, name,
184                                                     fmt_var_width (&format));
185                   var_set_both_formats (vars[j], &format);
186                   free (name);
187                 }
188               dict_create_vector_assert (dict, vectors[i], vars, n_vars);
189             }
190         }
191       else
192         {
193           lex_error (lexer, NULL);
194           goto fail;
195         }
196     }
197   while (lex_match (lexer, T_SLASH));
198
199   pool_destroy (pool);
200   return CMD_SUCCESS;
201
202 fail:
203   pool_destroy (pool);
204   return CMD_FAILURE;
205 }