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