1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2010, 2011 Free Software Foundation, Inc.
3 
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8 
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU 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, see <http://www.gnu.org/licenses/>. */
16 
17 #include <config.h>
18 
19 #include <stdio.h>
20 
21 #include "data/dataset.h"
22 #include "data/dictionary.h"
23 #include "data/variable.h"
24 #include "language/command.h"
25 #include "language/lexer/lexer.h"
26 #include "language/lexer/variable-parser.h"
27 #include "libpspp/message.h"
28 #include "libpspp/str.h"
29 
30 #include "gettext.h"
31 #define _(msgid) gettext (msgid)
32 
33 int
cmd_weight(struct lexer * lexer,struct dataset * ds)34 cmd_weight (struct lexer *lexer, struct dataset *ds)
35 {
36   struct dictionary *dict = dataset_dict (ds);
37   if (lex_match_id (lexer, "OFF"))
38     dict_set_weight (dataset_dict (ds), NULL);
39   else
40     {
41       struct variable *v;
42 
43       lex_match (lexer, T_BY);
44       v = parse_variable (lexer, dict);
45       if (!v)
46 	return CMD_CASCADING_FAILURE;
47       if (var_is_alpha (v))
48 	{
49 	  msg (SE, _("The weighting variable must be numeric."));
50 	  return CMD_CASCADING_FAILURE;
51 	}
52       if (dict_class_from_id (var_get_name (v)) == DC_SCRATCH)
53 	{
54 	  msg (SE, _("The weighting variable may not be scratch."));
55 	  return CMD_CASCADING_FAILURE;
56 	}
57 
58       dict_set_weight (dict, v);
59     }
60 
61   return CMD_SUCCESS;
62 }
63