2007-12-08 23:42:33 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
/// \file io.h
|
|
|
|
/// \brief I/O types and functions
|
|
|
|
//
|
|
|
|
// Copyright (C) 2007 Lasse Collin
|
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Lesser General Public
|
|
|
|
// License as published by the Free Software Foundation; either
|
|
|
|
// version 2.1 of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#ifndef IO_H
|
|
|
|
#define IO_H
|
|
|
|
|
|
|
|
#include "private.h"
|
|
|
|
|
2008-11-19 19:46:52 +01:00
|
|
|
|
|
|
|
// Some systems have suboptimal BUFSIZ. Use a bit bigger value on them.
|
2007-12-08 23:42:33 +01:00
|
|
|
#if BUFSIZ <= 1024
|
|
|
|
# define IO_BUFFER_SIZE 8192
|
|
|
|
#else
|
|
|
|
# define IO_BUFFER_SIZE BUFSIZ
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
2008-11-19 19:46:52 +01:00
|
|
|
/// Name of the source filename (as given on the command line) or
|
|
|
|
/// pointer to static "(stdin)" when reading from standard input.
|
2007-12-08 23:42:33 +01:00
|
|
|
const char *src_name;
|
2008-11-19 19:46:52 +01:00
|
|
|
|
|
|
|
/// Destination filename converted from src_name or pointer to static
|
|
|
|
/// "(stdout)" when writing to standard output.
|
2007-12-08 23:42:33 +01:00
|
|
|
char *dest_name;
|
|
|
|
|
2008-11-19 19:46:52 +01:00
|
|
|
/// File descriptor of the source file
|
2007-12-08 23:42:33 +01:00
|
|
|
int src_fd;
|
2008-11-19 19:46:52 +01:00
|
|
|
|
|
|
|
/// File descriptor of the target file
|
2007-12-08 23:42:33 +01:00
|
|
|
int dest_fd;
|
|
|
|
|
2008-11-19 19:46:52 +01:00
|
|
|
/// Stat of the source file.
|
2007-12-08 23:42:33 +01:00
|
|
|
struct stat src_st;
|
|
|
|
|
2008-11-19 19:46:52 +01:00
|
|
|
/// Stat of the destination file.
|
|
|
|
struct stat dest_st;
|
2007-12-08 23:42:33 +01:00
|
|
|
|
2008-11-19 19:46:52 +01:00
|
|
|
/// True once end of the source file has been detected.
|
|
|
|
bool src_eof;
|
2007-12-08 23:42:33 +01:00
|
|
|
|
2008-11-19 19:46:52 +01:00
|
|
|
} file_pair;
|
2007-12-08 23:42:33 +01:00
|
|
|
|
|
|
|
|
2008-11-19 19:46:52 +01:00
|
|
|
/// \brief Opens a file pair
|
2007-12-08 23:42:33 +01:00
|
|
|
extern file_pair *io_open(const char *src_name);
|
|
|
|
|
2008-11-19 19:46:52 +01:00
|
|
|
|
|
|
|
/// \brief Closes the file descriptors and frees possible allocated memory
|
|
|
|
///
|
|
|
|
/// The success argument determines if source or destination file gets
|
|
|
|
/// unlinked:
|
|
|
|
/// - false: The destination file is unlinked.
|
|
|
|
/// - true: The source file is unlinked unless writing to stdout or --keep
|
|
|
|
/// was used.
|
2007-12-08 23:42:33 +01:00
|
|
|
extern void io_close(file_pair *pair, bool success);
|
|
|
|
|
2008-11-19 19:46:52 +01:00
|
|
|
|
|
|
|
/// \brief Reads from the source file to a buffer
|
|
|
|
///
|
|
|
|
/// \param pair File pair having the source file open for reading
|
|
|
|
/// \param buf Destination buffer to hold the read data
|
|
|
|
/// \param size Size of the buffer; assumed be smaller than SSIZE_MAX
|
|
|
|
///
|
|
|
|
/// \return On success, number of bytes read is returned. On end of
|
|
|
|
/// file zero is returned and pair->src_eof set to true.
|
|
|
|
/// On error, SIZE_MAX is returned and error message printed.
|
2007-12-08 23:42:33 +01:00
|
|
|
extern size_t io_read(file_pair *pair, uint8_t *buf, size_t size);
|
|
|
|
|
|
|
|
|
2008-11-19 19:46:52 +01:00
|
|
|
/// \brief Writes a buffer to the destination file
|
|
|
|
///
|
|
|
|
/// \param pair File pair having the destination file open for writing
|
|
|
|
/// \param buf Buffer containing the data to be written
|
|
|
|
/// \param size Size of the buffer; assumed be smaller than SSIZE_MAX
|
|
|
|
///
|
|
|
|
/// \return On success, zero is returned. On error, -1 is returned
|
|
|
|
/// and error message printed.
|
|
|
|
extern bool io_write(const file_pair *pair, const uint8_t *buf, size_t size);
|
2007-12-08 23:42:33 +01:00
|
|
|
|
|
|
|
#endif
|