Banjo API 1.1.0-alpha
Low-level C99 game development API
Loading...
Searching...
No Matches
stream_file.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#include <banjo/string.h>
24
25#include <stdio.h>
26#include <string.h>
27
28#define PATH "stream_file_example.txt"
29
30static bj_bool write_text(const char* text, uint32_t flags) {
31 bj_error* error = 0;
32 bj_stream* file = bj_create_stream_from_path(BJ_STR(PATH), flags, &error);
33 if (file == 0) {
34 bj_err("Cannot open %s: %s", PATH, bj_error_message(error));
35 bj_clear_error(&error);
36 return BJ_FALSE;
37 }
38 const size_t length = strlen(text);
39 const bj_bool done = bj_write_stream(file, text, length) == length;
41 return done;
42}
43
44int main(int argc, char* argv[]) {
45 (void)argc; (void)argv;
46
47 // "w": create the file, or empty it. Then "a": every write goes to the
48 // end, wherever the cursor stands.
49 if (!write_text("Banjo streams read files\n", BJ_STREAM_WRITE | BJ_STREAM_CREATE | BJ_STREAM_TRUNCATE)
50 || !write_text("on demand, chunk by chunk.\n", BJ_STREAM_WRITE | BJ_STREAM_CREATE | BJ_STREAM_APPEND)) {
51 return 1;
52 }
53
54 // "r": a read-only stream, mapped where the platform allows it.
55 bj_error* error = 0;
57 if (file == 0) {
58 bj_err("Cannot read %s: %s", PATH, bj_error_message(error));
59 bj_clear_error(&error);
60 return 1;
61 }
62 bj_info("%s: %zu bytes, mapped: %s", PATH, bj_stream_length(file),
63 bj_stream_is_mapped(file) ? "yes" : "no");
64
65 // Read 16 bytes at a time: a short read means the status has the reason.
66 char chunk[17];
67 size_t got;
68 int index = 0;
69 while ((got = bj_read_stream(file, chunk, 16)) > 0) {
70 chunk[got] = '\0';
71 bj_info("chunk %d: \"%s\"", index++, chunk);
72 }
73 bj_info("Stopped with %s", bj_stream_status(file) == BJ_STREAM_STATUS_END ? "the end of the file" : "an error");
74
76 return remove(PATH) == 0 ? 0 : 1;
77}
struct bj_stream bj_stream
Definition api.h:374
struct bj_error bj_error
Definition api.h:348
uint32_t bj_bool
Boolean type used throughout the Banjo API.
Definition api.h:264
#define BJ_FALSE
Boolean false value (0).
Definition api.h:273
const char * bj_error_message(const struct bj_error *error)
Gets the error message from an error object.
void bj_clear_error(struct bj_error **error)
Frees an error and sets the pointer to NULL.
#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
size_t bj_stream_length(struct bj_stream *stream)
Return the total size of the stream, in bytes.
void bj_destroy_stream(struct bj_stream *stream)
Destroy a stream and release its memory.
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.
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_stream_is_mapped(const struct bj_stream *stream)
Whether the stream reads a file mapped into memory.
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_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_STREAM_STATUS_END
The data ran out, or a fixed-size stream is full.
Definition stream.h:152
#define BJ_STR(literal)
Create a view of a string literal.
Definition string.h:75
Logging utility functions.
Portable main() replacement with platform-aware entry shim.
Header file for struct bj_stream struct and related functions.
static bj_bool write_text(const char *text, uint32_t flags)
Definition stream_file.c:30
#define PATH
Definition stream_file.c:28
UTF-8 string views and owning strings.
int main(void)