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)
62 const struct variable **v_variables;
64 enum numbering numbering;
69 list_execute (const struct lst_cmd *lcmd, struct dataset *ds)
71 const struct dictionary *dict = dataset_dict (ds);
75 struct casegrouper *grouper;
76 struct casereader *group;
79 subcase_init_empty (&sc);
80 for (i = 0; i < lcmd->n_variables; i++)
81 subcase_add_var (&sc, lcmd->v_variables[i], SC_ASCEND);
84 grouper = casegrouper_create_splits (proc_open (ds), dict);
85 while (casegrouper_get_next_group (grouper, &group))
87 struct ccase *c = casereader_peek (group, 0);
90 output_split_file_values (ds, c);
94 group = casereader_project (group, &sc);
95 group = casereader_select (group, lcmd->first - 1,
96 (lcmd->last != LONG_MAX ? lcmd->last
97 : CASENUMBER_MAX), lcmd->step);
99 struct pivot_table *table = pivot_table_create (N_("Data List"));
100 table->show_values = table->show_variables = SETTINGS_VALUE_SHOW_VALUE;
102 struct pivot_dimension *variables = pivot_dimension_create (
103 table, PIVOT_AXIS_COLUMN, N_("Variables"));
104 for (int i = 0; i < lcmd->n_variables; i++)
105 pivot_category_create_leaf (
106 variables->root, pivot_value_new_variable (lcmd->v_variables[i]));
108 struct pivot_dimension *cases = pivot_dimension_create (
109 table, PIVOT_AXIS_ROW, N_("Case Number"));
110 if (lcmd->numbering == format_numbered)
111 cases->root->show_label = true;
113 cases->hide_all_labels = true;
115 casenumber case_num = lcmd->first;
116 for (; (c = casereader_read (group)) != NULL; case_unref (c))
118 int case_idx = pivot_category_create_leaf (
119 cases->root, pivot_value_new_integer (case_num));
120 case_num += lcmd->step;
122 for (int i = 0; i < lcmd->n_variables; i++)
123 pivot_table_put2 (table, i, case_idx,
124 pivot_value_new_var_value (
125 lcmd->v_variables[i], case_data_idx (c, i)));
127 casereader_destroy (group);
129 pivot_table_submit (table);
131 ok = casegrouper_destroy (grouper);
132 ok = proc_commit (ds) && ok;
134 subcase_uninit (&sc);
135 free (lcmd->v_variables);
137 return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;
141 /* Parses and executes the LIST procedure. */
143 cmd_list (struct lexer *lexer, struct dataset *ds)
146 const struct dictionary *dict = dataset_dict (ds);
148 /* Fill in defaults. */
153 cmd.v_variables = NULL;
154 cmd.numbering = format_unnumbered;
157 while (lex_token (lexer) != T_ENDCMD)
159 lex_match (lexer, T_SLASH);
160 if (lex_match_id (lexer, "VARIABLES"))
162 lex_match (lexer, T_EQUALS);
163 if (! parse_variables_const (lexer, dict, &cmd.v_variables, &cmd.n_variables, 0))
165 msg (SE, _("No variables specified."));
169 else if (lex_match_id (lexer, "FORMAT"))
171 lex_match (lexer, T_EQUALS);
172 if (lex_match_id (lexer, "NUMBERED"))
174 cmd.numbering = format_numbered;
176 else if (lex_match_id (lexer, "UNNUMBERED"))
178 cmd.numbering = format_unnumbered;
182 lex_error (lexer, NULL);
186 /* example: LIST /CASES=FROM 1 TO 25 BY 5. */
187 else if (lex_match_id (lexer, "CASES"))
189 lex_match (lexer, T_EQUALS);
190 if (lex_match_id (lexer, "FROM") && lex_force_int (lexer))
192 cmd.first = lex_integer (lexer);
196 if ((lex_match (lexer, T_TO) && lex_force_int (lexer))
197 || lex_is_integer (lexer))
199 cmd.last = lex_integer (lexer);
203 if (lex_match (lexer, T_BY) && lex_force_int (lexer))
205 cmd.step = lex_integer (lexer);
209 else if (! parse_variables_const (lexer, dict, &cmd.v_variables, &cmd.n_variables, 0))
216 /* Verify arguments. */
217 if (cmd.first > cmd.last)
220 msg (SW, _("The first case (%ld) specified precedes the last case (%ld) "
221 "specified. The values will be swapped."), cmd.first, cmd.last);
223 cmd.first = cmd.last;
229 msg (SW, _("The first case (%ld) to list is numbered less than 1. "
230 "The value is being reset to 1."), cmd.first);
236 msg (SW, _("The last case (%ld) to list is numbered less than 1. "
237 "The value is being reset to 1."), cmd.last);
243 msg (SW, _("The step value %ld is less than 1. The value is being "
244 "reset to 1."), cmd.step);
248 /* If no variables were explicitly provided, then default to ALL */
249 if (cmd.n_variables == 0)
250 dict_get_vars (dict, &cmd.v_variables, &cmd.n_variables,
251 DC_SYSTEM | DC_SCRATCH);
253 return list_execute (&cmd, ds);
256 free (cmd.v_variables);