lexer: New function lex_force_int_range().
[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 /* Set variables' alignment
37    This is the alignment for GUI display only.
38    It affects nothing but GUIs
39 */
40 int
41 cmd_variable_alignment (struct lexer *lexer, struct dataset *ds)
42 {
43   do
44     {
45       struct variable **v;
46       size_t nv;
47
48       size_t i;
49       enum alignment align;
50
51       if (!parse_variables (lexer, dataset_dict (ds), &v, &nv, PV_NONE))
52         return CMD_FAILURE;
53
54       if (lex_force_match (lexer, T_LPAREN))
55         {
56           if (lex_match_id (lexer, "LEFT"))
57             align = ALIGN_LEFT;
58           else if (lex_match_id (lexer, "RIGHT"))
59             align = ALIGN_RIGHT;
60           else if (lex_match_id (lexer, "CENTER"))
61             align = ALIGN_CENTRE;
62           else
63             {
64               free (v);
65               return CMD_FAILURE;
66             }
67
68           if (!lex_force_match (lexer, T_RPAREN))
69             return CMD_FAILURE;
70         }
71       else
72         {
73           free (v);
74           return CMD_FAILURE;
75         }
76
77       for(i = 0 ; i < nv ; ++i)
78         var_set_alignment (v[i], align);
79
80       while (lex_token (lexer) == T_SLASH)
81         lex_get (lexer);
82       free (v);
83
84     }
85   while (lex_token (lexer) != T_ENDCMD);
86   return CMD_SUCCESS;
87 }
88
89 /* Set variables' display width.
90    This is the width for GUI display only.
91    It affects nothing but GUIs
92 */
93 int
94 cmd_variable_width (struct lexer *lexer, struct dataset *ds)
95 {
96   do
97     {
98       struct variable **v;
99       long int width;
100       size_t nv;
101       size_t i;
102
103       if (!parse_variables (lexer, dataset_dict (ds), &v, &nv, PV_NONE))
104         return CMD_FAILURE;
105
106       if (!lex_force_match (lexer, T_LPAREN)
107           || !lex_force_int_range (lexer, NULL, 1, INT_MAX))
108         {
109           free (v);
110           return CMD_FAILURE;
111         }
112       width = lex_integer (lexer);
113       lex_get (lexer);
114       if (!lex_force_match (lexer, T_RPAREN))
115         {
116           free (v);
117           return CMD_FAILURE;
118         }
119
120       width = MIN (width, 2 * MAX_STRING);
121
122       for(i = 0 ; i < nv ; ++i)
123         var_set_display_width (v[i], width);
124
125       while (lex_token (lexer) == T_SLASH)
126         lex_get (lexer);
127       free (v);
128
129     }
130   while (lex_token (lexer) != T_ENDCMD);
131   return CMD_SUCCESS;
132 }
133
134 /* Set variables' measurement level */
135 int
136 cmd_variable_level (struct lexer *lexer, struct dataset *ds)
137 {
138   do
139     {
140       struct variable **v;
141       size_t nv;
142       enum measure level;
143       size_t i;
144
145       if (!parse_variables (lexer, dataset_dict (ds), &v, &nv, PV_NONE))
146         return CMD_FAILURE;
147
148       if (lex_force_match (lexer, T_LPAREN))
149         {
150           if (lex_match_id (lexer, "SCALE"))
151             level = MEASURE_SCALE;
152           else if (lex_match_id (lexer, "ORDINAL"))
153             level = MEASURE_ORDINAL;
154           else if (lex_match_id (lexer, "NOMINAL"))
155             level = MEASURE_NOMINAL;
156           else
157             {
158               free (v);
159               return CMD_FAILURE;
160             }
161
162           if (!lex_force_match (lexer, T_RPAREN))
163             return CMD_FAILURE;
164         }
165       else
166         {
167           free (v);
168           return CMD_FAILURE;
169         }
170
171       for(i = 0 ; i < nv ; ++i)
172         var_set_measure (v[i], level);
173
174
175       while (lex_token (lexer) == T_SLASH)
176         lex_get (lexer);
177       free (v);
178
179     }
180   while (lex_token (lexer) != T_ENDCMD);
181   return CMD_SUCCESS;
182 }
183
184 /* Set variables' role */
185 int
186 cmd_variable_role (struct lexer *lexer, struct dataset *ds)
187 {
188   while (lex_match (lexer, T_SLASH))
189     {
190       struct variable **v;
191       size_t nv;
192       enum var_role role;
193       size_t i;
194
195       if (lex_match_id (lexer, "INPUT"))
196         role = ROLE_INPUT;
197       else if (lex_match_id (lexer, "TARGET"))
198         role = ROLE_TARGET;
199       else if (lex_match_id (lexer, "BOTH"))
200         role = ROLE_BOTH;
201       else if (lex_match_id (lexer, "NONE"))
202         role = ROLE_NONE;
203       else if (lex_match_id (lexer, "PARTITION"))
204         role = ROLE_PARTITION;
205       else if (lex_match_id (lexer, "SPLIT"))
206         role = ROLE_SPLIT;
207       else
208         {
209           lex_error (lexer, NULL);
210           return CMD_FAILURE;
211         }
212
213       if (!parse_variables (lexer, dataset_dict (ds), &v, &nv, PV_NONE))
214         return CMD_FAILURE;
215
216       for (i = 0; i < nv; i++)
217         var_set_role (v[i], role);
218       free (v);
219     }
220
221   return CMD_SUCCESS;
222 }