1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2009 Free Software Foundation, Inc.
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.
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.
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/>. */
21 #include <data/dictionary.h>
22 #include <data/transformations.h>
23 #include <data/procedure.h>
24 #include <data/variable.h>
25 #include <language/command.h>
26 #include <language/expressions/public.h>
27 #include <language/lexer/lexer.h>
28 #include <language/lexer/variable-parser.h>
29 #include <libpspp/message.h>
30 #include <libpspp/str.h>
35 #define _(msgid) gettext (msgid)
37 /* SELECT IF transformation. */
40 struct expression *e; /* Test expression. */
43 static trns_proc_func select_if_proc;
44 static trns_free_func select_if_free;
46 /* Parses the SELECT IF transformation. */
48 cmd_select_if (struct lexer *lexer, struct dataset *ds)
51 struct select_if_trns *t;
53 e = expr_parse (lexer, ds, EXPR_BOOLEAN);
55 return CMD_CASCADING_FAILURE;
57 if (lex_token (lexer) != '.')
60 lex_error (lexer, _("expecting end of command"));
61 return CMD_CASCADING_FAILURE;
64 t = xmalloc (sizeof *t);
66 add_transformation (ds, select_if_proc, select_if_free, t);
71 /* Performs the SELECT IF transformation T on case C. */
73 select_if_proc (void *t_, struct ccase **c,
76 struct select_if_trns *t = t_;
77 return (expr_evaluate_num (t->e, *c, case_num) == 1.0
78 ? TRNS_CONTINUE : TRNS_DROP_CASE);
81 /* Frees SELECT IF transformation T. */
83 select_if_free (void *t_)
85 struct select_if_trns *t = t_;
91 /* Parses the FILTER command. */
93 cmd_filter (struct lexer *lexer, struct dataset *ds)
95 struct dictionary *dict = dataset_dict (ds);
96 if (lex_match_id (lexer, "OFF"))
97 dict_set_filter (dict, NULL);
98 else if (lex_token (lexer) == '.')
100 msg (SW, _("Syntax error expecting OFF or BY. "
101 "Turning off case filtering."));
102 dict_set_filter (dict, NULL);
108 lex_match (lexer, T_BY);
109 v = parse_variable (lexer, dict);
113 if (var_is_alpha (v))
115 msg (SE, _("The filter variable must be numeric."));
119 if (dict_class_from_id (var_get_name (v)) == DC_SCRATCH)
121 msg (SE, _("The filter variable may not be scratch."));
125 dict_set_filter (dict, v);
128 return lex_end_of_command (lexer);