1 /* PSPP - computes sample statistics.
2 Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3 Written by Ben Pfaff <blp@gnu.org>.
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.
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.
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
25 #include <data/procedure.h>
26 #include <data/value-labels.h>
27 #include <data/variable.h>
28 #include <language/command.h>
29 #include <language/lexer/lexer.h>
30 #include <libpspp/alloc.h>
31 #include <libpspp/hash.h>
32 #include <libpspp/message.h>
33 #include <libpspp/str.h>
36 #define _(msgid) gettext (msgid)
40 static int do_value_labels (int);
41 static int verify_val_labs (struct variable **vars, size_t var_cnt);
42 static void erase_labels (struct variable **vars, size_t var_cnt);
43 static int get_label (struct variable **vars, size_t var_cnt);
48 cmd_value_labels (void)
50 return do_value_labels (1);
54 cmd_add_value_labels (void)
56 return do_value_labels (0);
62 do_value_labels (int erase)
64 struct variable **vars; /* Variable list. */
65 size_t var_cnt; /* Number of variables. */
66 int parse_err=0; /* true if error parsing variables */
72 parse_err = !parse_variables (default_dict, &vars, &var_cnt,
79 if (!verify_val_labs (vars, var_cnt))
82 erase_labels (vars, var_cnt);
83 while (token != '/' && token != '.')
84 if (!get_label (vars, var_cnt))
101 return lex_end_of_command ();
108 /* Verifies that none of the VAR_CNT variables in VARS are long
111 verify_val_labs (struct variable **vars, size_t var_cnt)
115 for (i = 0; i < var_cnt; i++)
117 struct variable *vp = vars[i];
119 if (vp->type == ALPHA && vp->width > MAX_SHORT_STRING)
121 msg (SE, _("It is not possible to assign value labels to long "
122 "string variables such as %s."), vp->name);
129 /* Erases all the labels for the VAR_CNT variables in VARS. */
131 erase_labels (struct variable **vars, size_t var_cnt)
135 /* Erase old value labels if desired. */
136 for (i = 0; i < var_cnt; i++)
137 val_labs_clear (vars[i]->val_labs);
140 /* Parse all the labels for the VAR_CNT variables in VARS and add
141 the specified labels to those variables. */
143 get_label (struct variable **vars, size_t var_cnt)
145 /* Parse all the labels and add them to the variables. */
153 if (vars[0]->type == ALPHA)
155 if (token != T_STRING)
157 lex_error (_("expecting string"));
160 buf_copy_str_rpad (value.s, MAX_SHORT_STRING, ds_c_str (&tokstr));
164 if (!lex_is_number ())
166 lex_error (_("expecting integer"));
169 if (!lex_is_integer ())
170 msg (SW, _("Value label `%g' is not integer."), tokval);
176 if (!lex_force_string ())
178 if (ds_length (&tokstr) > 60)
180 msg (SW, _("Truncating value label to 60 characters."));
181 ds_truncate (&tokstr, 60);
183 label = ds_c_str (&tokstr);
185 for (i = 0; i < var_cnt; i++)
186 val_labs_replace (vars[i]->val_labs, value, label);
190 while (token != '/' && token != '.');