From bfc1e2d595452c068a2b66be1436aa34f1083f3d Mon Sep 17 00:00:00 2001
From: eichlan <eichlan@raspberry>
Date: Mon, 24 Jun 2019 16:10:03 -0700
Subject: Bu::BlobBuilder compiles & is closer to working...

---
 src/unstable/blobbuilder.cpp | 76 +++++++++++++++++++++++++++++++++++++++++---
 src/unstable/blobbuilder.h   |  7 ++--
 2 files changed, 76 insertions(+), 7 deletions(-)

diff --git a/src/unstable/blobbuilder.cpp b/src/unstable/blobbuilder.cpp
index a62b98c..7ea159d 100644
--- a/src/unstable/blobbuilder.cpp
+++ b/src/unstable/blobbuilder.cpp
@@ -8,22 +8,32 @@
 #include "bu/blobbuilder.h"
 #include "bu/blob.h"
 
+#define PAGE_SIZE 1024
+
 /////
 // BlobBuilderCore::Chunk
 //
 
-Bu::BlobBuilderCore::Chunk::Chunk() :
-    iLength( 0 ),
+Bu::BlobBuilderCore::Chunk::Chunk( const char *pSrc, int32_t iLength ) :
+    iLength( iLength ),
     pData( 0 ),
     pNext( 0 )
 {
+    if( iLength < PAGE_SIZE )
+    {
+        pData = new char[PAGE_SIZE];
+    }
+    else
+    {
+        pData = new char[iLength];
+    }
+    memcpy( pData, pSrc, iLength );
 }
 
 Bu::BlobBuilderCore::Chunk::~Chunk()
 {
     delete[] pData;
     pData = 0;
-    delete pNext;
     pNext = 0;
 
     // Why not delete pNext here? A BlobBuilder could easily wind up having
@@ -32,6 +42,24 @@ Bu::BlobBuilderCore::Chunk::~Chunk()
     // in the core down below.
 }
 
+void Bu::BlobBuilderCore::Chunk::append( const char *&pSrc, int32_t &iLength )
+{
+    if( this->iLength >= PAGE_SIZE )
+    {
+        // This chunk is full, just return.
+        return;
+    }
+    int32_t iCopy = PAGE_SIZE-this->iLength;
+    if( iCopy > iLength )
+    {
+        iCopy = iLength;
+    }
+    memcpy( pData+this->iLength, pSrc, iCopy );
+    this->iLength += iCopy;
+    pSrc += iCopy;
+    iLength -= iCopy;
+}
+
 /////
 // BlobBuilderCore
 //
@@ -64,15 +92,37 @@ void Bu::BlobBuilderCore::clear()
         Chunk *pNext = pCur->pNext;
         delete pCur;
         pCur = pNext;
-
     }
     delete pFirst;
     pFirst = pLast = 0;
     iLength = 0;
 }
 
-void Bu::BlobBuilderCore::append( const *pSrc, int32_t iLength )
+void Bu::BlobBuilderCore::append( const char *pSrc, int32_t iLength )
+{
+    if( pFirst == 0 )
+    {
+        // Nothing in the list, just add a chunk.
+        pFirst = pLast = new Chunk( pSrc, iLength );
+        return;
+    }
+    else if( pLast->iLength < 1024 )
+    {
+        // Append to the last chunk first, this will modify pSrc & iLength.
+        pLast->append( pSrc, iLength );
+    }
+
+    // If there's unused data at the end, append it now.
+    if( iLength > 0 )
+    {
+        pLast->pNext = new Chunk( pSrc, iLength );
+    }
+}
+
+void Bu::BlobBuilderCore::set( const char *pSrc, int32_t iLength )
 {
+    clear();
+    append( pSrc, iLength );
 }
 
 //////
@@ -94,46 +144,62 @@ Bu::BlobBuilder::~BlobBuilder()
 
 void Bu::BlobBuilder::set( const Blob &rSrc )
 {
+    _hardCopy();
+    core->set( rSrc.getData(), rSrc.getSize() );
 }
 
 void Bu::BlobBuilder::set( const char *pSrc, int32_t iLength )
 {
+    _hardCopy();
+    core->set( pSrc, iLength );
 }
 
 void Bu::BlobBuilder::append( const Blob &rSrc )
 {
+    _hardCopy();
+    core->append( rSrc.getData(), rSrc.getSize() );
 }
 
 void Bu::BlobBuilder::append( const char *pSrc, int32_t iLength )
 {
+    _hardCopy();
+    core->append( pSrc, iLength );
 }
 
 void Bu::BlobBuilder::prepend( const Blob &rSrc )
 {
+    _hardCopy();
 }
 
 void Bu::BlobBuilder::prepend( const char *pSrc, int32_t iLength )
 {
+    _hardCopy();
 }
 
 void Bu::BlobBuilder::insert( int32_t iBefore, const Blob &rSrc )
 {
+    _hardCopy();
 }
 
 void Bu::BlobBuilder::insert( int32_t iBefore, const char *pSrc, const Bu::Blob &rSrc )
 {
+    _hardCopy();
 }
 
 void Bu::BlobBuilder::clear()
 {
+    _hardCopy();
+    core->clear();
 }
 
 int32_t Bu::BlobBuilder::getSize() const
 {
+    return core->iLength;
 }
 
 Bu::Blob Bu::BlobBuilder::getBlob() const
 {
+    Blob bRet();
 }
 
 Bu::BlobBuilder &Bu::BlobBuilder::operator=( const Blob &rSrc )
diff --git a/src/unstable/blobbuilder.h b/src/unstable/blobbuilder.h
index c1dbed1..9b5e390 100644
--- a/src/unstable/blobbuilder.h
+++ b/src/unstable/blobbuilder.h
@@ -24,9 +24,11 @@ namespace Bu
         class Chunk
         {
         public:
-            Chunk();
+            Chunk( const char *pSrc, int32_t iLength );
             ~Chunk();
 
+            void append( const char *&pSrc, int32_t &iLength );
+
             int32_t iLength;
             char *pData;
             Chunk *pNext;
@@ -37,7 +39,8 @@ namespace Bu
         virtual ~BlobBuilderCore();
 
         void clear();
-        void append( const *pSrc, int32_t iLength );
+        void append( const char *pSrc, int32_t iLength );
+        void set( const char *pSrc, int32_t iLength );
 
         Chunk *pFirst;
         Chunk *pLast;
-- 
cgit v1.2.3