8479fe7fa74218feeb0c462aed2e86fa1c5df692
[pspp-builds.git] / src / language / dictionary / value-labels.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3    Written by Ben Pfaff <blp@gnu.org>.
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 #include <stdio.h>
22 #include <stdlib.h>
23 #include <libpspp/alloc.h>
24 #include <language/command.h>
25 #include <libpspp/message.h>
26 #include <libpspp/hash.h>
27 #include <language/lexer/lexer.h>
28 #include <libpspp/str.h>
29 #include <data/value-labels.h>
30 #include <data/variable.h>
31
32 #include "gettext.h"
33 #define _(msgid) gettext (msgid)
34 \f
35 /* Declarations. */
36
37 static int do_value_labels (int);
38 static int verify_val_labs (struct variable **vars, size_t var_cnt);
39 static void erase_labels (struct variable **vars, size_t var_cnt);
40 static int get_label (struct variable **vars, size_t var_cnt);
41 \f
42 /* Stubs. */
43
44 int
45 cmd_value_labels (void)
46 {
47   return do_value_labels (1);
48 }
49
50 int
51 cmd_add_value_labels (void)
52 {
53   return do_value_labels (0);
54 }
55 \f
56 /* Do it. */
57
58 static int
59 do_value_labels (int erase)
60 {
61   struct variable **vars; /* Variable list. */
62   size_t var_cnt;         /* Number of variables. */
63   int parse_err=0;        /* true if error parsing variables */
64
65   lex_match ('/');
66   
67   while (token != '.')
68     {
69       parse_err = !parse_variables (default_dict, &vars, &var_cnt, 
70                                     PV_SAME_TYPE) ;
71       if (var_cnt < 1)
72         {
73           free(vars);
74           return CMD_FAILURE;
75         }
76       if (!verify_val_labs (vars, var_cnt))
77         goto lossage;
78       if (erase)
79         erase_labels (vars, var_cnt);
80       while (token != '/' && token != '.')
81         if (!get_label (vars, var_cnt))
82           goto lossage;
83
84       if (token != '/')
85         {
86         free (vars);
87         break;
88         }
89
90       lex_get ();
91
92       free (vars);
93     }
94
95   if (parse_err)
96     return CMD_FAILURE;
97
98   return lex_end_of_command ();
99
100  lossage:
101   free (vars);
102   return CMD_FAILURE;
103 }
104
105 /* Verifies that none of the VAR_CNT variables in VARS are long
106    string variables. */
107 static int
108 verify_val_labs (struct variable **vars, size_t var_cnt)
109 {
110   size_t i;
111
112   for (i = 0; i < var_cnt; i++)
113     {
114       struct variable *vp = vars[i];
115
116       if (vp->type == ALPHA && vp->width > MAX_SHORT_STRING)
117         {
118           msg (SE, _("It is not possible to assign value labels to long "
119                      "string variables such as %s."), vp->name);
120           return 0;
121         }
122     }
123   return 1;
124 }
125
126 /* Erases all the labels for the VAR_CNT variables in VARS. */
127 static void
128 erase_labels (struct variable **vars, size_t var_cnt) 
129 {
130   size_t i;
131
132   /* Erase old value labels if desired. */
133   for (i = 0; i < var_cnt; i++)
134     val_labs_clear (vars[i]->val_labs);
135 }
136
137 /* Parse all the labels for the VAR_CNT variables in VARS and add
138    the specified labels to those variables.  */
139 static int
140 get_label (struct variable **vars, size_t var_cnt)
141 {
142   /* Parse all the labels and add them to the variables. */
143   do
144     {
145       union value value;
146       char *label;
147       size_t i;
148
149       /* Set value. */
150       if (vars[0]->type == ALPHA)
151         {
152           if (token != T_STRING)
153             {
154               lex_error (_("expecting string"));
155               return 0;
156             }
157           buf_copy_str_rpad (value.s, MAX_SHORT_STRING, ds_c_str (&tokstr));
158         }
159       else
160         {
161           if (!lex_is_number ())
162             {
163               lex_error (_("expecting integer"));
164               return 0;
165             }
166           if (!lex_is_integer ())
167             msg (SW, _("Value label `%g' is not integer."), tokval);
168           value.f = tokval;
169         }
170       lex_get ();
171
172       /* Set label. */
173       if (!lex_force_string ())
174         return 0;
175       if (ds_length (&tokstr) > 60)
176         {
177           msg (SW, _("Truncating value label to 60 characters."));
178           ds_truncate (&tokstr, 60);
179         }
180       label = ds_c_str (&tokstr);
181
182       for (i = 0; i < var_cnt; i++)
183         val_labs_replace (vars[i]->val_labs, value, label);
184
185       lex_get ();
186     }
187   while (token != '/' && token != '.');
188
189   return 1;
190 }