it works!!
[pspp] / src / language / commands / delete-variables.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2006, 2007, 2010, 2011, 2013 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 #include <stdlib.h>
20
21 #include "data/casereader.h"
22 #include "data/dataset.h"
23 #include "data/dictionary.h"
24 #include "language/command.h"
25 #include "language/lexer/lexer.h"
26 #include "language/lexer/variable-parser.h"
27 #include "libpspp/message.h"
28
29 #include "gettext.h"
30 #define _(msgid) gettext (msgid)
31
32 /* Performs DELETE VARIABLES command. */
33 int
34 cmd_delete_variables (struct lexer *lexer, struct dataset *ds)
35 {
36   if (proc_has_transformations (ds))
37     {
38       lex_ofs_error (lexer, 0, lex_ofs (lexer) - 1,
39                      _("%s may not be used when there are pending "
40                        "transformations (use %s to execute transformations)."),
41                      "DELETE VARIABLES", "EXECUTE");
42       return CMD_FAILURE;
43     }
44   if (proc_in_temporary_transformations (ds))
45     {
46       lex_ofs_error (lexer, 0, lex_ofs (lexer) - 1,
47                      _("%s may not be used after %s."),
48                      "DELETE VARIABLES", "TEMPORARY");
49       return CMD_FAILURE;
50     }
51
52   struct variable **vars;
53   size_t n_vars;
54   if (!parse_variables (lexer, dataset_dict (ds), &vars, &n_vars, PV_NONE))
55     return CMD_FAILURE;
56   if (n_vars == dict_get_n_vars (dataset_dict (ds)))
57     {
58       lex_ofs_error (lexer, 0, lex_ofs (lexer) - 1,
59                      _("%s may not be used to delete all variables "
60                        "from the active dataset dictionary.  "
61                        "Use %s instead."), "DELETE VARIABLES", "NEW FILE");
62       free (vars);
63       return CMD_FAILURE;
64     }
65
66   dataset_delete_vars (ds, vars, n_vars);
67   free (vars);
68
69   return CMD_SUCCESS;
70 }