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