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., 59 Temple Place - Suite 330, Boston, MA
29 #include "value-labels.h"
34 static int do_value_labels (int);
35 static int verify_val_labs (struct variable **vars, int var_cnt);
36 static void erase_labels (struct variable **vars, int var_cnt);
37 static int get_label (struct variable **vars, int var_cnt);
42 cmd_value_labels (void)
44 return do_value_labels (1);
48 cmd_add_value_labels (void)
50 return do_value_labels (0);
56 do_value_labels (int erase)
58 struct variable **vars; /* Variable list. */
59 int var_cnt; /* Number of variables. */
60 int parse_err=0; /* true if error parsing variables */
66 parse_err = !parse_variables (default_dict, &vars, &var_cnt,
73 if (!verify_val_labs (vars, var_cnt))
76 erase_labels (vars, var_cnt);
77 while (token != '/' && token != '.')
78 if (!get_label (vars, var_cnt))
92 return CMD_TRAILING_GARBAGE;
95 return parse_err ? CMD_PART_SUCCESS_MAYBE : CMD_SUCCESS;
99 return CMD_PART_SUCCESS_MAYBE;
102 /* Verifies that none of the VAR_CNT variables in VARS are long
105 verify_val_labs (struct variable **vars, int var_cnt)
109 for (i = 0; i < var_cnt; i++)
111 struct variable *vp = vars[i];
113 if (vp->type == ALPHA && vp->width > 8)
115 msg (SE, _("It is not possible to assign value labels to long "
116 "string variables such as %s."), vp->name);
123 /* Erases all the labels for the VAR_CNT variables in VARS. */
125 erase_labels (struct variable **vars, int var_cnt)
129 /* Erase old value labels if desired. */
130 for (i = 0; i < var_cnt; i++)
131 val_labs_clear (vars[i]->val_labs);
134 /* Parse all the labels for the VAR_CNT variables in VARS and add
135 the specified labels to those variables. */
137 get_label (struct variable **vars, int var_cnt)
139 /* Parse all the labels and add them to the variables. */
147 if (vars[0]->type == ALPHA)
149 if (token != T_STRING)
151 lex_error (_("expecting string"));
154 st_bare_pad_copy (value.s, ds_c_str (&tokstr), MAX_SHORT_STRING);
160 lex_error (_("expecting integer"));
163 if (!lex_integer_p ())
164 msg (SW, _("Value label `%g' is not integer."), tokval);
170 if (!lex_force_string ())
172 if (ds_length (&tokstr) > 60)
174 msg (SW, _("Truncating value label to 60 characters."));
175 ds_truncate (&tokstr, 60);
177 label = ds_c_str (&tokstr);
179 for (i = 0; i < var_cnt; i++)
180 val_labs_replace (vars[i]->val_labs, value, label);
184 while (token != '/' && token != '.');