1 /* PSPP - computes sample statistics.
2 Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3 Written by Ben Pfaff <blp@gnu.org>.
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.
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.
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
31 /* FIXME: should change weighting variable, etc. */
32 static int compare_name (const void *, const void *);
34 /* The code for this function is very similar to the code for the
35 RENAME subcommand of MODIFY VARS. */
37 cmd_rename_variables (void)
39 char (*names)[8] = NULL;
41 struct variable **old_names = NULL;
42 char **new_names = NULL;
45 struct variable *head, *tail, *iter;
49 lex_match_id ("RENAME");
50 lex_match_id ("VARIABLES");
54 int prev_nv_1 = n_rename;
55 int prev_nv_2 = n_rename;
59 msg (SE, _("`(' expected."));
62 if (!parse_variables (&default_dict, &old_names, &n_rename,
63 PV_APPEND | PV_NO_DUPLICATE))
67 msg (SE, _("`=' expected between lists of new and old variable names."));
70 if (!parse_DATA_LIST_vars (&new_names, &prev_nv_1, PV_APPEND))
72 if (prev_nv_1 != n_rename)
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++)
85 msg (SE, _("`)' expected after variable names."));
91 /* Form a linked list of the variables to be renamed; also, set
92 their p.mfv.new_name members. */
94 for (i = 0; i < n_rename; i++)
96 strcpy (old_names[i]->p.mfv.new_name, new_names[i]);
99 tail = tail->p.mfv.next = old_names[i];
101 head = tail = old_names[i];
103 tail->p.mfv.next = NULL;
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);
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)
122 strncpy (name, names[i], 8);
124 msg (SE, _("Duplicate variable name `%s' after renaming."), name);
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)
134 strcpy (iter->name, iter->p.mfv.new_name);
135 avl_force_insert (default_dict.var_by_name, iter);
142 for (i = 0; i < n_rename; i++)
151 compare_name (const void *a, const void *b)
153 return memcmp (a, b, 8);