1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2006, 2009-2011 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 "libpspp/compiler.h"
35 #include "libpspp/ll.h"
36 #include "libpspp/message.h"
37 #include "libpspp/misc.h"
38 #include "output/tab.h"
39 #include "output/table-item.h"
41 #include "gl/intprops.h"
42 #include "gl/minmax.h"
43 #include "gl/xalloc.h"
44 #include "gl/xmalloca.h"
47 #define _(msgid) gettext (msgid)
53 *variables=varlist("PV_NO_SCRATCH");
54 cases=:from n:first,"%s>0"/by n:step,"%s>0"/ *to n:last,"%s>0";
55 +format=numbering:numbered/!unnumbered,
62 static struct cmd_list cmd;
64 /* Parses and executes the LIST procedure. */
66 cmd_list (struct lexer *lexer, struct dataset *ds)
68 struct dictionary *dict = dataset_dict (ds);
69 struct casegrouper *grouper;
70 struct casereader *group;
75 if (!parse_list (lexer, ds, &cmd, NULL))
78 /* Fill in defaults. */
79 if (cmd.step == LONG_MIN)
81 if (cmd.first == LONG_MIN)
83 if (cmd.last == LONG_MIN)
85 if (!cmd.sbc_variables)
86 dict_get_vars (dict, &cmd.v_variables, &cmd.n_variables,
87 DC_SYSTEM | DC_SCRATCH);
88 if (cmd.n_variables == 0)
90 msg (SE, _("No variables specified."));
94 /* Verify arguments. */
95 if (cmd.first > cmd.last)
98 msg (SW, _("The first case (%ld) specified precedes the last case (%ld) "
99 "specified. The values will be swapped."), cmd.first, cmd.last);
101 cmd.first = cmd.last;
106 msg (SW, _("The first case (%ld) to list is less than 1. The value is "
107 "being reset to 1."), cmd.first);
112 msg (SW, _("The last case (%ld) to list is less than 1. The value is "
113 "being reset to 1."), cmd.last);
118 msg (SW, _("The step value %ld is less than 1. The value is being "
119 "reset to 1."), cmd.step);
123 subcase_init_empty (&sc);
124 for (i = 0; i < cmd.n_variables; i++)
125 subcase_add_var (&sc, cmd.v_variables[i], SC_ASCEND);
127 grouper = casegrouper_create_splits (proc_open (ds), dict);
128 while (casegrouper_get_next_group (grouper, &group))
133 group = casereader_project (group, &sc);
134 if (cmd.numbering == LST_NUMBERED)
135 group = casereader_create_arithmetic_sequence (group, 1, 1);
136 group = casereader_select (group, cmd.first - 1,
137 (cmd.last != LONG_MAX ? cmd.last
138 : CASENUMBER_MAX), cmd.step);
140 ccase = casereader_peek (group, 0);
143 output_split_file_values (ds, ccase);
147 if (cmd.numbering == LST_NUMBERED)
153 width = cmd.last == LONG_MAX ? 5 : intlog10 (cmd.last);
154 fmt = fmt_for_output (FMT_F, width, 0);
155 col = caseproto_get_n_widths (casereader_get_proto (group)) - 1;
157 t = table_from_casereader (group, col, _("Case Number"), &fmt);
162 for (i = 0; i < cmd.n_variables; i++)
164 const struct variable *var = cmd.v_variables[i];
167 c = table_from_casereader (group, i, var_get_name (var),
168 var_get_print_format (var));
169 t = table_hpaste (t, c);
172 casereader_destroy (group);
174 table_item_submit (table_item_create (t, "Data List"));
176 ok = casegrouper_destroy (grouper);
177 ok = proc_commit (ds) && ok;
179 subcase_destroy (&sc);
180 free (cmd.v_variables);
182 return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;