ac8d74d5dabbb5bc14db93ed095b9cc2a2542694
[pspp-builds.git] / src / language / dictionary / 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., 51 Franklin Street, Fifth Floor, Boston, MA
18    02110-1301, USA. */
19
20 #include <config.h>
21
22 #include <stdlib.h>
23
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>
35
36 #include "gettext.h"
37 #define _(msgid) gettext (msgid)
38
39 int
40 cmd_vector (struct lexer *lexer, struct dataset *ds)
41 {
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". */
46   char *vecnames;
47
48   /* vecnames iterators. */
49   char *cp, *cp2;
50
51   /* Maximum allocated position for vecnames, plus one position. */
52   char *endp = NULL;
53
54   struct dictionary *dict = dataset_dict (ds);
55
56   cp = vecnames = xmalloc (256);
57   endp = &vecnames[256];
58   do
59     {
60       /* Get the name(s) of the new vector(s). */
61       if (!lex_force_id (lexer))
62         return CMD_CASCADING_FAILURE;
63       while (lex_token (lexer) == T_ID)
64         {
65           if (cp + 16 > endp)
66             {
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;
71             }
72
73           for (cp2 = cp; cp2 < cp; cp2 += strlen (cp))
74             if (!strcasecmp (cp2, lex_tokid (lexer)))
75               {
76                 msg (SE, _("Vector name %s is given twice."), lex_tokid (lexer));
77                 goto fail;
78               }
79
80           if (dict_lookup_vector (dict, lex_tokid (lexer)))
81             {
82               msg (SE, _("There is already a vector with name %s."), lex_tokid (lexer));
83               goto fail;
84             }
85
86           cp = stpcpy (cp, lex_tokid (lexer)) + 1;
87           lex_get (lexer);
88           lex_match (lexer, ',');
89         }
90       *cp++ = 0;
91
92       /* Now that we have the names it's time to check for the short
93          or long forms. */
94       if (lex_match (lexer, '='))
95         {
96           /* Long form. */
97           struct variable **v;
98           size_t nv;
99
100           if (strchr (vecnames, '\0')[1])
101             {
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."));
106               goto fail;
107             }
108
109           if (!parse_variables (lexer, dict, &v, &nv,
110                                 PV_SAME_WIDTH | PV_DUPLICATE))
111             goto fail;
112
113           dict_create_vector (dict, vecnames, v, nv);
114           free (v);
115         }
116       else if (lex_match (lexer, '('))
117         {
118           int i;
119
120           /* Maximum number of digits in a number to add to the base
121              vecname. */
122           int ndig;
123
124           /* Name of an individual variable to be created. */
125           char name[SHORT_NAME_LEN + 1];
126
127           /* Vector variables. */
128           struct variable **v;
129           int nv;
130
131           if (!lex_force_int (lexer))
132             return CMD_CASCADING_FAILURE;
133           nv = lex_integer (lexer);
134           lex_get (lexer);
135           if (nv <= 0)
136             {
137               msg (SE, _("Vectors must have at least one element."));
138               goto fail;
139             }
140           if (!lex_force_match (lexer, ')'))
141             goto fail;
142
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;)
147             {
148               int len = strlen (cp);
149               if (len + ndig > LONG_NAME_LEN)
150                 {
151                   msg (SE, _("%s%d is too long for a variable name."), cp, nv);
152                   goto fail;
153                 }
154               cp += len + 1;
155             }
156
157           /* Next check that none of the variables exist. */
158           for (cp = vecnames; *cp;)
159             {
160               for (i = 0; i < nv; i++)
161                 {
162                   sprintf (name, "%s%d", cp, i + 1);
163                   if (dict_lookup_var (dict, name))
164                     {
165                       msg (SE, _("There is already a variable named %s."),
166                            name);
167                       goto fail;
168                     }
169                 }
170               cp += strlen (cp) + 1;
171             }
172
173           /* Finally create the variables and vectors. */
174           v = xmalloc (nv * sizeof *v);
175           for (cp = vecnames; *cp;)
176             {
177               for (i = 0; i < nv; i++)
178                 {
179                   sprintf (name, "%s%d", cp, i + 1);
180                   v[i] = dict_create_var_assert (dict, name, 0);
181                 }
182               if (!dict_create_vector (dict, cp, v, nv))
183                 NOT_REACHED ();
184               cp += strlen (cp) + 1;
185             }
186           free (v);
187         }
188       else
189         {
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."));
193           goto fail;
194         }
195
196       free (vecnames);
197       vecnames = NULL;
198     }
199   while (lex_match (lexer, '/'));
200
201   if (lex_token (lexer) != '.')
202     {
203       lex_error (lexer, _("expecting end of command"));
204       goto fail;
205     }
206   return CMD_SUCCESS;
207
208 fail:
209   free (vecnames);
210   return CMD_FAILURE;
211 }