]> icculus.org git repositories - icculus/xz.git/blob - src/xz/io.h
Cleanups to the code that detects the amount of RAM and
[icculus/xz.git] / src / xz / io.h
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       io.h
4 /// \brief      I/O types and functions
5 //
6 //  Copyright (C) 2007 Lasse Collin
7 //
8 //  This program is free software; you can redistribute it and/or
9 //  modify it under the terms of the GNU Lesser General Public
10 //  License as published by the Free Software Foundation; either
11 //  version 2.1 of the License, or (at your option) any later version.
12 //
13 //  This program is distributed in the hope that it will be useful,
14 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
15 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 //  Lesser General Public License for more details.
17 //
18 ///////////////////////////////////////////////////////////////////////////////
19
20 // Some systems have suboptimal BUFSIZ. Use a bit bigger value on them.
21 #if BUFSIZ <= 1024
22 #       define IO_BUFFER_SIZE 8192
23 #else
24 #       define IO_BUFFER_SIZE BUFSIZ
25 #endif
26
27
28 typedef struct {
29         /// Name of the source filename (as given on the command line) or
30         /// pointer to static "(stdin)" when reading from standard input.
31         const char *src_name;
32
33         /// Destination filename converted from src_name or pointer to static
34         /// "(stdout)" when writing to standard output.
35         char *dest_name;
36
37         /// File descriptor of the source file
38         int src_fd;
39
40         /// File descriptor of the target file
41         int dest_fd;
42
43         /// Stat of the source file.
44         struct stat src_st;
45
46         /// Stat of the destination file.
47         struct stat dest_st;
48
49         /// True once end of the source file has been detected.
50         bool src_eof;
51
52 } file_pair;
53
54
55 /// \brief      Initialize the I/O module
56 extern void io_init(void);
57
58
59 /// \brief      Opens a file pair
60 extern file_pair *io_open(const char *src_name);
61
62
63 /// \brief      Closes the file descriptors and frees possible allocated memory
64 ///
65 /// The success argument determines if source or destination file gets
66 /// unlinked:
67 ///  - false: The destination file is unlinked.
68 ///  - true: The source file is unlinked unless writing to stdout or --keep
69 ///    was used.
70 extern void io_close(file_pair *pair, bool success);
71
72
73 /// \brief      Reads from the source file to a buffer
74 ///
75 /// \param      pair    File pair having the source file open for reading
76 /// \param      buf     Destination buffer to hold the read data
77 /// \param      size    Size of the buffer; assumed be smaller than SSIZE_MAX
78 ///
79 /// \return     On success, number of bytes read is returned. On end of
80 ///             file zero is returned and pair->src_eof set to true.
81 ///             On error, SIZE_MAX is returned and error message printed.
82 extern size_t io_read(file_pair *pair, uint8_t *buf, size_t size);
83
84
85 /// \brief      Writes a buffer to the destination file
86 ///
87 /// \param      pair    File pair having the destination file open for writing
88 /// \param      buf     Buffer containing the data to be written
89 /// \param      size    Size of the buffer; assumed be smaller than SSIZE_MAX
90 ///
91 /// \return     On success, zero is returned. On error, -1 is returned
92 ///             and error message printed.
93 extern bool io_write(const file_pair *pair, const uint8_t *buf, size_t size);