Adopt use of gnulib for portability.
[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., 51 Franklin Street, Fifth Floor, Boston, MA
18    02110-1301, USA. */
19
20 #include <config.h>
21 #include <stdlib.h>
22 #include "error.h"
23 #include "alloc.h"
24 #include "command.h"
25 #include "dictionary.h"
26 #include "error.h"
27 #include "hash.h"
28 #include "lexer.h"
29 #include "str.h"
30 #include "var.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 (void)
39 {
40   struct variable **rename_vars = NULL;
41   char **rename_new_names = NULL;
42   int rename_cnt = 0;
43   char *err_name;
44
45   int status = CMD_FAILURE;
46
47   int i;
48
49   if (temporary != 0)
50     {
51       msg (SE, _("RENAME VARS may not be used after TEMPORARY.  "
52                  "Temporary transformations will be made permanent."));
53       cancel_temporary (); 
54     }
55
56   do
57     {
58       int prev_nv_1 = rename_cnt;
59       int prev_nv_2 = rename_cnt;
60
61       if (!lex_match ('('))
62         {
63           msg (SE, _("`(' expected."));
64           goto lossage;
65         }
66       if (!parse_variables (default_dict, &rename_vars, &rename_cnt,
67                             PV_APPEND | PV_NO_DUPLICATE))
68         goto lossage;
69       if (!lex_match ('='))
70         {
71           msg (SE, _("`=' expected between lists of new and old variable names."));
72           goto lossage;
73         }
74       if (!parse_DATA_LIST_vars (&rename_new_names, &prev_nv_1, PV_APPEND))
75         goto lossage;
76       if (prev_nv_1 != rename_cnt)
77         {
78           msg (SE, _("Differing number of variables in old name list "
79                "(%d) and in new name list (%d)."),
80                rename_cnt - prev_nv_2, 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 (')'))
88         {
89           msg (SE, _("`)' expected after variable names."));
90           goto lossage;
91         }
92     }
93   while (token != '.');
94
95   if (!dict_rename_vars (default_dict,
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       for (i = 0; i < rename_cnt; i++)
110         free (rename_new_names[i]);
111       free (rename_new_names); 
112     }
113   return status;
114 }