44 lines
1.2 KiB
C
44 lines
1.2 KiB
C
// vim:fileencoding=utf-8:foldmethod=marker
|
|
|
|
#include "stream.h"
|
|
#include "../../common/aliases/aliases.h"
|
|
#include "../../common/assert/assert.h"
|
|
|
|
void _streamValidate(const WpStream *stream, u64 item_size);
|
|
|
|
b8 wpStreamAtEnd(const WpStream *stream) {
|
|
return stream->position >= stream->count;
|
|
}
|
|
|
|
b8 _streamHasCount(const WpStream *stream, u64 count) {
|
|
return stream->position + count <= stream->count;
|
|
}
|
|
|
|
void *_streamPeekWithOffset(const WpStream *stream, u64 offset, u64 item_size) {
|
|
_streamValidate(stream, item_size);
|
|
|
|
if (_streamHasCount(stream, offset + 1)) {
|
|
u64 index = (stream->position + offset) * stream->item_size;
|
|
return (void *)(&((u8 *)(stream->data))[index]);
|
|
}
|
|
|
|
return NULL;
|
|
}
|
|
|
|
void *_streamConsumeCount(WpStream *stream, u64 count, u64 item_size) {
|
|
_streamValidate(stream, item_size);
|
|
|
|
if (_streamHasCount(stream, count)) {
|
|
u64 index = stream->position * stream->item_size;
|
|
stream->position += count;
|
|
return (void *)(&((u8 *)(stream->data))[index]);
|
|
}
|
|
|
|
return NULL;
|
|
}
|
|
|
|
void _streamValidate(const WpStream *stream, u64 item_size) {
|
|
wpDebugAssert(stream != NULL, "`stream` should not be NULL");
|
|
wpRuntimeAssert(stream->item_size == item_size, "Invalid item type provided");
|
|
}
|