1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2009 Free Software Foundation, Inc.
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.
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.
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/>. */
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/hash.h>
29 #include <libpspp/message.h>
30 #include <libpspp/str.h>
35 #define _(msgid) gettext (msgid)
39 static int do_value_labels (struct lexer *,
40 const struct dictionary *dict, bool);
41 static int verify_val_labs (struct variable **vars, size_t var_cnt);
42 static void erase_labels (struct variable **vars, size_t var_cnt);
43 static int get_label (struct lexer *, struct variable **vars, size_t var_cnt);
48 cmd_value_labels (struct lexer *lexer, struct dataset *ds)
50 return do_value_labels (lexer, dataset_dict (ds), true);
54 cmd_add_value_labels (struct lexer *lexer, struct dataset *ds)
56 return do_value_labels (lexer, dataset_dict (ds), false);
62 do_value_labels (struct lexer *lexer, const struct dictionary *dict, bool erase)
64 struct variable **vars; /* Variable list. */
65 size_t var_cnt; /* Number of variables. */
66 int parse_err=0; /* true if error parsing variables */
68 lex_match (lexer, '/');
70 while (lex_token (lexer) != '.')
72 parse_err = !parse_variables (lexer, dict, &vars, &var_cnt,
79 if (!verify_val_labs (vars, var_cnt))
82 erase_labels (vars, var_cnt);
83 while (lex_token (lexer) != '/' && lex_token (lexer) != '.')
84 if (!get_label (lexer, vars, var_cnt))
87 if (lex_token (lexer) != '/')
101 return lex_end_of_command (lexer);
108 /* Verifies that none of the VAR_CNT variables in VARS are long
111 verify_val_labs (struct variable **vars, size_t var_cnt)
115 for (i = 0; i < var_cnt; i++)
117 const struct variable *vp = vars[i];
119 if (var_is_long_string (vp))
121 msg (SE, _("It is not possible to assign value labels to long "
122 "string variables such as %s."), var_get_name (vp));
129 /* Erases all the labels for the VAR_CNT variables in VARS. */
131 erase_labels (struct variable **vars, size_t var_cnt)
135 /* Erase old value labels if desired. */
136 for (i = 0; i < var_cnt; i++)
137 var_clear_value_labels (vars[i]);
140 /* Parse all the labels for the VAR_CNT variables in VARS and add
141 the specified labels to those variables. */
143 get_label (struct lexer *lexer, struct variable **vars, size_t var_cnt)
145 /* Parse all the labels and add them to the variables. */
154 if (var_is_alpha (vars[0]))
156 if (lex_token (lexer) != T_STRING)
158 lex_error (lexer, _("expecting string"));
161 width = MAX_SHORT_STRING;
162 value_init (&value, width);
163 buf_copy_str_rpad (value_str_rw (&value, width), width,
164 ds_cstr (lex_tokstr (lexer)), ' ');
168 if (!lex_is_number (lexer))
170 lex_error (lexer, _("expecting number"));
174 value_init (&value, width);
175 value.f = lex_tokval (lexer);
178 lex_match (lexer, ',');
181 if (!lex_force_string (lexer))
183 value_destroy (&value, width);
187 ds_init_string (&label, lex_tokstr (lexer));
189 if (ds_length (&label) > 60)
191 msg (SW, _("Truncating value label to 60 characters."));
192 ds_truncate (&label, 60);
195 for (i = 0; i < var_cnt; i++)
196 var_replace_value_label (vars[i], &value, ds_cstr (&label));
199 value_destroy (&value, width);
202 lex_match (lexer, ',');
204 while (lex_token (lexer) != '/' && lex_token (lexer) != '.');