1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2006, 2009-2011, 2013, 2014, 2016 Free Software Foundation, Inc.
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.
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.
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/>. */
23 #include "data/casegrouper.h"
24 #include "data/casereader.h"
25 #include "data/dataset.h"
26 #include "data/dictionary.h"
27 #include "data/data-out.h"
28 #include "data/format.h"
29 #include "data/subcase.h"
30 #include "data/variable.h"
31 #include "language/command.h"
32 #include "language/dictionary/split-file.h"
33 #include "language/lexer/lexer.h"
34 #include "language/lexer/variable-parser.h"
35 #include "libpspp/compiler.h"
36 #include "libpspp/ll.h"
37 #include "libpspp/message.h"
38 #include "libpspp/misc.h"
39 #include "output/pivot-table.h"
41 #include "gl/intprops.h"
42 #include "gl/minmax.h"
43 #include "gl/xalloc.h"
44 #include "gl/xmalloca.h"
47 #define N_(msgid) msgid
48 #define _(msgid) gettext (msgid)
55 const struct variable **vars;
61 list_execute (const struct lst_cmd *lcmd, struct dataset *ds)
63 const struct dictionary *dict = dataset_dict (ds);
66 subcase_init_empty (&sc);
67 for (size_t i = 0; i < lcmd->n_vars; i++)
68 subcase_add_var (&sc, lcmd->vars[i], SC_ASCEND);
70 struct casegrouper *grouper;
71 struct casereader *group;
72 grouper = casegrouper_create_splits (proc_open (ds), dict);
73 while (casegrouper_get_next_group (grouper, &group))
75 struct ccase *c = casereader_peek (group, 0);
78 output_split_file_values (ds, c);
82 group = casereader_project (group, &sc);
83 group = casereader_select (group, lcmd->first - 1,
84 (lcmd->last != LONG_MAX ? lcmd->last
85 : CASENUMBER_MAX), lcmd->step);
87 struct pivot_table *table = pivot_table_create (N_("Data List"));
88 table->show_values = table->show_variables = SETTINGS_VALUE_SHOW_VALUE;
90 struct pivot_dimension *variables = pivot_dimension_create (
91 table, PIVOT_AXIS_COLUMN, N_("Variables"));
92 for (size_t i = 0; i < lcmd->n_vars; i++)
93 pivot_category_create_leaf (
94 variables->root, pivot_value_new_variable (lcmd->vars[i]));
96 struct pivot_dimension *cases = pivot_dimension_create (
97 table, PIVOT_AXIS_ROW, N_("Case Number"));
98 if (lcmd->number_cases)
99 cases->root->show_label = true;
101 cases->hide_all_labels = true;
103 casenumber case_num = lcmd->first;
104 for (; (c = casereader_read (group)) != NULL; case_unref (c))
106 int case_idx = pivot_category_create_leaf (
107 cases->root, pivot_value_new_integer (case_num));
108 case_num += lcmd->step;
110 for (size_t i = 0; i < lcmd->n_vars; i++)
111 pivot_table_put2 (table, i, case_idx,
112 pivot_value_new_var_value (
113 lcmd->vars[i], case_data_idx (c, i)));
115 casereader_destroy (group);
117 pivot_table_submit (table);
120 bool ok = casegrouper_destroy (grouper);
121 ok = proc_commit (ds) && ok;
123 subcase_uninit (&sc);
126 return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;
130 /* Parses and executes the LIST procedure. */
132 cmd_list (struct lexer *lexer, struct dataset *ds)
134 const struct dictionary *dict = dataset_dict (ds);
136 struct lst_cmd cmd = {
142 while (lex_token (lexer) != T_ENDCMD)
144 lex_match (lexer, T_SLASH);
145 if (lex_match_id (lexer, "VARIABLES"))
147 lex_match (lexer, T_EQUALS);
150 if (!parse_variables_const (lexer, dict, &cmd.vars, &cmd.n_vars,
154 else if (lex_match_id (lexer, "FORMAT"))
156 lex_match (lexer, T_EQUALS);
157 if (lex_match_id (lexer, "NUMBERED"))
158 cmd.number_cases = true;
159 else if (lex_match_id (lexer, "UNNUMBERED"))
160 cmd.number_cases = false;
163 lex_error_expecting (lexer, "NUMBERED", "UNNUMBERED");
167 else if (lex_match_id (lexer, "CASES"))
169 lex_match (lexer, T_EQUALS);
171 if (lex_match_id (lexer, "FROM"))
173 if (!lex_force_int_range (lexer, "FROM", 1, LONG_MAX))
175 cmd.first = lex_integer (lexer);
181 if (lex_match (lexer, T_TO) || lex_is_integer (lexer))
183 if (!lex_force_int_range (lexer, "TO", cmd.first, LONG_MAX))
185 cmd.last = lex_integer (lexer);
191 if (lex_match (lexer, T_BY))
193 if (!lex_force_int_range (lexer, "TO", 1, LONG_MAX))
195 cmd.step = lex_integer (lexer);
205 if (!parse_variables_const (lexer, dict, &cmd.vars, &cmd.n_vars,
212 dict_get_vars (dict, &cmd.vars, &cmd.n_vars, DC_SYSTEM | DC_SCRATCH);
213 return list_execute (&cmd, ds);