Use UTF-8 case-insensitive hashes and comparisons for language identifiers.
[pspp] / src / language / dictionary / vector.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2010, 2011, 2012 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       char **vectors;
51       size_t vector_cnt, vector_cap;
52
53       /* Get the name(s) of the new vector(s). */
54       if (!lex_force_id (lexer)
55           || !dict_id_is_valid (dict, lex_tokcstr (lexer), true))
56         return CMD_CASCADING_FAILURE;
57
58       vectors = NULL;
59       vector_cnt = vector_cap = 0;
60       while (lex_token (lexer) == T_ID)
61         {
62           size_t i;
63
64           if (dict_lookup_vector (dict, lex_tokcstr (lexer)))
65             {
66               msg (SE, _("A vector named %s already exists."),
67                    lex_tokcstr (lexer));
68               goto fail;
69             }
70
71           for (i = 0; i < vector_cnt; i++)
72             if (!utf8_strcasecmp (vectors[i], lex_tokcstr (lexer)))
73               {
74                 msg (SE, _("Vector name %s is given twice."),
75                      lex_tokcstr (lexer));
76                 goto fail;
77               }
78
79           if (vector_cnt == vector_cap)
80             vectors = pool_2nrealloc (pool,
81                                        vectors, &vector_cap, sizeof *vectors);
82           vectors[vector_cnt++] = pool_strdup (pool, lex_tokcstr (lexer));
83
84           lex_get (lexer);
85           lex_match (lexer, T_COMMA);
86         }
87
88       /* Now that we have the names it's time to check for the short
89          or long forms. */
90       if (lex_match (lexer, T_EQUALS))
91         {
92           /* Long form. */
93           struct variable **v;
94           size_t nv;
95
96           if (vector_cnt > 1)
97             {
98               msg (SE, _("A slash must separate each vector "
99                          "specification in VECTOR's long form."));
100               goto fail;
101             }
102
103           if (!parse_variables_pool (lexer, pool, dict, &v, &nv,
104                                      PV_SAME_WIDTH | PV_DUPLICATE))
105             goto fail;
106
107           dict_create_vector (dict, vectors[0], v, nv);
108         }
109       else if (lex_match (lexer, T_LPAREN))
110         {
111           /* Short form. */
112           struct fmt_spec format;
113           bool seen_format = false;
114
115           struct variable **vars;
116           int var_cnt;
117
118           size_t i;
119
120           var_cnt = 0;
121           format = fmt_for_output (FMT_F, 8, 2);
122           seen_format = false;
123           while (!lex_match (lexer, T_RPAREN))
124             {
125               if (lex_is_integer (lexer) && var_cnt == 0)
126                 {
127                   var_cnt = lex_integer (lexer);
128                   lex_get (lexer);
129                   if (var_cnt <= 0)
130                     {
131                       msg (SE, _("Vectors must have at least one element."));
132                       goto fail;
133                     }
134                 }
135               else if (lex_token (lexer) == T_ID && !seen_format)
136                 {
137                   seen_format = true;
138                   if (!parse_format_specifier (lexer, &format)
139                       || !fmt_check_output (&format)
140                       || !fmt_check_type_compat (&format, VAL_NUMERIC))
141                     goto fail;
142                 }
143               else
144                 {
145                   lex_error (lexer, NULL);
146                   goto fail;
147                 }
148               lex_match (lexer, T_COMMA);
149             }
150           if (var_cnt == 0)
151             {
152               lex_error (lexer, _("expecting vector length"));
153               goto fail;
154             }
155
156           /* Check that none of the variables exist and that their names are
157              not excessively long. */
158           for (i = 0; i < vector_cnt; i++)
159             {
160               int j;
161               for (j = 0; j < var_cnt; j++)
162                 {
163                   char *name = xasprintf ("%s%d", vectors[i], j + 1);
164                   if (!dict_id_is_valid (dict, name, true))
165                     {
166                       free (name);
167                       goto fail;
168                     }
169                   if (dict_lookup_var (dict, name))
170                     {
171                       free (name);
172                       msg (SE, _("%s is an existing variable name."), name);
173                       goto fail;
174                     }
175                   free (name);
176                 }
177             }
178
179           /* Finally create the variables and vectors. */
180           vars = pool_nmalloc (pool, var_cnt, sizeof *vars);
181           for (i = 0; i < vector_cnt; i++)
182             {
183               int j;
184               for (j = 0; j < var_cnt; j++)
185                 {
186                   char *name = xasprintf ("%s%d", vectors[i], j + 1);
187                   vars[j] = dict_create_var_assert (dict, name, 0);
188                   var_set_both_formats (vars[j], &format);
189                   free (name);
190                 }
191               dict_create_vector_assert (dict, vectors[i], vars, var_cnt);
192             }
193         }
194       else
195         {
196           lex_error (lexer, NULL);
197           goto fail;
198         }
199     }
200   while (lex_match (lexer, T_SLASH));
201
202   pool_destroy (pool);
203   return CMD_SUCCESS;
204
205 fail:
206   pool_destroy (pool);
207   return CMD_FAILURE;
208 }