This repository has been archived on 2026-04-17. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
bhlib-old/include/bh/io.h
Mikhail Romanko 692b5b4297 Sync to the latest version.
Can be broken or partially implemented.
2024-04-23 23:45:43 +03:00

183 lines
4.3 KiB
C

#ifndef BHLIB_IO_H
#define BHLIB_IO_H
#include "bh.h"
#define BH_IO_ERROR (1 << 0)
#define BH_IO_EOF (1 << 1)
#define BH_IO_CAST(x) \
((bh_io_t *)(x))
struct bh_io_s;
typedef struct bh_io_table_s
{
size_t (*read)(struct bh_io_s *io,
char *data,
size_t size);
size_t (*write)(struct bh_io_s *io,
const char *data,
size_t size);
void (*flush)(struct bh_io_s *io);
void (*seek)(struct bh_io_s *io,
bh_off_t offset,
int dir);
bh_off_t (*tell)(struct bh_io_s *io);
bh_off_t (*available)(struct bh_io_s *io);
void (*clear)(struct bh_io_s *io);
void (*destroy)(struct bh_io_s *io);
} bh_io_table_t;
typedef struct bh_io_s
{
bh_io_table_t *table;
int flags;
} bh_io_t;
/**
* @brief Create new IO object.
*
* @param table Valid pointer to the IO table.
* @param size Size of the IO object.
*
* @return If the function succeeds, the return value is a pointer to the
* new, semi-initalized, IO object.
* @return If the function fails, the return value is NULL.
*
* @warning This function should be used in context of implementing child
* IO objects (files, sockets, streaming compression, etc).
*
* @sa bh_io_free
*/
bh_io_t *bh_io_new(bh_io_table_t *table,
size_t size);
/**
* @brief Free IO object.
*
* @param io Valid pointer to the IO object.
*/
void bh_io_free(bh_io_t *io);
/**
* @brief Read data from IO.
*
* @param io Valid pointer to the IO object.
* @param data Valid pointer to the buffer.
* @param size Size of the buffer.
*
* @return If the function succeeds, the return value is the amount
* of bytes read from the IO object.
* @return If the function fails, the return value is zero and error
* flag is set.
*
* @sa bh_io_write, bh_io_eof, bh_io_error
*/
size_t bh_io_read(bh_io_t *io,
char *data,
size_t size);
/**
* @brief Write data to IO.
*
* @param io Valid pointer to the IO object.
* @param data Valid pointer to the buffer.
* @param size Size of the buffer.
*
* @return If the function succeeds, the return value is the amount
* of bytes written to the IO object.
* @return If the function fails, the return value is zero and error
* flag is set.
*
* @sa bh_io_read, bh_io_error, bh_io_flush
*/
size_t bh_io_write(bh_io_t *io,
const char* data,
size_t size);
/**
* @brief Writes any uncommited changes (if possible).
*
* @param io Valid pointer to the IO object.
*/
void bh_io_flush(bh_io_t *io);
/**
* @brief Seeks IO object to the specified position (if possible).
*
* @param io Valid pointer to the IO object.
* @param offset Position
* @param dir Direction
*
* @sa bh_io_tell
*/
void bh_io_seek(bh_io_t *io,
bh_off_t offset,
int dir);
/**
* @brief Return current position in IO.
*
* @param io Valid pointer to the IO object.
*
* @return If the function succeeds, the return value is current
* position in the IO.
* @return If the function fails, the return value is -1.
*
* @sa bh_io_seek
*/
bh_off_t bh_io_tell(bh_io_t *io);
/**
* @brief Return available bytes in the IO.
*
* @param io Valid pointer to the IO object.
*
* @return If the function succeeds, the return value is the amount
* of the available bytes for reading.
* @return If the function fails, the return value is zero.
*/
bh_off_t bh_io_available(bh_io_t *io);
/**
* @brief Return error flag of the IO.
*
* @param io Valid pointer to the IO object.
*
* @return The return value is error flag.
*
* @sa bh_io_eof, bh_io_clear
*/
int bh_io_error(bh_io_t *io);
/**
* @brief Return end-of-file flag of the IO.
*
* @param io Valid pointer to the IO object.
*
* @return The return value is end-of-file flag.
*
* @sa bh_io_error, bh_io_clear
*/
int bh_io_eof(bh_io_t *io);
/**
* @brief Crear IO object state.
*
* @param io Valid pointer to the IO object.
*
* @sa bh_io_error, bh_io_eof
*/
void bh_io_clear(bh_io_t *io);
#endif /* BHLIB_IO_H */