277db48e56b2eb53bd292c5819ebb71fd849b529
[pspp-builds.git] / src / language / dictionary / variable-display.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000 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/procedure.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 "xalloc.h"
31
32 #include "gettext.h"
33 #define _(msgid) gettext (msgid)
34
35 /* Set variables' alignment
36    This is the alignment for GUI display only.
37    It affects nothing but GUIs
38 */
39 int
40 cmd_variable_alignment (struct lexer *lexer, struct dataset *ds)
41 {
42   do
43     {
44       struct variable **v;
45       size_t nv;
46
47       size_t i;
48       enum alignment align;
49
50       if (!parse_variables (lexer, dataset_dict (ds), &v, &nv, PV_NONE))
51         return CMD_FAILURE;
52
53       if ( lex_force_match (lexer, '(') )
54         {
55           if ( lex_match_id (lexer, "LEFT"))
56             align = ALIGN_LEFT;
57           else if ( lex_match_id (lexer, "RIGHT"))
58             align = ALIGN_RIGHT;
59           else if ( lex_match_id (lexer, "CENTER"))
60             align = ALIGN_CENTRE;
61           else
62             {
63               free (v);
64               return CMD_FAILURE;
65             }
66
67           lex_force_match (lexer, ')');
68         }
69       else
70         {
71           free (v);
72           return CMD_FAILURE;
73         }
74
75       for( i = 0 ; i < nv ; ++i )
76         var_set_alignment (v[i], align);
77
78       while (lex_token (lexer) == '/')
79         lex_get (lexer);
80       free (v);
81
82     }
83   while (lex_token (lexer) != '.');
84   return CMD_SUCCESS;
85 }
86
87 /* Set variables' display width.
88    This is the width for GUI display only.
89    It affects nothing but GUIs
90 */
91 int
92 cmd_variable_width (struct lexer *lexer, struct dataset *ds)
93 {
94   do
95     {
96       struct variable **v;
97       long int width;
98       size_t nv;
99       size_t i;
100
101       if (!parse_variables (lexer, dataset_dict (ds), &v, &nv, PV_NONE))
102         return CMD_FAILURE;
103
104       if (!lex_force_match (lexer, '(') || !lex_force_int (lexer))
105         {
106           free (v);
107           return CMD_FAILURE;
108         }
109       width = lex_integer (lexer);
110       lex_get (lexer);
111       if (!lex_force_match (lexer, ')'))
112         {
113           free (v);
114           return CMD_FAILURE;
115         }
116
117       if (width < 0)
118         {
119           msg (SE, _("Variable display width must be a positive integer."));
120           free (v);
121           return CMD_FAILURE;
122         }
123       width = MIN (width, 2 * MAX_STRING);
124
125       for( i = 0 ; i < nv ; ++i )
126         var_set_display_width (v[i], width);
127
128       while (lex_token (lexer) == '/')
129         lex_get (lexer);
130       free (v);
131
132     }
133   while (lex_token (lexer) != '.');
134   return CMD_SUCCESS;
135 }
136
137 /* Set variables' measurement level */
138 int
139 cmd_variable_level (struct lexer *lexer, struct dataset *ds)
140 {
141   do
142     {
143       struct variable **v;
144       size_t nv;
145       enum measure level;
146       size_t i;
147
148       if (!parse_variables (lexer, dataset_dict (ds), &v, &nv, PV_NONE))
149         return CMD_FAILURE;
150
151       if ( lex_force_match (lexer, '(') )
152         {
153           if ( lex_match_id (lexer, "SCALE"))
154             level = MEASURE_SCALE;
155           else if ( lex_match_id (lexer, "ORDINAL"))
156             level = MEASURE_ORDINAL;
157           else if ( lex_match_id (lexer, "NOMINAL"))
158             level = MEASURE_NOMINAL;
159           else
160             {
161               free (v);
162               return CMD_FAILURE;
163             }
164
165           lex_force_match (lexer, ')');
166         }
167       else
168         {
169           free (v);
170           return CMD_FAILURE;
171         }
172
173       for( i = 0 ; i < nv ; ++i )
174         var_set_measure (v[i], level);
175
176
177       while (lex_token (lexer) == '/')
178         lex_get (lexer);
179       free (v);
180
181     }
182   while (lex_token (lexer) != '.');
183   return CMD_SUCCESS;
184 }