#ifndef SWIFT_IO_STREAM_H
#define SWIFT_IO_STREAM_H

#include <swift/atomic.h>
#include <swift/stdlib.h>

#include <swift/io/api.h>

typedef struct swift_stream_ops_t swift_stream_ops_t;
typedef struct swift_stream_t swift_stream_t;

typedef void (* swift_close_stream_func_t)(swift_stream_t *stream);
typedef int64_t (* swift_seek_stream_func_t)(swift_stream_t *stream,
	unsigned int whence, int64_t pos);
typedef size_t (* swift_read_stream_func_t)(swift_stream_t *stream,
	void *buffer, size_t size);
typedef size_t (* swift_write_stream_func_t)(swift_stream_t *stream,
	const void *buffer, size_t size);
typedef bool (* swift_flush_stream_func_t)(swift_stream_t *stream);

struct swift_stream_ops_t {
	swift_close_stream_func_t close;
	swift_seek_stream_func_t seek;
	swift_read_stream_func_t read;
	swift_write_stream_func_t write;
	swift_flush_stream_func_t flush;
};

struct swift_stream_t {
	swift_ref_t ref;
	swift_stream_ops_t *stream_ops;
};

SWIFT_IO_API void swift_close_stream(swift_stream_t *stream);
SWIFT_IO_API swift_stream_t *swift_ref_stream(swift_stream_t *stream);
SWIFT_IO_API int64_t swift_stream_seek(swift_stream_t *stream,
	unsigned int whence, int64_t pos);
SWIFT_IO_API size_t swift_stream_read(swift_stream_t *stream, void *buffer,
	size_t size);
SWIFT_IO_API size_t swift_stream_write(swift_stream_t *stream,
	const void *buffer, size_t size);
SWIFT_IO_API bool swift_stream_flush(swift_stream_t *stream);

SWIFT_IO_API bool swift_read_u8(swift_stream_t *stream, uint8_t *value);
SWIFT_IO_API bool swift_read_u16_be(swift_stream_t *stream, uint16_t *value);
SWIFT_IO_API bool swift_read_u16_le(swift_stream_t *stream, uint16_t *value);
SWIFT_IO_API bool swift_read_u32_be(swift_stream_t *stream, uint32_t *value);
SWIFT_IO_API bool swift_read_u32_le(swift_stream_t *stream, uint32_t *value);
SWIFT_IO_API bool swift_read_u64_be(swift_stream_t *stream, uint64_t *value);
SWIFT_IO_API bool swift_read_u64_le(swift_stream_t *stream, uint64_t *value);

SWIFT_IO_API bool swift_write_u8(swift_stream_t *stream, uint8_t value);
SWIFT_IO_API bool swift_write_u16_be(swift_stream_t *stream, uint16_t value);
SWIFT_IO_API bool swift_write_u16_le(swift_stream_t *stream, uint16_t value);
SWIFT_IO_API bool swift_write_u32_be(swift_stream_t *stream, uint32_t value);
SWIFT_IO_API bool swift_write_u32_le(swift_stream_t *stream, uint32_t value);
SWIFT_IO_API bool swift_write_u64_be(swift_stream_t *stream, uint64_t value);
SWIFT_IO_API bool swift_write_u64_le(swift_stream_t *stream, uint64_t value);

SWIFT_IO_API bool swift_read_f32_be(swift_stream_t *stream, float *value);
SWIFT_IO_API bool swift_read_f32_le(swift_stream_t *stream, float *value);
SWIFT_IO_API bool swift_read_f64_be(swift_stream_t *stream, double *value);
SWIFT_IO_API bool swift_read_f64_le(swift_stream_t *stream, double *value);

SWIFT_IO_API bool swift_write_f32_be(swift_stream_t *stream, double value);
SWIFT_IO_API bool swift_write_f32_le(swift_stream_t *stream, float value);
SWIFT_IO_API bool swift_write_f64_be(swift_stream_t *stream, double value);
SWIFT_IO_API bool swift_write_f64_le(swift_stream_t *stream, double value);

SWIFT_IO_API char *swift_read_cstr(swift_stream_t *stream);
SWIFT_IO_API bool swift_write_cstr(swift_stream_t *stream, const char *data);

#endif /* SWIFT_IO_STREAM_H */

