checkin of 0.3.0
[pspp-builds.git] / src / val-labs.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3    Written by Ben Pfaff <blp@gnu.org>.
4
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.
9
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.
14
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
18    02111-1307, USA. */
19
20 #include <config.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include "alloc.h"
24 #include "avl.h"
25 #include "command.h"
26 #include "error.h"
27 #include "lexer.h"
28 #include "str.h"
29 #include "var.h"
30 \f
31 /* Declarations. */
32
33 #undef DEBUGGING
34 /*#define DEBUGGING 1 */
35 #include "debug-print.h"
36
37 /* Variable list. */
38 static struct variable **v;
39
40 /* Number of variables. */
41 static int nv;
42
43 static int do_value_labels (int);
44 static int verify_val_labs (int erase);
45 static int get_label (void);
46
47 #if DEBUGGING
48 static void debug_print (void);
49 #endif
50 \f
51 /* Stubs. */
52
53 static void
54 init (void)
55 {
56   v = NULL;
57 }
58
59 static void
60 done (void)
61 {
62   free (v);
63 }
64
65 int
66 cmd_value_labels (void)
67 {
68   int code;
69   init ();
70   lex_match_id ("VALUE");
71   lex_match_id ("LABELS");
72   code = do_value_labels (1);
73   done ();
74   return code;
75 }
76
77 int
78 cmd_add_value_labels (void)
79 {
80   int code;
81   lex_match_id ("ADD");
82   lex_match_id ("VALUE");
83   lex_match_id ("LABELS");
84   code = do_value_labels (0);
85   done ();
86   return code;
87 }
88 \f
89 /* Do it. */
90
91 static int
92 do_value_labels (int erase)
93 {
94   lex_match ('/');
95   
96   while (token != '.')
97     {
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 != '.')
102         if (!get_label ())
103           return CMD_PART_SUCCESS_MAYBE;
104
105       if (token != '/')
106         break;
107       lex_get ();
108
109       free (v);
110       v = NULL;
111     }
112
113   if (token != '.')
114     {
115       lex_error (NULL);
116       return CMD_TRAILING_GARBAGE;
117     }
118
119 #if DEBUGGING
120   debug_print ();
121 #endif
122   return CMD_SUCCESS;
123 }
124
125 static int
126 verify_val_labs (int erase)
127 {
128   int i;
129
130   if (!nv)
131     return 1;
132
133   for (i = 0; i < nv; i++)
134     {
135       struct variable *vp = v[i];
136
137       if (vp->type == ALPHA && vp->width > 8)
138         {
139           msg (SE, _("It is not possible to assign value labels to long "
140                      "string variables such as %s."), vp->name);
141           return 0;
142         }
143
144       if (erase && v[i]->val_lab)
145         {
146           avl_destroy (vp->val_lab, free_val_lab);
147           vp->val_lab = NULL;
148         }
149     }
150   return 1;
151 }
152
153 /* Parse all the labels for a particular set of variables and add the
154    specified labels to those variables. */
155 static int
156 get_label (void)
157 {
158   int i;
159
160   /* Make sure there's some variables. */
161   if (!nv)
162     {
163       if (token != T_STRING && token != T_NUM)
164         return 0;
165       lex_get ();
166       return 1;
167     }
168
169   /* Parse all the labels and add them to the variables. */
170   do
171     {
172       struct value_label *label;
173
174       /* Allocate label. */
175       label = xmalloc (sizeof *label);
176 #if __CHECKER__
177       memset (&label->v, 0, sizeof label->v);
178 #endif
179       label->ref_count = nv;
180
181       /* Set label->v. */
182       if (v[0]->type == ALPHA)
183         {
184           if (token != T_STRING)
185             {
186               msg (SE, _("String expected for value."));
187               return 0;
188             }
189           st_bare_pad_copy (label->v.s, ds_value (&tokstr), MAX_SHORT_STRING);
190         }
191       else
192         {
193           if (token != T_NUM)
194             {
195               msg (SE, _("Number expected for value."));
196               return 0;
197             }
198           if (!lex_integer_p ())
199             msg (SW, _("Value label `%g' is not integer."), tokval);
200           label->v.f = tokval;
201         }
202
203       /* Set label->s. */
204       lex_get ();
205       if (!lex_force_string ())
206         return 0;
207       if (ds_length (&tokstr) > 60)
208         {
209           msg (SW, _("Truncating value label to 60 characters."));
210           ds_truncate (&tokstr, 60);
211         }
212       label->s = xstrdup (ds_value (&tokstr));
213
214       for (i = 0; i < nv; i++)
215         {
216           if (!v[i]->val_lab)
217             v[i]->val_lab = avl_create (NULL, val_lab_cmp,
218                                         (void *) (v[i]->width));
219           
220           {
221             struct value_label *old;
222             
223             old = avl_replace (v[i]->val_lab, label);
224             if (old)
225               free_value_label (old);
226           }
227         }
228
229       lex_get ();
230     }
231   while (token != '/' && token != '.');
232
233   return 1;
234 }
235
236 #if DEBUGGING
237 static void
238 debug_print ()
239 {
240   int i;
241
242   puts (_("Value labels:"));
243   for (i = 0; i < nvar; i++)
244     {
245       AVLtraverser *t = NULL;
246       struct value_label *val;
247
248       printf ("  %s\n", var[i]->name);
249       if (var[i]->val_lab)
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);
254         else
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);
258       else
259         printf (_("    (no value labels)\n"));
260     }
261 }
262 #endif /* DEBUGGING */
263
264 /* Compares two value labels and returns a strcmp()-type result. */
265 int
266 val_lab_cmp (const void *a, const void *b, void *param)
267 {
268   if ((int) param)
269     return strncmp (((struct value_label *) a)->v.s,
270                     ((struct value_label *) b)->v.s,
271                     (int) param);
272   else
273     {
274       int temp = (((struct value_label *) a)->v.f
275                   - ((struct value_label *) b)->v.f);
276       if (temp > 0)
277         return 1;
278       else if (temp < 0)
279         return -1;
280       else
281         return 0;
282     }
283 }
284
285 /* Callback function to increment the reference count for a value
286    label. */
287 void *
288 inc_ref_count (void *pv, void *param unused)
289 {
290   ((struct value_label *) pv)->ref_count++;
291   return pv;
292 }
293
294 /* Copy the avl tree of value labels and return a pointer to the
295    copy. */
296 avl_tree *
297 copy_value_labels (avl_tree *src)
298 {
299   avl_tree *dest;
300
301   if (src == NULL)
302     return NULL;
303   dest = avl_copy (NULL, src, inc_ref_count);
304
305   return dest;
306 }