330ae4371af1e0498fc4cc90d4d107144789a8e1
[pspp-builds.git] / src / sel-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., 59 Temple Place - Suite 330, Boston, MA
18    02111-1307, USA. */
19
20 #include <config.h>
21 #include "alloc.h"
22 #include "command.h"
23 #include "error.h"
24 #include "expr.h"
25 #include "lexer.h"
26 #include "str.h"
27 #include "var.h"
28
29 /* SELECT IF transformation. */
30 struct select_if_trns
31   {
32     struct trns_header h;
33     struct expression *e;       /* Test expression. */
34   };
35
36 static trns_proc_func select_if_proc;
37 static trns_free_func select_if_free;
38
39 /* Parses the SELECT IF transformation. */
40 int
41 cmd_select_if (void)
42 {
43   struct expression *e;
44   struct select_if_trns *t;
45
46   e = expr_parse (PXP_BOOLEAN);
47   if (!e)
48     return CMD_FAILURE;
49
50   if (token != '.')
51     {
52       expr_free (e);
53       lex_error (_("expecting end of command"));
54       return CMD_FAILURE;
55     }
56
57   t = xmalloc (sizeof *t);
58   t->h.proc = select_if_proc;
59   t->h.free = select_if_free;
60   t->e = e;
61   add_transformation ((struct trns_header *) t);
62
63   return CMD_SUCCESS;
64 }
65
66 /* Performs the SELECT IF transformation T on case C. */
67 static int
68 select_if_proc (struct trns_header * t, struct ccase * c,
69                 int case_num)
70 {
71   return (expr_evaluate (((struct select_if_trns *) t)->e, c,
72                          case_num, NULL) == 1.0) - 2;
73 }
74
75 /* Frees SELECT IF transformation T. */
76 static void
77 select_if_free (struct trns_header * t)
78 {
79   expr_free (((struct select_if_trns *) t)->e);
80 }
81
82 /* Parses the FILTER command. */
83 int
84 cmd_filter (void)
85 {
86   if (lex_match_id ("OFF"))
87     dict_set_filter (default_dict, NULL);
88   else
89     {
90       struct variable *v;
91
92       lex_match (T_BY);
93       v = parse_variable ();
94       if (!v)
95         return CMD_FAILURE;
96
97       if (v->type == ALPHA)
98         {
99           msg (SE, _("The filter variable must be numeric."));
100           return CMD_FAILURE;
101         }
102
103       if (dict_class_from_id (v->name) == DC_SCRATCH)
104         {
105           msg (SE, _("The filter variable may not be scratch."));
106           return CMD_FAILURE;
107         }
108
109       dict_set_filter (default_dict, v);
110
111       FILTER_before_TEMPORARY = !temporary;
112     }
113
114   return CMD_SUCCESS;
115 }
116
117 /* Parses the PROCESS IF command. */
118 int
119 cmd_process_if (void)
120 {
121   struct expression *e;
122
123   e = expr_parse (PXP_BOOLEAN);
124   if (!e)
125     return CMD_FAILURE;
126
127   if (token != '.')
128     {
129       expr_free (e);
130       lex_error (_("expecting end of command"));
131       return CMD_FAILURE;
132     }
133
134   if (process_if_expr)
135     {
136       msg (MW, _("Only last instance of this command is in effect."));
137       expr_free (process_if_expr);
138     }
139   process_if_expr = e;
140
141   return CMD_SUCCESS;
142 }