069451510d532ad5b76a3b771dbef500d370d90e
[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;
44   struct casereader *output;
45   bool ok = false;
46
47   lex_match (lexer, T_BY);
48
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;
53
54   if (settings_get_testing_mode () && lex_match (lexer, T_SLASH))
55     {
56       if (!lex_force_match_id (lexer, "BUFFERS") || !lex_match (lexer, T_EQUALS)
57           || !lex_force_int_range (lexer, "BUFFERS", 2, INT_MAX))
58         goto done;
59
60       min_buffers = max_buffers = lex_integer (lexer);
61
62       lex_get (lexer);
63     }
64
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;
69
70  done:
71   min_buffers = 64;
72   max_buffers = INT_MAX;
73
74   subcase_destroy (&ordering);
75   return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;
76 }
77