c68474398ba677ac673960d5640b686cb6a51d79
[pspp-builds.git] / src / language / dictionary / rename-variables.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3
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.
8
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.
13
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
17    02110-1301, USA. */
18
19 #include <config.h>
20
21 #include <stdlib.h>
22
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>
33
34 #include "gettext.h"
35 #define _(msgid) gettext (msgid)
36
37 /* The code for this function is very similar to the code for the
38    RENAME subcommand of MODIFY VARS. */
39 int
40 cmd_rename_variables (struct lexer *lexer, struct dataset *ds)
41 {
42   struct variable **rename_vars = NULL;
43   char **rename_new_names = NULL;
44   size_t rename_cnt = 0;
45   char *err_name;
46
47   int status = CMD_CASCADING_FAILURE;
48
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."));
52
53   do
54     {
55       size_t prev_nv_1 = rename_cnt;
56       size_t prev_nv_2 = rename_cnt;
57
58       if (!lex_match (lexer, '('))
59         {
60           msg (SE, _("`(' expected."));
61           goto lossage;
62         }
63       if (!parse_variables (lexer, dataset_dict (ds), &rename_vars, &rename_cnt,
64                             PV_APPEND | PV_NO_DUPLICATE))
65         goto lossage;
66       if (!lex_match (lexer, '='))
67         {
68           msg (SE, _("`=' expected between lists of new and old variable names."));
69           goto lossage;
70         }
71       if (!parse_DATA_LIST_vars (lexer, &rename_new_names, &prev_nv_1, PV_APPEND))
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                      "(%d) and in new name list (%d)."),
79                (int) (rename_cnt - prev_nv_2),
80                (int) (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;
85           goto lossage;
86         }
87       if (!lex_match (lexer, ')'))
88         {
89           msg (SE, _("`)' expected after variable names."));
90           goto lossage;
91         }
92     }
93   while (lex_token (lexer) != '.');
94
95   if (!dict_rename_vars (dataset_dict (ds),
96                          rename_vars, rename_new_names, rename_cnt,
97                          &err_name))
98     {
99       msg (SE, _("Renaming would duplicate variable name %s."), err_name);
100       goto lossage;
101     }
102
103   status = CMD_SUCCESS;
104
105  lossage:
106   free (rename_vars);
107   if (rename_new_names != NULL)
108     {
109       size_t i;
110       for (i = 0; i < rename_cnt; i++)
111         free (rename_new_names[i]);
112       free (rename_new_names);
113     }
114   return status;
115 }