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/>. */
21 #include <sys/types.h>
23 #include "data/dataset.h"
24 #include "data/settings.h"
25 #include "data/subcase.h"
26 #include "data/variable.h"
27 #include "language/command.h"
28 #include "language/lexer/lexer.h"
29 #include "language/stats/sort-criteria.h"
30 #include "libpspp/message.h"
31 #include "math/sort.h"
33 #include "gl/xalloc.h"
36 #define _(msgid) gettext (msgid)
39 /* Performs the SORT CASES procedures. */
41 cmd_sort_cases (struct lexer *lexer, struct dataset *ds)
43 struct subcase ordering;
44 struct casereader *output;
47 lex_match (lexer, T_BY);
49 proc_cancel_temporary_transformations (ds);
50 subcase_init_empty (&ordering);
51 if (!parse_sort_criteria (lexer, dataset_dict (ds), &ordering, NULL, NULL))
52 return CMD_CASCADING_FAILURE;
54 if (settings_get_testing_mode () && lex_match (lexer, T_SLASH))
56 if (!lex_force_match_id (lexer, "BUFFERS") || !lex_match (lexer, T_EQUALS)
57 || !lex_force_int_range (lexer, "BUFFERS", 2, INT_MAX))
60 min_buffers = max_buffers = lex_integer (lexer);
65 proc_discard_output (ds);
66 output = sort_execute (proc_open_filtering (ds, false), &ordering);
67 ok = proc_commit (ds);
68 ok = dataset_set_source (ds, output) && ok;
72 max_buffers = INT_MAX;
74 subcase_destroy (&ordering);
75 return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;