xref: /freebsd/usr.bin/sort/file.h (revision f05cddf9)
1 /*	$FreeBSD$	*/
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 /*
74  * Structure for zero-separated file reading (for input files list)
75  */
76 struct file0_reader
77 {
78 	char			*current_line;
79 	FILE			*f;
80 	size_t			 current_sz;
81 };
82 
83 /* memory */
84 
85 /**/
86 
87 extern unsigned long long free_memory;
88 extern unsigned long long available_free_memory;
89 
90 /* Are we using mmap ? */
91 extern bool use_mmap;
92 
93 /* temporary file dir */
94 
95 extern const char *tmpdir;
96 
97 /*
98  * Max number of simultaneously open files (including the output file).
99  */
100 extern size_t max_open_files;
101 
102 /*
103  * Compress program
104  */
105 extern const char* compress_program;
106 
107 /* funcs */
108 
109 struct file_reader *file_reader_init(const char *fsrc);
110 struct bwstring *file_reader_readline(struct file_reader *fr);
111 void file_reader_free(struct file_reader *fr);
112 
113 char *read_file0_line(struct file0_reader *f0r);
114 
115 void init_tmp_files(void);
116 void clear_tmp_files(void);
117 char *new_tmp_file_name(void);
118 void tmp_file_atexit(const char *tmp_file);
119 
120 void file_list_init(struct file_list *fl, bool tmp);
121 void file_list_add(struct file_list *fl, char *fn, bool allocate);
122 void file_list_populate(struct file_list *fl, int argc, char **argv, bool allocate);
123 void file_list_clean(struct file_list *fl);
124 
125 int check(const char *);
126 void merge_files(struct file_list *fl, const char *fn_out);
127 FILE *openfile(const char *, const char *);
128 void closefile(FILE *, const char *);
129 int procfile(const char *fn, struct sort_list *list, struct file_list *fl);
130 
131 void sort_list_init(struct sort_list *l);
132 void sort_list_add(struct sort_list *l, struct bwstring *str);
133 void sort_list_clean(struct sort_list *l);
134 void sort_list_dump(struct sort_list *l, const char *fn);
135 
136 void sort_list_to_file(struct sort_list *list, const char *outfile);
137 
138 #endif /* __SORT_FILE_H__ */
139