Change explicit variable name checks into use of dict_class_from_id().
[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   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                 int case_num)
73 {
74   return (expr_evaluate (((struct select_if_trns *) t)->e, c,
75                          case_num, NULL) == 1.0) - 2;
76 }
77
78 /* Frees SELECT IF transformation T. */
79 static void
80 select_if_free (struct trns_header * t)
81 {
82   expr_free (((struct select_if_trns *) t)->e);
83 }
84
85 /* Parses the FILTER command. */
86 int
87 cmd_filter (void)
88 {
89   lex_match_id ("FILTER");
90
91   if (lex_match_id ("OFF"))
92     dict_set_filter (default_dict, NULL);
93   else
94     {
95       struct variable *v;
96
97       lex_match (T_BY);
98       v = parse_variable ();
99       if (!v)
100         return CMD_FAILURE;
101
102       if (v->type == ALPHA)
103         {
104           msg (SE, _("The filter variable must be numeric."));
105           return CMD_FAILURE;
106         }
107
108       if (dict_class_from_id (v->name) == DC_SCRATCH)
109         {
110           msg (SE, _("The filter variable may not be scratch."));
111           return CMD_FAILURE;
112         }
113
114       dict_set_filter (default_dict, v);
115
116       FILTER_before_TEMPORARY = !temporary;
117     }
118
119   return CMD_SUCCESS;
120 }
121
122 /* Parses the PROCESS IF command. */
123 int
124 cmd_process_if (void)
125 {
126   struct expression *e;
127
128   lex_match_id ("PROCESS");
129   lex_match_id ("IF");
130
131   e = expr_parse (PXP_BOOLEAN);
132   if (!e)
133     return CMD_FAILURE;
134
135   if (token != '.')
136     {
137       expr_free (e);
138       lex_error (_("expecting end of command"));
139       return CMD_FAILURE;
140     }
141
142   if (process_if_expr)
143     {
144       msg (MW, _("Only last instance of this command is in effect."));
145       expr_free (process_if_expr);
146     }
147   process_if_expr = e;
148
149   return CMD_SUCCESS;
150 }