X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Flanguage%2Fdictionary%2Frename-variables.c;h=33d8f6887f61d64267b4a2d5587c79a822a12d2b;hb=5d1c47b1ef2d392ce223606139d45eac749d4c80;hp=90117ff69ede1ea6a33b1cb7a4aa65761dbe0ba3;hpb=9f4661992f4b481c6dafa6fd53c94ecfe7b3af8c;p=pspp diff --git a/src/language/dictionary/rename-variables.c b/src/language/dictionary/rename-variables.c index 90117ff69e..33d8f6887f 100644 --- a/src/language/dictionary/rename-variables.c +++ b/src/language/dictionary/rename-variables.c @@ -1,5 +1,5 @@ /* PSPP - a program for statistical analysis. - Copyright (C) 1997-9, 2000 Free Software Foundation, Inc. + Copyright (C) 1997-9, 2000, 2010, 2011, 2015, 2016 Free Software Foundation, Inc. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -18,17 +18,16 @@ #include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include "data/dataset.h" +#include "data/dictionary.h" +#include "data/variable.h" +#include "language/command.h" +#include "language/lexer/lexer.h" +#include "language/lexer/variable-parser.h" +#include "libpspp/message.h" +#include "libpspp/str.h" -#include "xalloc.h" +#include "gl/xalloc.h" #include "gettext.h" #define _(msgid) gettext (msgid) @@ -38,60 +37,56 @@ int cmd_rename_variables (struct lexer *lexer, struct dataset *ds) { - struct variable **rename_vars = NULL; - char **rename_new_names = NULL; - size_t rename_cnt = 0; + struct variable **vars_to_be_renamed = NULL; + size_t n_vars_to_be_renamed = 0; + + char **new_names = NULL; + size_t n_new_names = 0; + char *err_name; int status = CMD_CASCADING_FAILURE; if (proc_make_temporary_transformations_permanent (ds)) - msg (SE, _("RENAME VARS may not be used after TEMPORARY. " - "Temporary transformations will be made permanent.")); + msg (SE, _("%s may not be used after %s. " + "Temporary transformations will be made permanent."), "RENAME VARS", "TEMPORARY"); do { - size_t prev_nv_1 = rename_cnt; - size_t prev_nv_2 = rename_cnt; + int opts = PV_APPEND | PV_NO_DUPLICATE; - if (!lex_match (lexer, '(')) + if (!lex_match (lexer, T_LPAREN)) + opts |= PV_SINGLE; + if (!parse_variables (lexer, dataset_dict (ds), + &vars_to_be_renamed, &n_vars_to_be_renamed, opts)) { - msg (SE, _("`(' expected.")); goto lossage; } - if (!parse_variables (lexer, dataset_dict (ds), &rename_vars, &rename_cnt, - PV_APPEND | PV_NO_DUPLICATE)) - goto lossage; - if (!lex_match (lexer, '=')) + if (!lex_force_match (lexer, T_EQUALS)) { - msg (SE, _("`=' expected between lists of new and old variable names.")); goto lossage; } - if (!parse_DATA_LIST_vars (lexer, &rename_new_names, &prev_nv_1, PV_APPEND)) - goto lossage; - if (prev_nv_1 != rename_cnt) + if (!parse_DATA_LIST_vars (lexer, dataset_dict (ds), + &new_names, &n_new_names, opts)) { - size_t i; - - msg (SE, _("Differing number of variables in old name list " - "(%zu) and in new name list (%zu)."), - rename_cnt - prev_nv_2, prev_nv_1 - prev_nv_2); - for (i = 0; i < prev_nv_1; i++) - free (rename_new_names[i]); - free (rename_new_names); - rename_new_names = NULL; goto lossage; } - if (!lex_match (lexer, ')')) + if (n_new_names != n_vars_to_be_renamed) + { + msg (SE, _("Differing number of variables in old name list " + "(%zu) and in new name list (%zu)."), + n_vars_to_be_renamed, n_new_names); + goto lossage; + } + if (!(opts & PV_SINGLE) && !lex_force_match (lexer, T_RPAREN)) { - msg (SE, _("`)' expected after variable names.")); goto lossage; } } - while (lex_token (lexer) != '.'); + while (lex_token (lexer) != T_ENDCMD); if (!dict_rename_vars (dataset_dict (ds), - rename_vars, rename_new_names, rename_cnt, + vars_to_be_renamed, new_names, n_new_names, &err_name)) { msg (SE, _("Renaming would duplicate variable name %s."), err_name); @@ -101,13 +96,13 @@ cmd_rename_variables (struct lexer *lexer, struct dataset *ds) status = CMD_SUCCESS; lossage: - free (rename_vars); - if (rename_new_names != NULL) + free (vars_to_be_renamed); + if (new_names != NULL) { size_t i; - for (i = 0; i < rename_cnt; i++) - free (rename_new_names[i]); - free (rename_new_names); + for (i = 0; i < n_new_names; ++i) + free (new_names[i]); + free (new_names); } return status; }