Banjo API 1.1.0-alpha
Low-level C99 game development API
Loading...
Searching...
No Matches
stream.h
Go to the documentation of this file.
1
99#ifndef BJ_STREAM_H
100#define BJ_STREAM_H
101
102#include <banjo/api.h>
103#include <banjo/error.h>
104#include <banjo/string.h>
105
109struct bj_stream;
110
119#ifndef BJ_NO_TYPEDEF
121#endif
122
138#ifndef BJ_NO_TYPEDEF
140#endif
141
159// No typedef: `bj_stream_status` names the function that reads the status.
160
172typedef size_t (*bj_stream_read_fn)(
173 void* buffer,
174 size_t count,
175 void* user_data,
176 enum bj_stream_status* status
177);
178
189typedef size_t (*bj_stream_write_fn)(
190 const void* data,
191 size_t count,
192 void* user_data,
193 enum bj_stream_status* status
194);
195
207 size_t position,
208 void* user_data
209);
210
216typedef size_t (*bj_stream_size_fn)(
217 void* user_data
218);
219
227 void* user_data
228);
229
235typedef void (*bj_stream_close_fn)(
236 void* user_data
237);
238
265
279 void
280);
281
284
300 const void* data,
301 size_t length
302);
303
305BANJO_EXPORT struct bj_stream* bj_open_stream_read(const void* data, size_t length);
306
321 void* data,
322 size_t length,
323 uint32_t flags
324);
325
360 struct bj_str path,
361 uint32_t flags,
362 struct bj_error** error
363);
364
366BANJO_EXPORT struct bj_stream* bj_open_stream_file(const char* path, struct bj_error** error);
367
399 const struct bj_stream_callbacks* callbacks,
400 uint32_t flags,
401 void* user_data,
402 struct bj_error** error
403);
404
425 struct bj_stream* parent,
426 size_t offset,
427 size_t length
428);
429
445 struct bj_stream* stream
446);
447
450
473 struct bj_stream* stream,
474 void* dest,
475 size_t count
476);
477
494 struct bj_stream* stream,
495 const void* data,
496 size_t count
497);
498
510 struct bj_stream* stream
511);
512
522 struct bj_stream* stream
523);
524
527
544 struct bj_stream* stream,
545 ptrdiff_t position,
546 enum bj_seek_origin from
547);
548
557 struct bj_stream* stream
558);
559
561BANJO_EXPORT size_t bj_tell_stream(struct bj_stream* stream);
562
580 struct bj_stream* stream,
581 size_t offset,
582 size_t length
583);
584
592 const struct bj_stream* stream
593);
594
601 struct bj_stream* stream
602);
603
611 const struct bj_stream* stream
612);
613
622 const struct bj_stream* stream,
623 enum bj_stream_flag flag
624);
625
635 const struct bj_stream* stream
636);
637
647 const struct bj_stream* stream
648);
649
667BANJO_EXPORT bj_bool bj_read_stream_u8(struct bj_stream* stream, uint8_t* value);
668BANJO_EXPORT bj_bool bj_read_stream_i8(struct bj_stream* stream, int8_t* value);
669BANJO_EXPORT bj_bool bj_read_stream_u16_le(struct bj_stream* stream, uint16_t* value);
670BANJO_EXPORT bj_bool bj_read_stream_u16_be(struct bj_stream* stream, uint16_t* value);
671BANJO_EXPORT bj_bool bj_read_stream_i16_le(struct bj_stream* stream, int16_t* value);
672BANJO_EXPORT bj_bool bj_read_stream_i16_be(struct bj_stream* stream, int16_t* value);
673BANJO_EXPORT bj_bool bj_read_stream_u32_le(struct bj_stream* stream, uint32_t* value);
674BANJO_EXPORT bj_bool bj_read_stream_u32_be(struct bj_stream* stream, uint32_t* value);
675BANJO_EXPORT bj_bool bj_read_stream_i32_le(struct bj_stream* stream, int32_t* value);
676BANJO_EXPORT bj_bool bj_read_stream_i32_be(struct bj_stream* stream, int32_t* value);
677BANJO_EXPORT bj_bool bj_read_stream_u64_le(struct bj_stream* stream, uint64_t* value);
678BANJO_EXPORT bj_bool bj_read_stream_u64_be(struct bj_stream* stream, uint64_t* value);
679BANJO_EXPORT bj_bool bj_read_stream_i64_le(struct bj_stream* stream, int64_t* value);
680BANJO_EXPORT bj_bool bj_read_stream_i64_be(struct bj_stream* stream, int64_t* value);
681
682BANJO_EXPORT bj_bool bj_write_stream_u8(struct bj_stream* stream, uint8_t value);
683BANJO_EXPORT bj_bool bj_write_stream_i8(struct bj_stream* stream, int8_t value);
684BANJO_EXPORT bj_bool bj_write_stream_u16_le(struct bj_stream* stream, uint16_t value);
685BANJO_EXPORT bj_bool bj_write_stream_u16_be(struct bj_stream* stream, uint16_t value);
686BANJO_EXPORT bj_bool bj_write_stream_i16_le(struct bj_stream* stream, int16_t value);
687BANJO_EXPORT bj_bool bj_write_stream_i16_be(struct bj_stream* stream, int16_t value);
688BANJO_EXPORT bj_bool bj_write_stream_u32_le(struct bj_stream* stream, uint32_t value);
689BANJO_EXPORT bj_bool bj_write_stream_u32_be(struct bj_stream* stream, uint32_t value);
690BANJO_EXPORT bj_bool bj_write_stream_i32_le(struct bj_stream* stream, int32_t value);
691BANJO_EXPORT bj_bool bj_write_stream_i32_be(struct bj_stream* stream, int32_t value);
692BANJO_EXPORT bj_bool bj_write_stream_u64_le(struct bj_stream* stream, uint64_t value);
693BANJO_EXPORT bj_bool bj_write_stream_u64_be(struct bj_stream* stream, uint64_t value);
694BANJO_EXPORT bj_bool bj_write_stream_i64_le(struct bj_stream* stream, int64_t value);
695BANJO_EXPORT bj_bool bj_write_stream_i64_be(struct bj_stream* stream, int64_t value);
697
712#define bj_stream_read_t(stream, type, buffer) bj_read_stream(stream, buffer, sizeof(type))
713
720#define bj_stream_skip_t(stream, type) bj_read_stream(stream, 0, sizeof(type))
721
722#endif
General-purpose definitions for Banjo API.
bj_audio_play_note_data data
Definition audio_pcm.c:104
Recoverable error handling.
struct bj_stream bj_stream
Definition api.h:374
struct bj_error bj_error
Definition api.h:348
#define BANJO_EXPORT
Definition api.h:179
uint32_t bj_bool
Boolean type used throughout the Banjo API.
Definition api.h:264
bj_stream_size_fn size
Gives the size, or 0.
Definition stream.h:261
bj_stream_read_fn read
Reads bytes, or 0.
Definition stream.h:258
bj_stream_write_fn write
Writes bytes, or 0.
Definition stream.h:259
bj_stream_seek_fn seek
Moves the source's cursor, or 0.
Definition stream.h:260
bj_stream_flush_fn flush
Flushes, or 0.
Definition stream.h:262
bj_stream_close_fn close
Releases the source, or 0.
Definition stream.h:263
bj_bool bj_read_stream_u64_le(struct bj_stream *stream, uint64_t *value)
Reads a little-endian uint64_t.
bj_bool(* bj_stream_seek_fn)(size_t position, void *user_data)
Moves the source's cursor to an absolute position.
Definition stream.h:206
bj_bool bj_write_stream_i16_le(struct bj_stream *stream, int16_t value)
Writes a little-endian int16_t.
bj_bool bj_write_stream_u16_be(struct bj_stream *stream, uint16_t value)
Writes a big-endian uint16_t.
bj_bool bj_write_stream_u8(struct bj_stream *stream, uint8_t value)
Writes one byte.
bj_bool bj_write_stream_i32_be(struct bj_stream *stream, int32_t value)
Writes a big-endian int32_t.
bj_bool bj_read_stream_i32_le(struct bj_stream *stream, int32_t *value)
Reads a little-endian int32_t.
bj_stream_flag
What a stream is opened for, combined with |.
Definition stream.h:129
struct bj_stream * bj_open_stream_read(const void *data, size_t length)
bj_bool bj_write_stream_u64_le(struct bj_stream *stream, uint64_t value)
Writes a little-endian uint64_t.
size_t bj_seek_stream(struct bj_stream *stream, ptrdiff_t position, enum bj_seek_origin from)
Seeks to a new position in the stream relative to a specified origin.
size_t bj_stream_length(struct bj_stream *stream)
Return the total size of the stream, in bytes.
bj_bool bj_read_stream_u8(struct bj_stream *stream, uint8_t *value)
Reads one byte.
bj_bool(* bj_stream_flush_fn)(void *user_data)
Pushes the bytes the source holds back to where they go.
Definition stream.h:226
const void * bj_stream_peek(struct bj_stream *stream, size_t offset, size_t length)
Points at bytes of the stream without moving the cursor.
bj_bool bj_write_stream_u16_le(struct bj_stream *stream, uint16_t value)
Writes a little-endian uint16_t.
void bj_destroy_stream(struct bj_stream *stream)
Destroy a stream and release its memory.
size_t bj_stream_position(struct bj_stream *stream)
Return the current cursor position within the stream.
size_t bj_get_stream_length(struct bj_stream *stream)
bj_bool bj_read_stream_i64_le(struct bj_stream *stream, int64_t *value)
Reads a little-endian int64_t.
bj_bool bj_stream_has_flag(const struct bj_stream *stream, enum bj_stream_flag flag)
Whether the stream was created with one flag.
struct bj_stream * bj_allocate_stream(void)
bj_bool bj_write_stream_i32_le(struct bj_stream *stream, int32_t value)
Writes a little-endian int32_t.
size_t(* bj_stream_size_fn)(void *user_data)
Gives the current size of the source, in bytes.
Definition stream.h:216
bj_bool bj_flush_stream(struct bj_stream *stream)
Pushes what the stream wrote to where it goes.
struct bj_stream * bj_open_stream_file(const char *path, struct bj_error **error)
bj_seek_origin
Position in a bj_stream to use for origin.
Definition stream.h:114
bj_bool bj_read_stream_u16_le(struct bj_stream *stream, uint16_t *value)
Reads a little-endian uint16_t.
bj_bool bj_read_stream_u16_be(struct bj_stream *stream, uint16_t *value)
Reads a big-endian uint16_t.
bj_bool bj_read_stream_u32_le(struct bj_stream *stream, uint32_t *value)
Reads a little-endian uint32_t.
size_t bj_tell_stream(struct bj_stream *stream)
struct bj_stream * bj_create_stream_from_callbacks(const struct bj_stream_callbacks *callbacks, uint32_t flags, void *user_data, struct bj_error **error)
Create a stream over a source the program supplies.
bj_bool bj_read_stream_u64_be(struct bj_stream *stream, uint64_t *value)
Reads a big-endian uint64_t.
struct bj_stream * bj_create_stream_from_path(struct bj_str path, uint32_t flags, struct bj_error **error)
Create a stream that reads, writes or creates a file.
uint32_t bj_stream_flags(const struct bj_stream *stream)
The flags the stream was created with.
struct bj_stream * bj_create_stream_from_memory(void *data, size_t length, uint32_t flags)
Create a stream that reads and writes the caller's buffer in place.
bj_bool bj_write_stream_u32_le(struct bj_stream *stream, uint32_t value)
Writes a little-endian uint32_t.
bj_bool bj_write_stream_u64_be(struct bj_stream *stream, uint64_t value)
Writes a big-endian uint64_t.
void bj_close_stream(struct bj_stream *stream)
void(* bj_stream_close_fn)(void *user_data)
Releases the source, once, when the stream is destroyed.
Definition stream.h:235
bj_bool bj_read_stream_i8(struct bj_stream *stream, int8_t *value)
Reads one signed byte.
bj_bool bj_write_stream_i8(struct bj_stream *stream, int8_t value)
Writes one signed byte.
bj_bool bj_read_stream_i64_be(struct bj_stream *stream, int64_t *value)
Reads a big-endian int64_t.
struct bj_stream * bj_create_stream_span(struct bj_stream *parent, size_t offset, size_t length)
Create a span over the range [offset, offset + length) of another stream.
struct bj_stream * bj_create_stream_from_read_memory(const void *data, size_t length)
Create a read-only stream that reads from an in-memory buffer.
bj_bool bj_write_stream_u32_be(struct bj_stream *stream, uint32_t value)
Writes a big-endian uint32_t.
bj_bool bj_write_stream_i64_be(struct bj_stream *stream, int64_t value)
Writes a big-endian int64_t.
bj_bool bj_stream_is_seekable(const struct bj_stream *stream)
Whether the stream can move its cursor.
struct bj_stream * bj_create_stream(void)
Create an empty stream in memory, which reads and writes, and grows as it is written.
size_t(* bj_stream_write_fn)(const void *data, size_t count, void *user_data, enum bj_stream_status *status)
Moves up to count bytes from data into the source.
Definition stream.h:189
bj_bool bj_read_stream_i16_le(struct bj_stream *stream, int16_t *value)
Reads a little-endian int16_t.
size_t bj_write_stream(struct bj_stream *stream, const void *data, size_t count)
Writes bytes at the cursor, over those already there.
bj_bool bj_write_stream_i64_le(struct bj_stream *stream, int64_t value)
Writes a little-endian int64_t.
bj_bool bj_stream_is_mapped(const struct bj_stream *stream)
Whether the stream reads a file mapped into memory.
bj_bool bj_read_stream_u32_be(struct bj_stream *stream, uint32_t *value)
Reads a big-endian uint32_t.
bj_stream_status
Why the last read or write of a stream fell short.
Definition stream.h:150
void bj_clear_stream_status(struct bj_stream *stream)
Sets the status back to BJ_STREAM_STATUS_OK.
bj_bool bj_write_stream_i16_be(struct bj_stream *stream, int16_t value)
Writes a big-endian int16_t.
bj_bool bj_read_stream_i32_be(struct bj_stream *stream, int32_t *value)
Reads a big-endian int32_t.
size_t bj_read_stream(struct bj_stream *stream, void *dest, size_t count)
Reads data from the stream into a destination buffer.
size_t(* bj_stream_read_fn)(void *buffer, size_t count, void *user_data, enum bj_stream_status *status)
Moves up to count bytes from the source into buffer.
Definition stream.h:172
bj_bool bj_read_stream_i16_be(struct bj_stream *stream, int16_t *value)
Reads a big-endian int16_t.
@ BJ_STREAM_EXCLUSIVE
An existing file is refused.
Definition stream.h:135
@ BJ_STREAM_READ_WRITE
The stream reads and writes.
Definition stream.h:136
@ BJ_STREAM_CREATE
A missing file is created.
Definition stream.h:134
@ BJ_STREAM_WRITE
The stream writes.
Definition stream.h:131
@ BJ_STREAM_READ
The stream reads.
Definition stream.h:130
@ BJ_STREAM_TRUNCATE
The file is emptied when opened.
Definition stream.h:133
@ BJ_STREAM_APPEND
Every write goes to the end of the file.
Definition stream.h:132
@ BJ_SEEK_CURRENT
The current position of the stream.
Definition stream.h:116
@ BJ_SEEK_END
The end of the stream.
Definition stream.h:117
@ BJ_SEEK_BEGIN
The beginning of the stream.
Definition stream.h:115
@ BJ_STREAM_STATUS_LOCKED
A transfer on a stream held by a bj_text_wrapper, which is the only reader and writer while it lives.
Definition stream.h:157
@ BJ_STREAM_STATUS_END
The data ran out, or a fixed-size stream is full.
Definition stream.h:152
@ BJ_STREAM_STATUS_READ_ONLY
A write on a stream without BJ_STREAM_WRITE.
Definition stream.h:155
@ BJ_STREAM_STATUS_NOT_READY
The source could not answer yet, such as a socket whose timeout expired.
Definition stream.h:153
@ BJ_STREAM_STATUS_OK
Nothing fell short.
Definition stream.h:151
@ BJ_STREAM_STATUS_WRITE_ONLY
A read on a stream without BJ_STREAM_READ.
Definition stream.h:156
@ BJ_STREAM_STATUS_ERROR
The source failed.
Definition stream.h:154
The functions of a source the program supplies.
Definition stream.h:257
A borrowed view of a sequence of bytes.
Definition string.h:57
UTF-8 string views and owning strings.