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 ///////////////////////////////////////////////////////////////////////////////
26 // Some systems have suboptimal BUFSIZ. Use a bit bigger value on them.
28 # define IO_BUFFER_SIZE 8192
30 # define IO_BUFFER_SIZE BUFSIZ
35 /// Name of the source filename (as given on the command line) or
36 /// pointer to static "(stdin)" when reading from standard input.
39 /// Destination filename converted from src_name or pointer to static
40 /// "(stdout)" when writing to standard output.
43 /// File descriptor of the source file
46 /// File descriptor of the target file
49 /// Stat of the source file.
52 /// Stat of the destination file.
55 /// True once end of the source file has been detected.
61 /// \brief Opens a file pair
62 extern file_pair *io_open(const char *src_name);
65 /// \brief Closes the file descriptors and frees possible allocated memory
67 /// The success argument determines if source or destination file gets
69 /// - false: The destination file is unlinked.
70 /// - true: The source file is unlinked unless writing to stdout or --keep
72 extern void io_close(file_pair *pair, bool success);
75 /// \brief Reads from the source file to a buffer
77 /// \param pair File pair having the source file open for reading
78 /// \param buf Destination buffer to hold the read data
79 /// \param size Size of the buffer; assumed be smaller than SSIZE_MAX
81 /// \return On success, number of bytes read is returned. On end of
82 /// file zero is returned and pair->src_eof set to true.
83 /// On error, SIZE_MAX is returned and error message printed.
84 extern size_t io_read(file_pair *pair, uint8_t *buf, size_t size);
87 /// \brief Writes a buffer to the destination file
89 /// \param pair File pair having the destination file open for writing
90 /// \param buf Buffer containing the data to be written
91 /// \param size Size of the buffer; assumed be smaller than SSIZE_MAX
93 /// \return On success, zero is returned. On error, -1 is returned
94 /// and error message printed.
95 extern bool io_write(const file_pair *pair, const uint8_t *buf, size_t size);