File streams: write a file, append to it, then read it by chunks without loading it.
File streams: write a file, append to it, then read it by chunks without loading it.A file stream reads and writes on demand: it never loads the whole file, so a 10 GB file costs what you read of it. Flags say what the stream is opened for, as the modes of fopen do, and a file opened only for reading is mapped into memory, so the system pages it in as you go.
#include <stdio.h>
#define PATH "stream_file_example.txt"
if (file == 0) {
}
const size_t length = strlen(text);
return done;
}
int main(
int argc,
char* argv[]) {
(void)argc; (void)argv;
return 1;
}
if (file == 0) {
return 1;
}
char chunk[17];
size_t got;
int index = 0;
chunk[got] = '\0';
bj_info(
"chunk %d: \"%s\"", index++, chunk);
}
return remove(
PATH) == 0 ? 0 : 1;
}
struct bj_stream bj_stream
uint32_t bj_bool
Boolean type used throughout the Banjo API.
#define BJ_FALSE
Boolean false value (0).
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.
#define bj_err(...)
Log a message using the BJ_LOG_ERROR level.
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.
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.
@ BJ_STREAM_WRITE
The stream writes.
@ BJ_STREAM_READ
The stream reads.
@ BJ_STREAM_TRUNCATE
The file is emptied when opened.
@ BJ_STREAM_APPEND
Every write goes to the end of the file.
@ BJ_STREAM_STATUS_END
The data ran out, or a fixed-size stream is full.
#define BJ_STR(literal)
Create a view of a string literal.
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)
UTF-8 string views and owning strings.