d73a79c4b38310faa29102d054cecd0714443390
[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    Written by Ben Pfaff <blp@gnu.org>.
4
5    This program is free software; you can redistribute it and/or
6    modify it under the terms of the GNU General Public License as
7    published by the Free Software Foundation; either version 2 of the
8    License, or (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful, but
11    WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18    02110-1301, USA. */
19
20 #include <config.h>
21
22 #include <assert.h>
23 #include <stdlib.h>
24 #include <limits.h>
25
26 #include "sort-criteria.h"
27 #include <data/procedure.h>
28 #include <data/settings.h>
29 #include <data/variable.h>
30 #include <language/command.h>
31 #include <language/lexer/lexer.h>
32 #include <libpspp/alloc.h>
33 #include <libpspp/message.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 sort_criteria *criteria;
46   bool success = false;
47
48   lex_match (lexer, T_BY);
49
50   criteria = sort_parse_criteria (lexer, dataset_dict (ds), NULL, NULL, NULL, NULL);
51   if (criteria == NULL)
52     return CMD_CASCADING_FAILURE;
53
54   if (get_testing_mode () && lex_match (lexer, '/')) 
55     {
56       if (!lex_force_match_id (lexer, "BUFFERS") || !lex_match (lexer, '=')
57           || !lex_force_int (lexer))
58         goto done;
59
60       min_buffers = max_buffers = lex_integer (lexer);
61       allow_internal_sort = false;
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   success = sort_active_file_in_place (ds, criteria);
72
73  done:
74   min_buffers = 64;
75   max_buffers = INT_MAX;
76   allow_internal_sort = true;
77   
78   sort_destroy_criteria (criteria);
79   return success ? lex_end_of_command (lexer) : CMD_CASCADING_FAILURE;
80 }
81