1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 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/>. */
22 #include <sys/types.h>
24 #include "data/dataset.h"
25 #include "data/settings.h"
26 #include "data/subcase.h"
27 #include "data/variable.h"
28 #include "language/command.h"
29 #include "language/lexer/lexer.h"
30 #include "language/stats/sort-criteria.h"
31 #include "libpspp/message.h"
32 #include "math/sort.h"
34 #include "gl/xalloc.h"
37 #define _(msgid) gettext (msgid)
40 /* Performs the SORT CASES procedures. */
42 cmd_sort_cases (struct lexer *lexer, struct dataset *ds)
44 struct subcase ordering;
45 struct casereader *output;
48 lex_match (lexer, T_BY);
50 proc_cancel_temporary_transformations (ds);
51 subcase_init_empty (&ordering);
52 if (!parse_sort_criteria (lexer, dataset_dict (ds), &ordering, NULL, NULL))
53 return CMD_CASCADING_FAILURE;
55 if (settings_get_testing_mode () && lex_match (lexer, T_SLASH))
57 if (!lex_force_match_id (lexer, "BUFFERS") || !lex_match (lexer, T_EQUALS)
58 || !lex_force_int (lexer))
61 min_buffers = max_buffers = lex_integer (lexer);
64 msg (SE, _("Buffer limit must be at least 2."));
71 proc_discard_output (ds);
72 output = sort_execute (proc_open (ds), &ordering);
73 ok = proc_commit (ds);
74 ok = proc_set_active_file_data (ds, output) && ok;
78 max_buffers = INT_MAX;
80 subcase_destroy (&ordering);
81 return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;