86c31f854b2832738ee33a7169f4836208b8efe9
[pspp-builds.git] / src / language / stats / sort-cases.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or
5    modify it under the terms of the GNU General Public License as
6    published by the Free Software Foundation; either version 2 of the
7    License, or (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful, but
10    WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    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, write to the Free Software
16    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17    02110-1301, USA. */
18
19 #include <config.h>
20
21 #include <assert.h>
22 #include <stdlib.h>
23 #include <limits.h>
24
25 #include "sort-criteria.h"
26 #include <data/procedure.h>
27 #include <data/settings.h>
28 #include <data/variable.h>
29 #include <language/command.h>
30 #include <language/lexer/lexer.h>
31 #include <libpspp/alloc.h>
32 #include <libpspp/message.h>
33 #include <data/case-ordering.h>
34 #include <math/sort.h>
35 #include <sys/types.h>
36
37 #include "gettext.h"
38 #define _(msgid) gettext (msgid)
39
40
41 /* Performs the SORT CASES procedures. */
42 int
43 cmd_sort_cases (struct lexer *lexer, struct dataset *ds)
44 {
45   struct case_ordering *ordering;
46   struct casereader *output;
47   bool ok = false;
48
49   lex_match (lexer, T_BY);
50
51   proc_cancel_temporary_transformations (ds);
52   ordering = parse_case_ordering (lexer, dataset_dict (ds), NULL);
53   if (ordering == NULL)
54     return CMD_CASCADING_FAILURE;
55
56   if (get_testing_mode () && lex_match (lexer, '/'))
57     {
58       if (!lex_force_match_id (lexer, "BUFFERS") || !lex_match (lexer, '=')
59           || !lex_force_int (lexer))
60         goto done;
61
62       min_buffers = max_buffers = lex_integer (lexer);
63       if (max_buffers < 2)
64         {
65           msg (SE, _("Buffer limit must be at least 2."));
66           goto done;
67         }
68
69       lex_get (lexer);
70     }
71
72   proc_discard_output (ds);
73   output = sort_execute (proc_open (ds), ordering);
74   ordering = NULL;
75   ok = proc_commit (ds);
76   ok = proc_set_active_file_data (ds, output) && ok;
77
78  done:
79   min_buffers = 64;
80   max_buffers = INT_MAX;
81
82   case_ordering_destroy (ordering);
83   return ok ? lex_end_of_command (lexer) : CMD_CASCADING_FAILURE;
84 }
85