diff --git a/lib/socket.cpp b/lib/socket.cpp index 1c5a02f8..34647692 100644 --- a/lib/socket.cpp +++ b/lib/socket.cpp @@ -32,6 +32,18 @@ unsigned int Socket::Buffer::size(){ return data.size(); } +/// Returns either the amount of total bytes available in the buffer or max, whichever is smaller. +unsigned int Socket::Buffer::bytes(unsigned int max){ + unsigned int i = 0; + for (std::deque::iterator it = data.begin(); it != data.end(); ++it){ + i += ( *it).size(); + if (i >= max){ + return max; + } + } + return i; +} + /// Appends this string to the internal std::deque of std::string objects. /// It is automatically split every BUFFER_BLOCKSIZE bytes. void Socket::Buffer::append(const std::string & newdata){ @@ -62,6 +74,18 @@ void Socket::Buffer::append(const char * newdata, const unsigned int newdatasize } } +/// Prepends this data block to the internal std::deque of std::string objects. +/// It is _not_ automatically split every BUFFER_BLOCKSIZE bytes. +void Socket::Buffer::prepend(const std::string & newdata){ + data.push_back(newdata); +} + +/// Prepends this data block to the internal std::deque of std::string objects. +/// It is _not_ automatically split every BUFFER_BLOCKSIZE bytes. +void Socket::Buffer::prepend(const char * newdata, const unsigned int newdatasize){ + data.push_back(std::string(newdata, (size_t)newdatasize)); +} + /// Returns true if at least count bytes are available in this buffer. bool Socket::Buffer::available(unsigned int count){ unsigned int i = 0; diff --git a/lib/socket.h b/lib/socket.h index 73db0b0b..03aec9c2 100644 --- a/lib/socket.h +++ b/lib/socket.h @@ -30,8 +30,11 @@ namespace Socket { std::deque data; public: unsigned int size(); + unsigned int bytes(unsigned int max); void append(const std::string & newdata); void append(const char * newdata, const unsigned int newdatasize); + void prepend(const std::string & newdata); + void prepend(const char * newdata, const unsigned int newdatasize); std::string & get(); bool available(unsigned int count); std::string remove(unsigned int count);