Encapsulated the static data of procedure.[ch] into a single object, to be
[pspp-builds.git] / src / language / xforms / select-if.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3    Written by Ben Pfaff <blp@gnu.org>.
4
5    This program is free software; you can redistribute it and/or
6    modify it under the terms of the GNU General Public License as
7    published by the Free Software Foundation; either version 2 of the
8    License, or (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful, but
11    WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    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, write to the Free Software
17    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18    02110-1301, USA. */
19
20 #include <config.h>
21
22 #include <stdlib.h>
23
24 #include <data/dictionary.h>
25 #include <data/transformations.h>
26 #include <data/procedure.h>
27 #include <data/variable.h>
28 #include <language/command.h>
29 #include <language/expressions/public.h>
30 #include <language/lexer/lexer.h>
31 #include <language/lexer/variable-parser.h>
32 #include <libpspp/alloc.h>
33 #include <libpspp/message.h>
34 #include <libpspp/str.h>
35
36 #include "gettext.h"
37 #define _(msgid) gettext (msgid)
38
39 /* SELECT IF transformation. */
40 struct select_if_trns
41   {
42     struct expression *e;       /* Test expression. */
43   };
44
45 static trns_proc_func select_if_proc;
46 static trns_free_func select_if_free;
47
48 /* Parses the SELECT IF transformation. */
49 int
50 cmd_select_if (void)
51 {
52   struct expression *e;
53   struct select_if_trns *t;
54
55   e = expr_parse (dataset_dict (current_dataset), EXPR_BOOLEAN);
56   if (!e)
57     return CMD_CASCADING_FAILURE;
58
59   if (token != '.')
60     {
61       expr_free (e);
62       lex_error (_("expecting end of command"));
63       return CMD_CASCADING_FAILURE;
64     }
65
66   t = xmalloc (sizeof *t);
67   t->e = e;
68   add_transformation (current_dataset, select_if_proc, select_if_free, t);
69
70   return CMD_SUCCESS;
71 }
72
73 /* Performs the SELECT IF transformation T on case C. */
74 static int
75 select_if_proc (void *t_, struct ccase *c,
76                 casenum_t case_num)
77 {
78   struct select_if_trns *t = t_;
79   return (expr_evaluate_num (t->e, c, case_num) == 1.0
80           ? TRNS_CONTINUE : TRNS_DROP_CASE);
81 }
82
83 /* Frees SELECT IF transformation T. */
84 static bool
85 select_if_free (void *t_)
86 {
87   struct select_if_trns *t = t_;
88   expr_free (t->e);
89   free (t);
90   return true;
91 }
92
93 /* Parses the FILTER command. */
94 int
95 cmd_filter (void)
96 {
97   if (lex_match_id ("OFF"))
98     dict_set_filter (dataset_dict (current_dataset), NULL);
99   else if (token == '.') 
100     {
101       msg (SW, _("Syntax error expecting OFF or BY.  "
102                  "Turning off case filtering."));
103       dict_set_filter (dataset_dict (current_dataset), NULL);
104     }
105   else
106     {
107       struct variable *v;
108
109       lex_match (T_BY);
110       v = parse_variable ();
111       if (!v)
112         return CMD_FAILURE;
113
114       if (v->type == ALPHA)
115         {
116           msg (SE, _("The filter variable must be numeric."));
117           return CMD_FAILURE;
118         }
119
120       if (dict_class_from_id (v->name) == DC_SCRATCH)
121         {
122           msg (SE, _("The filter variable may not be scratch."));
123           return CMD_FAILURE;
124         }
125
126       dict_set_filter (dataset_dict (current_dataset), v);
127     }
128
129   return lex_end_of_command ();
130 }