1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2008, 2010, 2011 Free Software Foundation, Inc.
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.
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.
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/>. */
21 #include "data/attributes.h"
22 #include "data/dataset.h"
23 #include "data/dictionary.h"
24 #include "data/variable.h"
25 #include "language/command.h"
26 #include "language/lexer/lexer.h"
27 #include "language/lexer/variable-parser.h"
28 #include "libpspp/message.h"
30 #include "gl/xalloc.h"
33 #define _(msgid) gettext (msgid)
35 static enum cmd_result parse_attributes (struct lexer *,
36 const char *dict_encoding,
37 struct attrset **, size_t n);
39 /* Parses the DATAFILE ATTRIBUTE command. */
41 cmd_datafile_attribute (struct lexer *lexer, struct dataset *ds)
43 struct dictionary *dict = dataset_dict (ds);
44 struct attrset *set = dict_get_attributes (dict);
45 return parse_attributes (lexer, dict_get_encoding (dict), &set, 1);
48 /* Parses the VARIABLE ATTRIBUTE command. */
50 cmd_variable_attribute (struct lexer *lexer, struct dataset *ds)
52 struct dictionary *dict = dataset_dict (ds);
53 const char *dict_encoding = dict_get_encoding (dict);
57 struct variable **vars;
58 struct attrset **sets;
62 if (!lex_force_match_id (lexer, "VARIABLES")
63 || !lex_force_match (lexer, T_EQUALS)
64 || !parse_variables (lexer, dict, &vars, &n_vars, PV_NONE))
67 sets = xmalloc (n_vars * sizeof *sets);
68 for (i = 0; i < n_vars; i++)
69 sets[i] = var_get_attributes (vars[i]);
71 ok = parse_attributes (lexer, dict_encoding, sets, n_vars);
77 while (lex_match (lexer, T_SLASH));
82 /* Parses an attribute name and verifies that it is valid in DICT_ENCODING,
83 optionally followed by an index inside square brackets. Returns the
84 attribute name or NULL if there was a parse error. Stores the index into
87 parse_attribute_name (struct lexer *lexer, const char *dict_encoding,
90 if (!lex_force_id (lexer))
92 char *error = id_is_valid__ (lex_tokcstr (lexer), dict_encoding);
95 lex_error (lexer, "%s", error);
99 char *name = xstrdup (lex_tokcstr (lexer));
102 if (lex_match (lexer, T_LBRACK))
104 if (!lex_force_int_range (lexer, NULL, 1, 65535))
106 *index = lex_integer (lexer);
108 if (!lex_force_match (lexer, T_RBRACK))
121 add_attribute (struct lexer *lexer, const char *dict_encoding,
122 struct attrset **sets, size_t n)
128 name = parse_attribute_name (lexer, dict_encoding, &index);
131 if (!lex_force_match (lexer, T_LPAREN) || !lex_force_string (lexer))
136 value = lex_tokcstr (lexer);
138 for (i = 0; i < n; i++)
140 struct attribute *attr = attrset_lookup (sets[i], name);
143 attr = attribute_create (name);
144 attrset_add (sets[i], attr);
146 attribute_set_value (attr, index ? index - 1 : 0, value);
151 return lex_force_match (lexer, T_RPAREN);
155 delete_attribute (struct lexer *lexer, const char *dict_encoding,
156 struct attrset **sets, size_t n)
161 name = parse_attribute_name (lexer, dict_encoding, &index);
165 for (i = 0; i < n; i++)
167 struct attrset *set = sets[i];
169 attrset_delete (set, name);
172 struct attribute *attr = attrset_lookup (set, name);
175 attribute_del_value (attr, index - 1);
176 if (attribute_get_n_values (attr) == 0)
177 attrset_delete (set, name);
186 static enum cmd_result
187 parse_attributes (struct lexer *lexer, const char *dict_encoding,
188 struct attrset **sets, size_t n)
190 enum { UNKNOWN, ADD, DELETE } command = UNKNOWN;
193 if (lex_match_phrase (lexer, "ATTRIBUTE="))
195 else if (lex_match_phrase (lexer, "DELETE="))
197 else if (command == UNKNOWN)
199 lex_error_expecting (lexer, "ATTRIBUTE=", "DELETE=");
204 ? add_attribute (lexer, dict_encoding, sets, n)
205 : delete_attribute (lexer, dict_encoding, sets, n)))
208 while (lex_token (lexer) != T_SLASH && lex_token (lexer) != T_ENDCMD);