1 ///////////////////////////////////////////////////////////////////////////////
4 /// \brief I/O types and functions
6 // Author: Lasse Collin
8 // This file has been put into the public domain.
9 // You can do whatever you want with this file.
11 ///////////////////////////////////////////////////////////////////////////////
13 // Some systems have suboptimal BUFSIZ. Use a bit bigger value on them.
15 # define IO_BUFFER_SIZE 8192
17 # define IO_BUFFER_SIZE BUFSIZ
22 /// Name of the source filename (as given on the command line) or
23 /// pointer to static "(stdin)" when reading from standard input.
26 /// Destination filename converted from src_name or pointer to static
27 /// "(stdout)" when writing to standard output.
30 /// File descriptor of the source file
33 /// File descriptor of the target file
36 /// Stat of the source file.
39 /// Stat of the destination file.
42 /// True once end of the source file has been detected.
48 /// \brief Initialize the I/O module
49 extern void io_init(void);
52 /// \brief Opens a file pair
53 extern file_pair *io_open(const char *src_name);
56 /// \brief Closes the file descriptors and frees possible allocated memory
58 /// The success argument determines if source or destination file gets
60 /// - false: The destination file is unlinked.
61 /// - true: The source file is unlinked unless writing to stdout or --keep
63 extern void io_close(file_pair *pair, bool success);
66 /// \brief Reads from the source file to a buffer
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
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);
78 /// \brief Writes a buffer to the destination file
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
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);