1 /* PSPP - computes sample statistics.
2 Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3 Written by Ben Pfaff <blp@gnu.org>.
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.
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.
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
25 #include <data/procedure.h>
26 #include <data/value-labels.h>
27 #include <data/variable.h>
28 #include <language/command.h>
29 #include <language/lexer/lexer.h>
30 #include <language/lexer/variable-parser.h>
31 #include <libpspp/alloc.h>
32 #include <libpspp/hash.h>
33 #include <libpspp/message.h>
34 #include <libpspp/str.h>
37 #define _(msgid) gettext (msgid)
41 static int do_value_labels (struct lexer *,
42 const struct dictionary *dict, int);
43 static int verify_val_labs (struct variable **vars, size_t var_cnt);
44 static void erase_labels (struct variable **vars, size_t var_cnt);
45 static int get_label (struct lexer *, struct variable **vars, size_t var_cnt);
50 cmd_value_labels (struct lexer *lexer, struct dataset *ds)
52 return do_value_labels (lexer, dataset_dict (ds), 1);
56 cmd_add_value_labels (struct lexer *lexer, struct dataset *ds)
58 return do_value_labels (lexer, dataset_dict (ds), 0);
64 do_value_labels (struct lexer *lexer, const struct dictionary *dict, int erase)
66 struct variable **vars; /* Variable list. */
67 size_t var_cnt; /* Number of variables. */
68 int parse_err=0; /* true if error parsing variables */
70 lex_match (lexer, '/');
72 while (lex_token (lexer) != '.')
74 parse_err = !parse_variables (lexer, dict, &vars, &var_cnt,
81 if (!verify_val_labs (vars, var_cnt))
84 erase_labels (vars, var_cnt);
85 while (lex_token (lexer) != '/' && lex_token (lexer) != '.')
86 if (!get_label (lexer, vars, var_cnt))
89 if (lex_token (lexer) != '/')
103 return lex_end_of_command (lexer);
110 /* Verifies that none of the VAR_CNT variables in VARS are long
113 verify_val_labs (struct variable **vars, size_t var_cnt)
117 for (i = 0; i < var_cnt; i++)
119 struct variable *vp = vars[i];
121 if (vp->type == ALPHA && vp->width > MAX_SHORT_STRING)
123 msg (SE, _("It is not possible to assign value labels to long "
124 "string variables such as %s."), vp->name);
131 /* Erases all the labels for the VAR_CNT variables in VARS. */
133 erase_labels (struct variable **vars, size_t var_cnt)
137 /* Erase old value labels if desired. */
138 for (i = 0; i < var_cnt; i++)
139 val_labs_clear (vars[i]->val_labs);
142 /* Parse all the labels for the VAR_CNT variables in VARS and add
143 the specified labels to those variables. */
145 get_label (struct lexer *lexer, struct variable **vars, size_t var_cnt)
147 /* Parse all the labels and add them to the variables. */
155 if (vars[0]->type == ALPHA)
157 if (lex_token (lexer) != T_STRING)
159 lex_error (lexer, _("expecting string"));
162 buf_copy_str_rpad (value.s, MAX_SHORT_STRING, ds_cstr (lex_tokstr (lexer)));
166 if (!lex_is_number (lexer))
168 lex_error (lexer, _("expecting integer"));
171 if (!lex_is_integer (lexer))
172 msg (SW, _("Value label `%g' is not integer."), lex_tokval (lexer));
173 value.f = lex_tokval (lexer);
176 lex_match (lexer, ',');
179 if (!lex_force_string (lexer))
182 ds_init_string (&label, lex_tokstr (lexer));
184 if (ds_length (&label) > 60)
186 msg (SW, _("Truncating value label to 60 characters."));
187 ds_truncate (&label, 60);
190 for (i = 0; i < var_cnt; i++)
191 val_labs_replace (vars[i]->val_labs, value, ds_cstr (&label));
196 lex_match (lexer, ',');
198 while (lex_token (lexer) != '/' && lex_token (lexer) != '.');