fa4e3f996a5b8367b367b2a480756ff133a25c52
[pspp] / src / language / dictionary / vector.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2010, 2011, 2012, 2016 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>. */
16
17 #include <config.h>
18
19 #include <stdlib.h>
20
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"
35
36 #include "gl/intprops.h"
37 #include "gl/xalloc.h"
38
39 #include "gettext.h"
40 #define _(msgid) gettext (msgid)
41
42 int
43 cmd_vector (struct lexer *lexer, struct dataset *ds)
44 {
45   struct dictionary *dict = dataset_dict (ds);
46   struct pool *pool = pool_create ();
47
48   do
49     {
50       /* Get the name(s) of the new vector(s). */
51       if (!lex_force_id (lexer))
52         goto error;
53
54       int vectors_start = lex_ofs (lexer);
55       char **vectors = NULL;
56       size_t n_vectors = 0;
57       size_t allocated_vectors = 0;
58       while (lex_token (lexer) == T_ID)
59         {
60           char *error = dict_id_is_valid__ (dict, lex_tokcstr (lexer));
61           if (error)
62             {
63               lex_error (lexer, "%s", error);
64               free (error);
65               goto error;
66             }
67
68           if (dict_lookup_vector (dict, lex_tokcstr (lexer)))
69             {
70               lex_next_error (lexer, 0, 0,
71                               _("A vector named %s already exists."),
72                               lex_tokcstr (lexer));
73               goto error;
74             }
75
76           for (size_t i = 0; i < n_vectors; i++)
77             if (!utf8_strcasecmp (vectors[i], lex_tokcstr (lexer)))
78               {
79                 lex_ofs_error (lexer, vectors_start, lex_ofs (lexer),
80                                _("Vector name %s is given twice."),
81                                lex_tokcstr (lexer));
82                 goto error;
83               }
84
85           if (n_vectors == allocated_vectors)
86             vectors = pool_2nrealloc (pool, vectors, &allocated_vectors,
87                                       sizeof *vectors);
88           vectors[n_vectors++] = pool_strdup (pool, lex_tokcstr (lexer));
89
90           lex_get (lexer);
91           lex_match (lexer, T_COMMA);
92         }
93
94       /* Now that we have the names it's time to check for the short
95          or long forms. */
96       if (lex_match (lexer, T_EQUALS))
97         {
98           if (n_vectors > 1)
99             {
100               lex_ofs_error (lexer, vectors_start, lex_ofs (lexer) - 1,
101                              _("Only a single vector name may be specified "
102                                "when a list of variables is given."));
103               goto error;
104             }
105
106           struct variable **v;
107           size_t nv;
108           if (!parse_variables_pool (lexer, pool, dict, &v, &nv,
109                                      PV_SAME_WIDTH | PV_DUPLICATE))
110             goto error;
111
112           dict_create_vector (dict, vectors[0], v, nv);
113         }
114       else if (lex_match (lexer, T_LPAREN))
115         {
116           struct fmt_spec format = fmt_for_output (FMT_F, 8, 2);
117           bool seen_format = false;
118           size_t n_vars = 0;
119           int name_ofs = lex_ofs (lexer) - 2;
120           int lparen_ofs = lex_ofs (lexer) - 1;
121           while (!lex_match (lexer, T_RPAREN))
122             {
123               if (lex_is_integer (lexer))
124                 {
125                   if (n_vars)
126                     {
127                       lex_ofs_error (lexer, lparen_ofs, lex_ofs (lexer),
128                                      _("Vector length may only be specified "
129                                        "once."));
130                       goto error;
131                     }
132                   if (!lex_force_int_range (lexer, NULL, 1, INT_MAX))
133                     goto error;
134                   n_vars = lex_integer (lexer);
135                   lex_get (lexer);
136                 }
137               else if (lex_token (lexer) == T_ID)
138                 {
139                   if (seen_format)
140                     {
141                       lex_ofs_error (lexer, lparen_ofs, lex_ofs (lexer),
142                                      _("Only one format may be specified."));
143                       goto error;
144                     }
145                   seen_format = true;
146                   if (!parse_format_specifier (lexer, &format))
147                     goto error;
148                   char *error = fmt_check_output__ (&format);
149                   if (error)
150                     {
151                       lex_next_error (lexer, -1, -1, "%s", error);
152                       free (error);
153                       goto error;
154                     }
155                 }
156               else
157                 {
158                   lex_error (lexer, _("Syntax error expecting vector length "
159                                       "or format."));
160                   goto error;
161                 }
162               lex_match (lexer, T_COMMA);
163             }
164           int end_ofs = lex_ofs (lexer) - 1;
165           if (n_vars == 0)
166             {
167               lex_ofs_error (lexer, lparen_ofs, end_ofs,
168                              _("Vector length is required."));
169               goto error;
170             }
171
172           /* Check that none of the variables exist and that their names are
173              not excessively long. */
174           for (size_t i = 0; i < n_vectors; i++)
175             for (size_t j = 0; j < n_vars; j++)
176               {
177                 char *name = xasprintf ("%s%zu", vectors[i], j + 1);
178                 char *error = dict_id_is_valid__ (dict, name);
179                 if (error)
180                   {
181                     lex_ofs_error (lexer, name_ofs, end_ofs, "%s", error);
182                     free (error);
183                     free (name);
184                     goto error;
185                   }
186                 if (dict_lookup_var (dict, name))
187                   {
188                     lex_ofs_error (lexer, name_ofs, end_ofs,
189                                    _("%s is an existing variable name."),
190                                    name);
191                     free (name);
192                     goto error;
193                   }
194                 free (name);
195               }
196
197           /* Finally create the variables and vectors. */
198           struct variable **vars = pool_nmalloc (pool, n_vars, sizeof *vars);
199           for (size_t i = 0; i < n_vectors; i++)
200             {
201               for (size_t j = 0; j < n_vars; j++)
202                 {
203                   char *name = xasprintf ("%s%zu", vectors[i], j + 1);
204                   vars[j] = dict_create_var_assert (dict, name,
205                                                     fmt_var_width (&format));
206                   var_set_both_formats (vars[j], &format);
207                   free (name);
208                 }
209               dict_create_vector_assert (dict, vectors[i], vars, n_vars);
210             }
211         }
212       else
213         {
214           lex_error_expecting (lexer, "`='", "`('");
215           goto error;
216         }
217     }
218   while (lex_match (lexer, T_SLASH));
219
220   pool_destroy (pool);
221   return CMD_SUCCESS;
222
223 error:
224   pool_destroy (pool);
225   return CMD_FAILURE;
226 }