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,28 +23,35 @@
#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;
unsigned int mSize; unsigned int mSize;
unsigned char *mHead; unsigned char *mHead;
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;
@ -62,7 +69,7 @@ public:
} }
return mSize; return mSize;
} }
//unsigned int Allocation() const { return( mAllocation ); } // unsigned int Allocation() const { return( mAllocation ); }
void clear() { void clear() {
mSize = 0; mSize = 0;
@ -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;
@ -87,23 +95,25 @@ 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;
if ( mTail > (mHead + mSize) ) if (mTail > (mHead + mSize))
mTail = mHead + mSize; mTail = mHead + mSize;
tidy(0); tidy(0);
return count; return count;
} }
// Add to the end of the buffer // Add to the end of the buffer
unsigned int expand( unsigned int count ); unsigned int expand(unsigned int count);
// 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;
@ -126,13 +136,13 @@ public:
unsigned int append(const Buffer &buffer) { unsigned int append(const Buffer &buffer) {
return append(buffer.mHead, buffer.mSize); return append(buffer.mHead, buffer.mSize);
} }
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;
mTail = mHead + mSize; mTail = mHead + mSize;
} }
@ -140,38 +150,38 @@ 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);
}; };
#endif // ZM_BUFFER_H #endif // ZM_BUFFER_H