1
1
Fork 0
mirror of https://github.com/QB64-Phoenix-Edition/QB64pe.git synced 2024-07-09 17:45:13 +00:00
QB64-PE/internal/c/libqb/include/buffer.h
Matthew Kilgore 4052b8cc19 Add append-only buffer API
The Buffer API implements an append-only buffer, where you can write to
the end or read from the beginning. Data that is read is discarded from
the buffer, and you can query the buffer to get the current amount of
data inside.

Internally the buffer API is implemented as a chain of separate buffers.
This is done to avoid having to resize the existing buffer, which is
expensive. We keep track of where the reading currently is, and discard
the internal buffers after all the data in them is read.
2022-06-11 22:47:06 -04:00

35 lines
865 B
C

#ifndef INCLUDE_LIBQB_BUFFER_H
#define INCLUDE_LIBQB_BUFFER_H
struct libqb_buffer_entry {
size_t length;
char *data;
struct libqb_buffer_entry *next;
};
struct libqb_buffer {
size_t total_length;
size_t cur_entry_offset;
struct libqb_buffer_entry *head;
struct libqb_buffer_entry **tail;
};
void libqb_buffer_init(struct libqb_buffer *);
// Free's all data used by the buffer.
void libqb_buffer_clear(struct libqb_buffer *);
// Returns the current length of the buffer.
size_t libqb_buffer_length(struct libqb_buffer *);
// Reads data from the buffer. The data read is removed from the buffer
//
// Returns the number of bytes actually read
size_t libqb_buffer_read(struct libqb_buffer *, char *, size_t length);
// Writes data into the buffer
void libqb_buffer_write(struct libqb_buffer *, const char *, size_t length);
#endif