checkin of 0.3.0
[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 "avl.h"
25 #include "command.h"
26 #include "error.h"
27 #include "lexer.h"
28 #include "str.h"
29 #include "var.h"
30
31 /* FIXME: should change weighting variable, etc. */
32 static int compare_name (const void *, const void *);
33
34 /* The code for this function is very similar to the code for the
35    RENAME subcommand of MODIFY VARS. */
36 int
37 cmd_rename_variables (void)
38 {
39   char (*names)[8] = NULL;
40
41   struct variable **old_names = NULL;
42   char **new_names = NULL;
43   int n_rename = 0;
44
45   struct variable *head, *tail, *iter;
46
47   int i;
48
49   lex_match_id ("RENAME");
50   lex_match_id ("VARIABLES");
51
52   do
53     {
54       int prev_nv_1 = n_rename;
55       int prev_nv_2 = n_rename;
56
57       if (!lex_match ('('))
58         {
59           msg (SE, _("`(' expected."));
60           goto lossage;
61         }
62       if (!parse_variables (&default_dict, &old_names, &n_rename,
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 (&new_names, &prev_nv_1, PV_APPEND))
71         goto lossage;
72       if (prev_nv_1 != n_rename)
73         {
74           msg (SE, _("Differing number of variables in old name list "
75                "(%d) and in new name list (%d)."),
76                n_rename - prev_nv_2, prev_nv_1 - prev_nv_2);
77           for (i = 0; i < prev_nv_1; i++)
78             free (new_names[i]);
79           free (new_names);
80           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   /* Form a linked list of the variables to be renamed; also, set
92      their p.mfv.new_name members. */
93   head = NULL;
94   for (i = 0; i < n_rename; i++)
95     {
96       strcpy (old_names[i]->p.mfv.new_name, new_names[i]);
97       free (new_names[i]);
98       if (head != NULL)
99         tail = tail->p.mfv.next = old_names[i];
100       else
101         head = tail = old_names[i];
102     }
103   tail->p.mfv.next = NULL;
104   free (new_names);
105   free (old_names);
106   new_names = NULL;
107   old_names = NULL;
108
109   /* Construct a vector of all variables' new names. */
110   names = xmalloc (8 * default_dict.nvar);
111   for (i = 0; i < default_dict.nvar; i++)
112     strncpy (names[i], default_dict.var[i]->name, 8);
113   for (iter = head; iter; iter = iter->p.mfv.next)
114     strncpy (names[iter->index], iter->p.mfv.new_name, 8);
115
116   /* Sort the vector, then check for duplicates. */
117   qsort (names, default_dict.nvar, 8, compare_name);
118   for (i = 1; i < default_dict.nvar; i++)
119     if (memcmp (names[i], names[i - 1], 8) == 0)
120       {
121         char name[9];
122         strncpy (name, names[i], 8);
123         name[8] = 0;
124         msg (SE, _("Duplicate variable name `%s' after renaming."), name);
125         goto lossage;
126       }
127   free (names);
128
129   /* Finally, do the renaming. */
130   for (iter = head; iter; iter = iter->p.mfv.next)
131     avl_force_delete (default_dict.var_by_name, iter);
132   for (iter = head; iter; iter = iter->p.mfv.next)
133     {
134       strcpy (iter->name, iter->p.mfv.new_name);
135       avl_force_insert (default_dict.var_by_name, iter);
136     }
137
138   return CMD_SUCCESS;
139
140 lossage:
141   if (new_names)
142     for (i = 0; i < n_rename; i++)
143       free (new_names[i]);
144   free (new_names);
145   free (old_names);
146   free (names);
147   return CMD_FAILURE;
148 }
149
150 static int
151 compare_name (const void *a, const void *b)
152 {
153   return memcmp (a, b, 8);
154 }