code style updates

This commit is contained in:
Isaac Connor 2021-03-01 13:48:24 -05:00
parent 2a4723090b
commit 3ad76f18ec
1 changed files with 47 additions and 37 deletions

View File

@ -23,8 +23,7 @@
#include "zm_logger.h" #include "zm_logger.h"
#include <cstring> #include <cstring>
class Buffer class Buffer {
{
protected: protected:
unsigned char *mStorage; unsigned char *mStorage;
unsigned int mAllocation; unsigned int mAllocation;
@ -33,18 +32,26 @@ protected:
unsigned char *mTail; unsigned char *mTail;
public: public:
Buffer() : mStorage(nullptr), mAllocation(0), mSize(0), mHead(nullptr), mTail(nullptr) { Buffer() :
mStorage(nullptr),
mAllocation(0),
mSize(0),
mHead(nullptr),
mTail(nullptr) {
} }
explicit Buffer(unsigned int pSize) : mAllocation(pSize), mSize(0) { explicit Buffer(unsigned int pSize) : mAllocation(pSize), mSize(0) {
mHead = mStorage = new unsigned char[mAllocation]; mHead = mStorage = new unsigned char[mAllocation];
mTail = mHead; mTail = mHead;
} }
Buffer(const unsigned char *pStorage, unsigned int pSize) : mAllocation(pSize), mSize(pSize) { Buffer(const unsigned char *pStorage, unsigned int pSize) :
mAllocation(pSize), mSize(pSize) {
mHead = mStorage = new unsigned char[mSize]; mHead = mStorage = new unsigned char[mSize];
std::memcpy(mStorage, pStorage, mSize); std::memcpy(mStorage, pStorage, mSize);
mTail = mHead + mSize; mTail = mHead + mSize;
} }
Buffer(const Buffer &buffer) : mAllocation(buffer.mSize), mSize(buffer.mSize) { Buffer(const Buffer &buffer) :
mAllocation(buffer.mSize),
mSize(buffer.mSize) {
mHead = mStorage = new unsigned char[mSize]; mHead = mStorage = new unsigned char[mSize];
std::memcpy(mStorage, buffer.mHead, mSize); std::memcpy(mStorage, buffer.mHead, mSize);
mTail = mHead + mSize; mTail = mHead + mSize;
@ -77,7 +84,8 @@ public:
// Trim from the front of the buffer // Trim from the front of the buffer
unsigned int consume(unsigned int count) { unsigned int consume(unsigned int count) {
if ( count > mSize ) { if ( count > mSize ) {
Warning("Attempt to consume %d bytes of buffer, size is only %d bytes", count, mSize); Warning("Attempt to consume %d bytes of buffer, size is only %d bytes",
count, mSize);
count = mSize; count = mSize;
} }
mHead += count; mHead += count;
@ -88,7 +96,8 @@ public:
// Trim from the end of the buffer // Trim from the end of the buffer
unsigned int shrink(unsigned int count) { unsigned int shrink(unsigned int count) {
if (count > mSize) { if (count > mSize) {
Warning("Attempt to shrink buffer by %d bytes, size is only %d bytes", count, mSize); Warning("Attempt to shrink buffer by %d bytes, size is only %d bytes",
count, mSize);
count = mSize; count = mSize;
} }
mSize -= count; mSize -= count;
@ -103,7 +112,8 @@ public:
// Return pointer to the first pSize bytes and advance the head // Return pointer to the first pSize bytes and advance the head
unsigned char *extract(unsigned int pSize) { unsigned char *extract(unsigned int pSize) {
if (pSize > mSize) { if (pSize > mSize) {
Warning("Attempt to extract %d bytes of buffer, size is only %d bytes", pSize, mSize); Warning("Attempt to extract %d bytes of buffer, size is only %d bytes",
pSize, mSize);
pSize = mSize; pSize = mSize;
} }
unsigned char *oldHead = mHead; unsigned char *oldHead = mHead;
@ -128,9 +138,9 @@ public:
} }
void tidy(bool level=0) { void tidy(bool level=0) {
if (mHead != mStorage) { if (mHead != mStorage) {
if ( mSize == 0 ) if (mSize == 0) {
mHead = mTail = mStorage; mHead = mTail = mStorage;
else if ( level ) { } else if (level) {
if (((uintptr_t)mHead-(uintptr_t)mStorage) > mSize) { if (((uintptr_t)mHead-(uintptr_t)mStorage) > mSize) {
std::memcpy(mStorage, mHead, mSize); std::memcpy(mStorage, mHead, mSize);
mHead = mStorage; mHead = mStorage;
@ -142,34 +152,34 @@ public:
Buffer &operator=(const Buffer &buffer) { Buffer &operator=(const Buffer &buffer) {
assign(buffer); assign(buffer);
return( *this ); return *this;
} }
Buffer &operator+=(const Buffer &buffer) { Buffer &operator+=(const Buffer &buffer) {
append(buffer); append(buffer);
return( *this ); return *this;
} }
Buffer &operator+=(unsigned int count) { Buffer &operator+=(unsigned int count) {
expand(count); expand(count);
return( *this ); return *this;
} }
Buffer &operator-=(unsigned int count) { Buffer &operator-=(unsigned int count) {
consume(count); consume(count);
return *this; return *this;
} }
operator unsigned char *() const { operator unsigned char *() const {
return( mHead ); return mHead;
} }
operator char *() const { operator char *() const {
return( (char *)mHead ); return reinterpret_cast<char *>(mHead);
} }
unsigned char *operator+(int offset) const { unsigned char *operator+(int offset) const {
return( (unsigned char *)(mHead+offset) ); return (unsigned char *)(mHead+offset);
} }
unsigned char operator[](int index) const { unsigned char operator[](int index) const {
return( *(mHead+index) ); return *(mHead+index);
} }
operator int () const { operator int () const {
return( (int)mSize ); return static_cast<int>(mSize);
} }
int read_into(int sd, unsigned int bytes); int read_into(int sd, unsigned int bytes);
}; };