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 #include "debug-print.h"
37 static struct variable **v;
39 /* Number of variables. */
42 static int do_value_labels (int);
43 static int verify_val_labs (int erase);
44 static int get_label (void);
47 static void debug_print (void);
65 cmd_value_labels (void)
69 lex_match_id ("VALUE");
70 lex_match_id ("LABELS");
71 code = do_value_labels (1);
77 cmd_add_value_labels (void)
81 lex_match_id ("VALUE");
82 lex_match_id ("LABELS");
83 code = do_value_labels (0);
91 do_value_labels (int erase)
97 parse_variables (NULL, &v, &nv, PV_SAME_TYPE);
98 if (!verify_val_labs (erase))
99 return CMD_PART_SUCCESS_MAYBE;
100 while (token != '/' && token != '.')
102 return CMD_PART_SUCCESS_MAYBE;
115 return CMD_TRAILING_GARBAGE;
125 verify_val_labs (int erase)
132 for (i = 0; i < nv; i++)
134 struct variable *vp = v[i];
136 if (vp->type == ALPHA && vp->width > 8)
138 msg (SE, _("It is not possible to assign value labels to long "
139 "string variables such as %s."), vp->name);
144 val_labs_clear (vp->val_labs);
149 /* Parse all the labels for a particular set of variables and add the
150 specified labels to those variables. */
156 /* Make sure there's some variables. */
159 if (token != T_STRING && token != T_NUM)
165 /* Parse all the labels and add them to the variables. */
172 if (v[0]->type == ALPHA)
174 if (token != T_STRING)
176 msg (SE, _("String expected for value."));
179 st_bare_pad_copy (value.s, ds_value (&tokstr), MAX_SHORT_STRING);
185 msg (SE, _("Number expected for value."));
188 if (!lex_integer_p ())
189 msg (SW, _("Value label `%g' is not integer."), tokval);
195 if (!lex_force_string ())
197 if (ds_length (&tokstr) > 60)
199 msg (SW, _("Truncating value label to 60 characters."));
200 ds_truncate (&tokstr, 60);
202 label = ds_value (&tokstr);
204 for (i = 0; i < nv; i++)
205 val_labs_replace (v[i]->val_labs, value, label);
209 while (token != '/' && token != '.');
220 puts (_("Value labels:"));
221 for (i = 0; i < nvar; i++)
223 struct hsh_iterator i;
224 struct value_label *val;
226 printf (" %s\n", var[i]->name);
229 for (val = hsh_first (var[i]->val_lab, &i); val != NULL;
230 val = hsh_next (var[i]->val_lab, &i))
231 if (var[i]->type == NUMERIC)
232 printf (" %g: `%s'\n", val->v.f, val->s);
234 printf (" `%.8s': `%s'\n", val->v.s, val->s);
237 printf (_(" (no value labels)\n"));
240 #endif /* DEBUGGING */