Banjo API 1.1.0-alpha
Low-level C99 game development API
Loading...
Searching...
No Matches
stream_memory.c
Go to the documentation of this file.
1
22
23#include <banjo/log.h>
24#include <banjo/main.h>
25#include <banjo/stream.h>
26
27#define MAGIC 0x53415645u // "SAVE", big-endian
28
29int main(int argc, char* argv[]) {
30 (void)argc; (void)argv;
31
32 // An empty stream in memory: it owns its bytes and grows as we write.
34 if (save == 0) {
35 bj_err("Out of memory");
36 return 1;
37 }
38
39 // The record: a big-endian magic number, a version, a score and a
40 // signed position. Each writer returns BJ_TRUE when all its bytes went
41 // through, so a whole record can be checked with one condition.
42 const bj_bool written = bj_write_stream_u32_be(save, MAGIC)
43 && bj_write_stream_u16_le(save, 2)
44 && bj_write_stream_u32_le(save, 125000)
45 && bj_write_stream_i16_le(save, -42);
46 bj_info("Record written: %s, %zu bytes", written ? "yes" : "no", bj_stream_length(save));
47
48 // Peeking shows the bytes as the format stores them, without moving the
49 // cursor: the magic reads "SAVE" in memory because it is big-endian.
50 const unsigned char* bytes = (const unsigned char*)bj_stream_peek(save, 0, 4);
51 if (bytes != 0) {
52 bj_info("First bytes: %c%c%c%c", bytes[0], bytes[1], bytes[2], bytes[3]);
53 }
54
55 // Back to the start, and read the record as a parser would.
57 uint32_t magic = 0;
58 uint16_t version = 0;
59 uint32_t score = 0;
60 int16_t position = 0;
61 if (bj_read_stream_u32_be(save, &magic) && magic == MAGIC
62 && bj_read_stream_u16_le(save, &version)
63 && bj_read_stream_u32_le(save, &score)
64 && bj_read_stream_i16_le(save, &position)) {
65 bj_info("Version %u, score %lu, position %d", (unsigned)version, (unsigned long)score, (int)position);
66 }
67
68 // One read too many: the reader gives BJ_FALSE, and the status tells
69 // the end of the data from a failure. A seek clears it.
70 uint8_t extra = 0;
71 if (!bj_read_stream_u8(save, &extra)) {
72 bj_info("Past the record, the status is %s",
73 bj_stream_status(save) == BJ_STREAM_STATUS_END ? "END" : "an error");
74 }
75
77 return 0;
78}
struct bj_stream bj_stream
Definition api.h:374
uint32_t bj_bool
Boolean type used throughout the Banjo API.
Definition api.h:264
#define bj_info(...)
Log a message using the BJ_LOG_INFO level.
Definition log.h:141
#define bj_err(...)
Log a message using the BJ_LOG_ERROR level.
Definition log.h:169
bj_bool bj_write_stream_i16_le(struct bj_stream *stream, int16_t value)
Writes a little-endian int16_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.
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.
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_u32_le(struct bj_stream *stream, uint32_t *value)
Reads a little-endian uint32_t.
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_u32_be(struct bj_stream *stream, uint32_t value)
Writes a big-endian uint32_t.
struct bj_stream * bj_create_stream(void)
Create an empty stream in memory, which reads and writes, and grows as it is written.
bj_bool bj_read_stream_i16_le(struct bj_stream *stream, int16_t *value)
Reads a little-endian int16_t.
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
@ BJ_SEEK_BEGIN
The beginning of the stream.
Definition stream.h:115
@ BJ_STREAM_STATUS_END
The data ran out, or a fixed-size stream is full.
Definition stream.h:152
Logging utility functions.
Portable main() replacement with platform-aware entry shim.
Header file for struct bj_stream struct and related functions.
#define MAGIC
int main(void)