Banjo API 1.1.0-alpha
Low-level C99 game development API
Loading...
Searching...
No Matches
stream_spans.c
Go to the documentation of this file.
1
19
20#include <banjo/log.h>
21#include <banjo/main.h>
22#include <banjo/stream.h>
23
24int main(int argc, char* argv[]) {
25 (void)argc; (void)argv;
26
27 // A "file" of three tables, 4 bytes each: HEAD, NAME, DATA.
28 static const char file_bytes[] = "HEADname1234";
29 bj_stream* file = bj_create_stream_from_read_memory(file_bytes, 12);
30
31 // The NAME table: 4 bytes at offset 4. An out-of-range table, as a
32 // corrupt directory would declare, gives 0 here, once.
33 bj_stream* name = bj_create_stream_span(file, 4, 4);
34 bj_stream* broken = bj_create_stream_span(file, 10, 8);
35 bj_info("Span of NAME: %s; span of 8 bytes at 10: %s",
36 name != 0 ? "created" : "refused", broken != 0 ? "created" : "refused");
37 if (name == 0) {
38 bj_destroy_stream(broken);
40 return 1;
41 }
42
43 // A parser asks for 8 bytes, believing a wrong length: it gets the 4
44 // bytes of NAME, and the status says the table ended.
45 char buffer[9] = {0};
46 const size_t got = bj_read_stream(name, buffer, 8);
47 bj_info("Asked 8 bytes, got %zu: \"%s\", %s", got, buffer,
48 bj_stream_status(name) == BJ_STREAM_STATUS_END ? "end of the table" : "?");
49
50 // Peeking inside the span works; peeking past it gives 0.
51 const char* inside = (const char*)bj_stream_peek(name, 1, 2);
52 const void* outside = bj_stream_peek(name, 3, 2);
53 bj_info("Peek of 2 bytes at 1: \"%.2s\"; at 3: %s", inside, outside == 0 ? "0" : "a pointer");
54
55 // The parent's cursor never moved.
56 bj_info("The file's cursor is still at %zu", bj_stream_position(file));
57
58 // Spans borrow their parent: destroy them first. Destroying 0, the
59 // refused span, does nothing.
60 bj_destroy_stream(broken);
63 return 0;
64}
struct bj_stream bj_stream
Definition api.h:374
#define bj_info(...)
Log a message using the BJ_LOG_INFO level.
Definition log.h:141
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.
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.
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_stream_status
Why the last read or write of a stream fell short.
Definition stream.h:150
size_t bj_read_stream(struct bj_stream *stream, void *dest, size_t count)
Reads data from the stream into a destination buffer.
@ 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.
int main(void)