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/commands/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 output_split_file_values_peek (ds, group);
76 group = casereader_project (group, &sc);
77 group = casereader_select (group, lcmd->first - 1,
78 (lcmd->last != LONG_MAX ? lcmd->last
79 : CASENUMBER_MAX), lcmd->step);
81 struct pivot_table *table = pivot_table_create (N_("Data List"));
82 table->show_values = table->show_variables = SETTINGS_VALUE_SHOW_VALUE;
84 struct pivot_dimension *variables = pivot_dimension_create (
85 table, PIVOT_AXIS_COLUMN, N_("Variables"));
86 for (size_t i = 0; i < lcmd->n_vars; i++)
87 pivot_category_create_leaf (
88 variables->root, pivot_value_new_variable (lcmd->vars[i]));
90 struct pivot_dimension *cases = pivot_dimension_create (
91 table, PIVOT_AXIS_ROW, N_("Case Number"));
92 if (lcmd->number_cases)
93 cases->root->show_label = true;
95 cases->hide_all_labels = true;
97 casenumber case_num = lcmd->first;
99 for (; (c = casereader_read (group)) != NULL; case_unref (c))
101 int case_idx = pivot_category_create_leaf (
102 cases->root, pivot_value_new_integer (case_num));
103 case_num += lcmd->step;
105 for (size_t i = 0; i < lcmd->n_vars; i++)
106 pivot_table_put2 (table, i, case_idx,
107 pivot_value_new_var_value (
108 lcmd->vars[i], case_data_idx (c, i)));
110 casereader_destroy (group);
112 pivot_table_submit (table);
115 bool ok = casegrouper_destroy (grouper);
116 ok = proc_commit (ds) && ok;
118 subcase_uninit (&sc);
121 return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;
125 /* Parses and executes the LIST procedure. */
127 cmd_list (struct lexer *lexer, struct dataset *ds)
129 const struct dictionary *dict = dataset_dict (ds);
131 struct lst_cmd cmd = {
137 while (lex_token (lexer) != T_ENDCMD)
139 lex_match (lexer, T_SLASH);
140 if (lex_match_id (lexer, "VARIABLES"))
142 lex_match (lexer, T_EQUALS);
145 if (!parse_variables_const (lexer, dict, &cmd.vars, &cmd.n_vars,
149 else if (lex_match_id (lexer, "FORMAT"))
151 lex_match (lexer, T_EQUALS);
152 if (lex_match_id (lexer, "NUMBERED"))
153 cmd.number_cases = true;
154 else if (lex_match_id (lexer, "UNNUMBERED"))
155 cmd.number_cases = false;
158 lex_error_expecting (lexer, "NUMBERED", "UNNUMBERED");
162 else if (lex_match_id (lexer, "CASES"))
164 lex_match (lexer, T_EQUALS);
166 if (lex_match_id (lexer, "FROM"))
168 if (!lex_force_int_range (lexer, "FROM", 1, LONG_MAX))
170 cmd.first = lex_integer (lexer);
176 if (lex_match (lexer, T_TO) || lex_is_integer (lexer))
178 if (!lex_force_int_range (lexer, "TO", cmd.first, LONG_MAX))
180 cmd.last = lex_integer (lexer);
186 if (lex_match (lexer, T_BY))
188 if (!lex_force_int_range (lexer, "TO", 1, LONG_MAX))
190 cmd.step = lex_integer (lexer);
200 if (!parse_variables_const (lexer, dict, &cmd.vars, &cmd.n_vars,
207 dict_get_vars (dict, &cmd.vars, &cmd.n_vars, DC_SYSTEM | DC_SCRATCH);
208 return list_execute (&cmd, ds);