add a byte stream: pxl8_stream
This commit is contained in:
parent
ede16ca7de
commit
82ed6b4ea9
7 changed files with 350 additions and 273 deletions
|
|
@ -5,6 +5,80 @@
|
|||
|
||||
#include "pxl8_types.h"
|
||||
|
||||
typedef struct {
|
||||
const u8* bytes;
|
||||
u32 offset;
|
||||
u32 size;
|
||||
} pxl8_stream;
|
||||
|
||||
static inline pxl8_stream pxl8_stream_create(const u8* bytes, u32 size) {
|
||||
return (pxl8_stream){
|
||||
.bytes = bytes,
|
||||
.offset = 0,
|
||||
.size = size
|
||||
};
|
||||
}
|
||||
|
||||
static inline bool pxl8_stream_can_read(const pxl8_stream* stream, u32 count) {
|
||||
return stream->offset + count <= stream->size;
|
||||
}
|
||||
|
||||
static inline void pxl8_stream_seek(pxl8_stream* stream, u32 offset) {
|
||||
stream->offset = offset;
|
||||
}
|
||||
|
||||
static inline u32 pxl8_stream_position(const pxl8_stream* stream) {
|
||||
return stream->offset;
|
||||
}
|
||||
|
||||
static inline u8 pxl8_read_u8(pxl8_stream* stream) {
|
||||
return stream->bytes[stream->offset++];
|
||||
}
|
||||
|
||||
static inline u16 pxl8_read_u16(pxl8_stream* stream) {
|
||||
u16 val = (u16)stream->bytes[stream->offset] | ((u16)stream->bytes[stream->offset + 1] << 8);
|
||||
stream->offset += 2;
|
||||
return val;
|
||||
}
|
||||
|
||||
static inline u32 pxl8_read_u32(pxl8_stream* stream) {
|
||||
u32 val = (u32)stream->bytes[stream->offset] |
|
||||
((u32)stream->bytes[stream->offset + 1] << 8) |
|
||||
((u32)stream->bytes[stream->offset + 2] << 16) |
|
||||
((u32)stream->bytes[stream->offset + 3] << 24);
|
||||
stream->offset += 4;
|
||||
return val;
|
||||
}
|
||||
|
||||
static inline i16 pxl8_read_i16(pxl8_stream* stream) {
|
||||
return (i16)pxl8_read_u16(stream);
|
||||
}
|
||||
|
||||
static inline i32 pxl8_read_i32(pxl8_stream* stream) {
|
||||
return (i32)pxl8_read_u32(stream);
|
||||
}
|
||||
|
||||
static inline f32 pxl8_read_f32(pxl8_stream* stream) {
|
||||
u32 val = pxl8_read_u32(stream);
|
||||
return *(f32*)&val;
|
||||
}
|
||||
|
||||
static inline void pxl8_read_bytes(pxl8_stream* stream, void* dest, u32 count) {
|
||||
for (u32 i = 0; i < count; i++) {
|
||||
((u8*)dest)[i] = stream->bytes[stream->offset++];
|
||||
}
|
||||
}
|
||||
|
||||
static inline void pxl8_skip_bytes(pxl8_stream* stream, u32 count) {
|
||||
stream->offset += count;
|
||||
}
|
||||
|
||||
static inline const u8* pxl8_read_ptr(pxl8_stream* stream, u32 count) {
|
||||
const u8* ptr = &stream->bytes[stream->offset];
|
||||
stream->offset += count;
|
||||
return ptr;
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue