Change license from GPLv2+ to GPLv3+.
[pspp-builds.git] / src / language / dictionary / value-labels.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 #include <stdio.h>
20 #include <stdlib.h>
21
22 #include <data/procedure.h>
23 #include <data/value-labels.h>
24 #include <data/variable.h>
25 #include <language/command.h>
26 #include <language/lexer/lexer.h>
27 #include <language/lexer/variable-parser.h>
28 #include <libpspp/alloc.h>
29 #include <libpspp/hash.h>
30 #include <libpspp/message.h>
31 #include <libpspp/str.h>
32
33 #include "gettext.h"
34 #define _(msgid) gettext (msgid)
35 \f
36 /* Declarations. */
37
38 static int do_value_labels (struct lexer *,
39                             const struct dictionary *dict, bool);
40 static int verify_val_labs (struct variable **vars, size_t var_cnt);
41 static void erase_labels (struct variable **vars, size_t var_cnt);
42 static int get_label (struct lexer *, struct variable **vars, size_t var_cnt);
43 \f
44 /* Stubs. */
45
46 int
47 cmd_value_labels (struct lexer *lexer, struct dataset *ds)
48 {
49   return do_value_labels (lexer, dataset_dict (ds), true);
50 }
51
52 int
53 cmd_add_value_labels (struct lexer *lexer, struct dataset *ds)
54 {
55   return do_value_labels (lexer, dataset_dict (ds), false);
56 }
57 \f
58 /* Do it. */
59
60 static int
61 do_value_labels (struct lexer *lexer, const struct dictionary *dict, bool erase)
62 {
63   struct variable **vars; /* Variable list. */
64   size_t var_cnt;         /* Number of variables. */
65   int parse_err=0;        /* true if error parsing variables */
66
67   lex_match (lexer, '/');
68
69   while (lex_token (lexer) != '.')
70     {
71       parse_err = !parse_variables (lexer, dict, &vars, &var_cnt,
72                                     PV_SAME_TYPE) ;
73       if (var_cnt < 1)
74         {
75           free(vars);
76           return CMD_FAILURE;
77         }
78       if (!verify_val_labs (vars, var_cnt))
79         goto lossage;
80       if (erase)
81         erase_labels (vars, var_cnt);
82       while (lex_token (lexer) != '/' && lex_token (lexer) != '.')
83         if (!get_label (lexer, vars, var_cnt))
84           goto lossage;
85
86       if (lex_token (lexer) != '/')
87         {
88           free (vars);
89           break;
90         }
91
92       lex_get (lexer);
93
94       free (vars);
95     }
96
97   if (parse_err)
98     return CMD_FAILURE;
99
100   return lex_end_of_command (lexer);
101
102  lossage:
103   free (vars);
104   return CMD_FAILURE;
105 }
106
107 /* Verifies that none of the VAR_CNT variables in VARS are long
108    string variables. */
109 static int
110 verify_val_labs (struct variable **vars, size_t var_cnt)
111 {
112   size_t i;
113
114   for (i = 0; i < var_cnt; i++)
115     {
116       const struct variable *vp = vars[i];
117
118       if (var_is_long_string (vp))
119         {
120           msg (SE, _("It is not possible to assign value labels to long "
121                      "string variables such as %s."), var_get_name (vp));
122           return 0;
123         }
124     }
125   return 1;
126 }
127
128 /* Erases all the labels for the VAR_CNT variables in VARS. */
129 static void
130 erase_labels (struct variable **vars, size_t var_cnt)
131 {
132   size_t i;
133
134   /* Erase old value labels if desired. */
135   for (i = 0; i < var_cnt; i++)
136     var_clear_value_labels (vars[i]);
137 }
138
139 /* Parse all the labels for the VAR_CNT variables in VARS and add
140    the specified labels to those variables.  */
141 static int
142 get_label (struct lexer *lexer, struct variable **vars, size_t var_cnt)
143 {
144   /* Parse all the labels and add them to the variables. */
145   do
146     {
147       union value value;
148       struct string label;
149       size_t i;
150
151       /* Set value. */
152       if (var_is_alpha (vars[0]))
153         {
154           if (lex_token (lexer) != T_STRING)
155             {
156               lex_error (lexer, _("expecting string"));
157               return 0;
158             }
159           buf_copy_str_rpad (value.s, MAX_SHORT_STRING, ds_cstr (lex_tokstr (lexer)));
160         }
161       else
162         {
163           if (!lex_is_number (lexer))
164             {
165               lex_error (lexer, _("expecting integer"));
166               return 0;
167             }
168           if (!lex_is_integer (lexer))
169             msg (SW, _("Value label `%g' is not integer."), lex_tokval (lexer));
170           value.f = lex_tokval (lexer);
171         }
172       lex_get (lexer);
173       lex_match (lexer, ',');
174
175       /* Set label. */
176       if (!lex_force_string (lexer))
177         return 0;
178
179       ds_init_string (&label, lex_tokstr (lexer));
180
181       if (ds_length (&label) > 60)
182         {
183           msg (SW, _("Truncating value label to 60 characters."));
184           ds_truncate (&label, 60);
185         }
186
187       for (i = 0; i < var_cnt; i++)
188         var_replace_value_label (vars[i], &value, ds_cstr (&label));
189
190       ds_destroy (&label);
191
192       lex_get (lexer);
193       lex_match (lexer, ',');
194     }
195   while (lex_token (lexer) != '/' && lex_token (lexer) != '.');
196
197   return 1;
198 }