Whitespace changes only.
[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/pivot-table.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 N_(msgid) msgid
49 #define _(msgid) gettext (msgid)
50
51 enum numbering
52   {
53     format_unnumbered,
54     format_numbered
55   };
56
57
58 struct lst_cmd
59 {
60   long first;
61   long last;
62   long step;
63   const struct variable **v_variables;
64   size_t n_variables;
65   enum numbering numbering;
66 };
67
68
69 static int
70 list_execute (const struct lst_cmd *lcmd, struct dataset *ds)
71 {
72   const struct dictionary *dict = dataset_dict (ds);
73
74   bool ok;
75   int i;
76   struct casegrouper *grouper;
77   struct casereader *group;
78   struct subcase sc;
79
80   subcase_init_empty (&sc);
81   for (i = 0; i < lcmd->n_variables; i++)
82     subcase_add_var (&sc, lcmd->v_variables[i], SC_ASCEND);
83
84
85   grouper = casegrouper_create_splits (proc_open (ds), dict);
86   while (casegrouper_get_next_group (grouper, &group))
87     {
88       struct ccase *c = casereader_peek (group, 0);
89       if (c != NULL)
90         {
91           output_split_file_values (ds, c);
92           case_unref (c);
93         }
94
95       group = casereader_project (group, &sc);
96       group = casereader_select (group, lcmd->first - 1,
97                                  (lcmd->last != LONG_MAX ? lcmd->last
98                                   : CASENUMBER_MAX), lcmd->step);
99
100       struct pivot_table *table = pivot_table_create (N_("Data List"));
101       table->show_values = table->show_variables = SETTINGS_VALUE_SHOW_VALUE;
102
103       struct pivot_dimension *variables = pivot_dimension_create (
104         table, PIVOT_AXIS_COLUMN, N_("Variables"));
105       for (int i = 0; i < lcmd->n_variables; i++)
106         pivot_category_create_leaf (
107           variables->root, pivot_value_new_variable (lcmd->v_variables[i]));
108
109       struct pivot_dimension *cases = pivot_dimension_create (
110         table, PIVOT_AXIS_ROW, N_("Case Number"));
111       if (lcmd->numbering == format_numbered)
112         cases->root->show_label = true;
113       else
114         cases->hide_all_labels = true;
115
116       casenumber case_num = lcmd->first;
117       for (; (c = casereader_read (group)) != NULL; case_unref (c))
118         {
119           int case_idx = pivot_category_create_leaf (
120             cases->root, pivot_value_new_integer (case_num));
121           case_num += lcmd->step;
122
123           for (int i = 0; i < lcmd->n_variables; i++)
124             pivot_table_put2 (table, i, case_idx,
125                               pivot_value_new_var_value (
126                                 lcmd->v_variables[i], case_data_idx (c, i)));
127         }
128       casereader_destroy (group);
129
130       pivot_table_submit (table);
131     }
132   ok = casegrouper_destroy (grouper);
133   ok = proc_commit (ds) && ok;
134
135   subcase_destroy (&sc);
136   free (lcmd->v_variables);
137
138   return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;
139 }
140
141
142 /* Parses and executes the LIST procedure. */
143 int
144 cmd_list (struct lexer *lexer, struct dataset *ds)
145 {
146   struct lst_cmd cmd;
147   const struct dictionary *dict = dataset_dict (ds);
148
149   /* Fill in defaults. */
150   cmd.step = 1;
151   cmd.first = 1;
152   cmd.last = LONG_MAX;
153   cmd.n_variables = 0;
154   cmd.v_variables = NULL;
155   cmd.numbering = format_unnumbered;
156
157
158   while (lex_token (lexer) != T_ENDCMD)
159     {
160       lex_match (lexer, T_SLASH);
161       if (lex_match_id (lexer, "VARIABLES"))
162         {
163           lex_match (lexer, T_EQUALS);
164           if (! parse_variables_const (lexer, dict, &cmd.v_variables, &cmd.n_variables, 0))
165             {
166               msg (SE, _("No variables specified."));
167               return CMD_FAILURE;
168             }
169         }
170       else if (lex_match_id (lexer, "FORMAT"))
171         {
172           lex_match (lexer, T_EQUALS);
173           if (lex_match_id (lexer, "NUMBERED"))
174             {
175               cmd.numbering = format_numbered;
176             }
177           else if (lex_match_id (lexer, "UNNUMBERED"))
178             {
179               cmd.numbering = format_unnumbered;
180             }
181           else
182             {
183               lex_error (lexer, NULL);
184               goto error;
185             }
186         }
187       /* example: LIST /CASES=FROM 1 TO 25 BY 5. */
188       else if (lex_match_id (lexer, "CASES"))
189         {
190           lex_match (lexer, T_EQUALS);
191           if (lex_match_id (lexer, "FROM") && lex_force_int (lexer))
192             {
193               cmd.first = lex_integer (lexer);
194               lex_get (lexer);
195             }
196
197           if ((lex_match (lexer, T_TO) && lex_force_int (lexer))
198               || lex_is_integer (lexer))
199             {
200               cmd.last = lex_integer (lexer);
201               lex_get (lexer);
202             }
203
204           if (lex_match (lexer, T_BY) && lex_force_int (lexer))
205             {
206               cmd.step = lex_integer (lexer);
207               lex_get (lexer);
208             }
209         }
210       else if (! parse_variables_const (lexer, dict, &cmd.v_variables, &cmd.n_variables, 0))
211         {
212           return CMD_FAILURE;
213         }
214     }
215
216
217   /* Verify arguments. */
218   if (cmd.first > cmd.last)
219     {
220       int t;
221       msg (SW, _("The first case (%ld) specified precedes the last case (%ld) "
222                  "specified.  The values will be swapped."), cmd.first, cmd.last);
223       t = cmd.first;
224       cmd.first = cmd.last;
225       cmd.last = t;
226     }
227
228   if (cmd.first < 1)
229     {
230       msg (SW, _("The first case (%ld) to list is numbered less than 1.  "
231                  "The value is being reset to 1."), cmd.first);
232       cmd.first = 1;
233     }
234
235   if (cmd.last < 1)
236     {
237       msg (SW, _("The last case (%ld) to list is numbered less than 1.  "
238                  "The value is being reset to 1."), cmd.last);
239       cmd.last = 1;
240     }
241
242   if (cmd.step < 1)
243     {
244       msg (SW, _("The step value %ld is less than 1.  The value is being "
245                  "reset to 1."), cmd.step);
246       cmd.step = 1;
247     }
248
249   /* If no variables were explicitly provided, then default to ALL */
250   if (cmd.n_variables == 0)
251     dict_get_vars (dict, &cmd.v_variables, &cmd.n_variables,
252                    DC_SYSTEM | DC_SCRATCH);
253
254   return list_execute (&cmd, ds);
255
256  error:
257   free (cmd.v_variables);
258   return CMD_FAILURE;
259 }
260