1 /* PSPP - computes sample statistics.
2 Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3 Written by Ben Pfaff <blp@gnu.org>.
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24 #include <data/procedure.h>
25 #include <data/dictionary.h>
26 #include <data/variable.h>
27 #include <language/command.h>
28 #include <language/lexer/lexer.h>
29 #include <language/lexer/variable-parser.h>
30 #include <libpspp/alloc.h>
31 #include <libpspp/assertion.h>
32 #include <libpspp/message.h>
33 #include <libpspp/misc.h>
34 #include <libpspp/str.h>
37 #define _(msgid) gettext (msgid)
40 cmd_vector (struct dataset *ds)
42 /* Just to be different, points to a set of null terminated strings
43 containing the names of the vectors to be created. The list
44 itself is terminated by a empty string. So a list of three
45 elements, A B C, would look like this: "A\0B\0C\0\0". */
48 /* vecnames iterators. */
51 /* Maximum allocated position for vecnames, plus one position. */
54 struct dictionary *dict = dataset_dict (ds);
56 cp = vecnames = xmalloc (256);
57 endp = &vecnames[256];
60 /* Get the name(s) of the new vector(s). */
62 return CMD_CASCADING_FAILURE;
67 char *old_vecnames = vecnames;
68 vecnames = xrealloc (vecnames, endp - vecnames + 256);
69 cp = (cp - old_vecnames) + vecnames;
70 endp = (endp - old_vecnames) + vecnames + 256;
73 for (cp2 = cp; cp2 < cp; cp2 += strlen (cp))
74 if (!strcasecmp (cp2, tokid))
76 msg (SE, _("Vector name %s is given twice."), tokid);
80 if (dict_lookup_vector (dict, tokid))
82 msg (SE, _("There is already a vector with name %s."), tokid);
86 cp = stpcpy (cp, tokid) + 1;
92 /* Now that we have the names it's time to check for the short
100 if (strchr (vecnames, '\0')[1])
102 /* There's more than one vector name. */
103 msg (SE, _("A slash must be used to separate each vector "
104 "specification when using the long form. Commands "
105 "such as VECTOR A,B=Q1 TO Q20 are not supported."));
109 if (!parse_variables (dict, &v, &nv,
110 PV_SAME_TYPE | PV_DUPLICATE))
113 dict_create_vector (dict, vecnames, v, nv);
116 else if (lex_match ('('))
120 /* Maximum number of digits in a number to add to the base
124 /* Name of an individual variable to be created. */
125 char name[SHORT_NAME_LEN + 1];
127 /* Vector variables. */
131 if (!lex_force_int ())
132 return CMD_CASCADING_FAILURE;
137 msg (SE, _("Vectors must have at least one element."));
140 if (!lex_force_match (')'))
143 /* First check that all the generated variable names
144 are LONG_NAME_LEN characters or shorter. */
145 ndig = intlog10 (nv);
146 for (cp = vecnames; *cp;)
148 int len = strlen (cp);
149 if (len + ndig > LONG_NAME_LEN)
151 msg (SE, _("%s%d is too long for a variable name."), cp, nv);
157 /* Next check that none of the variables exist. */
158 for (cp = vecnames; *cp;)
160 for (i = 0; i < nv; i++)
162 sprintf (name, "%s%d", cp, i + 1);
163 if (dict_lookup_var (dict, name))
165 msg (SE, _("There is already a variable named %s."),
170 cp += strlen (cp) + 1;
173 /* Finally create the variables and vectors. */
174 v = xmalloc (nv * sizeof *v);
175 for (cp = vecnames; *cp;)
177 for (i = 0; i < nv; i++)
179 sprintf (name, "%s%d", cp, i + 1);
180 v[i] = dict_create_var_assert (dict, name, 0);
182 if (!dict_create_vector (dict, cp, v, nv))
184 cp += strlen (cp) + 1;
190 msg (SE, _("The syntax for this command does not match "
191 "the expected syntax for either the long form "
192 "or the short form of VECTOR."));
199 while (lex_match ('/'));
203 lex_error (_("expecting end of command"));