Change "union value" to dynamically allocate long strings.
[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 "minmax.h"
31 #include "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, '(') )
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           lex_force_match (lexer, ')');
69         }
70       else
71         {
72           free (v);
73           return CMD_FAILURE;
74         }
75
76       for( i = 0 ; i < nv ; ++i )
77         var_set_alignment (v[i], align);
78
79       while (lex_token (lexer) == '/')
80         lex_get (lexer);
81       free (v);
82
83     }
84   while (lex_token (lexer) != '.');
85   return CMD_SUCCESS;
86 }
87
88 /* Set variables' display width.
89    This is the width for GUI display only.
90    It affects nothing but GUIs
91 */
92 int
93 cmd_variable_width (struct lexer *lexer, struct dataset *ds)
94 {
95   do
96     {
97       struct variable **v;
98       long int width;
99       size_t nv;
100       size_t i;
101
102       if (!parse_variables (lexer, dataset_dict (ds), &v, &nv, PV_NONE))
103         return CMD_FAILURE;
104
105       if (!lex_force_match (lexer, '(') || !lex_force_int (lexer))
106         {
107           free (v);
108           return CMD_FAILURE;
109         }
110       width = lex_integer (lexer);
111       lex_get (lexer);
112       if (!lex_force_match (lexer, ')'))
113         {
114           free (v);
115           return CMD_FAILURE;
116         }
117
118       if (width < 0)
119         {
120           msg (SE, _("Variable display width must be a positive integer."));
121           free (v);
122           return CMD_FAILURE;
123         }
124       width = MIN (width, 2 * MAX_STRING);
125
126       for( i = 0 ; i < nv ; ++i )
127         var_set_display_width (v[i], width);
128
129       while (lex_token (lexer) == '/')
130         lex_get (lexer);
131       free (v);
132
133     }
134   while (lex_token (lexer) != '.');
135   return CMD_SUCCESS;
136 }
137
138 /* Set variables' measurement level */
139 int
140 cmd_variable_level (struct lexer *lexer, struct dataset *ds)
141 {
142   do
143     {
144       struct variable **v;
145       size_t nv;
146       enum measure level;
147       size_t i;
148
149       if (!parse_variables (lexer, dataset_dict (ds), &v, &nv, PV_NONE))
150         return CMD_FAILURE;
151
152       if ( lex_force_match (lexer, '(') )
153         {
154           if ( lex_match_id (lexer, "SCALE"))
155             level = MEASURE_SCALE;
156           else if ( lex_match_id (lexer, "ORDINAL"))
157             level = MEASURE_ORDINAL;
158           else if ( lex_match_id (lexer, "NOMINAL"))
159             level = MEASURE_NOMINAL;
160           else
161             {
162               free (v);
163               return CMD_FAILURE;
164             }
165
166           lex_force_match (lexer, ')');
167         }
168       else
169         {
170           free (v);
171           return CMD_FAILURE;
172         }
173
174       for( i = 0 ; i < nv ; ++i )
175         var_set_measure (v[i], level);
176
177
178       while (lex_token (lexer) == '/')
179         lex_get (lexer);
180       free (v);
181
182     }
183   while (lex_token (lexer) != '.');
184   return CMD_SUCCESS;
185 }