1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2008 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/dictionary.h>
23 #include <data/procedure.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>
33 #define _(msgid) gettext (msgid)
35 static enum cmd_result parse_attributes (struct lexer *, struct attrset **,
38 /* Parses the DATAFILE ATTRIBUTE command. */
40 cmd_datafile_attribute (struct lexer *lexer, struct dataset *ds)
42 struct attrset *set = dict_get_attributes (dataset_dict (ds));
43 return parse_attributes (lexer, &set, 1);
46 /* Parses the VARIABLE ATTRIBUTE command. */
48 cmd_variable_attribute (struct lexer *lexer, struct dataset *ds)
52 struct variable **vars;
53 struct attrset **sets;
57 if (!lex_force_match_id (lexer, "VARIABLES")
58 || !lex_force_match (lexer, '=')
59 || !parse_variables (lexer, dataset_dict (ds), &vars, &n_vars,
63 sets = xmalloc (n_vars * sizeof *sets);
64 for (i = 0; i < n_vars; i++)
65 sets[i] = var_get_attributes (vars[i]);
67 ok = parse_attributes (lexer, sets, n_vars);
73 while (lex_match (lexer, '/'));
75 return lex_end_of_command (lexer);
79 match_subcommand (struct lexer *lexer, const char *keyword)
81 if (lex_token (lexer) == T_ID
82 && lex_id_match (ss_cstr (lex_tokid (lexer)), ss_cstr (keyword))
83 && lex_look_ahead (lexer) == '=')
85 lex_get (lexer); /* Skip keyword. */
86 lex_get (lexer); /* Skip '='. */
94 parse_attribute_name (struct lexer *lexer, char name[VAR_NAME_LEN + 1],
97 if (!lex_force_id (lexer))
99 strcpy (name, lex_tokid (lexer));
102 if (lex_match (lexer, '['))
104 if (!lex_force_int (lexer))
106 if (lex_integer (lexer) < 1 || lex_integer (lexer) > 65535)
108 msg (SE, _("Attribute array index must be between 1 and 65535."));
111 *index = lex_integer (lexer);
113 if (!lex_force_match (lexer, ']'))
122 add_attribute (struct lexer *lexer, struct attrset **sets, size_t n)
124 char name[VAR_NAME_LEN + 1];
128 if (!parse_attribute_name (lexer, name, &index)
129 || !lex_force_match (lexer, '(')
130 || !lex_force_string (lexer))
132 value = ds_cstr (lex_tokstr (lexer));
134 for (i = 0; i < n; i++)
136 struct attribute *attr = attrset_lookup (sets[i], name);
139 attr = attribute_create (name);
140 attrset_add (sets[i], attr);
142 attribute_set_value (attr, index ? index - 1 : 0, value);
146 return lex_force_match (lexer, ')');
150 delete_attribute (struct lexer *lexer, struct attrset **sets, size_t n)
152 char name[VAR_NAME_LEN + 1];
155 if (!parse_attribute_name (lexer, name, &index))
158 for (i = 0; i < n; i++)
160 struct attrset *set = sets[i];
162 attrset_delete (set, name);
165 struct attribute *attr = attrset_lookup (set, name);
168 attribute_del_value (attr, index - 1);
169 if (attribute_get_n_values (attr) == 0)
170 attrset_delete (set, name);
177 static enum cmd_result
178 parse_attributes (struct lexer *lexer, struct attrset **sets, size_t n)
180 enum { UNKNOWN, ADD, DELETE } command = UNKNOWN;
183 if (match_subcommand (lexer, "ATTRIBUTE"))
185 else if (match_subcommand (lexer, "DELETE"))
187 else if (command == UNKNOWN)
189 lex_error (lexer, _("expecting ATTRIBUTE= or DELETE="));
194 ? add_attribute (lexer, sets, n)
195 : delete_attribute (lexer, sets, n)))
198 while (lex_token (lexer) != '/' && lex_token (lexer) != '.');