Implemented long variable names a la spss V12.
[pspp-builds.git] / src / vector.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3    Written by Ben Pfaff <blp@gnu.org>.
4
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.
9
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.
14
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
18    02111-1307, USA. */
19
20 #include <config.h>
21 #include "error.h"
22 #include <stdlib.h>
23 #include "alloc.h"
24 #include "command.h"
25 #include "dictionary.h"
26 #include "error.h"
27 #include "lexer.h"
28 #include "misc.h"
29 #include "str.h"
30 #include "var.h"
31
32 int
33 cmd_vector (void)
34 {
35   /* Just to be different, points to a set of null terminated strings
36      containing the names of the vectors to be created.  The list
37      itself is terminated by a empty string.  So a list of three
38      elements, A B C, would look like this: "A\0B\0C\0\0". */
39   char *vecnames;
40
41   /* vecnames iterators. */
42   char *cp, *cp2;
43
44   /* Maximum allocated position for vecnames, plus one position. */
45   char *endp = NULL;
46
47   cp = vecnames = xmalloc (256);
48   endp = &vecnames[256];
49   do
50     {
51       /* Get the name(s) of the new vector(s). */
52       if (!lex_force_id ())
53         return CMD_FAILURE;
54       while (token == T_ID)
55         {
56           if (cp + 16 > endp)
57             {
58               char *old_vecnames = vecnames;
59               vecnames = xrealloc (vecnames, endp - vecnames + 256);
60               cp = (cp - old_vecnames) + vecnames;
61               endp = (endp - old_vecnames) + vecnames + 256;
62             }
63
64           for (cp2 = cp; cp2 < cp; cp2 += strlen (cp))
65             if (!strcmp (cp2, tokid))
66               {
67                 msg (SE, _("Vector name %s is given twice."), tokid);
68                 goto fail;
69               }
70
71           if (dict_lookup_vector (default_dict, tokid))
72             {
73               msg (SE, _("There is already a vector with name %s."), tokid);
74               goto fail;
75             }
76
77           cp = stpcpy (cp, tokid) + 1;
78           lex_get ();
79           lex_match (',');
80         }
81       *cp++ = 0;
82
83       /* Now that we have the names it's time to check for the short
84          or long forms. */
85       if (lex_match ('='))
86         {
87           /* Long form. */
88           struct variable **v;
89           int nv;
90
91           if (strchr (vecnames, '\0')[1])
92             {
93               /* There's more than one vector name. */
94               msg (SE, _("A slash must be used to separate each vector "
95                    "specification when using the long form.  Commands "
96                    "such as VECTOR A,B=Q1 TO Q20 are not supported."));
97               goto fail;
98             }
99
100           if (!parse_variables (default_dict, &v, &nv,
101                                 PV_SAME_TYPE | PV_DUPLICATE))
102             goto fail;
103
104           dict_create_vector (default_dict, vecnames, v, nv);
105           free (v);
106         }
107       else if (lex_match ('('))
108         {
109           int i;
110
111           /* Maximum number of digits in a number to add to the base
112              vecname. */
113           int ndig;
114
115           /* Name of an individual variable to be created. */
116           char name[SHORT_NAME_LEN + 1];
117
118           /* Vector variables. */
119           struct variable **v;
120           int nv;
121
122           if (!lex_force_int ())
123             return CMD_FAILURE;
124           nv = lex_integer ();
125           lex_get ();
126           if (nv <= 0)
127             {
128               msg (SE, _("Vectors must have at least one element."));
129               goto fail;
130             }
131           if (!lex_force_match (')'))
132             goto fail;
133
134           /* First check that all the generated variable names are SHORT_NAME_LEN
135              characters or shorter. */
136           ndig = intlog10 (nv);
137           for (cp = vecnames; *cp;)
138             {
139               int len = strlen (cp);
140               if (len + ndig > SHORT_NAME_LEN)
141                 {
142                   msg (SE, _("%s%d is too long for a variable name."), cp, nv);
143                   goto fail;
144                 }
145               cp += len + 1;
146             }
147
148           /* Next check that none of the variables exist. */
149           for (cp = vecnames; *cp;)
150             {
151               for (i = 0; i < nv; i++)
152                 {
153                   sprintf (name, "%s%d", cp, i + 1);
154                   if (dict_lookup_var (default_dict, name))
155                     {
156                       msg (SE, _("There is already a variable named %s."), name);
157                       goto fail;
158                     }
159                 }
160               cp += strlen (cp) + 1;
161             }
162
163           /* Finally create the variables and vectors. */
164           v = xmalloc (nv * sizeof *v);
165           for (cp = vecnames; *cp;)
166             {
167               for (i = 0; i < nv; i++)
168                 {
169                   sprintf (name, "%s%d", cp, i + 1);
170                   v[i] = dict_create_var_assert (default_dict, name, 0);
171                 }
172               if (!dict_create_vector (default_dict, cp, v, nv))
173                 assert (0);
174               cp += strlen (cp) + 1;
175             }
176           free (v);
177         }
178       else
179         {
180           msg (SE, _("The syntax for this command does not match "
181                "the expected syntax for either the long form "
182                "or the short form of VECTOR."));
183           goto fail;
184         }
185
186       free (vecnames);
187       vecnames = NULL;
188     }
189   while (lex_match ('/'));
190
191   if (token != '.')
192     {
193       lex_error (_("expecting end of command"));
194       goto fail;
195     }
196   return CMD_SUCCESS;
197
198 fail:
199   free (vecnames);
200   return CMD_PART_SUCCESS_MAYBE;
201 }