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., 51 Franklin Street, Fifth Floor, Boston, MA
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/assertion.h>
32 #include <libpspp/array.h>
33 #include <libpspp/bit-vector.h>
34 #include <libpspp/compiler.h>
35 #include <libpspp/hash.h>
36 #include <libpspp/message.h>
37 #include <libpspp/message.h>
38 #include <libpspp/misc.h>
39 #include <libpspp/str.h>
42 #define _(msgid) gettext (msgid)
44 /* FIXME: should change weighting variable, etc. */
45 /* These control the ordering produced by
46 compare_variables_given_ordering(). */
49 int forward; /* 1=FORWARD, 0=BACKWARD. */
50 int positional; /* 1=POSITIONAL, 0=ALPHA. */
53 /* Increasing order of variable index. */
54 static struct ordering forward_positional_ordering = {1, 1};
56 static int compare_variables_given_ordering (const void *, const void *,
57 const void *ordering);
59 /* Explains how to modify the variables in a dictionary. */
60 struct var_modification
62 /* New variable ordering. */
63 struct variable **reorder_vars;
66 /* DROP/KEEP information. */
67 struct variable **drop_vars;
70 /* New variable names. */
71 struct variable **rename_vars;
76 static bool rearrange_dict (struct dictionary *d,
77 const struct var_modification *vm);
79 /* Performs MODIFY VARS command. */
81 cmd_modify_vars (struct lexer *lexer, struct dataset *ds)
83 /* Bits indicated whether we've already encountered a subcommand of
85 unsigned already_encountered = 0;
87 /* What we're gonna do to the active file. */
88 struct var_modification vm;
91 int ret_code = CMD_CASCADING_FAILURE;
95 if (proc_make_temporary_transformations_permanent (ds))
96 msg (SE, _("MODIFY VARS may not be used after TEMPORARY. "
97 "Temporary transformations will be made permanent."));
99 vm.reorder_vars = NULL;
101 vm.rename_vars = NULL;
107 /* Parse each subcommand. */
108 lex_match (lexer, '/');
111 if (lex_match_id (lexer, "REORDER"))
113 struct variable **v = NULL;
116 if (already_encountered & 1)
118 msg (SE, _("REORDER subcommand may be given at most once."));
121 already_encountered |= 1;
123 lex_match (lexer, '=');
126 struct ordering ordering;
129 ordering.forward = ordering.positional = 1;
130 if (lex_match_id (lexer, "FORWARD"));
131 else if (lex_match_id (lexer, "BACKWARD"))
132 ordering.forward = 0;
133 if (lex_match_id (lexer, "POSITIONAL"));
134 else if (lex_match_id (lexer, "ALPHA"))
135 ordering.positional = 0;
137 if (lex_match (lexer, T_ALL) || lex_token (lexer) == '/' || lex_token (lexer) == '.')
141 msg (SE, _("Cannot specify ALL after specifying a set "
145 dict_get_vars (dataset_dict (ds), &v, &nv, 1u << DC_SYSTEM);
149 if (!lex_match (lexer, '('))
151 msg (SE, _("`(' expected on REORDER subcommand."));
155 if (!parse_variables (lexer, dataset_dict (ds), &v, &nv,
156 PV_APPEND | PV_NO_DUPLICATE))
161 if (!lex_match (lexer, ')'))
163 msg (SE, _("`)' expected following variable names on "
164 "REORDER subcommand."));
169 sort (&v[prev_nv], nv - prev_nv, sizeof *v,
170 compare_variables_given_ordering, &ordering);
172 while (lex_token (lexer) != '/' && lex_token (lexer) != '.');
177 else if (lex_match_id (lexer, "RENAME"))
179 if (already_encountered & 2)
181 msg (SE, _("RENAME subcommand may be given at most once."));
184 already_encountered |= 2;
186 lex_match (lexer, '=');
189 size_t prev_nv_1 = vm.rename_cnt;
190 size_t prev_nv_2 = vm.rename_cnt;
192 if (!lex_match (lexer, '('))
194 msg (SE, _("`(' expected on RENAME subcommand."));
197 if (!parse_variables (lexer, dataset_dict (ds),
198 &vm.rename_vars, &vm.rename_cnt,
199 PV_APPEND | PV_NO_DUPLICATE))
201 if (!lex_match (lexer, '='))
203 msg (SE, _("`=' expected between lists of new and old variable "
204 "names on RENAME subcommand."));
207 if (!parse_DATA_LIST_vars (lexer, &vm.new_names,
208 &prev_nv_1, PV_APPEND))
210 if (prev_nv_1 != vm.rename_cnt)
212 msg (SE, _("Differing number of variables in old name list "
213 "(%d) and in new name list (%d)."),
214 vm.rename_cnt - prev_nv_2, prev_nv_1 - prev_nv_2);
215 for (i = 0; i < prev_nv_1; i++)
216 free (vm.new_names[i]);
221 if (!lex_match (lexer, ')'))
223 msg (SE, _("`)' expected after variable lists on RENAME "
228 while (lex_token (lexer) != '.' && lex_token (lexer) != '/');
230 else if (lex_match_id (lexer, "KEEP"))
232 struct variable **keep_vars, **all_vars, **drop_vars;
233 size_t keep_cnt, all_cnt, drop_cnt;
235 if (already_encountered & 4)
237 msg (SE, _("KEEP subcommand may be given at most once. It may not"
238 "be given in conjunction with the DROP subcommand."));
241 already_encountered |= 4;
243 lex_match (lexer, '=');
244 if (!parse_variables (lexer, dataset_dict (ds), &keep_vars, &keep_cnt, PV_NONE))
247 /* Transform the list of variables to keep into a list of
248 variables to drop. First sort the keep list, then figure
249 out which variables are missing. */
250 sort (keep_vars, keep_cnt, sizeof *keep_vars,
251 compare_variables_given_ordering, &forward_positional_ordering);
253 dict_get_vars (dataset_dict (ds), &all_vars, &all_cnt, 0);
254 assert (all_cnt >= keep_cnt);
256 drop_cnt = all_cnt - keep_cnt;
257 drop_vars = xnmalloc (drop_cnt, sizeof *keep_vars);
258 if (set_difference (all_vars, all_cnt,
262 compare_variables_given_ordering,
263 &forward_positional_ordering)
270 vm.drop_vars = drop_vars;
271 vm.drop_cnt = drop_cnt;
273 else if (lex_match_id (lexer, "DROP"))
275 struct variable **drop_vars;
278 if (already_encountered & 4)
280 msg (SE, _("DROP subcommand may be given at most once. It may "
281 "not be given in conjunction with the KEEP "
285 already_encountered |= 4;
287 lex_match (lexer, '=');
288 if (!parse_variables (lexer, dataset_dict (ds), &drop_vars, &drop_cnt, PV_NONE))
290 vm.drop_vars = drop_vars;
291 vm.drop_cnt = drop_cnt;
293 else if (lex_match_id (lexer, "MAP"))
295 struct dictionary *temp = dict_clone (dataset_dict (ds));
296 int success = rearrange_dict (temp, &vm);
299 /* FIXME: display new dictionary. */
305 if (lex_token (lexer) == T_ID)
306 msg (SE, _("Unrecognized subcommand name `%s'."), lex_tokid (lexer));
308 msg (SE, _("Subcommand name expected."));
312 if (lex_token (lexer) == '.')
314 if (lex_token (lexer) != '/')
316 msg (SE, _("`/' or `.' expected."));
322 if (already_encountered & (1 | 4))
325 if (!procedure (ds,NULL, NULL))
329 if (!rearrange_dict (dataset_dict (ds), &vm))
332 ret_code = CMD_SUCCESS;
335 free (vm.reorder_vars);
336 free (vm.rename_vars);
337 for (i = 0; i < vm.rename_cnt; i++)
338 free (vm.new_names[i]);
344 /* Compares A and B according to the settings in
345 ORDERING, returning a strcmp()-type result. */
347 compare_variables_given_ordering (const void *a_, const void *b_,
348 const void *ordering_)
350 struct variable *const *pa = a_;
351 struct variable *const *pb = b_;
352 const struct variable *a = *pa;
353 const struct variable *b = *pb;
354 const struct ordering *ordering = ordering_;
357 if (ordering->positional)
358 result = a->index < b->index ? -1 : a->index > b->index;
360 result = strcasecmp (var_get_name (a), var_get_name (b));
361 if (!ordering->forward)
366 /* Pairs a variable with a new name. */
369 struct variable *var;
370 char new_name[LONG_NAME_LEN + 1];
373 /* A algo_compare_func that compares new_name members in struct
374 var_renaming structures A and B. */
376 compare_var_renaming_by_new_name (const void *a_, const void *b_,
377 const void *aux UNUSED)
379 const struct var_renaming *a = a_;
380 const struct var_renaming *b = b_;
382 return strcasecmp (a->new_name, b->new_name);
385 /* Returns true if performing VM on dictionary D would not cause
386 problems such as duplicate variable names. Returns false
387 otherwise, and issues an error message. */
389 validate_var_modification (const struct dictionary *d,
390 const struct var_modification *vm)
392 /* Variable reordering can't be a problem, so we don't simulate
393 it. Variable renaming can cause duplicate names, but
394 dropping variables can eliminate them, so we simulate both
396 struct variable **all_vars;
397 struct variable **keep_vars;
398 struct variable **drop_vars;
399 size_t keep_cnt, drop_cnt;
402 struct var_renaming *var_renaming;
406 /* All variables, in index order. */
407 dict_get_vars (d, &all_vars, &all_cnt, 0);
409 /* Drop variables, in index order. */
410 drop_cnt = vm->drop_cnt;
411 drop_vars = xnmalloc (drop_cnt, sizeof *drop_vars);
412 memcpy (drop_vars, vm->drop_vars, drop_cnt * sizeof *drop_vars);
413 sort (drop_vars, drop_cnt, sizeof *drop_vars,
414 compare_variables_given_ordering, &forward_positional_ordering);
416 /* Keep variables, in index order. */
417 assert (all_cnt >= drop_cnt);
418 keep_cnt = all_cnt - drop_cnt;
419 keep_vars = xnmalloc (keep_cnt, sizeof *keep_vars);
420 if (set_difference (all_vars, all_cnt,
424 compare_variables_given_ordering,
425 &forward_positional_ordering) != keep_cnt)
428 /* Copy variables into var_renaming array. */
429 var_renaming = xnmalloc (keep_cnt, sizeof *var_renaming);
430 for (i = 0; i < keep_cnt; i++)
432 var_renaming[i].var = keep_vars[i];
433 strcpy (var_renaming[i].new_name, var_get_name (keep_vars[i]));
436 /* Rename variables in var_renaming array. */
437 for (i = 0; i < vm->rename_cnt; i++)
439 struct variable *const *kv;
440 struct var_renaming *vr;
442 /* Get the var_renaming element. */
443 kv = binary_search (keep_vars, keep_cnt, sizeof *keep_vars,
445 compare_variables_given_ordering,
446 &forward_positional_ordering);
449 vr = var_renaming + (kv - keep_vars);
451 strcpy (vr->new_name, vm->new_names[i]);
454 /* Sort var_renaming array by new names and check for
456 sort (var_renaming, keep_cnt, sizeof *var_renaming,
457 compare_var_renaming_by_new_name, NULL);
458 valid = adjacent_find_equal (var_renaming, keep_cnt, sizeof *var_renaming,
459 compare_var_renaming_by_new_name, NULL) == NULL;
470 /* Reoders, removes, and renames variables in dictionary D
471 according to VM. Returns true if successful, false if there
472 would have been duplicate variable names if the modifications
473 had been carried out. In the latter case, the dictionary is
476 rearrange_dict (struct dictionary *d, const struct var_modification *vm)
478 char **rename_old_names;
480 struct variable **rename_vars;
481 char **rename_new_names;
486 /* Check whether the modifications will cause duplicate
488 if (!validate_var_modification (d, vm))
491 /* Record the old names of variables to rename. After
492 variables are deleted, we can't depend on the variables to
493 still exist, but we can still look them up by name. */
494 rename_old_names = xnmalloc (vm->rename_cnt, sizeof *rename_old_names);
495 for (i = 0; i < vm->rename_cnt; i++)
496 rename_old_names[i] = xstrdup (var_get_name (vm->rename_vars[i]));
498 /* Reorder and delete variables. */
499 dict_reorder_vars (d, vm->reorder_vars, vm->reorder_cnt);
500 dict_delete_vars (d, vm->drop_vars, vm->drop_cnt);
502 /* Compose lists of variables to rename and their new names. */
503 rename_vars = xnmalloc (vm->rename_cnt, sizeof *rename_vars);
504 rename_new_names = xnmalloc (vm->rename_cnt, sizeof *rename_new_names);
506 for (i = 0; i < vm->rename_cnt; i++)
508 struct variable *var = dict_lookup_var (d, rename_old_names[i]);
512 rename_vars[rename_cnt] = var;
513 rename_new_names[rename_cnt] = vm->new_names[i];
518 if (dict_rename_vars (d, rename_vars, rename_new_names, rename_cnt,
523 for (i = 0; i < vm->rename_cnt; i++)
524 free (rename_old_names[i]);
525 free (rename_old_names);
527 free (rename_new_names);