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 return CMD_CASCADING_FAILURE;
56 char *error = dict_id_is_valid__ (dict, lex_tokcstr (lexer));
59 lex_error (lexer, "%s", error);
61 return CMD_CASCADING_FAILURE;
65 n_vectors = allocated_vectors = 0;
66 while (lex_token (lexer) == T_ID)
70 if (dict_lookup_vector (dict, lex_tokcstr (lexer)))
72 lex_next_error (lexer, 0, 0,
73 _("A vector named %s already exists."),
78 for (i = 0; i < n_vectors; i++)
79 if (!utf8_strcasecmp (vectors[i], lex_tokcstr (lexer)))
81 lex_next_error (lexer, 0, 0,
82 _("Vector name %s is given twice."),
87 if (n_vectors == allocated_vectors)
88 vectors = pool_2nrealloc (pool, vectors, &allocated_vectors,
90 vectors[n_vectors++] = pool_strdup (pool, lex_tokcstr (lexer));
93 lex_match (lexer, T_COMMA);
96 /* Now that we have the names it's time to check for the short
98 if (lex_match (lexer, T_EQUALS))
106 lex_error (lexer, _("A slash must separate each vector "
107 "specification in VECTOR's long form."));
111 if (!parse_variables_pool (lexer, pool, dict, &v, &nv,
112 PV_SAME_WIDTH | PV_DUPLICATE))
115 dict_create_vector (dict, vectors[0], v, nv);
117 else if (lex_match (lexer, T_LPAREN))
120 struct fmt_spec format = fmt_for_output (FMT_F, 8, 2);
121 bool seen_format = false;
123 int start_ofs = lex_ofs (lexer) - 2;
124 while (!lex_match (lexer, T_RPAREN))
126 if (lex_is_integer (lexer) && n_vars == 0)
128 if (!lex_force_int_range (lexer, NULL, 1, INT_MAX))
130 n_vars = lex_integer (lexer);
133 else if (lex_token (lexer) == T_ID && !seen_format)
136 if (!parse_format_specifier (lexer, &format))
138 char *error = fmt_check_output__ (&format);
141 lex_next_error (lexer, -1, -1, "%s", error);
148 lex_error (lexer, NULL);
151 lex_match (lexer, T_COMMA);
153 int end_ofs = lex_ofs (lexer) - 1;
156 lex_error (lexer, _("Syntax error expecting vector length."));
160 /* Check that none of the variables exist and that their names are
161 not excessively long. */
162 for (size_t i = 0; i < n_vectors; i++)
165 for (j = 0; j < n_vars; j++)
167 char *name = xasprintf ("%s%d", vectors[i], j + 1);
168 char *error = dict_id_is_valid__ (dict, name);
171 lex_ofs_error (lexer, start_ofs, end_ofs, "%s", error);
176 if (dict_lookup_var (dict, name))
178 lex_ofs_error (lexer, start_ofs, end_ofs,
179 _("%s is an existing variable name."),
188 /* Finally create the variables and vectors. */
189 struct variable **vars = pool_nmalloc (pool, n_vars, sizeof *vars);
190 for (size_t i = 0; i < n_vectors; i++)
192 for (size_t j = 0; j < n_vars; j++)
194 char *name = xasprintf ("%s%zu", vectors[i], j + 1);
195 vars[j] = dict_create_var_assert (dict, name,
196 fmt_var_width (&format));
197 var_set_both_formats (vars[j], &format);
200 dict_create_vector_assert (dict, vectors[i], vars, n_vars);
205 lex_error (lexer, NULL);
209 while (lex_match (lexer, T_SLASH));