Move all command implementations into a single 'commands' directory.
[pspp] / src / language / commands / attributes.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2008, 2010, 2011 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/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"
29
30 #include "gl/xalloc.h"
31
32 #include "gettext.h"
33 #define _(msgid) gettext (msgid)
34
35 static enum cmd_result parse_attributes (struct lexer *,
36                                          const char *dict_encoding,
37                                          struct attrset **, size_t n);
38
39 /* Parses the DATAFILE ATTRIBUTE command. */
40 int
41 cmd_datafile_attribute (struct lexer *lexer, struct dataset *ds)
42 {
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);
46 }
47
48 /* Parses the VARIABLE ATTRIBUTE command. */
49 int
50 cmd_variable_attribute (struct lexer *lexer, struct dataset *ds)
51 {
52   struct dictionary *dict = dataset_dict (ds);
53   const char *dict_encoding = dict_get_encoding (dict);
54
55   do
56     {
57       struct variable **vars;
58       struct attrset **sets;
59       size_t n_vars, i;
60       bool ok;
61
62       if (!lex_force_match_phrase (lexer, "VARIABLES=")
63           || !parse_variables (lexer, dict, &vars, &n_vars, PV_NONE))
64         return CMD_FAILURE;
65
66       sets = xmalloc (n_vars * sizeof *sets);
67       for (i = 0; i < n_vars; i++)
68         sets[i] = var_get_attributes (vars[i]);
69
70       ok = parse_attributes (lexer, dict_encoding, sets, n_vars);
71       free (vars);
72       free (sets);
73       if (!ok)
74         return CMD_FAILURE;
75     }
76   while (lex_match (lexer, T_SLASH));
77
78   return CMD_SUCCESS;
79 }
80
81 /* Parses an attribute name and verifies that it is valid in DICT_ENCODING,
82    optionally followed by an index inside square brackets.  Returns the
83    attribute name or NULL if there was a parse error.  Stores the index into
84    *INDEX. */
85 static char *
86 parse_attribute_name (struct lexer *lexer, const char *dict_encoding,
87                       size_t *index)
88 {
89   if (!lex_force_id (lexer))
90     return NULL;
91   char *error = id_is_valid__ (lex_tokcstr (lexer), dict_encoding);
92   if (error)
93     {
94       lex_error (lexer, "%s", error);
95       free (error);
96       return NULL;
97     }
98   char *name = xstrdup (lex_tokcstr (lexer));
99   lex_get (lexer);
100
101   if (lex_match (lexer, T_LBRACK))
102     {
103       if (!lex_force_int_range (lexer, NULL, 1, 65535))
104         goto error;
105       *index = lex_integer (lexer);
106       lex_get (lexer);
107       if (!lex_force_match (lexer, T_RBRACK))
108         goto error;
109     }
110   else
111     *index = 0;
112   return name;
113
114 error:
115   free (name);
116   return NULL;
117 }
118
119 static bool
120 add_attribute (struct lexer *lexer, const char *dict_encoding,
121                struct attrset **sets, size_t n)
122 {
123   const char *value;
124   size_t index, i;
125   char *name;
126
127   name = parse_attribute_name (lexer, dict_encoding, &index);
128   if (name == NULL)
129     return false;
130   if (!lex_force_match (lexer, T_LPAREN) || !lex_force_string (lexer))
131     {
132       free (name);
133       return false;
134     }
135   value = lex_tokcstr (lexer);
136
137   for (i = 0; i < n; i++)
138     {
139       struct attribute *attr = attrset_lookup (sets[i], name);
140       if (attr == NULL)
141         {
142           attr = attribute_create (name);
143           attrset_add (sets[i], attr);
144         }
145       attribute_set_value (attr, index ? index - 1 : 0, value);
146     }
147
148   lex_get (lexer);
149   free (name);
150   return lex_force_match (lexer, T_RPAREN);
151 }
152
153 static bool
154 delete_attribute (struct lexer *lexer, const char *dict_encoding,
155                   struct attrset **sets, size_t n)
156 {
157   size_t index, i;
158   char *name;
159
160   name = parse_attribute_name (lexer, dict_encoding, &index);
161   if (name == NULL)
162     return false;
163
164   for (i = 0; i < n; i++)
165     {
166       struct attrset *set = sets[i];
167       if (index == 0)
168         attrset_delete (set, name);
169       else
170         {
171           struct attribute *attr = attrset_lookup (set, name);
172           if (attr != NULL)
173             {
174               attribute_del_value (attr, index - 1);
175               if (attribute_get_n_values (attr) == 0)
176                 attrset_delete (set, name);
177             }
178         }
179     }
180
181   free (name);
182   return true;
183 }
184
185 static enum cmd_result
186 parse_attributes (struct lexer *lexer, const char *dict_encoding,
187                   struct attrset **sets, size_t n)
188 {
189   enum { UNKNOWN, ADD, DELETE } command = UNKNOWN;
190   do
191     {
192       if (lex_match_phrase (lexer, "ATTRIBUTE="))
193         command = ADD;
194       else if (lex_match_phrase (lexer, "DELETE="))
195         command = DELETE;
196       else if (command == UNKNOWN)
197         {
198           lex_error_expecting (lexer, "ATTRIBUTE=", "DELETE=");
199           return CMD_FAILURE;
200         }
201
202       if (!(command == ADD
203             ? add_attribute (lexer, dict_encoding, sets, n)
204             : delete_attribute (lexer, dict_encoding, sets, n)))
205         return CMD_FAILURE;
206     }
207   while (lex_token (lexer) != T_SLASH && lex_token (lexer) != T_ENDCMD);
208   return CMD_SUCCESS;
209 }