Rename procedure.[ch] to dataset.[ch].
[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/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_id (lexer, "VARIABLES")
63           || !lex_force_match (lexer, T_EQUALS)
64           || !parse_variables (lexer, dict, &vars, &n_vars, PV_NONE))
65         return CMD_FAILURE;
66
67       sets = xmalloc (n_vars * sizeof *sets);
68       for (i = 0; i < n_vars; i++)
69         sets[i] = var_get_attributes (vars[i]);
70
71       ok = parse_attributes (lexer, dict_encoding, sets, n_vars);
72       free (vars);
73       free (sets);
74       if (!ok)
75         return CMD_FAILURE;
76     }
77   while (lex_match (lexer, T_SLASH));
78
79   return CMD_SUCCESS;
80 }
81
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
85    *INDEX. */
86 static char *
87 parse_attribute_name (struct lexer *lexer, const char *dict_encoding,
88                       size_t *index)
89 {
90   char *name;
91
92   if (!lex_force_id (lexer)
93       || !id_is_valid (lex_tokcstr (lexer), dict_encoding, true))
94     return NULL;
95   name = xstrdup (lex_tokcstr (lexer));
96   lex_get (lexer);
97
98   if (lex_match (lexer, T_LBRACK))
99     {
100       if (!lex_force_int (lexer))
101         goto error;
102       if (lex_integer (lexer) < 1 || lex_integer (lexer) > 65535)
103         {
104           msg (SE, _("Attribute array index must be between 1 and 65535."));
105           goto error;
106         }
107       *index = lex_integer (lexer);
108       lex_get (lexer);
109       if (!lex_force_match (lexer, T_RBRACK))
110         goto error;
111     }
112   else
113     *index = 0;
114   return name;
115
116 error:
117   free (name);
118   return NULL;
119 }
120
121 static bool
122 add_attribute (struct lexer *lexer, const char *dict_encoding,
123                struct attrset **sets, size_t n) 
124 {
125   const char *value;
126   size_t index, i;
127   char *name;
128
129   name = parse_attribute_name (lexer, dict_encoding, &index);
130   if (name == NULL)
131     return false;
132   if (!lex_force_match (lexer, T_LPAREN) || !lex_force_string (lexer))
133     {
134       free (name);
135       return false;
136     }
137   value = lex_tokcstr (lexer);
138
139   for (i = 0; i < n; i++)
140     {
141       struct attribute *attr = attrset_lookup (sets[i], name);
142       if (attr == NULL) 
143         {
144           attr = attribute_create (name);
145           attrset_add (sets[i], attr); 
146         }
147       attribute_set_value (attr, index ? index - 1 : 0, value);
148     }
149
150   lex_get (lexer);
151   free (name);
152   return lex_force_match (lexer, T_RPAREN);
153 }
154
155 static bool
156 delete_attribute (struct lexer *lexer, const char *dict_encoding,
157                   struct attrset **sets, size_t n) 
158 {
159   size_t index, i;
160   char *name;
161
162   name = parse_attribute_name (lexer, dict_encoding, &index);
163   if (name == NULL)
164     return false;
165
166   for (i = 0; i < n; i++) 
167     {
168       struct attrset *set = sets[i];
169       if (index == 0)
170         attrset_delete (set, name);
171       else
172         {
173           struct attribute *attr = attrset_lookup (set, name);
174           if (attr != NULL) 
175             {
176               attribute_del_value (attr, index - 1);
177               if (attribute_get_n_values (attr) == 0)
178                 attrset_delete (set, name); 
179             }
180         }
181     }
182
183   free (name);
184   return true;
185 }
186
187 static enum cmd_result
188 parse_attributes (struct lexer *lexer, const char *dict_encoding,
189                   struct attrset **sets, size_t n) 
190 {
191   enum { UNKNOWN, ADD, DELETE } command = UNKNOWN;
192   do 
193     {
194       if (lex_match_phrase (lexer, "ATTRIBUTE="))
195         command = ADD;
196       else if (lex_match_phrase (lexer, "DELETE="))
197         command = DELETE;
198       else if (command == UNKNOWN)
199         {
200           lex_error (lexer, _("expecting %s or %s"), "ATTRIBUTE=", "DELETE=");
201           return CMD_FAILURE;
202         }
203
204       if (!(command == ADD
205             ? add_attribute (lexer, dict_encoding, sets, n)
206             : delete_attribute (lexer, dict_encoding, sets, n)))
207         return CMD_FAILURE;
208     }
209   while (lex_token (lexer) != T_SLASH && lex_token (lexer) != T_ENDCMD);
210   return CMD_SUCCESS;
211 }