Separate settings and the SET command, for modularity.
[pspp-builds.git] / src / sort.c
index bb4220b75943a231d6bdd5395d6615bf3acef70a..8dad3566ccdcca1ad8e9fabf63ace81b42d7019f 100644 (file)
@@ -26,7 +26,7 @@
 #include <errno.h>
 #include "algorithm.h"
 #include "alloc.h"
-#include "bool.h"
+#include <stdbool.h>
 #include "case.h"
 #include "casefile.h"
 #include "command.h"
@@ -42,6 +42,9 @@
 #include "vfm.h"
 #include "vfmP.h"
 
+#include "gettext.h"
+#define _(msgid) gettext (msgid)
+
 #include "debug-print.h"
 
 /* These should only be changed for testing purposes. */
@@ -69,7 +72,7 @@ cmd_sort_cases (void)
   if (criteria == NULL)
     return CMD_FAILURE;
 
-  if (test_mode && lex_match ('/')) 
+  if (get_testing_mode () && lex_match ('/')) 
     {
       if (!lex_force_match_id ("BUFFERS") || !lex_match ('=')
           || !lex_force_int ())
@@ -192,7 +195,7 @@ do_internal_sort (struct casereader *reader,
   dst = casefile_create (casefile_get_value_cnt (src));
   if (case_cnt != 0) 
     {
-      struct indexed_case *cases = malloc (sizeof *cases * case_cnt);
+      struct indexed_case *cases = nmalloc (sizeof *cases, case_cnt);
       if (cases != NULL) 
         {
           unsigned long i;
@@ -275,7 +278,7 @@ do_external_sort (struct casereader *reader,
   xsrt->value_cnt = casefile_get_value_cnt (casereader_get_casefile (reader));
   xsrt->run_cap = 512;
   xsrt->run_cnt = 0;
-  xsrt->runs = xmalloc (sizeof *xsrt->runs * xsrt->run_cap);
+  xsrt->runs = xnmalloc (xsrt->run_cap, sizeof *xsrt->runs);
   if (write_runs (xsrt, reader))
     {
       struct casefile *output = merge (xsrt);
@@ -440,16 +443,17 @@ allocate_cases (struct initial_run_state *irs)
   approx_case_cost = (sizeof *irs->records
                       + irs->xsrt->value_cnt * sizeof (union value)
                       + 4 * sizeof (void *));
-  max_cases = get_max_workspace() / approx_case_cost;
+  max_cases = get_workspace() / approx_case_cost;
   if (max_cases > max_buffers)
     max_cases = max_buffers;
-  irs->records = malloc (sizeof *irs->records * max_cases);
-  for (i = 0; i < max_cases; i++)
-    if (!case_try_create (&irs->records[i].record, irs->xsrt->value_cnt))
-      {
-        max_cases = i;
-        break;
-      }
+  irs->records = nmalloc (sizeof *irs->records, max_cases);
+  if (irs->records != NULL)
+    for (i = 0; i < max_cases; i++)
+      if (!case_try_create (&irs->records[i].record, irs->xsrt->value_cnt))
+        {
+          max_cases = i;
+          break;
+        }
   irs->record_cap = max_cases;
 
   /* Fail if we didn't allocate an acceptable number of cases. */
@@ -458,7 +462,7 @@ allocate_cases (struct initial_run_state *irs)
       msg (SE, _("Out of memory.  Could not allocate room for minimum of %d "
                 "cases of %d bytes each.  (PSPP workspace is currently "
                 "restricted to a maximum of %d KB.)"),
-          min_buffers, approx_case_cost, get_max_workspace() / 1024);
+          min_buffers, approx_case_cost, get_workspace() / 1024);
       return 0;
     }
   return 1;
@@ -545,8 +549,8 @@ end_run (struct initial_run_state *irs)
       if (xsrt->run_cnt >= xsrt->run_cap) 
         {
           xsrt->run_cap *= 2;
-          xsrt->runs = xrealloc (xsrt->runs,
-                                 sizeof *xsrt->runs * xsrt->run_cap);
+          xsrt->runs = xnrealloc (xsrt->runs,
+                                  xsrt->run_cap, sizeof *xsrt->runs);
         }
       xsrt->runs[xsrt->run_cnt++] = irs->casefile;
       irs->casefile = NULL; 
@@ -671,7 +675,7 @@ merge_once (struct external_sort *xsrt,
   int i;
 
   /* Open input files. */
-  runs = xmalloc (sizeof *runs * run_cnt);
+  runs = xnmalloc (run_cnt, sizeof *runs);
   for (i = 0; i < run_cnt; i++) 
     {
       struct run *r = &runs[i];