2025-01-18 17:24:36 +03:00
|
|
|
#ifndef BH_IO_H
|
|
|
|
|
#define BH_IO_H
|
|
|
|
|
|
|
|
|
|
|
2025-02-02 21:40:31 +03:00
|
|
|
#include "Common.h"
|
2025-01-18 17:24:36 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
#define BH_IO_INFO_CB 0x0000
|
|
|
|
|
#define BH_IO_INIT_CB 0x0001
|
|
|
|
|
#define BH_IO_DESTROY_CB 0x0002
|
|
|
|
|
#define BH_IO_OPEN_CB 0x0003
|
|
|
|
|
#define BH_IO_CLOSE_CB 0x0004
|
|
|
|
|
#define BH_IO_READ_CB 0x0005
|
|
|
|
|
#define BH_IO_WRITE_CB 0x0006
|
|
|
|
|
#define BH_IO_PEEK_CB 0x0007
|
|
|
|
|
#define BH_IO_TELL_CB 0x0008
|
|
|
|
|
#define BH_IO_SEEK_CB 0x0009
|
|
|
|
|
#define BH_IO_FLUSH_CB 0x000A
|
|
|
|
|
#define BH_IO_SIZE_CB 0x000B
|
|
|
|
|
#define BH_IO_FLAGS_CB 0x000C
|
|
|
|
|
#define BH_IO_CLEAR_CB 0x000D
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define BH_IO_READ 0x0001
|
|
|
|
|
#define BH_IO_WRITE 0x0002
|
|
|
|
|
#define BH_IO_READWRITE 0x0003
|
|
|
|
|
#define BH_IO_APPEND 0x0010
|
|
|
|
|
#define BH_IO_TRUNCATE 0x0020
|
|
|
|
|
#define BH_IO_CREATE 0x0040
|
|
|
|
|
#define BH_IO_EXIST 0x0080
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define BH_IO_SEEK_SET 0x0000
|
|
|
|
|
#define BH_IO_SEEK_CUR 0x0001
|
|
|
|
|
#define BH_IO_SEEK_END 0x0002
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define BH_IO_FLAG_OK 0x0000
|
|
|
|
|
#define BH_IO_FLAG_ERROR 0x0001
|
|
|
|
|
#define BH_IO_FLAG_EOF 0x0002
|
|
|
|
|
#define BH_IO_FLAG_OPEN 0x0004
|
|
|
|
|
|
2025-01-30 13:53:26 +03:00
|
|
|
#define BH_FILE_CLASSNAME "BH_File"
|
2025-01-18 17:24:36 +03:00
|
|
|
|
2025-01-30 13:53:26 +03:00
|
|
|
|
|
|
|
|
typedef struct BH_IO BH_IO;
|
|
|
|
|
typedef int (*BH_IOCallback)(void *, int ,void *, void *);
|
2025-01-18 17:24:36 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates the IO handle that represents file.
|
|
|
|
|
*
|
|
|
|
|
* \param path Path to the file
|
|
|
|
|
*
|
|
|
|
|
* \return On success, returns IO handle.
|
|
|
|
|
* \return On failure, returns NULL pointer.
|
|
|
|
|
*/
|
2025-01-30 13:53:26 +03:00
|
|
|
BH_IO *BH_FileNew(const char *path);
|
2025-01-18 17:24:36 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates the IO handle that represents buffered IO.
|
|
|
|
|
*
|
|
|
|
|
* \param io IO handle
|
|
|
|
|
*
|
|
|
|
|
* \return On success, returns IO handle.
|
|
|
|
|
* \return On failure, returns NULL pointer.
|
|
|
|
|
*/
|
2025-01-30 13:53:26 +03:00
|
|
|
BH_IO *BH_BufferNew(BH_IO *io);
|
2025-01-18 17:24:36 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates the IO handle with specified handler and context.
|
|
|
|
|
*
|
|
|
|
|
* \param func IO actions handler
|
|
|
|
|
* \param data Initialization data
|
|
|
|
|
*
|
|
|
|
|
* \return On success, returns IO handle.
|
|
|
|
|
* \return On failure, returns NULL pointer.
|
|
|
|
|
*/
|
2025-01-30 13:53:26 +03:00
|
|
|
BH_IO *BH_IONew(BH_IOCallback cb,
|
|
|
|
|
void *data);
|
2025-01-18 17:24:36 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Destroys the IO.
|
|
|
|
|
*
|
|
|
|
|
* \param io IO handle
|
|
|
|
|
*/
|
2025-01-30 13:53:26 +03:00
|
|
|
void BH_IOFree(BH_IO *io);
|
2025-01-18 17:24:36 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the IO instance classname.
|
|
|
|
|
*
|
|
|
|
|
* \param io IO handle
|
|
|
|
|
*
|
|
|
|
|
* \return On success, returns pointer to constant string.
|
|
|
|
|
* \return On failure, returns NULL pointer
|
|
|
|
|
*/
|
2025-01-30 13:53:26 +03:00
|
|
|
const char *BH_IOClassname(BH_IO* io);
|
2025-01-18 17:24:36 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Opens the IO in specified mode of operation.
|
|
|
|
|
*
|
|
|
|
|
* \param io IO handle
|
|
|
|
|
* \param mode Mode of operation
|
|
|
|
|
*
|
|
|
|
|
* \return On success, returns zero value.
|
|
|
|
|
* \return On failure, returns error code.
|
|
|
|
|
*/
|
2025-01-30 13:53:26 +03:00
|
|
|
int BH_IOOpen(BH_IO *io,
|
|
|
|
|
int mode);
|
2025-01-18 17:24:36 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Closes the IO.
|
|
|
|
|
*
|
|
|
|
|
* \param io IO handle
|
|
|
|
|
*
|
|
|
|
|
* \return On success, returns zero value.
|
|
|
|
|
* \return On failure, returns error code.
|
|
|
|
|
*/
|
2025-01-30 13:53:26 +03:00
|
|
|
int BH_IOClose(BH_IO *io);
|
2025-01-18 17:24:36 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Reads up to specified amount of bytes from the IO into memory buffer.
|
|
|
|
|
*
|
|
|
|
|
* \param io IO handle
|
|
|
|
|
* \param buffer Pointer to the buffer
|
|
|
|
|
* \param size Bytes to read
|
|
|
|
|
* \param actual Bytes read
|
|
|
|
|
*
|
|
|
|
|
* \return On success, returns zero value.
|
|
|
|
|
* \return On failure, returns error code.
|
|
|
|
|
*/
|
2025-01-30 13:53:26 +03:00
|
|
|
int BH_IORead(BH_IO *io,
|
|
|
|
|
char *buffer,
|
|
|
|
|
size_t size,
|
|
|
|
|
size_t *actual);
|
2025-01-18 17:24:36 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Writes up to specified amount of bytes into the IO from the memory buffer.
|
|
|
|
|
*
|
|
|
|
|
* \param io IO handle
|
|
|
|
|
* \param buffer Pointer to the buffer
|
|
|
|
|
* \param size Bytes to write
|
|
|
|
|
* \param actual Bytes written
|
|
|
|
|
*
|
|
|
|
|
* \return On success, returns zero value.
|
|
|
|
|
* \return On failure, returns error code.
|
|
|
|
|
*/
|
2025-01-30 13:53:26 +03:00
|
|
|
int BH_IOWrite(BH_IO *io,
|
|
|
|
|
const char *buffer,
|
|
|
|
|
size_t size,
|
|
|
|
|
size_t *actual);
|
2025-01-18 17:24:36 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Peeks up to specified amount of bytes from the IO handle.
|
|
|
|
|
*
|
|
|
|
|
* \param io IO handle
|
|
|
|
|
* \param buffer Pointer to the buffer
|
|
|
|
|
* \param size Bytes to peek
|
|
|
|
|
* \param actial Bytes peeked
|
|
|
|
|
*
|
|
|
|
|
* \return On success, returns zero value.
|
|
|
|
|
* \return On failure, returns error code.
|
|
|
|
|
*/
|
2025-01-30 13:53:26 +03:00
|
|
|
int BH_IOPeek(BH_IO *io,
|
|
|
|
|
char *buffer,
|
|
|
|
|
size_t size,
|
|
|
|
|
size_t *actual);
|
2025-01-18 17:24:36 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Tells current offset in the IO.
|
|
|
|
|
*
|
|
|
|
|
* \param io IO handle
|
|
|
|
|
* \param position Offset
|
|
|
|
|
*
|
|
|
|
|
* \return On success, returns zero value.
|
|
|
|
|
* \return On failure, returns error code.
|
|
|
|
|
*/
|
2025-01-30 13:53:26 +03:00
|
|
|
int BH_IOTell(BH_IO *io,
|
|
|
|
|
int64_t *position);
|
2025-01-18 17:24:36 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Seeks to specified offset in the IO.
|
|
|
|
|
*
|
|
|
|
|
* \param io IO handle
|
|
|
|
|
* \param position Offset
|
|
|
|
|
* \param direction Direction
|
|
|
|
|
*
|
|
|
|
|
* \return On success, returns zero value.
|
|
|
|
|
* \return On failure, returns error code.
|
|
|
|
|
*/
|
2025-01-30 13:53:26 +03:00
|
|
|
int BH_IOSeek(BH_IO *io,
|
|
|
|
|
int64_t position,
|
|
|
|
|
int direction);
|
2025-01-18 17:24:36 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Flushes the internal buffers of the IO.
|
|
|
|
|
*
|
|
|
|
|
* \param io IO handle
|
|
|
|
|
*
|
|
|
|
|
* \return On success, returns zero value.
|
|
|
|
|
* \return On failure, returns error code.
|
|
|
|
|
*/
|
2025-01-30 13:53:26 +03:00
|
|
|
int BH_IOFlush(BH_IO *io);
|
2025-01-18 17:24:36 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the size of the IO (either total or available size).
|
|
|
|
|
*
|
|
|
|
|
* \param io IO handle
|
|
|
|
|
* \param size Available/total size
|
|
|
|
|
*
|
|
|
|
|
* \return On success, returns zero value.
|
|
|
|
|
* \return On failure, returns error code.
|
|
|
|
|
*/
|
2025-01-30 13:53:26 +03:00
|
|
|
int BH_IOSize(BH_IO *io,
|
|
|
|
|
int64_t *size);
|
2025-01-18 17:24:36 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns flags of the IO.
|
|
|
|
|
*
|
|
|
|
|
* \param io IO handle
|
|
|
|
|
*
|
|
|
|
|
* \return Flags of the IO
|
|
|
|
|
*/
|
2025-01-30 13:53:26 +03:00
|
|
|
int BH_IOFlags(BH_IO *io);
|
2025-01-18 17:24:36 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Clears errors of the IO.
|
|
|
|
|
*
|
|
|
|
|
* \param io IO handle
|
|
|
|
|
*
|
|
|
|
|
* \return On success, returns zero value.
|
|
|
|
|
* \return On failure, returns error code.
|
|
|
|
|
*/
|
2025-01-30 13:53:26 +03:00
|
|
|
int BH_IOClear(BH_IO *io);
|
2025-01-18 17:24:36 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
#endif /* BH_IO_H */
|