9d289cbce50f7c707637092332d1f53ae59b5fdc
[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 "command.h"
25 #include "error.h"
26 #include "hash.h"
27 #include "lexer.h"
28 #include "str.h"
29 #include "value-labels.h"
30 #include "var.h"
31 \f
32 /* Declarations. */
33
34 static int do_value_labels (int);
35 static int verify_val_labs (struct variable **vars, int var_cnt);
36 static void erase_labels (struct variable **vars, int var_cnt);
37 static int get_label (struct variable **vars, int var_cnt);
38 \f
39 /* Stubs. */
40
41 int
42 cmd_value_labels (void)
43 {
44   lex_match_id ("VALUE");
45   lex_match_id ("LABELS");
46   return do_value_labels (1);
47 }
48
49 int
50 cmd_add_value_labels (void)
51 {
52   lex_match_id ("ADD");
53   lex_match_id ("VALUE");
54   lex_match_id ("LABELS");
55   return do_value_labels (0);
56 }
57 \f
58 /* Do it. */
59
60 static int
61 do_value_labels (int erase)
62 {
63   struct variable **vars; /* Variable list. */
64   int var_cnt;            /* Number of variables. */
65
66   lex_match ('/');
67   
68   while (token != '.')
69     {
70       parse_variables (default_dict, &vars, &var_cnt, PV_SAME_TYPE);
71       if (!verify_val_labs (vars, var_cnt))
72         goto lossage;
73       if (erase)
74         erase_labels (vars, var_cnt);
75       while (token != '/' && token != '.')
76         if (!get_label (vars, var_cnt))
77           goto lossage;
78
79       if (token != '/')
80         break;
81       lex_get ();
82
83       free (vars);
84     }
85
86   if (token != '.')
87     {
88       lex_error (NULL);
89       return CMD_TRAILING_GARBAGE;
90     }
91
92   return CMD_SUCCESS;
93
94  lossage:
95   free (vars);
96   return CMD_PART_SUCCESS_MAYBE;
97 }
98
99 /* Verifies that none of the VAR_CNT variables in VARS are long
100    string variables. */
101 static int
102 verify_val_labs (struct variable **vars, int var_cnt)
103 {
104   int i;
105
106   for (i = 0; i < var_cnt; i++)
107     {
108       struct variable *vp = vars[i];
109
110       if (vp->type == ALPHA && vp->width > 8)
111         {
112           msg (SE, _("It is not possible to assign value labels to long "
113                      "string variables such as %s."), vp->name);
114           return 0;
115         }
116     }
117   return 1;
118 }
119
120 /* Erases all the labels for the VAR_CNT variables in VARS. */
121 static void
122 erase_labels (struct variable **vars, int var_cnt) 
123 {
124   int i;
125
126   /* Erase old value labels if desired. */
127   for (i = 0; i < var_cnt; i++)
128     val_labs_clear (vars[i]->val_labs);
129 }
130
131 /* Parse all the labels for the VAR_CNT variables in VARS and add
132    the specified labels to those variables.  */
133 static int
134 get_label (struct variable **vars, int var_cnt)
135 {
136   /* Parse all the labels and add them to the variables. */
137   do
138     {
139       union value value;
140       char *label;
141       int i;
142
143       /* Set value. */
144       if (vars[0]->type == ALPHA)
145         {
146           if (token != T_STRING)
147             {
148               lex_error (_("expecting string"));
149               return 0;
150             }
151           st_bare_pad_copy (value.s, ds_value (&tokstr), MAX_SHORT_STRING);
152         }
153       else
154         {
155           if (token != T_NUM)
156             {
157               lex_error (_("expecting integer"));
158               return 0;
159             }
160           if (!lex_integer_p ())
161             msg (SW, _("Value label `%g' is not integer."), tokval);
162           value.f = tokval;
163         }
164       lex_get ();
165
166       /* Set label. */
167       if (!lex_force_string ())
168         return 0;
169       if (ds_length (&tokstr) > 60)
170         {
171           msg (SW, _("Truncating value label to 60 characters."));
172           ds_truncate (&tokstr, 60);
173         }
174       label = ds_value (&tokstr);
175
176       for (i = 0; i < var_cnt; i++)
177         val_labs_replace (vars[i]->val_labs, value, label);
178
179       lex_get ();
180     }
181   while (token != '/' && token != '.');
182
183   return 1;
184 }