c1d31996bacfa453b289cfeaa73b1488ceab2bf5
[pspp-builds.git] / src / language / dictionary / rename-variables.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2010, 2011 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/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/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 **rename_vars = NULL;
41   char **rename_new_names = NULL;
42   size_t rename_cnt = 0;
43   char *err_name;
44
45   int status = CMD_CASCADING_FAILURE;
46
47   if (proc_make_temporary_transformations_permanent (ds))
48     msg (SE, _("RENAME VARS may not be used after TEMPORARY.  "
49                "Temporary transformations will be made permanent."));
50
51   do
52     {
53       size_t prev_nv_1 = rename_cnt;
54       size_t prev_nv_2 = rename_cnt;
55
56       if (!lex_match (lexer, T_LPAREN))
57         {
58           msg (SE, _("`(' expected."));
59           goto lossage;
60         }
61       if (!parse_variables (lexer, dataset_dict (ds), &rename_vars, &rename_cnt,
62                             PV_APPEND | PV_NO_DUPLICATE))
63         goto lossage;
64       if (!lex_match (lexer, T_EQUALS))
65         {
66           msg (SE, _("`=' expected between lists of new and old variable names."));
67           goto lossage;
68         }
69       if (!parse_DATA_LIST_vars (lexer, dataset_dict (ds),
70                                  &rename_new_names, &prev_nv_1,
71                                  PV_APPEND | PV_NO_DUPLICATE))
72         goto lossage;
73       if (prev_nv_1 != rename_cnt)
74         {
75           size_t i;
76
77           msg (SE, _("Differing number of variables in old name list "
78                      "(%zu) and in new name list (%zu)."),
79                rename_cnt - prev_nv_2, prev_nv_1 - prev_nv_2);
80           for (i = 0; i < prev_nv_1; i++)
81             free (rename_new_names[i]);
82           free (rename_new_names);
83           rename_new_names = NULL;
84           goto lossage;
85         }
86       if (!lex_match (lexer, T_RPAREN))
87         {
88           msg (SE, _("`)' expected after variable names."));
89           goto lossage;
90         }
91     }
92   while (lex_token (lexer) != T_ENDCMD);
93
94   if (!dict_rename_vars (dataset_dict (ds),
95                          rename_vars, rename_new_names, rename_cnt,
96                          &err_name))
97     {
98       msg (SE, _("Renaming would duplicate variable name %s."), err_name);
99       goto lossage;
100     }
101
102   status = CMD_SUCCESS;
103
104  lossage:
105   free (rename_vars);
106   if (rename_new_names != NULL)
107     {
108       size_t i;
109       for (i = 0; i < rename_cnt; i++)
110         free (rename_new_names[i]);
111       free (rename_new_names);
112     }
113   return status;
114 }