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
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2025-02-03 15:18:57 +03:00
|
|
|
* Creates the IO that represents file with the given \a path.
|
2025-01-18 17:24:36 +03:00
|
|
|
*
|
2025-02-03 15:18:57 +03:00
|
|
|
* \param path File path
|
2025-01-18 17:24:36 +03:00
|
|
|
*
|
2025-02-03 15:18:57 +03:00
|
|
|
* \return On success, returns IO pointer.
|
2025-01-18 17:24:36 +03:00
|
|
|
* \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
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2025-02-03 15:18:57 +03:00
|
|
|
* Creates the IO that buffers access to other \a io.
|
2025-01-18 17:24:36 +03:00
|
|
|
*
|
2025-02-03 15:18:57 +03:00
|
|
|
* \param io IO pointer
|
2025-01-18 17:24:36 +03:00
|
|
|
*
|
2025-02-03 15:18:57 +03:00
|
|
|
* \return On success, returns IO pointer.
|
2025-01-18 17:24:36 +03:00
|
|
|
* \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
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2025-02-03 15:18:57 +03:00
|
|
|
* Creates the IO with specified callback \a cb and \a data.
|
2025-01-18 17:24:36 +03:00
|
|
|
*
|
2025-02-03 15:18:57 +03:00
|
|
|
* \param cb Callback
|
2025-01-18 17:24:36 +03:00
|
|
|
* \param data Initialization data
|
|
|
|
|
*
|
2025-02-03 15:18:57 +03:00
|
|
|
* \return On success, returns IO pointer.
|
2025-01-18 17:24:36 +03:00
|
|
|
* \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
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2025-02-03 15:18:57 +03:00
|
|
|
* Destroys the \a io.
|
2025-01-18 17:24:36 +03:00
|
|
|
*
|
2025-02-03 15:18:57 +03:00
|
|
|
* \param io IO pointer
|
2025-01-18 17:24:36 +03:00
|
|
|
*/
|
2025-01-30 13:53:26 +03:00
|
|
|
void BH_IOFree(BH_IO *io);
|
2025-01-18 17:24:36 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2025-02-03 15:18:57 +03:00
|
|
|
* Returns the \a io classname.
|
2025-01-18 17:24:36 +03:00
|
|
|
*
|
2025-02-03 15:18:57 +03:00
|
|
|
* \param io IO pointer
|
2025-01-18 17:24:36 +03:00
|
|
|
*
|
|
|
|
|
* \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
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2025-02-03 15:18:57 +03:00
|
|
|
* Opens the \a io in specified \a mode of operation.
|
2025-01-18 17:24:36 +03:00
|
|
|
*
|
2025-02-03 15:18:57 +03:00
|
|
|
* \param io IO pointer
|
2025-01-18 17:24:36 +03:00
|
|
|
* \param mode Mode of operation
|
|
|
|
|
*
|
2025-02-03 15:18:57 +03:00
|
|
|
* \return On success, returns zero.
|
2025-01-18 17:24:36 +03:00
|
|
|
* \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
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2025-02-03 15:18:57 +03:00
|
|
|
* Closes the \a io.
|
2025-01-18 17:24:36 +03:00
|
|
|
*
|
2025-02-03 15:18:57 +03:00
|
|
|
* \param io IO pointer
|
2025-01-18 17:24:36 +03:00
|
|
|
*
|
2025-02-03 15:18:57 +03:00
|
|
|
* \return On success, returns zero.
|
2025-01-18 17:24:36 +03:00
|
|
|
* \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
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2025-02-03 15:18:57 +03:00
|
|
|
* Reads up to \a size bytes from the \a io into \a buffer.
|
2025-01-18 17:24:36 +03:00
|
|
|
*
|
2025-02-03 15:18:57 +03:00
|
|
|
* \param io IO pointer
|
|
|
|
|
* \param buffer Buffer pointer
|
|
|
|
|
* \param size Buffer size
|
|
|
|
|
* \param actual Bytes read (optional)
|
2025-01-18 17:24:36 +03:00
|
|
|
*
|
2025-02-03 15:18:57 +03:00
|
|
|
* \return On success, returns zero.
|
2025-01-18 17:24:36 +03:00
|
|
|
* \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
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2025-02-03 15:18:57 +03:00
|
|
|
* Writes up to \a size bytes to the \a io from \a buffer.
|
2025-01-18 17:24:36 +03:00
|
|
|
*
|
2025-02-03 15:18:57 +03:00
|
|
|
* \param io IO pointer
|
|
|
|
|
* \param buffer Buffer pointer
|
|
|
|
|
* \param size Buffer size
|
|
|
|
|
* \param actual Bytes written (optional)
|
2025-01-18 17:24:36 +03:00
|
|
|
*
|
2025-02-03 15:18:57 +03:00
|
|
|
* \return On success, returns zero.
|
2025-01-18 17:24:36 +03:00
|
|
|
* \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
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2025-02-03 15:18:57 +03:00
|
|
|
* Peeks up to \a size bytes from \a io into \a buffer.
|
2025-01-18 17:24:36 +03:00
|
|
|
*
|
2025-02-03 15:18:57 +03:00
|
|
|
* \param io IO pointer
|
|
|
|
|
* \param buffer Buffer pointer
|
|
|
|
|
* \param size Buffer size
|
|
|
|
|
* \param actial Bytes peeked (optional)
|
2025-01-18 17:24:36 +03:00
|
|
|
*
|
2025-02-03 15:18:57 +03:00
|
|
|
* \return On success, returns zero.
|
2025-01-18 17:24:36 +03:00
|
|
|
* \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
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2025-02-03 15:18:57 +03:00
|
|
|
* Tells current \a position in the \a io.
|
2025-01-18 17:24:36 +03:00
|
|
|
*
|
2025-02-03 15:18:57 +03:00
|
|
|
* \param io IO pointer
|
|
|
|
|
* \param position Position
|
2025-01-18 17:24:36 +03:00
|
|
|
*
|
2025-02-03 15:18:57 +03:00
|
|
|
* \return On success, returns zero.
|
2025-01-18 17:24:36 +03:00
|
|
|
* \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
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2025-02-03 15:18:57 +03:00
|
|
|
* Seeks to specified \a position and \a direction in the \a io.
|
2025-01-18 17:24:36 +03:00
|
|
|
*
|
2025-02-03 15:18:57 +03:00
|
|
|
* \param io IO pointer
|
|
|
|
|
* \param position Position
|
2025-01-18 17:24:36 +03:00
|
|
|
* \param direction Direction
|
|
|
|
|
*
|
2025-02-03 15:18:57 +03:00
|
|
|
* \return On success, returns zero.
|
2025-01-18 17:24:36 +03:00
|
|
|
* \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
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2025-02-03 15:18:57 +03:00
|
|
|
* Flushes the internal buffers of the \a io.
|
2025-01-18 17:24:36 +03:00
|
|
|
*
|
2025-02-03 15:18:57 +03:00
|
|
|
* \param io IO pointer
|
2025-01-18 17:24:36 +03:00
|
|
|
*
|
2025-02-03 15:18:57 +03:00
|
|
|
* \return On success, returns zero.
|
2025-01-18 17:24:36 +03:00
|
|
|
* \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
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2025-02-03 15:18:57 +03:00
|
|
|
* Returns total or available size of the \a io.
|
2025-01-18 17:24:36 +03:00
|
|
|
*
|
2025-02-03 15:18:57 +03:00
|
|
|
* \param io IO pointer
|
2025-01-18 17:24:36 +03:00
|
|
|
* \param size Available/total size
|
|
|
|
|
*
|
2025-02-03 15:18:57 +03:00
|
|
|
* \return On success, returns zero.
|
2025-01-18 17:24:36 +03:00
|
|
|
* \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
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2025-02-03 15:18:57 +03:00
|
|
|
* Returns flags of the \a io.
|
2025-01-18 17:24:36 +03:00
|
|
|
*
|
2025-02-03 15:18:57 +03:00
|
|
|
* \param io IO pointer
|
2025-01-18 17:24:36 +03:00
|
|
|
*
|
|
|
|
|
* \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
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2025-02-03 15:18:57 +03:00
|
|
|
* Clears errors of the \a io.
|
2025-01-18 17:24:36 +03:00
|
|
|
*
|
2025-02-03 15:18:57 +03:00
|
|
|
* \param io IO pointer
|
2025-01-18 17:24:36 +03:00
|
|
|
*
|
2025-02-03 15:18:57 +03:00
|
|
|
* \return On success, returns zero.
|
2025-01-18 17:24:36 +03:00
|
|
|
* \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 */
|