Remove "Written by Ben Pfaff <blp@gnu.org>" lines everywhere.
[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 <math/sort.h>
34 #include <sys/types.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 sort_criteria *criteria;
45   bool success = false;
46
47   lex_match (lexer, T_BY);
48
49   criteria = sort_parse_criteria (lexer, dataset_dict (ds), NULL, NULL, NULL, NULL);
50   if (criteria == NULL)
51     return CMD_CASCADING_FAILURE;
52
53   if (get_testing_mode () && lex_match (lexer, '/')) 
54     {
55       if (!lex_force_match_id (lexer, "BUFFERS") || !lex_match (lexer, '=')
56           || !lex_force_int (lexer))
57         goto done;
58
59       min_buffers = max_buffers = lex_integer (lexer);
60       allow_internal_sort = false;
61       if (max_buffers < 2) 
62         {
63           msg (SE, _("Buffer limit must be at least 2."));
64           goto done;
65         }
66
67       lex_get (lexer);
68     }
69
70   success = sort_active_file_in_place (ds, criteria);
71
72  done:
73   min_buffers = 64;
74   max_buffers = INT_MAX;
75   allow_internal_sort = true;
76   
77   sort_destroy_criteria (criteria);
78   return success ? lex_end_of_command (lexer) : CMD_CASCADING_FAILURE;
79 }
80