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