checkin of 0.3.0
[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 int select_if_proc (struct trns_header *, struct ccase *);
37 static void select_if_free (struct trns_header *);
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   lex_match_id ("SELECT");
47   lex_match_id ("IF");
48
49   e = expr_parse (PXP_BOOLEAN);
50   if (!e)
51     return CMD_FAILURE;
52
53   if (token != '.')
54     {
55       expr_free (e);
56       lex_error (_("expecting end of command"));
57       return CMD_FAILURE;
58     }
59
60   t = xmalloc (sizeof *t);
61   t->h.proc = select_if_proc;
62   t->h.free = select_if_free;
63   t->e = e;
64   add_transformation ((struct trns_header *) t);
65
66   return CMD_SUCCESS;
67 }
68
69 /* Performs the SELECT IF transformation T on case C. */
70 static int
71 select_if_proc (struct trns_header * t, struct ccase * c)
72 {
73   return (expr_evaluate (((struct select_if_trns *) t)->e, c, NULL) == 1.0) - 2;
74 }
75
76 /* Frees SELECT IF transformation T. */
77 static void
78 select_if_free (struct trns_header * t)
79 {
80   expr_free (((struct select_if_trns *) t)->e);
81 }
82
83 /* Parses the FILTER command. */
84 int
85 cmd_filter (void)
86 {
87   lex_match_id ("FILTER");
88
89   if (lex_match_id ("OFF"))
90     default_dict.filter_var[0] = 0;
91   else
92     {
93       struct variable *v;
94
95       lex_match (T_BY);
96       v = parse_variable ();
97       if (!v)
98         return CMD_FAILURE;
99
100       if (v->type == ALPHA)
101         {
102           msg (SE, _("The filter variable must be numeric."));
103           return CMD_FAILURE;
104         }
105
106       if (v->name[0] == '#')
107         {
108           msg (SE, _("The filter variable may not be scratch."));
109           return CMD_FAILURE;
110         }
111       
112       strcpy (default_dict.filter_var, v->name);
113
114       FILTER_before_TEMPORARY = !temporary;
115     }
116
117   return CMD_SUCCESS;
118 }
119
120 /* Parses the PROCESS IF command. */
121 int
122 cmd_process_if (void)
123 {
124   struct expression *e;
125
126   lex_match_id ("PROCESS");
127   lex_match_id ("IF");
128
129   e = expr_parse (PXP_BOOLEAN);
130   if (!e)
131     return CMD_FAILURE;
132
133   if (token != '.')
134     {
135       expr_free (e);
136       lex_error (_("expecting end of command"));
137       return CMD_FAILURE;
138     }
139
140   if (process_if_expr)
141     {
142       msg (MW, _("Only last instance of this command is in effect."));
143       expr_free (process_if_expr);
144     }
145   process_if_expr = e;
146
147   return CMD_SUCCESS;
148 }