Clean up how transformations work.
[pspp] / src / language / xforms / select-if.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2009, 2010, 2011 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/dataset.h"
22 #include "data/dictionary.h"
23 #include "data/transformations.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"
31
32 #include "gl/xalloc.h"
33
34 #include "gettext.h"
35 #define _(msgid) gettext (msgid)
36
37 /* SELECT IF transformation. */
38 struct select_if_trns
39   {
40     struct expression *e;       /* Test expression. */
41   };
42
43 static const struct trns_class select_if_trns_class;
44
45 /* Parses the SELECT IF transformation. */
46 int
47 cmd_select_if (struct lexer *lexer, struct dataset *ds)
48 {
49   struct expression *e;
50   struct select_if_trns *t;
51
52   e = expr_parse_bool (lexer, ds);
53   if (!e)
54     return CMD_CASCADING_FAILURE;
55
56   if (lex_token (lexer) != T_ENDCMD)
57     {
58       expr_free (e);
59       lex_error (lexer, _("expecting end of command"));
60       return CMD_CASCADING_FAILURE;
61     }
62
63   t = xmalloc (sizeof *t);
64   t->e = e;
65   add_transformation (ds, &select_if_trns_class, t);
66
67   return CMD_SUCCESS;
68 }
69
70 /* Performs the SELECT IF transformation T on case C. */
71 static enum trns_result
72 select_if_proc (void *t_, struct ccase **c,
73                 casenumber case_num)
74 {
75   struct select_if_trns *t = t_;
76   return (expr_evaluate_num (t->e, *c, case_num) == 1.0
77           ? TRNS_CONTINUE : TRNS_DROP_CASE);
78 }
79
80 /* Frees SELECT IF transformation T. */
81 static bool
82 select_if_free (void *t_)
83 {
84   struct select_if_trns *t = t_;
85   expr_free (t->e);
86   free (t);
87   return true;
88 }
89
90 static const struct trns_class select_if_trns_class = {
91   .name = "SELECT IF",
92   .execute = select_if_proc,
93   .destroy = select_if_free,
94 };
95
96 /* Parses the FILTER command. */
97 int
98 cmd_filter (struct lexer *lexer, struct dataset *ds)
99 {
100   struct dictionary *dict = dataset_dict (ds);
101   if (lex_match_id (lexer, "OFF"))
102     dict_set_filter (dict, NULL);
103   else if (lex_token (lexer) == T_ENDCMD)
104     {
105       msg (SW, _("Syntax error expecting OFF or BY.  "
106                  "Turning off case filtering."));
107       dict_set_filter (dict, NULL);
108     }
109   else
110     {
111       struct variable *v;
112
113       lex_match (lexer, T_BY);
114       v = parse_variable (lexer, dict);
115       if (!v)
116         return CMD_FAILURE;
117
118       if (var_is_alpha (v))
119         {
120           msg (SE, _("The filter variable must be numeric."));
121           return CMD_FAILURE;
122         }
123
124       if (dict_class_from_id (var_get_name (v)) == DC_SCRATCH)
125         {
126           msg (SE, _("The filter variable may not be scratch."));
127           return CMD_FAILURE;
128         }
129
130       dict_set_filter (dict, v);
131     }
132
133   return CMD_SUCCESS;
134 }