From 3f1c8998166466245aee2860197fb4908e55f1a2 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Wed, 8 Oct 2008 16:17:26 +0000 Subject: Ok...corrected a problem with new block allocation in nids, and it no longer goes into an infinite loop while doing certain kinds of read. Also, it zeros out new blocks to make things easier to cope with in the hex editor, it'll probably also compress better. I also fixed Bu::MemBuf so that you can now write to arbitrary places mid-stream. --- src/membuf.cpp | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) (limited to 'src/membuf.cpp') diff --git a/src/membuf.cpp b/src/membuf.cpp index 6d850bf..e7c7ac8 100644 --- a/src/membuf.cpp +++ b/src/membuf.cpp @@ -41,9 +41,29 @@ size_t Bu::MemBuf::read( void *pBuf, size_t nBytes ) size_t Bu::MemBuf::write( const void *pBuf, size_t nBytes ) { - sBuf.append( (const char *)pBuf, nBytes ); - nPos += nBytes; - return nBytes; + if( nPos == sBuf.getSize() ) + { + // Easiest, just append the data. + sBuf.append( (const char *)pBuf, nBytes ); + nPos += nBytes; + return nBytes; + } + else + { + // Trickier, we must do this in two parts, overwrite, then append + // Frist, overwrite. + int iOver = sBuf.getSize() - nPos; + if( iOver > nBytes ) + iOver = nBytes; + memcpy( sBuf.getStr()+nPos, pBuf, iOver ); + // Then append + if( iOver < nBytes ) + { + sBuf.append( ((const char *)pBuf)+iOver, nBytes-iOver ); + } + nPos += nBytes; + return nBytes; + } } long Bu::MemBuf::tell() -- cgit v1.2.3