diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2015-02-10 21:29:19 +0100 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2015-02-10 21:29:19 +0100 |
commit | b55df0b8e46f46fea4d69f98a729cd237d77a6ed (patch) | |
tree | 25e12c22d844bb449dd08d7661d20d42ca8ab0f5 /ByteBuffer.cpp | |
parent | 1c97f6df59c825b26ecb18975fd9f62e14fc46ce (diff) | |
download | ble-toys-b55df0b8e46f46fea4d69f98a729cd237d77a6ed.tar.gz ble-toys-b55df0b8e46f46fea4d69f98a729cd237d77a6ed.tar.bz2 ble-toys-b55df0b8e46f46fea4d69f98a729cd237d77a6ed.tar.xz ble-toys-b55df0b8e46f46fea4d69f98a729cd237d77a6ed.zip |
o wip.
Diffstat (limited to 'ByteBuffer.cpp')
-rw-r--r-- | ByteBuffer.cpp | 54 |
1 files changed, 39 insertions, 15 deletions
diff --git a/ByteBuffer.cpp b/ByteBuffer.cpp index 9714c8c..70b9a8e 100644 --- a/ByteBuffer.cpp +++ b/ByteBuffer.cpp @@ -1,38 +1,62 @@ #include "ByteBuffer.h" -#include <stdexcept> +#include <stdint-gcc.h> +#include <string.h> using namespace std; -ByteBuffer::ByteBuffer(uint8_t *bytes, ssize_t size, ssize_t zero) : - bytes(bytes), size(size), ptr(zero), zero(zero) { +ByteBuffer::ByteBuffer(uint8_t *bytes, size_t capacity, size_t size, size_t zero) : + bytes(bytes), capacity(capacity), size(size), cursor(zero), zero(zero) { } ByteBuffer &ByteBuffer::add8(uint8_t value) { - canAccessNextBytes(1); - bytes[zero + ptr++] = value; + checkAndUpdateSize(1); + bytes[zero + cursor++] = value; return *this; } ByteBuffer &ByteBuffer::add16le(uint16_t value) { - canAccessNextBytes(2); - bytes[zero + ptr++] = (uint8_t) (value & 0xff); - bytes[zero + ptr++] = (uint8_t) ((value >> 8) & 0xff); + checkAndUpdateSize(2); + bytes[zero + cursor++] = (uint8_t) (value & 0xff); + bytes[zero + cursor++] = (uint8_t) ((value >> 8) & 0xff); return *this; } -uint8_t ByteBuffer::get8(ssize_t index) { +uint8_t ByteBuffer::get8(size_t index) { canAccessIndex(index); - return bytes[zero + ptr]; + return bytes[zero + cursor]; } -void ByteBuffer::canAccessNextBytes(ssize_t newBytes) { - if (zero + ptr + newBytes >= size) { - throw exception(); +uint8_t ByteBuffer::get8() { + canAccessIndex(cursor); + return bytes[zero + cursor++]; +} + +uint16_t ByteBuffer::get16le() { + canAccessIndex(cursor + 1); + uint16_t value; + value = bytes[zero + cursor++]; + value = ((uint16_t) bytes[zero + cursor++]) << 8; + return value; +} + +void ByteBuffer::copy(uint8_t *bytes, size_t length) { + canAccessIndex(cursor + length); + + memcpy(bytes, &this->bytes[zero + cursor], length); + cursor += length; +} + +void ByteBuffer::checkAndUpdateSize(size_t newBytes) { + size_t newSize = zero + cursor + newBytes; + if (newSize >= capacity) { + throw ByteBufferException(string("Out of bounds! zero=") + to_string(zero) + ", cursor=" + to_string(cursor) + ", size=" + to_string(size) + ", capacity=" + to_string(capacity) + ", newSize=" + to_string(newSize)); } + + size = max(newSize, size); } -void ByteBuffer::canAccessIndex(ssize_t index) { +void ByteBuffer::canAccessIndex(size_t index) { if (zero + index >= size) { - throw exception(); + throw ByteBufferException(string("Out of bounds! zero=") + to_string(zero) + ", index=" + to_string(index) + ", size=" + to_string(size)); } } |