]> icculus.org git repositories - icculus/xz.git/blob - src/xz/io.h
Put the interesting parts of XZ Utils into the public domain.
[icculus/xz.git] / src / xz / io.h
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       io.h
4 /// \brief      I/O types and functions
5 //
6 //  Author:     Lasse Collin
7 //
8 //  This file has been put into the public domain.
9 //  You can do whatever you want with this file.
10 //
11 ///////////////////////////////////////////////////////////////////////////////
12
13 // Some systems have suboptimal BUFSIZ. Use a bit bigger value on them.
14 #if BUFSIZ <= 1024
15 #       define IO_BUFFER_SIZE 8192
16 #else
17 #       define IO_BUFFER_SIZE BUFSIZ
18 #endif
19
20
21 typedef struct {
22         /// Name of the source filename (as given on the command line) or
23         /// pointer to static "(stdin)" when reading from standard input.
24         const char *src_name;
25
26         /// Destination filename converted from src_name or pointer to static
27         /// "(stdout)" when writing to standard output.
28         char *dest_name;
29
30         /// File descriptor of the source file
31         int src_fd;
32
33         /// File descriptor of the target file
34         int dest_fd;
35
36         /// Stat of the source file.
37         struct stat src_st;
38
39         /// Stat of the destination file.
40         struct stat dest_st;
41
42         /// True once end of the source file has been detected.
43         bool src_eof;
44
45 } file_pair;
46
47
48 /// \brief      Initialize the I/O module
49 extern void io_init(void);
50
51
52 /// \brief      Opens a file pair
53 extern file_pair *io_open(const char *src_name);
54
55
56 /// \brief      Closes the file descriptors and frees possible allocated memory
57 ///
58 /// The success argument determines if source or destination file gets
59 /// unlinked:
60 ///  - false: The destination file is unlinked.
61 ///  - true: The source file is unlinked unless writing to stdout or --keep
62 ///    was used.
63 extern void io_close(file_pair *pair, bool success);
64
65
66 /// \brief      Reads from the source file to a buffer
67 ///
68 /// \param      pair    File pair having the source file open for reading
69 /// \param      buf     Destination buffer to hold the read data
70 /// \param      size    Size of the buffer; assumed be smaller than SSIZE_MAX
71 ///
72 /// \return     On success, number of bytes read is returned. On end of
73 ///             file zero is returned and pair->src_eof set to true.
74 ///             On error, SIZE_MAX is returned and error message printed.
75 extern size_t io_read(file_pair *pair, uint8_t *buf, size_t size);
76
77
78 /// \brief      Writes a buffer to the destination file
79 ///
80 /// \param      pair    File pair having the destination file open for writing
81 /// \param      buf     Buffer containing the data to be written
82 /// \param      size    Size of the buffer; assumed be smaller than SSIZE_MAX
83 ///
84 /// \return     On success, zero is returned. On error, -1 is returned
85 ///             and error message printed.
86 extern bool io_write(const file_pair *pair, const uint8_t *buf, size_t size);