1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2010, 2011, 2015, 2016 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/>. */
21 #include "data/dataset.h"
22 #include "data/dictionary.h"
23 #include "data/variable.h"
24 #include "language/command.h"
25 #include "language/lexer/lexer.h"
26 #include "language/lexer/variable-parser.h"
27 #include "libpspp/message.h"
28 #include "libpspp/str.h"
30 #include "gl/xalloc.h"
33 #define _(msgid) gettext (msgid)
36 cmd_rename_variables (struct lexer *lexer, struct dataset *ds)
38 struct variable **vars_to_be_renamed = NULL;
39 size_t n_vars_to_be_renamed = 0;
41 char **new_names = NULL;
42 size_t n_new_names = 0;
46 int status = CMD_CASCADING_FAILURE;
48 if (proc_make_temporary_transformations_permanent (ds))
49 lex_ofs_error (lexer, 0, lex_ofs (lexer) - 1,
50 _("%s may not be used after %s. "
51 "Temporary transformations will be made permanent."),
52 "RENAME VARS", "TEMPORARY");
56 int opts = PV_APPEND | PV_NO_DUPLICATE;
58 if (!lex_match (lexer, T_LPAREN))
60 int start_ofs = lex_ofs (lexer);
61 if (!parse_variables (lexer, dataset_dict (ds),
62 &vars_to_be_renamed, &n_vars_to_be_renamed, opts))
66 if (!lex_force_match (lexer, T_EQUALS))
70 if (!parse_DATA_LIST_vars (lexer, dataset_dict (ds),
71 &new_names, &n_new_names, opts))
75 int end_ofs = lex_ofs (lexer) - 1;
76 if (n_new_names != n_vars_to_be_renamed)
78 lex_ofs_error (lexer, start_ofs, end_ofs,
79 _("Differing number of variables in old name list "
80 "(%zu) and in new name list (%zu)."),
81 n_vars_to_be_renamed, n_new_names);
84 if (!(opts & PV_SINGLE) && !lex_force_match (lexer, T_RPAREN))
89 while (lex_token (lexer) != T_ENDCMD);
91 if (!dict_rename_vars (dataset_dict (ds),
92 vars_to_be_renamed, new_names, n_new_names,
95 lex_ofs_error (lexer, 2, lex_ofs (lexer) - 1,
96 _("Renaming would duplicate variable name %s."), err_name);
100 status = CMD_SUCCESS;
103 free (vars_to_be_renamed);
104 if (new_names != NULL)
107 for (i = 0; i < n_new_names; ++i)