1 /* PSPP - computes sample statistics.
2 Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 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, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 #include <data/dictionary.h>
24 #include <data/procedure.h>
25 #include <data/variable.h>
26 #include <language/command.h>
27 #include <language/lexer/lexer.h>
28 #include <language/lexer/variable-parser.h>
29 #include <libpspp/alloc.h>
30 #include <libpspp/hash.h>
31 #include <libpspp/message.h>
32 #include <libpspp/str.h>
35 #define _(msgid) gettext (msgid)
37 /* The code for this function is very similar to the code for the
38 RENAME subcommand of MODIFY VARS. */
40 cmd_rename_variables (struct lexer *lexer, struct dataset *ds)
42 struct variable **rename_vars = NULL;
43 char **rename_new_names = NULL;
44 size_t rename_cnt = 0;
47 int status = CMD_CASCADING_FAILURE;
49 if (proc_make_temporary_transformations_permanent (ds))
50 msg (SE, _("RENAME VARS may not be used after TEMPORARY. "
51 "Temporary transformations will be made permanent."));
55 size_t prev_nv_1 = rename_cnt;
56 size_t prev_nv_2 = rename_cnt;
58 if (!lex_match (lexer, '('))
60 msg (SE, _("`(' expected."));
63 if (!parse_variables (lexer, dataset_dict (ds), &rename_vars, &rename_cnt,
64 PV_APPEND | PV_NO_DUPLICATE))
66 if (!lex_match (lexer, '='))
68 msg (SE, _("`=' expected between lists of new and old variable names."));
71 if (!parse_DATA_LIST_vars (lexer, &rename_new_names, &prev_nv_1, PV_APPEND))
73 if (prev_nv_1 != rename_cnt)
77 msg (SE, _("Differing number of variables in old name list "
78 "(%u) and in new name list (%u)."),
79 (unsigned) rename_cnt - prev_nv_2,
80 (unsigned) prev_nv_1 - prev_nv_2);
81 for (i = 0; i < prev_nv_1; i++)
82 free (rename_new_names[i]);
83 free (rename_new_names);
84 rename_new_names = NULL;
87 if (!lex_match (lexer, ')'))
89 msg (SE, _("`)' expected after variable names."));
93 while (lex_token (lexer) != '.');
95 if (!dict_rename_vars (dataset_dict (ds),
96 rename_vars, rename_new_names, rename_cnt,
99 msg (SE, _("Renaming would duplicate variable name %s."), err_name);
103 status = CMD_SUCCESS;
107 if (rename_new_names != NULL)
110 for (i = 0; i < rename_cnt; i++)
111 free (rename_new_names[i]);
112 free (rename_new_names);