1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2006, 2007, 2008, 2010, 2011,
3 2019, 2020 Free Software Foundation, Inc.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU 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, see <http://www.gnu.org/licenses/>. */
20 #include "language/commands/trim.h"
24 #include "data/dictionary.h"
25 #include "data/variable.h"
26 #include "language/lexer/lexer.h"
27 #include "language/lexer/variable-parser.h"
28 #include "libpspp/message.h"
29 #include "libpspp/misc.h"
31 #include "gl/xalloc.h"
34 #define _(msgid) gettext (msgid)
36 /* Commands that read and write system files share a great deal of common
37 syntactic structure for rearranging and dropping variables. This function
38 parses this syntax and modifies DICT appropriately. Returns true on
39 success, false on failure. */
41 parse_dict_trim (struct lexer *lexer, struct dictionary *dict)
43 if (lex_match_id (lexer, "MAP"))
48 else if (lex_match_id (lexer, "DROP"))
49 return parse_dict_drop (lexer, dict);
50 else if (lex_match_id (lexer, "KEEP"))
51 return parse_dict_keep (lexer, dict);
52 else if (lex_match_id (lexer, "RENAME"))
53 return parse_dict_rename (lexer, dict);
56 lex_error_expecting (lexer, "MAP", "DROP", "KEEP", "RENAME");
61 /* Parses and performs the RENAME subcommand of GET, SAVE, and
64 parse_dict_rename (struct lexer *lexer, struct dictionary *dict)
66 lex_match (lexer, T_EQUALS);
67 int start_ofs = lex_ofs (lexer);
69 struct variable **old_vars = NULL;
70 size_t n_old_vars = 0;
72 char **new_vars = NULL;
73 size_t n_new_vars = 0;
76 while (lex_token (lexer) != T_SLASH && lex_token (lexer) != T_ENDCMD)
78 size_t prev_n_old = n_old_vars;
79 size_t prev_n_new = n_new_vars;
81 bool paren = lex_match (lexer, T_LPAREN);
82 int pv_opts = PV_NO_DUPLICATE | PV_APPEND | (paren ? 0 : PV_SINGLE);
84 int old_vars_start = lex_ofs (lexer);
85 if (!parse_variables (lexer, dict, &old_vars, &n_old_vars, pv_opts))
87 int old_vars_end = lex_ofs (lexer) - 1;
89 if (!lex_force_match (lexer, T_EQUALS))
92 int new_vars_start = lex_ofs (lexer);
93 if (!parse_DATA_LIST_vars (lexer, dict, &new_vars, &n_new_vars, pv_opts))
95 int new_vars_end = lex_ofs (lexer) - 1;
97 if (paren && !lex_force_match (lexer, T_RPAREN))
100 if (n_new_vars != n_old_vars)
102 size_t added_old = n_old_vars - prev_n_old;
103 size_t added_new = n_new_vars - prev_n_new;
105 msg (SE, _("Old and new variable counts do not match."));
106 lex_ofs_msg (lexer, SN, old_vars_start, old_vars_end,
107 ngettext ("There is %zu old variable.",
108 "There are %zu old variables.", added_old),
110 lex_ofs_msg (lexer, SN, new_vars_start, new_vars_end,
111 ngettext ("There is %zu new variable name.",
112 "There are %zu new variable names.",
118 int end_ofs = lex_ofs (lexer) - 1;
120 char *dup_name = NULL;
121 if (!dict_rename_vars (dict, old_vars, new_vars, n_new_vars, &dup_name))
123 lex_ofs_error (lexer, start_ofs, end_ofs,
124 _("Requested renaming duplicates variable name %s."),
132 for (size_t i = 0; i < n_new_vars; ++i)
138 /* Parses and performs the DROP subcommand of GET, SAVE, and
140 Returns true if successful, false on failure.*/
142 parse_dict_drop (struct lexer *lexer, struct dictionary *dict)
144 int start_ofs = lex_ofs (lexer) - 1;
145 lex_match (lexer, T_EQUALS);
149 if (!parse_variables (lexer, dict, &v, &nv, PV_NONE))
151 dict_delete_vars (dict, v, nv);
154 if (dict_get_n_vars (dict) == 0)
156 lex_ofs_error (lexer, start_ofs, lex_ofs (lexer) - 1,
157 _("Cannot DROP all variables from dictionary."));
163 /* Parses and performs the KEEP subcommand of GET, SAVE, and
165 Returns true if successful, false on failure.*/
167 parse_dict_keep (struct lexer *lexer, struct dictionary *dict)
173 lex_match (lexer, T_EQUALS);
174 if (!parse_variables (lexer, dict, &v, &nv, PV_NONE))
177 /* Move the specified variables to the beginning. */
178 dict_reorder_vars (dict, v, nv);
180 /* Delete the remaining variables. */
181 if (dict_get_n_vars (dict) == nv)
187 v = xnrealloc (v, dict_get_n_vars (dict) - nv, sizeof *v);
188 for (i = nv; i < dict_get_n_vars (dict); i++)
189 v[i - nv] = dict_get_var (dict, i);
190 dict_delete_vars (dict, v, dict_get_n_vars (dict) - nv);