Rename procedure.[ch] to dataset.[ch].
[pspp-builds.git] / 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 <assert.h>
20 #include <limits.h>
21 #include <stdlib.h>
22 #include <sys/types.h>
23
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"
33
34 #include "gl/xalloc.h"
35
36 #include "gettext.h"
37 #define _(msgid) gettext (msgid)
38
39
40 /* Performs the SORT CASES procedures. */
41 int
42 cmd_sort_cases (struct lexer *lexer, struct dataset *ds)
43 {
44   struct subcase ordering;
45   struct casereader *output;
46   bool ok = false;
47
48   lex_match (lexer, T_BY);
49
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;
54
55   if (settings_get_testing_mode () && lex_match (lexer, T_SLASH))
56     {
57       if (!lex_force_match_id (lexer, "BUFFERS") || !lex_match (lexer, T_EQUALS)
58           || !lex_force_int (lexer))
59         goto done;
60
61       min_buffers = max_buffers = lex_integer (lexer);
62       if (max_buffers < 2)
63         {
64           msg (SE, _("Buffer limit must be at least 2."));
65           goto done;
66         }
67
68       lex_get (lexer);
69     }
70
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;
75
76  done:
77   min_buffers = 64;
78   max_buffers = INT_MAX;
79
80   subcase_destroy (&ordering);
81   return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;
82 }
83