Move all command implementations into a single 'commands' directory.
[pspp] / src / language / commands / trim.c
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.
4
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.
9
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.
14
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/>. */
17
18 #include <config.h>
19
20 #include "language/commands/trim.h"
21
22 #include <stdlib.h>
23
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"
30
31 #include "gl/xalloc.h"
32
33 #include "gettext.h"
34 #define _(msgid) gettext (msgid)
35
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. */
40 bool
41 parse_dict_trim (struct lexer *lexer, struct dictionary *dict)
42 {
43   if (lex_match_id (lexer, "MAP"))
44     {
45       /* FIXME. */
46       return true;
47     }
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);
54   else
55     {
56       lex_error_expecting (lexer, "MAP", "DROP", "KEEP", "RENAME");
57       return false;
58     }
59 }
60
61 /* Parses and performs the RENAME subcommand of GET, SAVE, and
62    related commands. */
63 bool
64 parse_dict_rename (struct lexer *lexer, struct dictionary *dict)
65 {
66   lex_match (lexer, T_EQUALS);
67   int start_ofs = lex_ofs (lexer);
68
69   struct variable **old_vars = NULL;
70   size_t n_old_vars = 0;
71
72   char **new_vars = NULL;
73   size_t n_new_vars = 0;
74
75   bool ok = false;
76   while (lex_token (lexer) != T_SLASH && lex_token (lexer) != T_ENDCMD)
77     {
78       size_t prev_n_old = n_old_vars;
79       size_t prev_n_new = n_new_vars;
80
81       bool paren = lex_match (lexer, T_LPAREN);
82       int pv_opts = PV_NO_DUPLICATE | PV_APPEND | (paren ? 0 : PV_SINGLE);
83
84       int old_vars_start = lex_ofs (lexer);
85       if (!parse_variables (lexer, dict, &old_vars, &n_old_vars, pv_opts))
86         goto done;
87       int old_vars_end = lex_ofs (lexer) - 1;
88
89       if (!lex_force_match (lexer, T_EQUALS))
90         goto done;
91
92       int new_vars_start = lex_ofs (lexer);
93       if (!parse_DATA_LIST_vars (lexer, dict, &new_vars, &n_new_vars, pv_opts))
94         goto done;
95       int new_vars_end = lex_ofs (lexer) - 1;
96
97       if (paren && !lex_force_match (lexer, T_RPAREN))
98         goto done;
99
100       if (n_new_vars != n_old_vars)
101         {
102           size_t added_old = n_old_vars - prev_n_old;
103           size_t added_new = n_new_vars - prev_n_new;
104
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),
109                        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.",
113                                  added_new),
114                        added_new);
115           goto done;
116         }
117     }
118   int end_ofs = lex_ofs (lexer) - 1;
119
120   char *dup_name = NULL;
121   if (!dict_rename_vars (dict, old_vars, new_vars, n_new_vars, &dup_name))
122     {
123       lex_ofs_error (lexer, start_ofs, end_ofs,
124                      _("Requested renaming duplicates variable name %s."),
125                      dup_name);
126       goto done;
127     }
128   ok = true;
129
130 done:
131   free (old_vars);
132   for (size_t i = 0; i < n_new_vars; ++i)
133     free (new_vars[i]);
134   free (new_vars);
135   return ok;
136 }
137
138 /* Parses and performs the DROP subcommand of GET, SAVE, and
139    related commands.
140    Returns true if successful, false on failure.*/
141 bool
142 parse_dict_drop (struct lexer *lexer, struct dictionary *dict)
143 {
144   int start_ofs = lex_ofs (lexer) - 1;
145   lex_match (lexer, T_EQUALS);
146
147   struct variable **v;
148   size_t nv;
149   if (!parse_variables (lexer, dict, &v, &nv, PV_NONE))
150     return false;
151   dict_delete_vars (dict, v, nv);
152   free (v);
153
154   if (dict_get_n_vars (dict) == 0)
155     {
156       lex_ofs_error (lexer, start_ofs, lex_ofs (lexer) - 1,
157                      _("Cannot DROP all variables from dictionary."));
158       return false;
159     }
160   return true;
161 }
162
163 /* Parses and performs the KEEP subcommand of GET, SAVE, and
164    related commands.
165    Returns true if successful, false on failure.*/
166 bool
167 parse_dict_keep (struct lexer *lexer, struct dictionary *dict)
168 {
169   struct variable **v;
170   size_t nv;
171   size_t i;
172
173   lex_match (lexer, T_EQUALS);
174   if (!parse_variables (lexer, dict, &v, &nv, PV_NONE))
175     return false;
176
177   /* Move the specified variables to the beginning. */
178   dict_reorder_vars (dict, v, nv);
179
180   /* Delete the remaining variables. */
181   if (dict_get_n_vars (dict) == nv)
182     {
183       free (v);
184       return true;
185     }
186
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);
191   free (v);
192
193   return true;
194 }