LIST: Allow a subset of the CASES settings to be specified.
[pspp] / src / language / data-io / list.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2006, 2009-2011, 2013, 2014, 2016 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 <stdint.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22
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/tab.h"
40 #include "output/table-item.h"
41
42 #include "gl/intprops.h"
43 #include "gl/minmax.h"
44 #include "gl/xalloc.h"
45 #include "gl/xmalloca.h"
46
47 #include "gettext.h"
48 #define _(msgid) gettext (msgid)
49
50 enum numbering
51   {
52     format_unnumbered,
53     format_numbered
54   };
55
56
57 struct lst_cmd
58 {
59   long first;
60   long last;
61   long step;
62   const struct variable **v_variables;
63   size_t n_variables;
64   enum numbering numbering;
65 };
66
67
68 static int
69 list_execute (const struct lst_cmd *lcmd, struct dataset *ds)
70 {
71   const struct dictionary *dict = dataset_dict (ds);
72
73   bool ok;
74   int i;
75   struct casegrouper *grouper;
76   struct casereader *group;
77   struct subcase sc;
78
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);
82
83
84   grouper = casegrouper_create_splits (proc_open (ds), dict);
85   while (casegrouper_get_next_group (grouper, &group))
86     {
87       struct ccase *ccase;
88       struct table *t;
89
90       ccase = casereader_peek (group, 0);
91       if (ccase != NULL)
92         {
93           output_split_file_values (ds, ccase);
94           case_unref (ccase);
95         }
96
97       group = casereader_project (group, &sc);
98       if (lcmd->numbering == format_numbered)
99         group = casereader_create_arithmetic_sequence (group, 1, 1);
100       group = casereader_select (group, lcmd->first - 1,
101                                  (lcmd->last != LONG_MAX ? lcmd->last
102                                   : CASENUMBER_MAX), lcmd->step);
103
104       if (lcmd->numbering == format_numbered)
105         {
106           struct fmt_spec fmt;
107           size_t col;
108           int width;
109
110           width = lcmd->last == LONG_MAX ? 5 : intlog10 (lcmd->last);
111           fmt = fmt_for_output (FMT_F, width, 0);
112           col = caseproto_get_n_widths (casereader_get_proto (group)) - 1;
113
114           t = table_from_casereader (group, col, _("Case Number"), &fmt);
115         }
116       else
117         t = NULL;
118
119       for (i = 0; i < lcmd->n_variables; i++)
120         {
121           const struct variable *var = lcmd->v_variables[i];
122           struct table *c;
123
124           c = table_from_casereader (group, i, var_get_name (var),
125                                      var_get_print_format (var));
126           t = table_hpaste (t, c);
127         }
128
129       casereader_destroy (group);
130
131       table_item_submit (table_item_create (t, "Data List", NULL));
132     }
133   ok = casegrouper_destroy (grouper);
134   ok = proc_commit (ds) && ok;
135
136   subcase_destroy (&sc);
137   free (lcmd->v_variables);
138
139   return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;
140 }
141
142
143 /* Parses and executes the LIST procedure. */
144 int
145 cmd_list (struct lexer *lexer, struct dataset *ds)
146 {
147   struct lst_cmd cmd;
148   const struct dictionary *dict = dataset_dict (ds);
149
150   /* Fill in defaults. */
151   cmd.step = 1;
152   cmd.first = 1;
153   cmd.last = LONG_MAX;
154   cmd.n_variables = 0;
155   cmd.v_variables = NULL;
156   cmd.numbering = format_unnumbered;
157
158
159   while (lex_token (lexer) != T_ENDCMD)
160     {
161       lex_match (lexer, T_SLASH);
162       if (lex_match_id (lexer, "VARIABLES") )
163         {
164           lex_match (lexer, T_EQUALS);
165           if (! parse_variables_const (lexer, dict, &cmd.v_variables, &cmd.n_variables, 0 ))
166             {
167               msg (SE, _("No variables specified."));
168               return CMD_FAILURE;
169             }
170         }
171       else if (lex_match_id (lexer, "FORMAT") )
172         {
173           lex_match (lexer, T_EQUALS);
174           if (lex_match_id (lexer, "NUMBERED") )
175             {
176               cmd.numbering = format_numbered;
177             }
178           else if (lex_match_id (lexer, "UNNUMBERED") )
179             {
180               cmd.numbering = format_unnumbered;
181             }
182           else
183             {
184               lex_error (lexer, NULL);
185               goto error;
186             }
187         }
188       /* example: LIST /CASES=FROM 1 TO 25 BY 5. */
189       else if (lex_match_id (lexer, "CASES"))
190         {
191           lex_match (lexer, T_EQUALS);
192           if (lex_match_id (lexer, "FROM") && lex_force_int (lexer))
193             {
194               cmd.first = lex_integer (lexer);
195               lex_get (lexer);
196             }
197
198           if ((lex_match (lexer, T_TO) && lex_force_int (lexer))
199               || lex_is_integer (lexer))
200             {
201               cmd.last = lex_integer (lexer);
202               lex_get (lexer);
203             }
204
205           if (lex_match (lexer, T_BY) && lex_force_int (lexer))
206             {
207               cmd.step = lex_integer (lexer);
208               lex_get (lexer);
209             }
210         }
211       else if (! parse_variables_const (lexer, dict, &cmd.v_variables, &cmd.n_variables, 0 ))
212         {
213           return CMD_FAILURE;
214         }
215     }
216         
217
218   /* Verify arguments. */
219   if (cmd.first > cmd.last)
220     {
221       int t;
222       msg (SW, _("The first case (%ld) specified precedes the last case (%ld) "
223                  "specified.  The values will be swapped."), cmd.first, cmd.last);
224       t = cmd.first;
225       cmd.first = cmd.last;
226       cmd.last = t;
227     }
228
229   if (cmd.first < 1)
230     {
231       msg (SW, _("The first case (%ld) to list is less than 1.  The value is "
232                  "being reset to 1."), cmd.first);
233       cmd.first = 1;
234     }
235
236   if (cmd.last < 1)
237     {
238       msg (SW, _("The last case (%ld) to list is less than 1.  The value is "
239                  "being reset to 1."), cmd.last);
240       cmd.last = 1;
241     }
242
243   if (cmd.step < 1)
244     {
245       msg (SW, _("The step value %ld is less than 1.  The value is being "
246                  "reset to 1."), cmd.step);
247       cmd.step = 1;
248     }
249
250   /* If no variables were explicitly provided, then default to ALL */
251   if (cmd.n_variables == 0)
252     dict_get_vars (dict, &cmd.v_variables, &cmd.n_variables,
253                    DC_SYSTEM | DC_SCRATCH);
254
255   return list_execute (&cmd, ds);
256
257  error:
258   free (cmd.v_variables);
259   return CMD_FAILURE;
260 }
261