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