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
34 /*#define DEBUGGING 1 */
35 #include "debug-print.h"
38 static struct variable **v;
40 /* Number of variables. */
43 static int do_value_labels (int);
44 static int verify_val_labs (int erase);
45 static int get_label (void);
48 static void debug_print (void);
66 cmd_value_labels (void)
70 lex_match_id ("VALUE");
71 lex_match_id ("LABELS");
72 code = do_value_labels (1);
78 cmd_add_value_labels (void)
82 lex_match_id ("VALUE");
83 lex_match_id ("LABELS");
84 code = do_value_labels (0);
92 do_value_labels (int erase)
98 parse_variables (NULL, &v, &nv, PV_SAME_TYPE);
99 if (!verify_val_labs (erase))
100 return CMD_PART_SUCCESS_MAYBE;
101 while (token != '/' && token != '.')
103 return CMD_PART_SUCCESS_MAYBE;
116 return CMD_TRAILING_GARBAGE;
126 verify_val_labs (int erase)
133 for (i = 0; i < nv; i++)
135 struct variable *vp = v[i];
137 if (vp->type == ALPHA && vp->width > 8)
139 msg (SE, _("It is not possible to assign value labels to long "
140 "string variables such as %s."), vp->name);
144 if (erase && v[i]->val_lab)
146 avl_destroy (vp->val_lab, free_val_lab);
153 /* Parse all the labels for a particular set of variables and add the
154 specified labels to those variables. */
160 /* Make sure there's some variables. */
163 if (token != T_STRING && token != T_NUM)
169 /* Parse all the labels and add them to the variables. */
172 struct value_label *label;
174 /* Allocate label. */
175 label = xmalloc (sizeof *label);
176 label->ref_count = nv;
179 if (v[0]->type == ALPHA)
181 if (token != T_STRING)
183 msg (SE, _("String expected for value."));
186 st_bare_pad_copy (label->v.s, ds_value (&tokstr), MAX_SHORT_STRING);
192 msg (SE, _("Number expected for value."));
195 if (!lex_integer_p ())
196 msg (SW, _("Value label `%g' is not integer."), tokval);
202 if (!lex_force_string ())
204 if (ds_length (&tokstr) > 60)
206 msg (SW, _("Truncating value label to 60 characters."));
207 ds_truncate (&tokstr, 60);
209 label->s = xstrdup (ds_value (&tokstr));
211 for (i = 0; i < nv; i++)
214 v[i]->val_lab = avl_create (NULL, val_lab_cmp,
215 (void *) (v[i]->width));
218 struct value_label *old;
220 old = avl_replace (v[i]->val_lab, label);
222 free_value_label (old);
228 while (token != '/' && token != '.');
239 puts (_("Value labels:"));
240 for (i = 0; i < nvar; i++)
242 AVLtraverser *t = NULL;
243 struct value_label *val;
245 printf (" %s\n", var[i]->name);
247 if (var[i]->type == NUMERIC)
248 for (val = avltrav (var[i]->val_lab, &t);
249 val; val = avltrav (var[i]->val_lab, &t))
250 printf (" %g: `%s'\n", val->v.f, val->s);
252 for (val = avltrav (var[i]->val_lab, &t);
253 val; val = avltrav (var[i]->val_lab, &t))
254 printf (" `%.8s': `%s'\n", val->v.s, val->s);
256 printf (_(" (no value labels)\n"));
259 #endif /* DEBUGGING */
261 /* Compares two value labels and returns a strcmp()-type result. */
263 val_lab_cmp (const void *a, const void *b, void *param)
266 return strncmp (((struct value_label *) a)->v.s,
267 ((struct value_label *) b)->v.s,
271 int temp = (((struct value_label *) a)->v.f
272 - ((struct value_label *) b)->v.f);
282 /* Callback function to increment the reference count for a value
285 inc_ref_count (void *pv, void *param unused)
287 ((struct value_label *) pv)->ref_count++;
291 /* Copy the avl tree of value labels and return a pointer to the
294 copy_value_labels (avl_tree *src)
300 dest = avl_copy (NULL, src, inc_ref_count);