Labrador/Desktop_Interface/isobufferbuffer.cpp

50 lines
1.0 KiB
C++
Raw Normal View History

#include "isobufferbuffer.h"
2018-11-05 23:15:48 +00:00
isoBufferBuffer::isoBufferBuffer(uint32_t length)
: bufferLength(length)
{
2018-11-05 23:15:48 +00:00
mid = bufferLength/2;
buffer = (char *) malloc((bufferLength * 3) / 2);
}
2018-11-05 23:15:48 +00:00
void isoBufferBuffer::add(std::string newString)
{
for (char& newChar : newString)
add(newChar);
}
void isoBufferBuffer::add(char newChar){
buffer[ptr] = newChar;
2018-11-05 23:15:48 +00:00
if(ptr < mid)
buffer[ptr + bufferLength] = newChar;
if (ptr >= bufferLength)
ptr = 0;
else ptr++;
2018-11-05 23:15:48 +00:00
numCharsInBuffer = std::min(numCharsInBuffer + 1, mid);
}
2018-11-05 23:55:45 +00:00
void isoBufferBuffer::add(uint8_t newByte)
{
char newString[5];
sprintf(newString, "0x%02hhx", newByte);
add(newString);
}
2018-11-05 23:15:48 +00:00
uint32_t isoBufferBuffer::getNumCharsInBuffer()
{
return numCharsInBuffer;
}
2018-11-05 23:15:48 +00:00
char *isoBufferBuffer::get(uint32_t length){
if (length > mid)
qFatal("isoBuffer::get; length requested is too high.");
if(ptr < mid)
return &buffer[ptr + bufferLength - length];
else
return &buffer[ptr - length];
}