xref: /freebsd/usr.bin/sort/file.h (revision 7750ad47a9a7dbc83f87158464170c8640723293)
1 /*	$FreeBSD$	*/
2 
3 /*-
4  * Copyright (C) 2009 Gabor Kovesdan <gabor@FreeBSD.org>
5  * Copyright (C) 2012 Oleg Moskalenko <oleg.moskalenko@citrix.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 /*
43  * List of data to be sorted.
44  */
45 struct sort_list
46 {
47 	struct sort_list_item	**list;
48 	unsigned long long	 memsize;
49 	size_t			 count;
50 	size_t			 size;
51 	size_t			 sub_list_pos;
52 };
53 
54 /*
55  * File reader object
56  */
57 struct file_reader;
58 
59 /*
60  * List of files to be sorted
61  */
62 struct file_list
63 {
64 	char			**fns;
65 	int			 count;
66 	int			 sz;
67 	bool			 tmp;
68 };
69 
70 /*
71  * Structure for zero-separated file reading (for input files list)
72  */
73 struct file0_reader
74 {
75 	char			*current_line;
76 	FILE			*f;
77 	size_t			 current_sz;
78 };
79 
80 /* memory */
81 
82 /**/
83 
84 extern unsigned long long free_memory;
85 extern unsigned long long available_free_memory;
86 
87 /* Are we using mmap ? */
88 extern bool use_mmap;
89 
90 /* temporary file dir */
91 
92 extern const char *tmpdir;
93 
94 /*
95  * Max number of simultaneously open files (including the output file).
96  */
97 extern size_t max_open_files;
98 
99 /*
100  * Compress program
101  */
102 extern const char* compress_program;
103 
104 /* funcs */
105 
106 struct file_reader *file_reader_init(const char *fsrc);
107 struct bwstring *file_reader_readline(struct file_reader *fr);
108 void file_reader_free(struct file_reader *fr);
109 
110 char *read_file0_line(struct file0_reader *f0r);
111 
112 void init_tmp_files(void);
113 void clear_tmp_files(void);
114 char *new_tmp_file_name(void);
115 void tmp_file_atexit(const char *tmp_file);
116 
117 void file_list_init(struct file_list *fl, bool tmp);
118 void file_list_add(struct file_list *fl, char *fn, bool allocate);
119 void file_list_populate(struct file_list *fl, int argc, char **argv, bool allocate);
120 void file_list_clean(struct file_list *fl);
121 
122 int check(const char *);
123 void merge_files(struct file_list *fl, const char *fn_out);
124 FILE *openfile(const char *, const char *);
125 void closefile(FILE *, const char *);
126 int procfile(const char *fn, struct sort_list *list, struct file_list *fl);
127 
128 void sort_list_init(struct sort_list *l);
129 void sort_list_add(struct sort_list *l, struct bwstring *str);
130 void sort_list_clean(struct sort_list *l);
131 void sort_list_dump(struct sort_list *l, const char *fn);
132 
133 void sort_list_to_file(struct sort_list *list, const char *outfile);
134 
135 #endif /* __SORT_FILE_H__ */
136