Remove unneeded #includes.
[pspp] / src / language / stats / sort-cases.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2010, 2011 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 <limits.h>
20 #include <stdlib.h>
21 #include <sys/types.h>
22
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"
32
33 #include "gl/xalloc.h"
34
35 #include "gettext.h"
36 #define _(msgid) gettext (msgid)
37
38
39 /* Performs the SORT CASES procedures. */
40 int
41 cmd_sort_cases (struct lexer *lexer, struct dataset *ds)
42 {
43   struct subcase ordering = SUBCASE_EMPTY_INITIALIZER;
44   bool ok = false;
45
46   lex_match (lexer, T_BY);
47
48   proc_cancel_temporary_transformations (ds);
49   if (!parse_sort_criteria (lexer, dataset_dict (ds), &ordering, NULL, NULL))
50     return CMD_CASCADING_FAILURE;
51
52   if (settings_get_testing_mode () && lex_match (lexer, T_SLASH))
53     {
54       if (!lex_force_match_id (lexer, "BUFFERS"))
55         goto done;
56       lex_match (lexer, T_EQUALS);
57       if (!lex_force_int_range (lexer, "BUFFERS", 2, INT_MAX))
58         goto done;
59       min_buffers = max_buffers = lex_integer (lexer);
60       lex_get (lexer);
61     }
62
63   proc_discard_output (ds);
64   struct casereader *output = sort_execute (proc_open_filtering (ds, false),
65                                             &ordering);
66   ok = proc_commit (ds);
67   ok = dataset_set_source (ds, output) && ok;
68
69  done:
70   min_buffers = 64;
71   max_buffers = INT_MAX;
72
73   subcase_uninit (&ordering);
74   return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;
75 }
76