0dff0b02e32eb7058a614b0eacf21b9841a0cbc2
[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    Written by Ben Pfaff <blp@gnu.org>.
4
5    This program is free software; you can redistribute it and/or
6    modify it under the terms of the GNU General Public License as
7    published by the Free Software Foundation; either version 2 of the
8    License, or (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful, but
11    WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18    02110-1301, USA. */
19
20 #include <config.h>
21
22 #include <stdlib.h>
23
24 #include <data/dictionary.h>
25 #include <data/procedure.h>
26 #include <data/variable.h>
27 #include <language/command.h>
28 #include <language/lexer/lexer.h>
29 #include <language/lexer/variable-parser.h>
30 #include <libpspp/alloc.h>
31 #include <libpspp/hash.h>
32 #include <libpspp/message.h>
33 #include <libpspp/str.h>
34
35 #include "gettext.h"
36 #define _(msgid) gettext (msgid)
37
38 /* The code for this function is very similar to the code for the
39    RENAME subcommand of MODIFY VARS. */
40 int
41 cmd_rename_variables (struct lexer *lexer, struct dataset *ds)
42 {
43   struct variable **rename_vars = NULL;
44   char **rename_new_names = NULL;
45   size_t rename_cnt = 0;
46   char *err_name;
47
48   int status = CMD_CASCADING_FAILURE;
49
50   if (proc_make_temporary_transformations_permanent (ds))
51     msg (SE, _("RENAME VARS may not be used after TEMPORARY.  "
52                "Temporary transformations will be made permanent."));
53
54   do
55     {
56       size_t prev_nv_1 = rename_cnt;
57       size_t prev_nv_2 = rename_cnt;
58
59       if (!lex_match (lexer, '('))
60         {
61           msg (SE, _("`(' expected."));
62           goto lossage;
63         }
64       if (!parse_variables (lexer, dataset_dict (ds), &rename_vars, &rename_cnt,
65                             PV_APPEND | PV_NO_DUPLICATE))
66         goto lossage;
67       if (!lex_match (lexer, '='))
68         {
69           msg (SE, _("`=' expected between lists of new and old variable names."));
70           goto lossage;
71         }
72       if (!parse_DATA_LIST_vars (lexer, &rename_new_names, &prev_nv_1, PV_APPEND))
73         goto lossage;
74       if (prev_nv_1 != rename_cnt)
75         {
76           size_t i;
77
78           msg (SE, _("Differing number of variables in old name list "
79                      "(%u) and in new name list (%u)."),
80                (unsigned) rename_cnt - prev_nv_2,
81                (unsigned) prev_nv_1 - prev_nv_2);
82           for (i = 0; i < prev_nv_1; i++)
83             free (rename_new_names[i]);
84           free (rename_new_names);
85           rename_new_names = NULL;
86           goto lossage;
87         }
88       if (!lex_match (lexer, ')'))
89         {
90           msg (SE, _("`)' expected after variable names."));
91           goto lossage;
92         }
93     }
94   while (lex_token (lexer) != '.');
95
96   if (!dict_rename_vars (dataset_dict (ds),
97                          rename_vars, rename_new_names, rename_cnt,
98                          &err_name)) 
99     {
100       msg (SE, _("Renaming would duplicate variable name %s."), err_name);
101       goto lossage;
102     }
103
104   status = CMD_SUCCESS;
105
106  lossage:
107   free (rename_vars);
108   if (rename_new_names != NULL) 
109     {
110       size_t i;
111       for (i = 0; i < rename_cnt; i++)
112         free (rename_new_names[i]);
113       free (rename_new_names); 
114     }
115   return status;
116 }