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