1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2010, 2011, 2012, 2016 Free Software Foundation, Inc.
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.
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.
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/>. */
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"
36 #include "gl/intprops.h"
37 #include "gl/xalloc.h"
40 #define _(msgid) gettext (msgid)
43 cmd_vector (struct lexer *lexer, struct dataset *ds)
45 struct dictionary *dict = dataset_dict (ds);
46 struct pool *pool = pool_create ();
51 size_t n_vectors, allocated_vectors;
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;
59 n_vectors = allocated_vectors = 0;
60 while (lex_token (lexer) == T_ID)
64 if (dict_lookup_vector (dict, lex_tokcstr (lexer)))
66 lex_next_error (lexer, 0, 0,
67 _("A vector named %s already exists."),
72 for (i = 0; i < n_vectors; i++)
73 if (!utf8_strcasecmp (vectors[i], lex_tokcstr (lexer)))
75 lex_next_error (lexer, 0, 0,
76 _("Vector name %s is given twice."),
81 if (n_vectors == allocated_vectors)
82 vectors = pool_2nrealloc (pool, vectors, &allocated_vectors,
84 vectors[n_vectors++] = pool_strdup (pool, lex_tokcstr (lexer));
87 lex_match (lexer, T_COMMA);
90 /* Now that we have the names it's time to check for the short
92 if (lex_match (lexer, T_EQUALS))
100 lex_error (lexer, _("A slash must separate each vector "
101 "specification in VECTOR's long form."));
105 if (!parse_variables_pool (lexer, pool, dict, &v, &nv,
106 PV_SAME_WIDTH | PV_DUPLICATE))
109 dict_create_vector (dict, vectors[0], v, nv);
111 else if (lex_match (lexer, T_LPAREN))
114 struct fmt_spec format;
115 bool seen_format = false;
117 struct variable **vars;
123 format = fmt_for_output (FMT_F, 8, 2);
125 while (!lex_match (lexer, T_RPAREN))
127 if (lex_is_integer (lexer) && n_vars == 0)
129 if (!lex_force_int_range (lexer, NULL, 1, INT_MAX))
131 n_vars = lex_integer (lexer);
134 else if (lex_token (lexer) == T_ID && !seen_format)
137 if (!parse_format_specifier (lexer, &format)
138 || !fmt_check_output (&format))
143 lex_error (lexer, NULL);
146 lex_match (lexer, T_COMMA);
150 lex_error (lexer, _("Syntax error expecting vector length."));
154 /* Check that none of the variables exist and that their names are
155 not excessively long. */
156 for (i = 0; i < n_vectors; i++)
159 for (j = 0; j < n_vars; j++)
161 char *name = xasprintf ("%s%d", vectors[i], j + 1);
162 if (!dict_id_is_valid (dict, name, true))
167 if (dict_lookup_var (dict, name))
169 msg (SE, _("%s is an existing variable name."), name);
177 /* Finally create the variables and vectors. */
178 vars = pool_nmalloc (pool, n_vars, sizeof *vars);
179 for (i = 0; i < n_vectors; i++)
182 for (j = 0; j < n_vars; j++)
184 char *name = xasprintf ("%s%d", vectors[i], j + 1);
185 vars[j] = dict_create_var_assert (dict, name,
186 fmt_var_width (&format));
187 var_set_both_formats (vars[j], &format);
190 dict_create_vector_assert (dict, vectors[i], vars, n_vars);
195 lex_error (lexer, NULL);
199 while (lex_match (lexer, T_SLASH));