Move all command implementations into a single 'commands' directory.
[pspp] / src / language / commands / split-file.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2009, 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 <stdlib.h>
20
21 #include "data/case.h"
22 #include "data/casereader.h"
23 #include "data/data-out.h"
24 #include "data/dataset.h"
25 #include "data/dictionary.h"
26 #include "data/format.h"
27 #include "data/value-labels.h"
28 #include "data/variable.h"
29 #include "language/command.h"
30 #include "language/commands/split-file.h"
31 #include "language/lexer/lexer.h"
32 #include "language/lexer/variable-parser.h"
33 #include "libpspp/message.h"
34 #include "libpspp/str.h"
35 #include "output/pivot-table.h"
36
37 #include "gl/xalloc.h"
38
39 #include "gettext.h"
40 #define N_(msgid) msgid
41 #define _(msgid) gettext (msgid)
42
43 int
44 cmd_split_file (struct lexer *lexer, struct dataset *ds)
45 {
46   if (lex_match_id (lexer, "OFF"))
47     dict_clear_split_vars (dataset_dict (ds));
48   else
49     {
50       struct variable **v;
51       size_t n;
52
53       enum split_type type = (!lex_match_id (lexer, "LAYERED")
54                               && lex_match_id (lexer, "SEPARATE")
55                               ? SPLIT_SEPARATE
56                               : SPLIT_LAYERED);
57
58       lex_match (lexer, T_BY);
59       int vars_start = lex_ofs (lexer);
60       if (!parse_variables (lexer, dataset_dict (ds), &v, &n, PV_NO_DUPLICATE))
61         return CMD_CASCADING_FAILURE;
62       int vars_end = lex_ofs (lexer) - 1;
63
64       if (n > MAX_SPLITS)
65         {
66           verify (MAX_SPLITS == 8);
67           lex_ofs_error (lexer, vars_start, vars_end,
68                          _("At most 8 split variables may be specified."));
69           free (v);
70           return CMD_CASCADING_FAILURE;
71         }
72
73       dict_set_split_vars (dataset_dict (ds), v, n, type);
74       free (v);
75     }
76
77   return CMD_SUCCESS;
78 }
79
80 /* Dumps out the values of all the split variables for the case C. */
81 void
82 output_split_file_values (const struct dataset *ds, const struct ccase *c)
83 {
84   const struct dictionary *dict = dataset_dict (ds);
85   size_t n_vars = dict_get_n_splits (dict);
86   if (n_vars == 0)
87     return;
88
89   struct pivot_table *table = pivot_table_create (N_("Split Values"));
90   pivot_dimension_create (table, PIVOT_AXIS_COLUMN, N_("Value"),
91                           N_("Value"));
92   struct pivot_dimension *variables = pivot_dimension_create (
93     table, PIVOT_AXIS_ROW, N_("Variable"));
94   variables->root->show_label = true;
95
96   for (size_t i = 0; i < n_vars; i++)
97     {
98       const struct variable *v = dict_get_split_vars (dict)[i];
99       int row = pivot_category_create_leaf (variables->root,
100                                             pivot_value_new_variable (v));
101
102       pivot_table_put2 (table, 0, row,
103                         pivot_value_new_var_value (v, case_data (c, v)));
104     }
105
106   pivot_table_submit (table);
107 }
108
109 /* Dumps out the values of all the split variables for the first case in
110    READER. */
111 void
112 output_split_file_values_peek (const struct dataset *ds,
113                                const struct casereader *reader)
114 {
115   struct ccase *c = casereader_peek (reader, 0);
116   if (c)
117     {
118       output_split_file_values (ds, c);
119       case_unref (c);
120     }
121 }