4666f52db9f1964a32df42a524e249089319dbc8
[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 <libpspp/alloc.h>
32 #include <libpspp/message.h>
33 #include <libpspp/str.h>
34
35 #include "gettext.h"
36 #define _(msgid) gettext (msgid)
37
38 /* SELECT IF transformation. */
39 struct select_if_trns
40   {
41     struct expression *e;       /* Test expression. */
42   };
43
44 static trns_proc_func select_if_proc;
45 static trns_free_func select_if_free;
46
47 /* Parses the SELECT IF transformation. */
48 int
49 cmd_select_if (void)
50 {
51   struct expression *e;
52   struct select_if_trns *t;
53
54   e = expr_parse (default_dict, EXPR_BOOLEAN);
55   if (!e)
56     return CMD_CASCADING_FAILURE;
57
58   if (token != '.')
59     {
60       expr_free (e);
61       lex_error (_("expecting end of command"));
62       return CMD_CASCADING_FAILURE;
63     }
64
65   t = xmalloc (sizeof *t);
66   t->e = e;
67   add_transformation (select_if_proc, select_if_free, t);
68
69   return CMD_SUCCESS;
70 }
71
72 /* Performs the SELECT IF transformation T on case C. */
73 static int
74 select_if_proc (void *t_, struct ccase *c,
75                 int case_num)
76 {
77   struct select_if_trns *t = t_;
78   return (expr_evaluate_num (t->e, c, case_num) == 1.0
79           ? TRNS_CONTINUE : TRNS_DROP_CASE);
80 }
81
82 /* Frees SELECT IF transformation T. */
83 static bool
84 select_if_free (void *t_)
85 {
86   struct select_if_trns *t = t_;
87   expr_free (t->e);
88   free (t);
89   return true;
90 }
91
92 /* Parses the FILTER command. */
93 int
94 cmd_filter (void)
95 {
96   if (lex_match_id ("OFF"))
97     dict_set_filter (default_dict, NULL);
98   else
99     {
100       struct variable *v;
101
102       lex_match (T_BY);
103       v = parse_variable ();
104       if (!v)
105         return CMD_CASCADING_FAILURE;
106
107       if (v->type == ALPHA)
108         {
109           msg (SE, _("The filter variable must be numeric."));
110           return CMD_CASCADING_FAILURE;
111         }
112
113       if (dict_class_from_id (v->name) == DC_SCRATCH)
114         {
115           msg (SE, _("The filter variable may not be scratch."));
116           return CMD_CASCADING_FAILURE;
117         }
118
119       dict_set_filter (default_dict, v);
120     }
121
122   return CMD_SUCCESS;
123 }
124 \f
125 /* Expression on PROCESS IF. */
126 struct expression *process_if_expr;
127
128 /* Parses the PROCESS IF command. */
129 int
130 cmd_process_if (void)
131 {
132   struct expression *e;
133
134   e = expr_parse (default_dict, EXPR_BOOLEAN);
135   if (!e)
136     return CMD_FAILURE;
137
138   if (token != '.')
139     {
140       expr_free (e);
141       lex_error (_("expecting end of command"));
142       return CMD_FAILURE;
143     }
144
145   if (process_if_expr)
146     {
147       msg (SW, _("Only last instance of this command is in effect."));
148       expr_free (process_if_expr);
149     }
150   process_if_expr = e;
151
152   return CMD_SUCCESS;
153 }