variable-display: Improve coding style and error messages.
[pspp] / src / language / dictionary / variable-display.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2010, 2011, 2013 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 <stdio.h>
20 #include <stdlib.h>
21
22 #include "data/dataset.h"
23 #include "data/variable.h"
24 #include "language/command.h"
25 #include "language/lexer/lexer.h"
26 #include "language/lexer/variable-parser.h"
27 #include "libpspp/message.h"
28 #include "libpspp/str.h"
29
30 #include "gl/minmax.h"
31 #include "gl/xalloc.h"
32
33 #include "gettext.h"
34 #define _(msgid) gettext (msgid)
35
36 int
37 cmd_variable_alignment (struct lexer *lexer, struct dataset *ds)
38 {
39   do
40     {
41       struct variable **v;
42       size_t nv;
43
44       if (!parse_variables (lexer, dataset_dict (ds), &v, &nv, PV_NONE))
45         return CMD_FAILURE;
46
47       if (!lex_force_match (lexer, T_LPAREN))
48         goto error;
49
50       enum alignment align;
51       if (lex_match_id (lexer, "LEFT"))
52         align = ALIGN_LEFT;
53       else if (lex_match_id (lexer, "RIGHT"))
54         align = ALIGN_RIGHT;
55       else if (lex_match_id (lexer, "CENTER"))
56         align = ALIGN_CENTRE;
57       else
58         {
59           lex_error_expecting (lexer, "LEFT", "RIGHT", "CENTER");
60           goto error;
61         }
62
63       if (!lex_force_match (lexer, T_RPAREN))
64         goto error;
65
66       for (size_t i = 0; i < nv; ++i)
67         var_set_alignment (v[i], align);
68
69       while (lex_token (lexer) == T_SLASH)
70         lex_get (lexer);
71       free (v);
72       continue;
73
74     error:
75       free (v);
76       return CMD_FAILURE;
77     }
78   while (lex_token (lexer) != T_ENDCMD);
79   return CMD_SUCCESS;
80 }
81
82 int
83 cmd_variable_width (struct lexer *lexer, struct dataset *ds)
84 {
85   do
86     {
87       struct variable **v;
88       size_t nv;
89       if (!parse_variables (lexer, dataset_dict (ds), &v, &nv, PV_NONE))
90         return CMD_FAILURE;
91
92       if (!lex_force_match (lexer, T_LPAREN)
93           || !lex_force_int_range (lexer, NULL, 1, INT_MAX))
94         goto error;
95       long width = lex_integer (lexer);
96       lex_get (lexer);
97       if (!lex_force_match (lexer, T_RPAREN))
98         goto error;
99
100       width = MIN (width, 2 * MAX_STRING);
101
102       for (size_t i = 0; i < nv; ++i)
103         var_set_display_width (v[i], width);
104
105       while (lex_token (lexer) == T_SLASH)
106         lex_get (lexer);
107       free (v);
108       continue;
109
110     error:
111       free (v);
112       return CMD_FAILURE;
113     }
114   while (lex_token (lexer) != T_ENDCMD);
115   return CMD_SUCCESS;
116 }
117
118 /* Set variables' measurement level */
119 int
120 cmd_variable_level (struct lexer *lexer, struct dataset *ds)
121 {
122   do
123     {
124       struct variable **v;
125       size_t nv;
126
127       if (!parse_variables (lexer, dataset_dict (ds), &v, &nv, PV_NONE))
128         return CMD_FAILURE;
129
130       if (!lex_force_match (lexer, T_LPAREN))
131         goto error;
132
133       enum measure level;
134       if (lex_match_id (lexer, "SCALE"))
135         level = MEASURE_SCALE;
136       else if (lex_match_id (lexer, "ORDINAL"))
137         level = MEASURE_ORDINAL;
138       else if (lex_match_id (lexer, "NOMINAL"))
139         level = MEASURE_NOMINAL;
140       else
141         {
142           lex_error_expecting (lexer, "SCALE", "ORDINAL", "NOMINAL");
143           goto error;
144         }
145
146       if (!lex_force_match (lexer, T_RPAREN))
147         goto error;
148
149       for (size_t i = 0; i < nv; ++i)
150         var_set_measure (v[i], level);
151
152       while (lex_token (lexer) == T_SLASH)
153         lex_get (lexer);
154       free (v);
155       continue;
156
157     error:
158       free (v);
159       return CMD_FAILURE;
160     }
161   while (lex_token (lexer) != T_ENDCMD);
162   return CMD_SUCCESS;
163 }
164
165 int
166 cmd_variable_role (struct lexer *lexer, struct dataset *ds)
167 {
168   do
169     {
170       if (!lex_force_match (lexer, T_SLASH))
171         return CMD_FAILURE;
172
173       enum var_role role;
174       if (lex_match_id (lexer, "INPUT"))
175         role = ROLE_INPUT;
176       else if (lex_match_id (lexer, "TARGET"))
177         role = ROLE_TARGET;
178       else if (lex_match_id (lexer, "BOTH"))
179         role = ROLE_BOTH;
180       else if (lex_match_id (lexer, "NONE"))
181         role = ROLE_NONE;
182       else if (lex_match_id (lexer, "PARTITION"))
183         role = ROLE_PARTITION;
184       else if (lex_match_id (lexer, "SPLIT"))
185         role = ROLE_SPLIT;
186       else
187         {
188           lex_error_expecting (lexer, "INPUT", "TARGET", "BOTH",
189                                "NONE", "PARTITION", "SPLIT");
190           return CMD_FAILURE;
191         }
192
193       struct variable **v;
194       size_t nv;
195       if (!parse_variables (lexer, dataset_dict (ds), &v, &nv, PV_NONE))
196         return CMD_FAILURE;
197
198       for (size_t i = 0; i < nv; i++)
199         var_set_role (v[i], role);
200
201       free (v);
202     }
203   while (lex_token (lexer) != T_ENDCMD);
204
205   return CMD_SUCCESS;
206 }