Changed DFM from open-at-first-access to explicit-open. Before,
[pspp-builds.git] / src / rename-vars.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., 59 Temple Place - Suite 330, Boston, MA
18    02111-1307, USA. */
19
20 #include <config.h>
21 #include <stdlib.h>
22 #include <assert.h>
23 #include "alloc.h"
24 #include "command.h"
25 #include "error.h"
26 #include "hash.h"
27 #include "lexer.h"
28 #include "str.h"
29 #include "var.h"
30
31 /* The code for this function is very similar to the code for the
32    RENAME subcommand of MODIFY VARS. */
33 int
34 cmd_rename_variables (void)
35 {
36   struct variable **rename_vars = NULL;
37   char **rename_new_names = NULL;
38   int rename_cnt = 0;
39   char *err_name;
40
41   int status = CMD_FAILURE;
42
43   int i;
44
45   if (temporary != 0)
46     {
47       msg (SE, _("RENAME VARS may not be used after TEMPORARY.  "
48                  "Temporary transformations will be made permanent."));
49       cancel_temporary (); 
50     }
51
52   do
53     {
54       int prev_nv_1 = rename_cnt;
55       int prev_nv_2 = rename_cnt;
56
57       if (!lex_match ('('))
58         {
59           msg (SE, _("`(' expected."));
60           goto lossage;
61         }
62       if (!parse_variables (default_dict, &rename_vars, &rename_cnt,
63                             PV_APPEND | PV_NO_DUPLICATE))
64         goto lossage;
65       if (!lex_match ('='))
66         {
67           msg (SE, _("`=' expected between lists of new and old variable names."));
68           goto lossage;
69         }
70       if (!parse_DATA_LIST_vars (&rename_new_names, &prev_nv_1, PV_APPEND))
71         goto lossage;
72       if (prev_nv_1 != rename_cnt)
73         {
74           msg (SE, _("Differing number of variables in old name list "
75                "(%d) and in new name list (%d)."),
76                rename_cnt - prev_nv_2, prev_nv_1 - prev_nv_2);
77           for (i = 0; i < prev_nv_1; i++)
78             free (rename_new_names[i]);
79           free (rename_new_names);
80           rename_new_names = NULL;
81           goto lossage;
82         }
83       if (!lex_match (')'))
84         {
85           msg (SE, _("`)' expected after variable names."));
86           goto lossage;
87         }
88     }
89   while (token != '.');
90
91   if (!dict_rename_vars (default_dict,
92                          rename_vars, rename_new_names, rename_cnt,
93                          &err_name)) 
94     {
95       msg (SE, _("Renaming would duplicate variable name %s."), err_name);
96       goto lossage;
97     }
98
99   status = CMD_SUCCESS;
100
101  lossage:
102   free (rename_vars);
103   if (rename_new_names != NULL) 
104     {
105       for (i = 0; i < rename_cnt; i++)
106         free (rename_new_names[i]);
107       free (rename_new_names); 
108     }
109   return status;
110 }