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
33 #include "debug-print.h"
36 static struct variable **v;
38 /* Number of variables. */
41 static int do_value_labels (int);
42 static int verify_val_labs (int erase);
43 static int get_label (void);
46 static void debug_print (void);
64 cmd_value_labels (void)
68 lex_match_id ("VALUE");
69 lex_match_id ("LABELS");
70 code = do_value_labels (1);
76 cmd_add_value_labels (void)
80 lex_match_id ("VALUE");
81 lex_match_id ("LABELS");
82 code = do_value_labels (0);
90 do_value_labels (int erase)
96 parse_variables (NULL, &v, &nv, PV_SAME_TYPE);
97 if (!verify_val_labs (erase))
98 return CMD_PART_SUCCESS_MAYBE;
99 while (token != '/' && token != '.')
101 return CMD_PART_SUCCESS_MAYBE;
114 return CMD_TRAILING_GARBAGE;
124 verify_val_labs (int erase)
131 for (i = 0; i < nv; i++)
133 struct variable *vp = v[i];
135 if (vp->type == ALPHA && vp->width > 8)
137 msg (SE, _("It is not possible to assign value labels to long "
138 "string variables such as %s."), vp->name);
142 if (erase && v[i]->val_lab)
144 avl_destroy (vp->val_lab, free_val_lab);
151 /* Parse all the labels for a particular set of variables and add the
152 specified labels to those variables. */
158 /* Make sure there's some variables. */
161 if (token != T_STRING && token != T_NUM)
167 /* Parse all the labels and add them to the variables. */
170 struct value_label *label;
172 /* Allocate label. */
173 label = xmalloc (sizeof *label);
174 label->ref_count = nv;
177 if (v[0]->type == ALPHA)
179 if (token != T_STRING)
181 msg (SE, _("String expected for value."));
184 st_bare_pad_copy (label->v.s, ds_value (&tokstr), MAX_SHORT_STRING);
190 msg (SE, _("Number expected for value."));
193 if (!lex_integer_p ())
194 msg (SW, _("Value label `%g' is not integer."), tokval);
200 if (!lex_force_string ())
202 if (ds_length (&tokstr) > 60)
204 msg (SW, _("Truncating value label to 60 characters."));
205 ds_truncate (&tokstr, 60);
207 label->s = xstrdup (ds_value (&tokstr));
209 for (i = 0; i < nv; i++)
212 v[i]->val_lab = avl_create (NULL, val_lab_cmp,
213 (void *) (v[i]->width));
216 struct value_label *old;
218 old = avl_replace (v[i]->val_lab, label);
220 free_value_label (old);
226 while (token != '/' && token != '.');
237 puts (_("Value labels:"));
238 for (i = 0; i < nvar; i++)
240 AVLtraverser *t = NULL;
241 struct value_label *val;
243 printf (" %s\n", var[i]->name);
245 if (var[i]->type == NUMERIC)
246 for (val = avltrav (var[i]->val_lab, &t);
247 val; val = avltrav (var[i]->val_lab, &t))
248 printf (" %g: `%s'\n", val->v.f, val->s);
250 for (val = avltrav (var[i]->val_lab, &t);
251 val; val = avltrav (var[i]->val_lab, &t))
252 printf (" `%.8s': `%s'\n", val->v.s, val->s);
254 printf (_(" (no value labels)\n"));
257 #endif /* DEBUGGING */
259 /* Compares two value labels and returns a strcmp()-type result. */
261 val_lab_cmp (const void *a, const void *b, void *param)
264 return strncmp (((struct value_label *) a)->v.s,
265 ((struct value_label *) b)->v.s,
269 int temp = (((struct value_label *) a)->v.f
270 - ((struct value_label *) b)->v.f);
280 /* Callback function to increment the reference count for a value
283 inc_ref_count (void *pv, void *param unused)
285 ((struct value_label *) pv)->ref_count++;
289 /* Copy the avl tree of value labels and return a pointer to the
292 copy_value_labels (avl_tree *src)
298 dest = avl_copy (NULL, src, inc_ref_count);