1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2010, 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/>. */
19 #include "language/command.h"
21 #include "data/dataset.h"
22 #include "data/session.h"
23 #include "language/lexer/lexer.h"
24 #include "libpspp/message.h"
25 #include "output/pivot-table.h"
28 #define N_(msgid) msgid
29 #define _(msgid) gettext (msgid)
32 parse_window (struct lexer *lexer, unsigned int allowed,
33 enum dataset_display def)
35 if (!lex_match_id (lexer, "WINDOW"))
37 lex_match (lexer, T_EQUALS);
39 if (allowed & (1 << DATASET_MINIMIZED) && lex_match_id (lexer, "MINIMIZED"))
40 return DATASET_MINIMIZED;
41 else if (allowed & (1 << DATASET_ASIS) && lex_match_id (lexer, "ASIS"))
43 else if (allowed & (1 << DATASET_FRONT) && lex_match_id (lexer, "FRONT"))
45 else if (allowed & (1 << DATASET_HIDDEN) && lex_match_id (lexer, "HIDDEN"))
46 return DATASET_HIDDEN;
48 lex_error (lexer, NULL);
52 static struct dataset *
53 parse_dataset_name (struct lexer *lexer, struct session *session)
57 if (!lex_force_id (lexer))
60 ds = session_lookup_dataset (session, lex_tokcstr (lexer));
64 lex_error (lexer, _("There is no dataset named %s."), lex_tokcstr (lexer));
69 cmd_dataset_name (struct lexer *lexer, struct dataset *active)
73 if (!lex_force_id (lexer))
75 dataset_set_name (active, lex_tokcstr (lexer));
78 display = parse_window (lexer, (1 << DATASET_ASIS) | (1 << DATASET_FRONT),
82 else if (display != DATASET_ASIS)
83 dataset_set_display (active, display);
89 cmd_dataset_activate (struct lexer *lexer, struct dataset *active)
91 struct session *session = dataset_session (active);
95 ds = parse_dataset_name (lexer, session);
101 proc_execute (active);
102 session_set_active_dataset (session, ds);
103 if (dataset_name (active)[0] == '\0')
104 dataset_destroy (active);
108 display = parse_window (lexer, (1 << DATASET_ASIS) | (1 << DATASET_FRONT),
112 else if (display != DATASET_ASIS)
113 dataset_set_display (ds, display);
119 cmd_dataset_copy (struct lexer *lexer, struct dataset *old)
121 struct session *session = dataset_session (old);
126 /* Parse the entire command first. proc_execute() can attempt to parse
127 BEGIN DATA...END DATA and it will fail confusingly if we are in the
128 middle of the command at the point. */
129 if (!lex_force_id (lexer))
131 name = xstrdup (lex_tokcstr (lexer));
134 display = parse_window (lexer, ((1 << DATASET_MINIMIZED)
135 | (1 << DATASET_HIDDEN)
136 | (1 << DATASET_FRONT)),
144 if (session_lookup_dataset (session, name) == old)
147 dataset_set_name (old, "");
152 new = dataset_clone (old, name);
154 dataset_set_display (new, display);
161 cmd_dataset_declare (struct lexer *lexer, struct dataset *ds)
163 struct session *session = dataset_session (ds);
167 if (!lex_force_id (lexer))
170 new = session_lookup_dataset (session, lex_tokcstr (lexer));
172 new = dataset_create (session, lex_tokcstr (lexer));
175 display = parse_window (lexer, ((1 << DATASET_MINIMIZED)
176 | (1 << DATASET_HIDDEN)
177 | (1 << DATASET_FRONT)),
181 dataset_set_display (new, display);
187 dataset_close_cb (struct dataset *ds, void *session_)
189 struct session *session = session_;
191 if (ds != session_active_dataset (session))
192 dataset_destroy (ds);
196 cmd_dataset_close (struct lexer *lexer, struct dataset *ds)
198 struct session *session = dataset_session (ds);
200 if (lex_match (lexer, T_ALL))
202 session_for_each_dataset (session, dataset_close_cb, session);
203 dataset_set_name (session_active_dataset (session), "");
207 if (!lex_match (lexer, T_ASTERISK))
209 ds = parse_dataset_name (lexer, session);
214 if (ds == session_active_dataset (session))
215 dataset_set_name (ds, "");
217 dataset_destroy (ds);
224 dataset_display_cb (struct dataset *ds, void *p_)
226 struct dataset ***p = p_;
232 sort_datasets (const void *a_, const void *b_)
234 struct dataset *const *a = a_;
235 struct dataset *const *b = b_;
237 return strcmp (dataset_name (*a), dataset_name (*b));
241 cmd_dataset_display (struct lexer *lexer UNUSED, struct dataset *ds)
243 struct session *session = dataset_session (ds);
244 size_t n = session_n_datasets (session);
245 struct dataset **datasets = xmalloc (n * sizeof *datasets);
246 struct dataset **p = datasets;
247 session_for_each_dataset (session, dataset_display_cb, &p);
248 qsort (datasets, n, sizeof *datasets, sort_datasets);
250 struct pivot_table *table = pivot_table_create (N_("Datasets"));
252 struct pivot_dimension *datasets_dim = pivot_dimension_create (
253 table, PIVOT_AXIS_ROW, N_("Dataset"));
254 datasets_dim->hide_all_labels = true;
256 for (size_t i = 0; i < n; i++)
258 struct dataset *ds = datasets[i];
261 name = dataset_name (ds);
263 name = _("unnamed dataset");
265 char *text = (ds == session_active_dataset (session)
266 ? xasprintf ("%s (%s)", name, _("active dataset"))
269 int dataset_idx = pivot_category_create_leaf (
270 datasets_dim->root, pivot_value_new_integer (i));
272 pivot_table_put1 (table, dataset_idx,
273 pivot_value_new_user_text_nocopy (text));
278 pivot_table_submit (table);