1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000 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/dictionary.h>
22 #include <data/procedure.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/hash.h>
28 #include <libpspp/message.h>
29 #include <libpspp/str.h>
34 #define _(msgid) gettext (msgid)
36 /* The code for this function is very similar to the code for the
37 RENAME subcommand of MODIFY VARS. */
39 cmd_rename_variables (struct lexer *lexer, struct dataset *ds)
41 struct variable **rename_vars = NULL;
42 char **rename_new_names = NULL;
43 size_t rename_cnt = 0;
46 int status = CMD_CASCADING_FAILURE;
48 if (proc_make_temporary_transformations_permanent (ds))
49 msg (SE, _("RENAME VARS may not be used after TEMPORARY. "
50 "Temporary transformations will be made permanent."));
54 size_t prev_nv_1 = rename_cnt;
55 size_t prev_nv_2 = rename_cnt;
57 if (!lex_match (lexer, '('))
59 msg (SE, _("`(' expected."));
62 if (!parse_variables (lexer, dataset_dict (ds), &rename_vars, &rename_cnt,
63 PV_APPEND | PV_NO_DUPLICATE))
65 if (!lex_match (lexer, '='))
67 msg (SE, _("`=' expected between lists of new and old variable names."));
70 if (!parse_DATA_LIST_vars (lexer, &rename_new_names, &prev_nv_1, PV_APPEND))
72 if (prev_nv_1 != rename_cnt)
76 msg (SE, _("Differing number of variables in old name list "
77 "(%zu) and in new name list (%zu)."),
78 rename_cnt - prev_nv_2, prev_nv_1 - prev_nv_2);
79 for (i = 0; i < prev_nv_1; i++)
80 free (rename_new_names[i]);
81 free (rename_new_names);
82 rename_new_names = NULL;
85 if (!lex_match (lexer, ')'))
87 msg (SE, _("`)' expected after variable names."));
91 while (lex_token (lexer) != '.');
93 if (!dict_rename_vars (dataset_dict (ds),
94 rename_vars, rename_new_names, rename_cnt,
97 msg (SE, _("Renaming would duplicate variable name %s."), err_name);
101 status = CMD_SUCCESS;
105 if (rename_new_names != NULL)
108 for (i = 0; i < rename_cnt; i++)
109 free (rename_new_names[i]);
110 free (rename_new_names);