02111-1307, USA. */
#include <config.h>
+#include "sort.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include "heap.h"
#include "lexer.h"
#include "misc.h"
-#include "sort.h"
#include "str.h"
#include "var.h"
#include "vfm.h"
static int do_internal_sort (int separate);
static int do_external_sort (int separate);
int parse_sort_variables (void);
-void read_sort_output (int (*write_case) (void));
+void read_sort_output (write_case_func *write_case, write_case_data wc_data);
/* Performs the SORT CASES procedures. */
int
int prev_nv_sort = nv_sort;
int order = SRT_ASCEND;
- if (!parse_variables (&default_dict, &v_sort, &nv_sort,
+ if (!parse_variables (default_dict, &v_sort, &nv_sort,
PV_NO_DUPLICATE | PV_APPEND | PV_NO_SCRATCH))
return 0;
if (lex_match ('('))
/* Not sure this is necessary but it's good to be safe. */
if (separate && vfm_source == &sort_stream)
- procedure (NULL, NULL, NULL);
+ procedure (NULL, NULL, NULL, NULL);
/* SORT CASES cancels PROCESS IF. */
expr_free (process_if_expr);
if (vfm_source != &vfm_disk_stream)
{
if (vfm_source != &vfm_memory_stream)
- procedure (NULL, NULL, NULL);
+ procedure (NULL, NULL, NULL, NULL);
if (vfm_source == &vfm_memory_stream)
{
struct case_list **case_tab = malloc (sizeof *case_tab
if (dir == NULL)
dir = P_tmpdir;
#endif
-#if __unix__
+#ifdef unix
if (dir == NULL)
dir = "/tmp";
-#elif __MSDOS__
+#elif defined (__MSDOS__)
if (dir == NULL)
dir = getenv ("TEMP");
if (dir == NULL)
buf = xmalloc (strlen (dir) + 1 + 4 + 8 + 4 + 1 + INT_DIGITS + 1);
cp = spprintf (buf, "%s%c%04lX%04lXpspp", dir, DIR_SEPARATOR,
((long) time (0)) & 0xffff, ((long) getpid ()) & 0xffff);
+#ifndef __MSDOS__
if (-1 == mkdir (buf, S_IRWXU))
+#else
+ if (-1 == mkdir (buf))
+#endif
{
free (buf);
msg (SE, _("%s: Cannot create temporary directory: %s."),
{
/* This is the size of one case. */
const int case_size = (sizeof (struct repl_sel_tree)
- + sizeof (union value) * (default_dict.nval - 1)
+ + (sizeof (union value)
+ * (dict_get_value_cnt (default_dict) - 1))
+ sizeof (struct repl_sel_tree *));
x = NULL;
for (i = 0; i < x_max; i++)
{
x[i] = malloc (sizeof (struct repl_sel_tree)
- + sizeof (union value) * (default_dict.nval - 1));
+ + (sizeof (union value)
+ * (dict_get_value_cnt (default_dict) - 1)));
if (x[i] == NULL)
break;
}
J->rn = 0;
J->fe = x[(x_max + j) / 2];
J->fi = x[j / 2];
- memset (J->record, 0, default_dict.nval * sizeof (union value));
+ memset (J->record, 0,
+ dict_get_value_cnt (default_dict) * sizeof (union value));
}
}
vfm_sink->destroy_sink ();
vfm_sink = &sort_stream;
}
- procedure (NULL, NULL, NULL);
+ procedure (NULL, NULL, NULL, NULL);
/* Final iterations of steps R4, R5, R6, R7, R2, R3, ... */
for (;;)
order = MAX_MERGE_ORDER;
if (x_max / order < MIN_BUFFER_SIZE_RECS)
order = x_max / MIN_BUFFER_SIZE_RECS;
- else if (x_max / order * sizeof (union value) * default_dict.nval
+ else if (x_max / order * sizeof (union value) * dict_get_value_cnt (default_dict)
< MIN_BUFFER_SIZE_BYTES)
order = x_max / (MIN_BUFFER_SIZE_BYTES
- / (sizeof (union value) * (default_dict.nval - 1)));
+ / (sizeof (union value)
+ * (dict_get_value_cnt (default_dict) - 1)));
/* Make sure the order of merge is bounded. */
if (order < 2)
buffered[i] = min (records_per_buffer, run_length[i]);
for (j = 0; j < buffered[i]; j++)
if ((int) fread (x[j + ofs]->record, sizeof (union value),
- default_dict.nval, handle[i])
- != default_dict.nval)
+ dict_get_value_cnt (default_dict), handle[i])
+ != dict_get_value_cnt (default_dict))
{
sprintf (tmp_extname, "%08x", run_index[i]);
if (ferror (handle[i]))
min = i;
if ((int) fwrite (x[buffer_ptr[min]]->record, sizeof (union value),
- default_dict.nval, handle[N_INPUT_BUFFERS])
- != default_dict.nval)
+ dict_get_value_cnt (default_dict),
+ handle[N_INPUT_BUFFERS])
+ != dict_get_value_cnt (default_dict))
{
sprintf (tmp_extname, "%08x", run_index[i]);
msg (SE, _("%s: Error writing temporary file in "
buffered[min] = min (records_per_buffer, run_length[min]);
for (j = 0; j < buffered[min]; j++)
if ((int) fread (x[j + ofs]->record, sizeof (union value),
- default_dict.nval, handle[min])
- != default_dict.nval)
+ dict_get_value_cnt (default_dict),
+ handle[min])
+ != dict_get_value_cnt (default_dict))
{
sprintf (tmp_extname, "%08x", run_index[min]);
if (ferror (handle[min]))
/* Reads all the records from the source stream and passes them
to write_case(). */
-void
-sort_stream_read (void)
+static void
+sort_stream_read (write_case_func *write_case, write_case_data wc_data)
{
- read_sort_output (write_case);
+ read_sort_output (write_case, wc_data);
}
/* Reads all the records from the output stream and passes them to the
function provided, which must have an interface identical to
write_case(). */
void
-read_sort_output (int (*write_case) (void))
+read_sort_output (write_case_func *write_case, write_case_data wc_data)
{
int i;
FILE *f;
for (p = separate_case_tab; *p; p++)
{
temp_case = &(*p)->c;
- write_case ();
+ write_case (wc_data);
}
free (separate_case_tab);
break;
}
- if (!write_case ())
+ if (!write_case (wc_data))
break;
}
}
/* Switches mode from sink to source. */
-void
+static void
sort_stream_mode (void)
{
/* If this is not done, then we get the following source/sink pairs: