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., 59 Temple Place - Suite 330, Boston, MA
33 /* Vectors created on VECTOR. */
36 /* Number of vectors in vec. */
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 /* Variables on list (long form only). */
55 struct variable **v = NULL;
58 lex_match_id ("VECTOR");
60 cp = vecnames = xmalloc (256);
61 endp = &vecnames[256];
64 /* Get the name(s) of the new vector(s). */
71 char *old_vecnames = vecnames;
72 vecnames = xrealloc (vecnames, endp - vecnames + 256);
73 cp = (cp - old_vecnames) + vecnames;
74 endp = (endp - old_vecnames) + vecnames + 256;
77 for (cp2 = cp; cp2 < cp; cp2 += strlen (cp))
78 if (!strcmp (cp2, tokid))
80 msg (SE, _("Vector name %s is given twice."), tokid);
84 if (find_vector (tokid))
86 msg (SE, _("There is already a vector with name %s."), tokid);
90 cp = stpcpy (cp, tokid) + 1;
96 /* Now that we have the names it's time to check for the short
102 if (strchr (vecnames, '\0')[1])
104 /* There's more than one vector name. */
105 msg (SE, _("A slash must be used to separate each vector "
106 "specification when using the long form. Commands "
107 "such as VECTOR A,B=Q1 TO Q20 are not supported."));
111 if (!parse_variables (NULL, &v, &nv, PV_SAME_TYPE | PV_DUPLICATE))
114 vec = xrealloc (vec, sizeof *vec * (nvec + 1));
115 vec[nvec].index = nvec;
116 strcpy (vec[nvec].name, vecnames);
120 v = NULL; /* prevent block from being freed on error */
122 else if (lex_match ('('))
126 /* Maximum number of digits in a number to add to the base
130 /* Name of an individual variable to be created. */
133 if (!lex_force_int ())
139 msg (SE, _("Vectors must have at least one element."));
142 if (!lex_force_match (')'))
145 /* First check that all the generated variable names are 8
146 characters or shorter. */
147 ndig = intlog10 (nv);
148 for (cp = vecnames; *cp;)
150 int len = strlen (cp);
153 msg (SE, _("%s%d is too long for a variable name."), cp, nv);
159 /* Next check that none of the variables exist. */
160 for (cp = vecnames; *cp;)
162 for (i = 0; i < nv; i++)
164 sprintf (name, "%s%d", cp, i + 1);
165 if (is_varname (name))
167 msg (SE, _("There is already a variable named %s."), name);
171 cp += strlen (cp) + 1;
174 /* Finally create the variables and vectors. */
175 vec = xrealloc (vec, sizeof *vec * (nvec + nv));
176 for (cp = vecnames; *cp;)
178 vec[nvec].index = nvec;
179 strcpy (vec[nvec].name, cp);
180 vec[nvec].v = xmalloc (sizeof *vec[nvec].v * nv);
182 for (i = 0; i < nv; i++)
184 sprintf (name, "%s%d", cp, i + 1);
185 vec[nvec].v[i] = force_create_variable (&default_dict, name,
187 envector (vec[nvec].v[i]);
190 cp += strlen (cp) + 1;
195 msg (SE, _("The syntax for this command does not match "
196 "the expected syntax for either the long form "
197 "or the short form of VECTOR."));
204 while (lex_match ('/'));
208 lex_error (_("expecting end of command"));
216 return CMD_PART_SUCCESS_MAYBE;
219 /* Returns a pointer to the vector with name NAME, or NULL on
222 find_vector (const char *name)
226 for (i = 0; i < nvec; i++)
227 if (!strcmp (vec[i].name, name))