From bebc878b054a06ff8e541db695b1e586fff2b022 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Tue, 27 Mar 2007 21:05:10 +0000 Subject: Added a new helper to the flexbuf, and likewise to the connection class, since it uses it heavily. --- src/connection.cpp | 5 +++++ src/connection.h | 2 ++ src/flexbuf.cpp | 23 +++++++++++++++++++++++ src/flexbuf.h | 2 ++ 4 files changed, 32 insertions(+) diff --git a/src/connection.cpp b/src/connection.cpp index a5fac5b..f042705 100644 --- a/src/connection.cpp +++ b/src/connection.cpp @@ -29,6 +29,11 @@ Connection::~Connection() if( pProtocol != NULL ) delete pProtocol; } +void Connection::ensureCapacity( int nSize ) +{ + xOutputBuf.ensureCapacity( nSize ); +} + bool Connection::appendOutput( const char *lpOutput, int nSize ) { return xOutputBuf.appendData( lpOutput, nSize ); diff --git a/src/connection.h b/src/connection.h index 5d2d9bd..7e1141d 100644 --- a/src/connection.h +++ b/src/connection.h @@ -51,6 +51,8 @@ public: */ bool open( const char *sAddr, int nPort ); + void ensureCapacity( int nSize ); + /** Append the given data to the output. The data is presumed to be null * terminated. To put binary data into the stream, use the other * appendOutput function. This should be the only method used to diff --git a/src/flexbuf.cpp b/src/flexbuf.cpp index acd55a7..6d55294 100644 --- a/src/flexbuf.cpp +++ b/src/flexbuf.cpp @@ -204,3 +204,26 @@ int FlexBuf::findChar( char cTarget ) return -1; } +void FlexBuf::ensureCapacity( int nAmount ) +{ + if( nLastChar + nAmount + 1 > nSize ) + { + if( nFill + nAmount + 1 < nSize ) + { + memcpy( lpBuf, lpBuf+nFirstChar, nFill ); + nLastChar -= nFirstChar; + nFirstChar = 0; + } + else + { + nSize += nAmount+1; + char *lpNewBuf = new char[nSize]; + memcpy( lpNewBuf, lpBuf+nFirstChar, nFill ); + delete[] lpBuf; + lpBuf = lpNewBuf; + nLastChar -= nFirstChar; + nFirstChar = 0; + } + } +} + diff --git a/src/flexbuf.h b/src/flexbuf.h index a68dcc6..7d7f11a 100644 --- a/src/flexbuf.h +++ b/src/flexbuf.h @@ -144,6 +144,8 @@ public: */ int findChar( char cTarget ); + void ensureCapacity( int nAmount ); + private: /** The raw storage location of the FlexBuf. */ char *lpBuf; -- cgit v1.2.3