1fa17017e09556c662bf8ab7d0da5a7cd398eb71
[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) || !lex_force_int (lexer))
107         {
108           free (v);
109           return CMD_FAILURE;
110         }
111       width = lex_integer (lexer);
112       lex_get (lexer);
113       if (!lex_force_match (lexer, T_RPAREN))
114         {
115           free (v);
116           return CMD_FAILURE;
117         }
118
119       if (width < 0)
120         {
121           msg (SE, _("Variable display width must be a positive integer."));
122           free (v);
123           return CMD_FAILURE;
124         }
125       width = MIN (width, 2 * MAX_STRING);
126
127       for(i = 0 ; i < nv ; ++i)
128         var_set_display_width (v[i], width);
129
130       while (lex_token (lexer) == T_SLASH)
131         lex_get (lexer);
132       free (v);
133
134     }
135   while (lex_token (lexer) != T_ENDCMD);
136   return CMD_SUCCESS;
137 }
138
139 /* Set variables' measurement level */
140 int
141 cmd_variable_level (struct lexer *lexer, struct dataset *ds)
142 {
143   do
144     {
145       struct variable **v;
146       size_t nv;
147       enum measure level;
148       size_t i;
149
150       if (!parse_variables (lexer, dataset_dict (ds), &v, &nv, PV_NONE))
151         return CMD_FAILURE;
152
153       if (lex_force_match (lexer, T_LPAREN))
154         {
155           if (lex_match_id (lexer, "SCALE"))
156             level = MEASURE_SCALE;
157           else if (lex_match_id (lexer, "ORDINAL"))
158             level = MEASURE_ORDINAL;
159           else if (lex_match_id (lexer, "NOMINAL"))
160             level = MEASURE_NOMINAL;
161           else
162             {
163               free (v);
164               return CMD_FAILURE;
165             }
166
167           if (!lex_force_match (lexer, T_RPAREN))
168             return CMD_FAILURE;
169         }
170       else
171         {
172           free (v);
173           return CMD_FAILURE;
174         }
175
176       for(i = 0 ; i < nv ; ++i)
177         var_set_measure (v[i], level);
178
179
180       while (lex_token (lexer) == T_SLASH)
181         lex_get (lexer);
182       free (v);
183
184     }
185   while (lex_token (lexer) != T_ENDCMD);
186   return CMD_SUCCESS;
187 }
188
189 /* Set variables' role */
190 int
191 cmd_variable_role (struct lexer *lexer, struct dataset *ds)
192 {
193   while (lex_match (lexer, T_SLASH))
194     {
195       struct variable **v;
196       size_t nv;
197       enum var_role role;
198       size_t i;
199
200       if (lex_match_id (lexer, "INPUT"))
201         role = ROLE_INPUT;
202       else if (lex_match_id (lexer, "TARGET"))
203         role = ROLE_TARGET;
204       else if (lex_match_id (lexer, "BOTH"))
205         role = ROLE_BOTH;
206       else if (lex_match_id (lexer, "NONE"))
207         role = ROLE_NONE;
208       else if (lex_match_id (lexer, "PARTITION"))
209         role = ROLE_PARTITION;
210       else if (lex_match_id (lexer, "SPLIT"))
211         role = ROLE_SPLIT;
212       else
213         {
214           lex_error (lexer, NULL);
215           return CMD_FAILURE;
216         }
217
218       if (!parse_variables (lexer, dataset_dict (ds), &v, &nv, PV_NONE))
219         return CMD_FAILURE;
220
221       for (i = 0; i < nv; i++)
222         var_set_role (v[i], role);
223       free (v);
224     }
225
226   return CMD_SUCCESS;
227 }