94331c4afe86d383db7aa3f90d4f861e762861e0
[pspp] / src / language / dictionary / rename-variables.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2010, 2011, 2015, 2016 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>. */
16
17 #include <config.h>
18
19 #include <stdlib.h>
20
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"
29
30 #include "gl/xalloc.h"
31
32 #include "gettext.h"
33 #define _(msgid) gettext (msgid)
34
35 /* The code for this function is very similar to the code for the
36    RENAME subcommand of MODIFY VARS. */
37 int
38 cmd_rename_variables (struct lexer *lexer, struct dataset *ds)
39 {
40   struct variable **vars_to_be_renamed = NULL;
41   size_t n_vars_to_be_renamed = 0;
42
43   char **new_names = NULL;
44   size_t n_new_names = 0;
45
46   char *err_name;
47
48   int status = CMD_CASCADING_FAILURE;
49
50   if (proc_make_temporary_transformations_permanent (ds))
51     lex_ofs_error (lexer, 0, lex_ofs (lexer) - 1,
52                    _("%s may not be used after %s.  "
53                      "Temporary transformations will be made permanent."),
54                    "RENAME VARS", "TEMPORARY");
55
56   do
57     {
58       int opts = PV_APPEND | PV_NO_DUPLICATE;
59
60       if (!lex_match (lexer, T_LPAREN))
61         opts |= PV_SINGLE;
62       if (!parse_variables (lexer, dataset_dict (ds),
63                             &vars_to_be_renamed, &n_vars_to_be_renamed, opts))
64         {
65           goto lossage;
66         }
67       if (!lex_force_match (lexer, T_EQUALS))
68         {
69           goto lossage;
70         }
71       if (!parse_DATA_LIST_vars (lexer, dataset_dict (ds),
72                                  &new_names, &n_new_names, opts))
73         {
74           goto lossage;
75         }
76       if (n_new_names != n_vars_to_be_renamed)
77         {
78           msg (SE, _("Differing number of variables in old name list "
79                      "(%zu) and in new name list (%zu)."),
80                n_vars_to_be_renamed, n_new_names);
81           goto lossage;
82         }
83       if (!(opts & PV_SINGLE) && !lex_force_match (lexer, T_RPAREN))
84         {
85           goto lossage;
86         }
87     }
88   while (lex_token (lexer) != T_ENDCMD);
89
90   if (!dict_rename_vars (dataset_dict (ds),
91                          vars_to_be_renamed, new_names, n_new_names,
92                          &err_name))
93     {
94       msg (SE, _("Renaming would duplicate variable name %s."), err_name);
95       goto lossage;
96     }
97
98   status = CMD_SUCCESS;
99
100  lossage:
101   free (vars_to_be_renamed);
102   if (new_names != NULL)
103     {
104       size_t i;
105       for (i = 0; i < n_new_names; ++i)
106         free (new_names[i]);
107       free (new_names);
108     }
109   return status;
110 }