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