1235267Sgabor/*	$FreeBSD$	*/
2235267Sgabor
3235267Sgabor/*-
4235267Sgabor * Copyright (C) 2009 Gabor Kovesdan <gabor@FreeBSD.org>
5251245Sgabor * Copyright (C) 2012 Oleg Moskalenko <mom040267@gmail.com>
6235267Sgabor * All rights reserved.
7235267Sgabor *
8235267Sgabor * Redistribution and use in source and binary forms, with or without
9235267Sgabor * modification, are permitted provided that the following conditions
10235267Sgabor * are met:
11235267Sgabor * 1. Redistributions of source code must retain the above copyright
12235267Sgabor *    notice, this list of conditions and the following disclaimer.
13235267Sgabor * 2. Redistributions in binary form must reproduce the above copyright
14235267Sgabor *    notice, this list of conditions and the following disclaimer in the
15235267Sgabor *    documentation and/or other materials provided with the distribution.
16235267Sgabor *
17235267Sgabor * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18235267Sgabor * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19235267Sgabor * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20235267Sgabor * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21235267Sgabor * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22235267Sgabor * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23235267Sgabor * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24235267Sgabor * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25235267Sgabor * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26235267Sgabor * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27235267Sgabor * SUCH DAMAGE.
28235267Sgabor */
29235267Sgabor
30235267Sgabor#if !defined(__SORT_FILE_H__)
31265160Spfg#define	__SORT_FILE_H__
32235267Sgabor
33235267Sgabor#include "coll.h"
34235267Sgabor#include "sort.h"
35235267Sgabor
36235267Sgabor#define	SORT_DEFAULT	0
37235267Sgabor#define	SORT_QSORT	1
38235267Sgabor#define	SORT_MERGESORT	2
39235267Sgabor#define	SORT_HEAPSORT	3
40235267Sgabor#define	SORT_RADIXSORT  4
41235267Sgabor
42265160Spfg#define	DEFAULT_SORT_ALGORITHM SORT_HEAPSORT
43265160Spfg#define	DEFAULT_SORT_FUNC heapsort
44238108Sgabor
45235267Sgabor/*
46235267Sgabor * List of data to be sorted.
47235267Sgabor */
48235267Sgaborstruct sort_list
49235267Sgabor{
50235267Sgabor	struct sort_list_item	**list;
51235267Sgabor	unsigned long long	 memsize;
52235267Sgabor	size_t			 count;
53235267Sgabor	size_t			 size;
54235267Sgabor	size_t			 sub_list_pos;
55235267Sgabor};
56235267Sgabor
57235267Sgabor/*
58235267Sgabor * File reader object
59235267Sgabor */
60235267Sgaborstruct file_reader;
61235267Sgabor
62235267Sgabor/*
63235267Sgabor * List of files to be sorted
64235267Sgabor */
65235267Sgaborstruct file_list
66235267Sgabor{
67235267Sgabor	char			**fns;
68242430Sgabor	size_t			 count;
69242430Sgabor	size_t			 sz;
70235267Sgabor	bool			 tmp;
71235267Sgabor};
72235267Sgabor
73235267Sgabor/* memory */
74235267Sgabor
75235267Sgabor/**/
76235267Sgabor
77235267Sgaborextern unsigned long long free_memory;
78235267Sgaborextern unsigned long long available_free_memory;
79235267Sgabor
80235987Sgabor/* Are we using mmap ? */
81235987Sgaborextern bool use_mmap;
82235987Sgabor
83235267Sgabor/* temporary file dir */
84235267Sgabor
85235267Sgaborextern const char *tmpdir;
86235267Sgabor
87235267Sgabor/*
88235267Sgabor * Max number of simultaneously open files (including the output file).
89235267Sgabor */
90235267Sgaborextern size_t max_open_files;
91235267Sgabor
92235267Sgabor/*
93235267Sgabor * Compress program
94235267Sgabor */
95235267Sgaborextern const char* compress_program;
96235267Sgabor
97235267Sgabor/* funcs */
98235267Sgabor
99235267Sgaborstruct file_reader *file_reader_init(const char *fsrc);
100235267Sgaborstruct bwstring *file_reader_readline(struct file_reader *fr);
101235267Sgaborvoid file_reader_free(struct file_reader *fr);
102235267Sgabor
103235267Sgaborvoid init_tmp_files(void);
104235267Sgaborvoid clear_tmp_files(void);
105235267Sgaborchar *new_tmp_file_name(void);
106235267Sgaborvoid tmp_file_atexit(const char *tmp_file);
107235267Sgabor
108235267Sgaborvoid file_list_init(struct file_list *fl, bool tmp);
109235267Sgaborvoid file_list_add(struct file_list *fl, char *fn, bool allocate);
110235267Sgaborvoid file_list_populate(struct file_list *fl, int argc, char **argv, bool allocate);
111235267Sgaborvoid file_list_clean(struct file_list *fl);
112235267Sgabor
113235267Sgaborint check(const char *);
114235267Sgaborvoid merge_files(struct file_list *fl, const char *fn_out);
115235267SgaborFILE *openfile(const char *, const char *);
116235267Sgaborvoid closefile(FILE *, const char *);
117235267Sgaborint procfile(const char *fn, struct sort_list *list, struct file_list *fl);
118235267Sgabor
119235267Sgaborvoid sort_list_init(struct sort_list *l);
120235267Sgaborvoid sort_list_add(struct sort_list *l, struct bwstring *str);
121235267Sgaborvoid sort_list_clean(struct sort_list *l);
122235267Sgaborvoid sort_list_dump(struct sort_list *l, const char *fn);
123235267Sgabor
124235267Sgaborvoid sort_list_to_file(struct sort_list *list, const char *outfile);
125235267Sgabor
126235267Sgabor#endif /* __SORT_FILE_H__ */
127