1466222b99622bebf28a51736898c88a75f170dd
[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
4    This program is free software; you can redistribute it and/or
5    modify it under the terms of the GNU General Public License as
6    published by the Free Software Foundation; either version 2 of the
7    License, or (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful, but
10    WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software
16    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17    02110-1301, USA. */
18
19 #include <config.h>
20
21 #include <stdlib.h>
22
23 #include <data/dictionary.h>
24 #include <data/transformations.h>
25 #include <data/procedure.h>
26 #include <data/variable.h>
27 #include <language/command.h>
28 #include <language/expressions/public.h>
29 #include <language/lexer/lexer.h>
30 #include <language/lexer/variable-parser.h>
31 #include <libpspp/alloc.h>
32 #include <libpspp/message.h>
33 #include <libpspp/str.h>
34
35 #include "gettext.h"
36 #define _(msgid) gettext (msgid)
37
38 /* SELECT IF transformation. */
39 struct select_if_trns
40   {
41     struct expression *e;       /* Test expression. */
42   };
43
44 static trns_proc_func select_if_proc;
45 static trns_free_func select_if_free;
46
47 /* Parses the SELECT IF transformation. */
48 int
49 cmd_select_if (struct lexer *lexer, struct dataset *ds)
50 {
51   struct expression *e;
52   struct select_if_trns *t;
53
54   e = expr_parse (lexer, ds, EXPR_BOOLEAN);
55   if (!e)
56     return CMD_CASCADING_FAILURE;
57
58   if (lex_token (lexer) != '.')
59     {
60       expr_free (e);
61       lex_error (lexer, _("expecting end of command"));
62       return CMD_CASCADING_FAILURE;
63     }
64
65   t = xmalloc (sizeof *t);
66   t->e = e;
67   add_transformation (ds, select_if_proc, select_if_free, t);
68
69   return CMD_SUCCESS;
70 }
71
72 /* Performs the SELECT IF transformation T on case C. */
73 static int
74 select_if_proc (void *t_, struct ccase *c,
75                 casenumber case_num)
76 {
77   struct select_if_trns *t = t_;
78   return (expr_evaluate_num (t->e, c, case_num) == 1.0
79           ? TRNS_CONTINUE : TRNS_DROP_CASE);
80 }
81
82 /* Frees SELECT IF transformation T. */
83 static bool
84 select_if_free (void *t_)
85 {
86   struct select_if_trns *t = t_;
87   expr_free (t->e);
88   free (t);
89   return true;
90 }
91
92 /* Parses the FILTER command. */
93 int
94 cmd_filter (struct lexer *lexer, struct dataset *ds)
95 {
96   struct dictionary *dict = dataset_dict (ds);
97   if (lex_match_id (lexer, "OFF"))
98     dict_set_filter (dict, NULL);
99   else if (lex_token (lexer) == '.')
100     {
101       msg (SW, _("Syntax error expecting OFF or BY.  "
102                  "Turning off case filtering."));
103       dict_set_filter (dict, NULL);
104     }
105   else
106     {
107       struct variable *v;
108
109       lex_match (lexer, T_BY);
110       v = parse_variable (lexer, dict);
111       if (!v)
112         return CMD_FAILURE;
113
114       if (var_is_alpha (v))
115         {
116           msg (SE, _("The filter variable must be numeric."));
117           return CMD_FAILURE;
118         }
119
120       if (dict_class_from_id (var_get_name (v)) == DC_SCRATCH)
121         {
122           msg (SE, _("The filter variable may not be scratch."));
123           return CMD_FAILURE;
124         }
125
126       dict_set_filter (dict, v);
127     }
128
129   return lex_end_of_command (lexer);
130 }