c36ff28b414dc074e1095aed78859b58280fd1b8
[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       label->ref_count = nv;
177
178       /* Set label->v. */
179       if (v[0]->type == ALPHA)
180         {
181           if (token != T_STRING)
182             {
183               msg (SE, _("String expected for value."));
184               return 0;
185             }
186           st_bare_pad_copy (label->v.s, ds_value (&tokstr), MAX_SHORT_STRING);
187         }
188       else
189         {
190           if (token != T_NUM)
191             {
192               msg (SE, _("Number expected for value."));
193               return 0;
194             }
195           if (!lex_integer_p ())
196             msg (SW, _("Value label `%g' is not integer."), tokval);
197           label->v.f = tokval;
198         }
199
200       /* Set label->s. */
201       lex_get ();
202       if (!lex_force_string ())
203         return 0;
204       if (ds_length (&tokstr) > 60)
205         {
206           msg (SW, _("Truncating value label to 60 characters."));
207           ds_truncate (&tokstr, 60);
208         }
209       label->s = xstrdup (ds_value (&tokstr));
210
211       for (i = 0; i < nv; i++)
212         {
213           if (!v[i]->val_lab)
214             v[i]->val_lab = avl_create (NULL, val_lab_cmp,
215                                         (void *) (v[i]->width));
216           
217           {
218             struct value_label *old;
219             
220             old = avl_replace (v[i]->val_lab, label);
221             if (old)
222               free_value_label (old);
223           }
224         }
225
226       lex_get ();
227     }
228   while (token != '/' && token != '.');
229
230   return 1;
231 }
232
233 #if DEBUGGING
234 static void
235 debug_print ()
236 {
237   int i;
238
239   puts (_("Value labels:"));
240   for (i = 0; i < nvar; i++)
241     {
242       AVLtraverser *t = NULL;
243       struct value_label *val;
244
245       printf ("  %s\n", var[i]->name);
246       if (var[i]->val_lab)
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);
251         else
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);
255       else
256         printf (_("    (no value labels)\n"));
257     }
258 }
259 #endif /* DEBUGGING */
260
261 /* Compares two value labels and returns a strcmp()-type result. */
262 int
263 val_lab_cmp (const void *a, const void *b, void *param)
264 {
265   if ((int) param)
266     return strncmp (((struct value_label *) a)->v.s,
267                     ((struct value_label *) b)->v.s,
268                     (int) param);
269   else
270     {
271       int temp = (((struct value_label *) a)->v.f
272                   - ((struct value_label *) b)->v.f);
273       if (temp > 0)
274         return 1;
275       else if (temp < 0)
276         return -1;
277       else
278         return 0;
279     }
280 }
281
282 /* Callback function to increment the reference count for a value
283    label. */
284 void *
285 inc_ref_count (void *pv, void *param unused)
286 {
287   ((struct value_label *) pv)->ref_count++;
288   return pv;
289 }
290
291 /* Copy the avl tree of value labels and return a pointer to the
292    copy. */
293 avl_tree *
294 copy_value_labels (avl_tree *src)
295 {
296   avl_tree *dest;
297
298   if (src == NULL)
299     return NULL;
300   dest = avl_copy (NULL, src, inc_ref_count);
301
302   return dest;
303 }