1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2010, 2011 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/message.h"
31 #include "libpspp/misc.h"
32 #include "libpspp/pool.h"
33 #include "libpspp/str.h"
35 #include "gl/intprops.h"
36 #include "gl/xalloc.h"
39 #define _(msgid) gettext (msgid)
42 cmd_vector (struct lexer *lexer, struct dataset *ds)
44 struct dictionary *dict = dataset_dict (ds);
45 struct pool *pool = pool_create ();
50 size_t vector_cnt, vector_cap;
52 /* Get the name(s) of the new vector(s). */
53 if (!lex_force_id (lexer)
54 || !dict_id_is_valid (dict, lex_tokcstr (lexer), true))
55 return CMD_CASCADING_FAILURE;
58 vector_cnt = vector_cap = 0;
59 while (lex_token (lexer) == T_ID)
63 if (dict_lookup_vector (dict, lex_tokcstr (lexer)))
65 msg (SE, _("A vector named %s already exists."),
70 for (i = 0; i < vector_cnt; i++)
71 if (!strcasecmp (vectors[i], lex_tokcstr (lexer)))
73 msg (SE, _("Vector name %s is given twice."),
78 if (vector_cnt == vector_cap)
79 vectors = pool_2nrealloc (pool,
80 vectors, &vector_cap, sizeof *vectors);
81 vectors[vector_cnt++] = pool_strdup (pool, lex_tokcstr (lexer));
84 lex_match (lexer, T_COMMA);
87 /* Now that we have the names it's time to check for the short
89 if (lex_match (lexer, T_EQUALS))
97 msg (SE, _("A slash must separate each vector "
98 "specification in VECTOR's long form."));
102 if (!parse_variables_pool (lexer, pool, dict, &v, &nv,
103 PV_SAME_WIDTH | PV_DUPLICATE))
106 dict_create_vector (dict, vectors[0], v, nv);
108 else if (lex_match (lexer, T_LPAREN))
111 struct fmt_spec format;
112 bool seen_format = false;
114 struct variable **vars;
120 format = fmt_for_output (FMT_F, 8, 2);
122 while (!lex_match (lexer, T_RPAREN))
124 if (lex_is_integer (lexer) && var_cnt == 0)
126 var_cnt = lex_integer (lexer);
130 msg (SE, _("Vectors must have at least one element."));
134 else if (lex_token (lexer) == T_ID && !seen_format)
137 if (!parse_format_specifier (lexer, &format)
138 || !fmt_check_output (&format)
139 || !fmt_check_type_compat (&format, VAL_NUMERIC))
144 lex_error (lexer, NULL);
147 lex_match (lexer, T_COMMA);
151 lex_error (lexer, _("expecting vector length"));
155 /* Check that none of the variables exist and that their names are
156 not excessively long. */
157 for (i = 0; i < vector_cnt; i++)
160 for (j = 0; j < var_cnt; j++)
162 char *name = xasprintf ("%s%d", vectors[i], j + 1);
163 if (!dict_id_is_valid (dict, name, true))
168 if (dict_lookup_var (dict, name))
171 msg (SE, _("%s is an existing variable name."), name);
178 /* Finally create the variables and vectors. */
179 vars = pool_nmalloc (pool, var_cnt, sizeof *vars);
180 for (i = 0; i < vector_cnt; i++)
183 for (j = 0; j < var_cnt; j++)
185 char *name = xasprintf ("%s%d", vectors[i], j + 1);
186 vars[j] = dict_create_var_assert (dict, name, 0);
187 var_set_both_formats (vars[j], &format);
190 dict_create_vector_assert (dict, vectors[i], vars, var_cnt);
195 lex_error (lexer, NULL);
199 while (lex_match (lexer, T_SLASH));