LIST: Improve error messages.
[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
41 #include "gl/intprops.h"
42 #include "gl/minmax.h"
43 #include "gl/xalloc.h"
44 #include "gl/xmalloca.h"
45
46 #include "gettext.h"
47 #define N_(msgid) msgid
48 #define _(msgid) gettext (msgid)
49
50 struct lst_cmd
51   {
52     long first;
53     long last;
54     long step;
55     const struct variable **vars;
56     size_t n_vars;
57     bool number_cases;
58   };
59
60 static int
61 list_execute (const struct lst_cmd *lcmd, struct dataset *ds)
62 {
63   const struct dictionary *dict = dataset_dict (ds);
64
65   struct subcase sc;
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);
69
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))
74     {
75       struct ccase *c = casereader_peek (group, 0);
76       if (c != NULL)
77         {
78           output_split_file_values (ds, c);
79           case_unref (c);
80         }
81
82       group = casereader_project (group, &sc);
83       group = casereader_select (group, lcmd->first - 1,
84                                  (lcmd->last != LONG_MAX ? lcmd->last
85                                   : CASENUMBER_MAX), lcmd->step);
86
87       struct pivot_table *table = pivot_table_create (N_("Data List"));
88       table->show_values = table->show_variables = SETTINGS_VALUE_SHOW_VALUE;
89
90       struct pivot_dimension *variables = pivot_dimension_create (
91         table, PIVOT_AXIS_COLUMN, N_("Variables"));
92       for (size_t i = 0; i < lcmd->n_vars; i++)
93         pivot_category_create_leaf (
94           variables->root, pivot_value_new_variable (lcmd->vars[i]));
95
96       struct pivot_dimension *cases = pivot_dimension_create (
97         table, PIVOT_AXIS_ROW, N_("Case Number"));
98       if (lcmd->number_cases)
99         cases->root->show_label = true;
100       else
101         cases->hide_all_labels = true;
102
103       casenumber case_num = lcmd->first;
104       for (; (c = casereader_read (group)) != NULL; case_unref (c))
105         {
106           int case_idx = pivot_category_create_leaf (
107             cases->root, pivot_value_new_integer (case_num));
108           case_num += lcmd->step;
109
110           for (size_t i = 0; i < lcmd->n_vars; i++)
111             pivot_table_put2 (table, i, case_idx,
112                               pivot_value_new_var_value (
113                                 lcmd->vars[i], case_data_idx (c, i)));
114         }
115       casereader_destroy (group);
116
117       pivot_table_submit (table);
118     }
119
120   bool ok = casegrouper_destroy (grouper);
121   ok = proc_commit (ds) && ok;
122
123   subcase_uninit (&sc);
124   free (lcmd->vars);
125
126   return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;
127 }
128
129
130 /* Parses and executes the LIST procedure. */
131 int
132 cmd_list (struct lexer *lexer, struct dataset *ds)
133 {
134   const struct dictionary *dict = dataset_dict (ds);
135
136   struct lst_cmd cmd = {
137     .step = 1,
138     .first = 1,
139     .last = LONG_MAX,
140   };
141
142   while (lex_token (lexer) != T_ENDCMD)
143     {
144       lex_match (lexer, T_SLASH);
145       if (lex_match_id (lexer, "VARIABLES"))
146         {
147           lex_match (lexer, T_EQUALS);
148           free (cmd.vars);
149           cmd.vars = NULL;
150           if (!parse_variables_const (lexer, dict, &cmd.vars, &cmd.n_vars,
151                                       PV_DUPLICATE))
152             goto error;
153         }
154       else if (lex_match_id (lexer, "FORMAT"))
155         {
156           lex_match (lexer, T_EQUALS);
157           if (lex_match_id (lexer, "NUMBERED"))
158             cmd.number_cases = true;
159           else if (lex_match_id (lexer, "UNNUMBERED"))
160             cmd.number_cases = false;
161           else
162             {
163               lex_error_expecting (lexer, "NUMBERED", "UNNUMBERED");
164               goto error;
165             }
166         }
167       else if (lex_match_id (lexer, "CASES"))
168         {
169           lex_match (lexer, T_EQUALS);
170
171           if (lex_match_id (lexer, "FROM"))
172             {
173               if (!lex_force_int_range (lexer, "FROM", 1, LONG_MAX))
174                 goto error;
175               cmd.first = lex_integer (lexer);
176               lex_get (lexer);
177             }
178           else
179             cmd.first = 1;
180
181           if (lex_match (lexer, T_TO) || lex_is_integer (lexer))
182             {
183               if (!lex_force_int_range (lexer, "TO", cmd.first, LONG_MAX))
184                 goto error;
185               cmd.last = lex_integer (lexer);
186               lex_get (lexer);
187             }
188           else
189             cmd.last = LONG_MAX;
190
191           if (lex_match (lexer, T_BY))
192             {
193               if (!lex_force_int_range (lexer, "TO", 1, LONG_MAX))
194                 goto error;
195               cmd.step = lex_integer (lexer);
196               lex_get (lexer);
197             }
198           else
199             cmd.step = 1;
200         }
201       else
202         {
203           free (cmd.vars);
204           cmd.vars = NULL;
205           if (!parse_variables_const (lexer, dict, &cmd.vars, &cmd.n_vars,
206                                        PV_DUPLICATE))
207             goto error;
208         }
209     }
210
211   if (!cmd.n_vars)
212     dict_get_vars (dict, &cmd.vars, &cmd.n_vars, DC_SYSTEM | DC_SCRATCH);
213   return list_execute (&cmd, ds);
214
215  error:
216   free (cmd.vars);
217   return CMD_FAILURE;
218 }