file.h revision 281535
1/*	$FreeBSD: stable/10/usr.bin/sort/file.h 281535 2015-04-14 18:57:50Z pfg $	*/
2
3/*-
4 * Copyright (C) 2009 Gabor Kovesdan <gabor@FreeBSD.org>
5 * Copyright (C) 2012 Oleg Moskalenko <mom040267@gmail.com>
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#if !defined(__SORT_FILE_H__)
31#define	__SORT_FILE_H__
32
33#include "coll.h"
34#include "sort.h"
35
36#define	SORT_DEFAULT	0
37#define	SORT_QSORT	1
38#define	SORT_MERGESORT	2
39#define	SORT_HEAPSORT	3
40#define	SORT_RADIXSORT  4
41
42#define	DEFAULT_SORT_ALGORITHM SORT_HEAPSORT
43#define	DEFAULT_SORT_FUNC heapsort
44
45/*
46 * List of data to be sorted.
47 */
48struct sort_list
49{
50	struct sort_list_item	**list;
51	unsigned long long	 memsize;
52	size_t			 count;
53	size_t			 size;
54	size_t			 sub_list_pos;
55};
56
57/*
58 * File reader object
59 */
60struct file_reader;
61
62/*
63 * List of files to be sorted
64 */
65struct file_list
66{
67	char			**fns;
68	size_t			 count;
69	size_t			 sz;
70	bool			 tmp;
71};
72
73/* memory */
74
75/**/
76
77extern unsigned long long free_memory;
78extern unsigned long long available_free_memory;
79
80/* Are we using mmap ? */
81extern bool use_mmap;
82
83/* temporary file dir */
84
85extern const char *tmpdir;
86
87/*
88 * Max number of simultaneously open files (including the output file).
89 */
90extern size_t max_open_files;
91
92/*
93 * Compress program
94 */
95extern const char* compress_program;
96
97/* funcs */
98
99struct file_reader *file_reader_init(const char *fsrc);
100struct bwstring *file_reader_readline(struct file_reader *fr);
101void file_reader_free(struct file_reader *fr);
102
103void init_tmp_files(void);
104void clear_tmp_files(void);
105char *new_tmp_file_name(void);
106void tmp_file_atexit(const char *tmp_file);
107
108void file_list_init(struct file_list *fl, bool tmp);
109void file_list_add(struct file_list *fl, char *fn, bool allocate);
110void file_list_populate(struct file_list *fl, int argc, char **argv, bool allocate);
111void file_list_clean(struct file_list *fl);
112
113int check(const char *);
114void merge_files(struct file_list *fl, const char *fn_out);
115FILE *openfile(const char *, const char *);
116void closefile(FILE *, const char *);
117int procfile(const char *fn, struct sort_list *list, struct file_list *fl);
118
119void sort_list_init(struct sort_list *l);
120void sort_list_add(struct sort_list *l, struct bwstring *str);
121void sort_list_clean(struct sort_list *l);
122void sort_list_dump(struct sort_list *l, const char *fn);
123
124void sort_list_to_file(struct sort_list *list, const char *outfile);
125
126#endif /* __SORT_FILE_H__ */
127