file.h revision 235987
1235267Sgabor/*	$FreeBSD: head/usr.bin/sort/file.h 235987 2012-05-25 09:30:16Z gabor $	*/
2235267Sgabor
3235267Sgabor/*-
4235267Sgabor * Copyright (C) 2009 Gabor Kovesdan <gabor@FreeBSD.org>
5235267Sgabor * Copyright (C) 2012 Oleg Moskalenko <oleg.moskalenko@citrix.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__)
31235267Sgabor#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
42235267Sgabor/*
43235267Sgabor * List of data to be sorted.
44235267Sgabor */
45235267Sgaborstruct sort_list
46235267Sgabor{
47235267Sgabor	struct sort_list_item	**list;
48235267Sgabor	unsigned long long	 memsize;
49235267Sgabor	size_t			 count;
50235267Sgabor	size_t			 size;
51235267Sgabor	size_t			 sub_list_pos;
52235267Sgabor};
53235267Sgabor
54235267Sgabor/*
55235267Sgabor * File reader object
56235267Sgabor */
57235267Sgaborstruct file_reader;
58235267Sgabor
59235267Sgabor/*
60235267Sgabor * List of files to be sorted
61235267Sgabor */
62235267Sgaborstruct file_list
63235267Sgabor{
64235267Sgabor	char			**fns;
65235267Sgabor	int			 count;
66235267Sgabor	int			 sz;
67235267Sgabor	bool			 tmp;
68235267Sgabor};
69235267Sgabor
70235267Sgabor/*
71235267Sgabor * Structure for zero-separated file reading (for input files list)
72235267Sgabor */
73235267Sgaborstruct file0_reader
74235267Sgabor{
75235267Sgabor	char			*current_line;
76235267Sgabor	FILE			*f;
77235267Sgabor	size_t			 current_sz;
78235267Sgabor};
79235267Sgabor
80235267Sgabor/* memory */
81235267Sgabor
82235267Sgabor/**/
83235267Sgabor
84235267Sgaborextern unsigned long long free_memory;
85235267Sgaborextern unsigned long long available_free_memory;
86235267Sgabor
87235987Sgabor/* Are we using mmap ? */
88235987Sgaborextern bool use_mmap;
89235987Sgabor
90235267Sgabor/* temporary file dir */
91235267Sgabor
92235267Sgaborextern const char *tmpdir;
93235267Sgabor
94235267Sgabor/*
95235267Sgabor * Max number of simultaneously open files (including the output file).
96235267Sgabor */
97235267Sgaborextern size_t max_open_files;
98235267Sgabor
99235267Sgabor/*
100235267Sgabor * Compress program
101235267Sgabor */
102235267Sgaborextern const char* compress_program;
103235267Sgabor
104235267Sgabor/* funcs */
105235267Sgabor
106235267Sgaborstruct file_reader *file_reader_init(const char *fsrc);
107235267Sgaborstruct bwstring *file_reader_readline(struct file_reader *fr);
108235267Sgaborvoid file_reader_free(struct file_reader *fr);
109235267Sgabor
110235267Sgaborchar *read_file0_line(struct file0_reader *f0r);
111235267Sgabor
112235267Sgaborvoid init_tmp_files(void);
113235267Sgaborvoid clear_tmp_files(void);
114235267Sgaborchar *new_tmp_file_name(void);
115235267Sgaborvoid tmp_file_atexit(const char *tmp_file);
116235267Sgabor
117235267Sgaborvoid file_list_init(struct file_list *fl, bool tmp);
118235267Sgaborvoid file_list_add(struct file_list *fl, char *fn, bool allocate);
119235267Sgaborvoid file_list_populate(struct file_list *fl, int argc, char **argv, bool allocate);
120235267Sgaborvoid file_list_clean(struct file_list *fl);
121235267Sgabor
122235267Sgaborint check(const char *);
123235267Sgaborvoid merge_files(struct file_list *fl, const char *fn_out);
124235267SgaborFILE *openfile(const char *, const char *);
125235267Sgaborvoid closefile(FILE *, const char *);
126235267Sgaborint procfile(const char *fn, struct sort_list *list, struct file_list *fl);
127235267Sgabor
128235267Sgaborvoid sort_list_init(struct sort_list *l);
129235267Sgaborvoid sort_list_add(struct sort_list *l, struct bwstring *str);
130235267Sgaborvoid sort_list_clean(struct sort_list *l);
131235267Sgaborvoid sort_list_dump(struct sort_list *l, const char *fn);
132235267Sgabor
133235267Sgaborvoid sort_list_to_file(struct sort_list *list, const char *outfile);
134235267Sgabor
135235267Sgabor#endif /* __SORT_FILE_H__ */
136