1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2010, 2011, 2012 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 vector_cnt, vector_cap;
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 vector_cnt = vector_cap = 0;
60 while (lex_token (lexer) == T_ID)
64 if (dict_lookup_vector (dict, lex_tokcstr (lexer)))
66 msg (SE, _("A vector named %s already exists."),
71 for (i = 0; i < vector_cnt; i++)
72 if (!utf8_strcasecmp (vectors[i], lex_tokcstr (lexer)))
74 msg (SE, _("Vector name %s is given twice."),
79 if (vector_cnt == vector_cap)
80 vectors = pool_2nrealloc (pool,
81 vectors, &vector_cap, sizeof *vectors);
82 vectors[vector_cnt++] = pool_strdup (pool, lex_tokcstr (lexer));
85 lex_match (lexer, T_COMMA);
88 /* Now that we have the names it's time to check for the short
90 if (lex_match (lexer, T_EQUALS))
98 msg (SE, _("A slash must separate each vector "
99 "specification in VECTOR's long form."));
103 if (!parse_variables_pool (lexer, pool, dict, &v, &nv,
104 PV_SAME_WIDTH | PV_DUPLICATE))
107 dict_create_vector (dict, vectors[0], v, nv);
109 else if (lex_match (lexer, T_LPAREN))
112 struct fmt_spec format;
113 bool seen_format = false;
115 struct variable **vars;
121 format = fmt_for_output (FMT_F, 8, 2);
123 while (!lex_match (lexer, T_RPAREN))
125 if (lex_is_integer (lexer) && var_cnt == 0)
127 var_cnt = lex_integer (lexer);
131 msg (SE, _("Vectors must have at least one element."));
135 else if (lex_token (lexer) == T_ID && !seen_format)
138 if (!parse_format_specifier (lexer, &format)
139 || !fmt_check_output (&format)
140 || !fmt_check_type_compat (&format, VAL_NUMERIC))
145 lex_error (lexer, NULL);
148 lex_match (lexer, T_COMMA);
152 lex_error (lexer, _("expecting vector length"));
156 /* Check that none of the variables exist and that their names are
157 not excessively long. */
158 for (i = 0; i < vector_cnt; i++)
161 for (j = 0; j < var_cnt; j++)
163 char *name = xasprintf ("%s%d", vectors[i], j + 1);
164 if (!dict_id_is_valid (dict, name, true))
169 if (dict_lookup_var (dict, name))
172 msg (SE, _("%s is an existing variable name."), name);
179 /* Finally create the variables and vectors. */
180 vars = pool_nmalloc (pool, var_cnt, sizeof *vars);
181 for (i = 0; i < vector_cnt; i++)
184 for (j = 0; j < var_cnt; j++)
186 char *name = xasprintf ("%s%d", vectors[i], j + 1);
187 vars[j] = dict_create_var_assert (dict, name, 0);
188 var_set_both_formats (vars[j], &format);
191 dict_create_vector_assert (dict, vectors[i], vars, var_cnt);
196 lex_error (lexer, NULL);
200 while (lex_match (lexer, T_SLASH));