diff options
| author | Stefan Boberg <[email protected]> | 2026-04-23 18:16:57 +0200 |
|---|---|---|
| committer | Stefan Boberg <[email protected]> | 2026-04-23 18:16:57 +0200 |
| commit | 0232b991cd7d8e3a2114ea30e4591dd3e7b65c36 (patch) | |
| tree | 94730e7594fd09ae1fa820391ce311f6daf13905 /thirdparty/tourist/foundation/src/stream.cpp | |
| parent | Fix forward declaration order for s_GotSigWinch and SigWinchHandler (diff) | |
| parent | trace: declare Region event name fields as AnsiString (#1012) (diff) | |
| download | archived-zen-sb/zen-help.tar.xz archived-zen-sb/zen-help.zip | |
Merge branch 'main' into sb/zen-helpsb/zen-help
- Combine HelpCommand (this branch) with HistoryCommand (main) in zen CLI dispatcher
- Keep filter-aware TuiPickOne rewrite; adopt main's ASCII arrow glyphs in doc comment
Diffstat (limited to 'thirdparty/tourist/foundation/src/stream.cpp')
| -rw-r--r-- | thirdparty/tourist/foundation/src/stream.cpp | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/thirdparty/tourist/foundation/src/stream.cpp b/thirdparty/tourist/foundation/src/stream.cpp new file mode 100644 index 000000000..c560436d3 --- /dev/null +++ b/thirdparty/tourist/foundation/src/stream.cpp @@ -0,0 +1,72 @@ +#include <foundation/buffer.h> + +#include "slab.h" + +//------------------------------------------------------------------------------ +BufferStream::BufferStream(Slab* slab, const uint8* ptr, uint32 size) +: _ptr(ptr) +, _slab_offset(uint32(uintptr(ptr) - uintptr(slab))) +, _end(size) +{ +} + +//------------------------------------------------------------------------------ +Slab* BufferStream::get_slab() const +{ + return (Slab*)(_ptr - _slab_offset); +} + +//------------------------------------------------------------------------------ +bool BufferStream::has_data() const +{ + return (_cursor < _end); +} + +//------------------------------------------------------------------------------ +uint32 BufferStream::get_consumed() const +{ + return _cursor; +} + +//------------------------------------------------------------------------------ +uint32 BufferStream::get_remaining() const +{ + return uint32(ptrdiff_t(_end - _cursor)); +} + +//------------------------------------------------------------------------------ +const uint8* BufferStream::read(uint32 size) +{ + _cursor += size; + return (uint8*)_ptr + _cursor - size; +} + +//------------------------------------------------------------------------------ +Buffer BufferStream::read_buf(uint32 size) +{ + const uint8* ptr = read(size); + + Buffer buffer; + buffer._slab = get_slab(); + buffer._offset = uint32(uintptr(ptr) - uintptr(buffer._slab)); + buffer._size = size; + return buffer; +} + +//------------------------------------------------------------------------------ +Pointer BufferStream::read_ptr(uint32 size) +{ + const uint8* ret = read(size); + Slab* slab = get_slab(); + return Pointer(slab, ret); +} + +//------------------------------------------------------------------------------ +template <> int8 BufferStream::read< int8>() { return *( int8 *)read(sizeof( int8)); } +template <> int16 BufferStream::read< int16>() { return *( int16*)read(sizeof( int16)); } +template <> int32 BufferStream::read< int32>() { return *( int32*)read(sizeof( int32)); } +template <> int64 BufferStream::read< int64>() { return *( int64*)read(sizeof( int64)); } +template <> uint8 BufferStream::read<uint8>() { return *(uint8 *)read(sizeof(uint8)); } +template <> uint16 BufferStream::read<uint16>() { return *(uint16*)read(sizeof(uint16)); } +template <> uint32 BufferStream::read<uint32>() { return *(uint32*)read(sizeof(uint32)); } +template <> uint64 BufferStream::read<uint64>() { return *(uint64*)read(sizeof(uint64)); } |