xref: /dragonfly/usr.bin/sort/file.h (revision 73610d44)
1 /*	$FreeBSD: head/usr.bin/sort/file.h 281182 2015-04-07 01:17:49Z 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  */
48 struct 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  */
60 struct file_reader;
61 
62 /*
63  * List of files to be sorted
64  */
65 struct file_list
66 {
67 	char			**fns;
68 	size_t			 count;
69 	size_t			 sz;
70 	bool			 tmp;
71 };
72 
73 /* memory */
74 
75 /**/
76 
77 extern unsigned long long free_memory;
78 extern unsigned long long available_free_memory;
79 
80 /* Are we using mmap ? */
81 extern bool use_mmap;
82 
83 /* temporary file dir */
84 
85 extern const char *tmpdir;
86 
87 /*
88  * Max number of simultaneously open files (including the output file).
89  */
90 extern size_t max_open_files;
91 
92 /*
93  * Compress program
94  */
95 extern const char* compress_program;
96 
97 /* funcs */
98 
99 struct file_reader *file_reader_init(const char *fsrc);
100 struct bwstring *file_reader_readline(struct file_reader *fr);
101 void file_reader_free(struct file_reader *fr);
102 
103 void init_tmp_files(void);
104 void clear_tmp_files(void);
105 char *new_tmp_file_name(void);
106 void tmp_file_atexit(const char *tmp_file);
107 
108 void file_list_init(struct file_list *fl, bool tmp);
109 void file_list_add(struct file_list *fl, char *fn, bool allocate);
110 void file_list_populate(struct file_list *fl, int argc, char **argv, bool allocate);
111 void file_list_clean(struct file_list *fl);
112 
113 int check(const char *);
114 void merge_files(struct file_list *fl, const char *fn_out);
115 FILE *openfile(const char *, const char *);
116 void closefile(FILE *, const char *);
117 int procfile(const char *fn, struct sort_list *list, struct file_list *fl);
118 
119 void sort_list_init(struct sort_list *l);
120 void sort_list_add(struct sort_list *l, struct bwstring *str);
121 void sort_list_clean(struct sort_list *l);
122 void sort_list_dump(struct sort_list *l, const char *fn);
123 
124 void sort_list_to_file(struct sort_list *list, const char *outfile);
125 
126 #endif /* __SORT_FILE_H__ */
127