aboutsummaryrefslogtreecommitdiff
path: root/src/unstable/blobbuilder.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/unstable/blobbuilder.cpp')
-rw-r--r--src/unstable/blobbuilder.cpp76
1 files changed, 71 insertions, 5 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 @@
8#include "bu/blobbuilder.h" 8#include "bu/blobbuilder.h"
9#include "bu/blob.h" 9#include "bu/blob.h"
10 10
11#define PAGE_SIZE 1024
12
11///// 13/////
12// BlobBuilderCore::Chunk 14// BlobBuilderCore::Chunk
13// 15//
14 16
15Bu::BlobBuilderCore::Chunk::Chunk() : 17Bu::BlobBuilderCore::Chunk::Chunk( const char *pSrc, int32_t iLength ) :
16 iLength( 0 ), 18 iLength( iLength ),
17 pData( 0 ), 19 pData( 0 ),
18 pNext( 0 ) 20 pNext( 0 )
19{ 21{
22 if( iLength < PAGE_SIZE )
23 {
24 pData = new char[PAGE_SIZE];
25 }
26 else
27 {
28 pData = new char[iLength];
29 }
30 memcpy( pData, pSrc, iLength );
20} 31}
21 32
22Bu::BlobBuilderCore::Chunk::~Chunk() 33Bu::BlobBuilderCore::Chunk::~Chunk()
23{ 34{
24 delete[] pData; 35 delete[] pData;
25 pData = 0; 36 pData = 0;
26 delete pNext;
27 pNext = 0; 37 pNext = 0;
28 38
29 // Why not delete pNext here? A BlobBuilder could easily wind up having 39 // Why not delete pNext here? A BlobBuilder could easily wind up having
@@ -32,6 +42,24 @@ Bu::BlobBuilderCore::Chunk::~Chunk()
32 // in the core down below. 42 // in the core down below.
33} 43}
34 44
45void Bu::BlobBuilderCore::Chunk::append( const char *&pSrc, int32_t &iLength )
46{
47 if( this->iLength >= PAGE_SIZE )
48 {
49 // This chunk is full, just return.
50 return;
51 }
52 int32_t iCopy = PAGE_SIZE-this->iLength;
53 if( iCopy > iLength )
54 {
55 iCopy = iLength;
56 }
57 memcpy( pData+this->iLength, pSrc, iCopy );
58 this->iLength += iCopy;
59 pSrc += iCopy;
60 iLength -= iCopy;
61}
62
35///// 63/////
36// BlobBuilderCore 64// BlobBuilderCore
37// 65//
@@ -64,15 +92,37 @@ void Bu::BlobBuilderCore::clear()
64 Chunk *pNext = pCur->pNext; 92 Chunk *pNext = pCur->pNext;
65 delete pCur; 93 delete pCur;
66 pCur = pNext; 94 pCur = pNext;
67
68 } 95 }
69 delete pFirst; 96 delete pFirst;
70 pFirst = pLast = 0; 97 pFirst = pLast = 0;
71 iLength = 0; 98 iLength = 0;
72} 99}
73 100
74void Bu::BlobBuilderCore::append( const *pSrc, int32_t iLength ) 101void Bu::BlobBuilderCore::append( const char *pSrc, int32_t iLength )
102{
103 if( pFirst == 0 )
104 {
105 // Nothing in the list, just add a chunk.
106 pFirst = pLast = new Chunk( pSrc, iLength );
107 return;
108 }
109 else if( pLast->iLength < 1024 )
110 {
111 // Append to the last chunk first, this will modify pSrc & iLength.
112 pLast->append( pSrc, iLength );
113 }
114
115 // If there's unused data at the end, append it now.
116 if( iLength > 0 )
117 {
118 pLast->pNext = new Chunk( pSrc, iLength );
119 }
120}
121
122void Bu::BlobBuilderCore::set( const char *pSrc, int32_t iLength )
75{ 123{
124 clear();
125 append( pSrc, iLength );
76} 126}
77 127
78////// 128//////
@@ -94,46 +144,62 @@ Bu::BlobBuilder::~BlobBuilder()
94 144
95void Bu::BlobBuilder::set( const Blob &rSrc ) 145void Bu::BlobBuilder::set( const Blob &rSrc )
96{ 146{
147 _hardCopy();
148 core->set( rSrc.getData(), rSrc.getSize() );
97} 149}
98 150
99void Bu::BlobBuilder::set( const char *pSrc, int32_t iLength ) 151void Bu::BlobBuilder::set( const char *pSrc, int32_t iLength )
100{ 152{
153 _hardCopy();
154 core->set( pSrc, iLength );
101} 155}
102 156
103void Bu::BlobBuilder::append( const Blob &rSrc ) 157void Bu::BlobBuilder::append( const Blob &rSrc )
104{ 158{
159 _hardCopy();
160 core->append( rSrc.getData(), rSrc.getSize() );
105} 161}
106 162
107void Bu::BlobBuilder::append( const char *pSrc, int32_t iLength ) 163void Bu::BlobBuilder::append( const char *pSrc, int32_t iLength )
108{ 164{
165 _hardCopy();
166 core->append( pSrc, iLength );
109} 167}
110 168
111void Bu::BlobBuilder::prepend( const Blob &rSrc ) 169void Bu::BlobBuilder::prepend( const Blob &rSrc )
112{ 170{
171 _hardCopy();
113} 172}
114 173
115void Bu::BlobBuilder::prepend( const char *pSrc, int32_t iLength ) 174void Bu::BlobBuilder::prepend( const char *pSrc, int32_t iLength )
116{ 175{
176 _hardCopy();
117} 177}
118 178
119void Bu::BlobBuilder::insert( int32_t iBefore, const Blob &rSrc ) 179void Bu::BlobBuilder::insert( int32_t iBefore, const Blob &rSrc )
120{ 180{
181 _hardCopy();
121} 182}
122 183
123void Bu::BlobBuilder::insert( int32_t iBefore, const char *pSrc, const Bu::Blob &rSrc ) 184void Bu::BlobBuilder::insert( int32_t iBefore, const char *pSrc, const Bu::Blob &rSrc )
124{ 185{
186 _hardCopy();
125} 187}
126 188
127void Bu::BlobBuilder::clear() 189void Bu::BlobBuilder::clear()
128{ 190{
191 _hardCopy();
192 core->clear();
129} 193}
130 194
131int32_t Bu::BlobBuilder::getSize() const 195int32_t Bu::BlobBuilder::getSize() const
132{ 196{
197 return core->iLength;
133} 198}
134 199
135Bu::Blob Bu::BlobBuilder::getBlob() const 200Bu::Blob Bu::BlobBuilder::getBlob() const
136{ 201{
202 Blob bRet();
137} 203}
138 204
139Bu::BlobBuilder &Bu::BlobBuilder::operator=( const Blob &rSrc ) 205Bu::BlobBuilder &Bu::BlobBuilder::operator=( const Blob &rSrc )