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