1 ///////////////////////////////////////////////////////////////////////////////
4 /// \brief I/O types and functions
6 // Copyright (C) 2007 Lasse Collin
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.
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.
18 ///////////////////////////////////////////////////////////////////////////////
20 // Some systems have suboptimal BUFSIZ. Use a bit bigger value on them.
22 # define IO_BUFFER_SIZE 8192
24 # define IO_BUFFER_SIZE BUFSIZ
29 /// Name of the source filename (as given on the command line) or
30 /// pointer to static "(stdin)" when reading from standard input.
33 /// Destination filename converted from src_name or pointer to static
34 /// "(stdout)" when writing to standard output.
37 /// File descriptor of the source file
40 /// File descriptor of the target file
43 /// Stat of the source file.
46 /// Stat of the destination file.
49 /// True once end of the source file has been detected.
55 /// \brief Initialize the I/O module
56 extern void io_init(void);
59 /// \brief Opens a file pair
60 extern file_pair *io_open(const char *src_name);
63 /// \brief Closes the file descriptors and frees possible allocated memory
65 /// The success argument determines if source or destination file gets
67 /// - false: The destination file is unlinked.
68 /// - true: The source file is unlinked unless writing to stdout or --keep
70 extern void io_close(file_pair *pair, bool success);
73 /// \brief Reads from the source file to a buffer
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
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);
85 /// \brief Writes a buffer to the destination file
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
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);