First step in making struct variable opaque: the boring mechanical
[pspp-builds.git] / src / language / dictionary / variable-display.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3    Written by John Darrington <john@darrington.wattle.id.au>
4
5    This program is free software; you can redistribute it and/or
6    modify it under the terms of the GNU General Public License as
7    published by the Free Software Foundation; either version 2 of the
8    License, or (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful, but
11    WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18    02110-1301, USA. */
19
20 #include <config.h>
21
22 #include <stdio.h>
23 #include <stdlib.h>
24
25 #include <data/procedure.h>
26 #include <data/variable.h>
27 #include <language/command.h>
28 #include <language/lexer/lexer.h>
29 #include <language/lexer/variable-parser.h>
30 #include <libpspp/alloc.h>
31 #include <libpspp/message.h>
32 #include <libpspp/str.h>
33
34 /* Set variables' alignment
35    This is the alignment for GUI display only.
36    It affects nothing but GUIs
37 */
38 int
39 cmd_variable_alignment (struct lexer *lexer, struct dataset *ds)
40 {
41   do
42     {
43       struct variable **v;
44       size_t nv;
45
46       size_t i;
47       enum alignment align;
48
49       if (!parse_variables (lexer, dataset_dict (ds), &v, &nv, PV_NONE))
50         return CMD_FAILURE;
51
52       if ( lex_force_match (lexer, '(') ) 
53         {
54           if ( lex_match_id (lexer, "LEFT"))
55             align = ALIGN_LEFT;
56           else if ( lex_match_id (lexer, "RIGHT"))
57             align = ALIGN_RIGHT;
58           else if ( lex_match_id (lexer, "CENTER"))
59             align = ALIGN_CENTRE;
60           else 
61             {
62               free (v);
63               return CMD_FAILURE; 
64             }
65
66           lex_force_match (lexer, ')');
67         }
68       else 
69         {
70           free (v);
71           return CMD_FAILURE; 
72         }
73
74       for( i = 0 ; i < nv ; ++i )
75         var_set_alignment (v[i], align);
76
77       while (lex_token (lexer) == '/')
78         lex_get (lexer);
79       free (v);
80
81     }
82   while (lex_token (lexer) != '.');
83   return CMD_SUCCESS;
84 }
85
86 /* Set variables' display width.
87    This is the width for GUI display only.
88    It affects nothing but GUIs
89 */
90 int
91 cmd_variable_width (struct lexer *lexer, struct dataset *ds)
92 {
93   do
94     {
95       struct variable **v;
96       size_t nv;
97       size_t i;
98
99       if (!parse_variables (lexer, dataset_dict (ds), &v, &nv, PV_NONE))
100         return CMD_FAILURE;
101
102       if ( lex_force_match (lexer, '(') ) 
103         {
104           if ( lex_force_int (lexer)) 
105             lex_get (lexer);
106           else
107             return CMD_FAILURE;
108           lex_force_match (lexer, ')');
109         }
110
111       for( i = 0 ; i < nv ; ++i ) 
112         var_set_display_width (v[i], lex_integer (lexer));
113
114       while (lex_token (lexer) == '/')
115         lex_get (lexer);
116       free (v);
117
118     }
119   while (lex_token (lexer) != '.');
120   return CMD_SUCCESS;
121 }
122
123 /* Set variables' measurement level */
124 int
125 cmd_variable_level (struct lexer *lexer, struct dataset *ds)
126 {
127   do
128     {
129       struct variable **v;
130       size_t nv;
131       enum measure level;
132       size_t i;
133
134       if (!parse_variables (lexer, dataset_dict (ds), &v, &nv, PV_NONE))
135         return CMD_FAILURE;
136
137       if ( lex_force_match (lexer, '(') ) 
138         {
139           if ( lex_match_id (lexer, "SCALE"))
140             level = MEASURE_SCALE;
141           else if ( lex_match_id (lexer, "ORDINAL"))
142             level = MEASURE_ORDINAL;
143           else if ( lex_match_id (lexer, "NOMINAL"))
144             level = MEASURE_NOMINAL;
145           else 
146             {
147               free (v);
148               return CMD_FAILURE; 
149             }
150
151           lex_force_match (lexer, ')');
152         }
153       else
154         {
155           free (v);
156           return CMD_FAILURE; 
157         }
158       
159       for( i = 0 ; i < nv ; ++i ) 
160         var_set_measure (v[i], level);
161
162
163       while (lex_token (lexer) == '/')
164         lex_get (lexer);
165       free (v);
166
167     }
168   while (lex_token (lexer) != '.');
169   return CMD_SUCCESS;
170 }