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);
177 memset (&label->v, 0, sizeof label->v);
179 label->ref_count = nv;
182 if (v[0]->type == ALPHA)
184 if (token != T_STRING)
186 msg (SE, _("String expected for value."));
189 st_bare_pad_copy (label->v.s, ds_value (&tokstr), MAX_SHORT_STRING);
195 msg (SE, _("Number expected for value."));
198 if (!lex_integer_p ())
199 msg (SW, _("Value label `%g' is not integer."), tokval);
205 if (!lex_force_string ())
207 if (ds_length (&tokstr) > 60)
209 msg (SW, _("Truncating value label to 60 characters."));
210 ds_truncate (&tokstr, 60);
212 label->s = xstrdup (ds_value (&tokstr));
214 for (i = 0; i < nv; i++)
217 v[i]->val_lab = avl_create (NULL, val_lab_cmp,
218 (void *) (v[i]->width));
221 struct value_label *old;
223 old = avl_replace (v[i]->val_lab, label);
225 free_value_label (old);
231 while (token != '/' && token != '.');
242 puts (_("Value labels:"));
243 for (i = 0; i < nvar; i++)
245 AVLtraverser *t = NULL;
246 struct value_label *val;
248 printf (" %s\n", var[i]->name);
250 if (var[i]->type == NUMERIC)
251 for (val = avltrav (var[i]->val_lab, &t);
252 val; val = avltrav (var[i]->val_lab, &t))
253 printf (" %g: `%s'\n", val->v.f, val->s);
255 for (val = avltrav (var[i]->val_lab, &t);
256 val; val = avltrav (var[i]->val_lab, &t))
257 printf (" `%.8s': `%s'\n", val->v.s, val->s);
259 printf (_(" (no value labels)\n"));
262 #endif /* DEBUGGING */
264 /* Compares two value labels and returns a strcmp()-type result. */
266 val_lab_cmp (const void *a, const void *b, void *param)
269 return strncmp (((struct value_label *) a)->v.s,
270 ((struct value_label *) b)->v.s,
274 int temp = (((struct value_label *) a)->v.f
275 - ((struct value_label *) b)->v.f);
285 /* Callback function to increment the reference count for a value
288 inc_ref_count (void *pv, void *param unused)
290 ((struct value_label *) pv)->ref_count++;
294 /* Copy the avl tree of value labels and return a pointer to the
297 copy_value_labels (avl_tree *src)
303 dest = avl_copy (NULL, src, inc_ref_count);