dd0387418ac30a9c92b25ba84aa1ed4e2bc25bf5
[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 #include <sys/types.h>
22 #include <assert.h>
23 #include <stdlib.h>
24 #include <limits.h>
25 #include <libpspp/alloc.h>
26 #include <language/command.h>
27 #include <libpspp/message.h>
28 #include <language/lexer/lexer.h>
29 #include <data/settings.h>
30 #include "sort-criteria.h"
31 #include <math/sort.h>
32 #include <data/variable.h>
33
34 #include "gettext.h"
35 #define _(msgid) gettext (msgid)
36
37
38 /* Performs the SORT CASES procedures. */
39 int
40 cmd_sort_cases (void)
41 {
42   struct sort_criteria *criteria;
43   bool success = false;
44
45   lex_match (T_BY);
46
47   criteria = sort_parse_criteria (default_dict, NULL, NULL, NULL, NULL);
48   if (criteria == NULL)
49     return CMD_CASCADING_FAILURE;
50
51   if (get_testing_mode () && lex_match ('/')) 
52     {
53       if (!lex_force_match_id ("BUFFERS") || !lex_match ('=')
54           || !lex_force_int ())
55         goto done;
56
57       min_buffers = max_buffers = lex_integer ();
58       allow_internal_sort = false;
59       if (max_buffers < 2) 
60         {
61           msg (SE, _("Buffer limit must be at least 2."));
62           goto done;
63         }
64
65       lex_get ();
66     }
67
68   success = sort_active_file_in_place (criteria);
69
70  done:
71   min_buffers = 64;
72   max_buffers = INT_MAX;
73   allow_internal_sort = true;
74   
75   sort_destroy_criteria (criteria);
76   return success ? lex_end_of_command () : CMD_CASCADING_FAILURE;
77 }
78