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/*
74235267Sgabor * Structure for zero-separated file reading (for input files list)
75235267Sgabor */
76235267Sgaborstruct file0_reader
77235267Sgabor{
78235267Sgabor	char			*current_line;
79235267Sgabor	FILE			*f;
80235267Sgabor	size_t			 current_sz;
81235267Sgabor};
82235267Sgabor
83235267Sgabor/* memory */
84235267Sgabor
85235267Sgabor/**/
86235267Sgabor
87235267Sgaborextern unsigned long long free_memory;
88235267Sgaborextern unsigned long long available_free_memory;
89235267Sgabor
90235987Sgabor/* Are we using mmap ? */
91235987Sgaborextern bool use_mmap;
92235987Sgabor
93235267Sgabor/* temporary file dir */
94235267Sgabor
95235267Sgaborextern const char *tmpdir;
96235267Sgabor
97235267Sgabor/*
98235267Sgabor * Max number of simultaneously open files (including the output file).
99235267Sgabor */
100235267Sgaborextern size_t max_open_files;
101235267Sgabor
102235267Sgabor/*
103235267Sgabor * Compress program
104235267Sgabor */
105235267Sgaborextern const char* compress_program;
106235267Sgabor
107235267Sgabor/* funcs */
108235267Sgabor
109235267Sgaborstruct file_reader *file_reader_init(const char *fsrc);
110235267Sgaborstruct bwstring *file_reader_readline(struct file_reader *fr);
111235267Sgaborvoid file_reader_free(struct file_reader *fr);
112235267Sgabor
113235267Sgaborchar *read_file0_line(struct file0_reader *f0r);
114235267Sgabor
115235267Sgaborvoid init_tmp_files(void);
116235267Sgaborvoid clear_tmp_files(void);
117235267Sgaborchar *new_tmp_file_name(void);
118235267Sgaborvoid tmp_file_atexit(const char *tmp_file);
119235267Sgabor
120235267Sgaborvoid file_list_init(struct file_list *fl, bool tmp);
121235267Sgaborvoid file_list_add(struct file_list *fl, char *fn, bool allocate);
122235267Sgaborvoid file_list_populate(struct file_list *fl, int argc, char **argv, bool allocate);
123235267Sgaborvoid file_list_clean(struct file_list *fl);
124235267Sgabor
125235267Sgaborint check(const char *);
126235267Sgaborvoid merge_files(struct file_list *fl, const char *fn_out);
127235267SgaborFILE *openfile(const char *, const char *);
128235267Sgaborvoid closefile(FILE *, const char *);
129235267Sgaborint procfile(const char *fn, struct sort_list *list, struct file_list *fl);
130235267Sgabor
131235267Sgaborvoid sort_list_init(struct sort_list *l);
132235267Sgaborvoid sort_list_add(struct sort_list *l, struct bwstring *str);
133235267Sgaborvoid sort_list_clean(struct sort_list *l);
134235267Sgaborvoid sort_list_dump(struct sort_list *l, const char *fn);
135235267Sgabor
136235267Sgaborvoid sort_list_to_file(struct sort_list *list, const char *outfile);
137235267Sgabor
138235267Sgabor#endif /* __SORT_FILE_H__ */
139