summaryrefslogtreecommitdiff
path: root/src/nids.cpp
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2009-01-27 15:25:46 +0000
committerMike Buland <eichlan@xagasoft.com>2009-01-27 15:25:46 +0000
commit9098237f5bb16b204a5ea999b702e5eb170f68ac (patch)
treeddf0c3013f0877d1f406401c6b4509d11bfb23e3 /src/nids.cpp
parent8bc5ac336d5d684341a05e97d1cb1b18ecba0331 (diff)
downloadlibbu++-9098237f5bb16b204a5ea999b702e5eb170f68ac.tar.gz
libbu++-9098237f5bb16b204a5ea999b702e5eb170f68ac.tar.bz2
libbu++-9098237f5bb16b204a5ea999b702e5eb170f68ac.tar.xz
libbu++-9098237f5bb16b204a5ea999b702e5eb170f68ac.zip
Corrected some larger read/write issues in corner cases that I hit suprisingly
often within nids. There's still a problem somewhere, but I'll find it. Also, even after having the file class canRead and canWrite functions work properly, and using them before trying to write to a nids to update info, we never ever write anything, so something is still wrong there. For now, all utilities that open a nids stream read-only will crash when it closes. Pretty minor really.
Diffstat (limited to 'src/nids.cpp')
-rw-r--r--src/nids.cpp40
1 files changed, 38 insertions, 2 deletions
diff --git a/src/nids.cpp b/src/nids.cpp
index 06895ac..00d5f2b 100644
--- a/src/nids.cpp
+++ b/src/nids.cpp
@@ -21,18 +21,19 @@ Bu::Nids::Nids( Bu::Stream &sStore ) :
21 sStore( sStore ), 21 sStore( sStore ),
22 iBlockSize( 0 ), 22 iBlockSize( 0 ),
23 iBlocks( 0 ), 23 iBlocks( 0 ),
24 iBlockStart( -1 ) 24 iBlockStart( -1 ),
25 iUsed( 0 )
25{ 26{
26} 27}
27 28
28Bu::Nids::~Nids() 29Bu::Nids::~Nids()
29{ 30{
31 updateHeader();
30} 32}
31 33
32void Bu::Nids::initialize() 34void Bu::Nids::initialize()
33{ 35{
34 unsigned char buf[4]; 36 unsigned char buf[4];
35 int iUsed;
36 if( sStore.read( buf, 4 ) < 4 ) 37 if( sStore.read( buf, 4 ) < 4 )
37 throw NidsException("Input stream appears to be empty."); 38 throw NidsException("Input stream appears to be empty.");
38 if( memcmp( buf, NIDS_MAGIC_CODE, 4 ) ) 39 if( memcmp( buf, NIDS_MAGIC_CODE, 4 ) )
@@ -111,6 +112,15 @@ void Bu::Nids::initialize( int iBlockSize, int iPreAllocate )
111 delete[] (char *)block; 112 delete[] (char *)block;
112} 113}
113 114
115void Bu::Nids::updateHeader()
116{
117// if( !sStore.canWrite() )
118// return;
119 sStore.setPos( 10 ); // Skip the magic number, version, bpi, block size
120 sStore.write( &iBlocks, 4 );
121 sStore.write( &iUsed, 4 );
122}
123
114void Bu::Nids::initBlock( uint32_t uPos, uint32_t uFirstBlock, 124void Bu::Nids::initBlock( uint32_t uPos, uint32_t uFirstBlock,
115 uint32_t uPrevBlock, bool bNew ) 125 uint32_t uPrevBlock, bool bNew )
116{ 126{
@@ -129,6 +139,7 @@ void Bu::Nids::initBlock( uint32_t uPos, uint32_t uFirstBlock,
129 sStore.write( buf, iSize ); 139 sStore.write( buf, iSize );
130 delete[] buf; 140 delete[] buf;
131 } 141 }
142 iUsed++;
132} 143}
133 144
134uint32_t Bu::Nids::createBlock( uint32_t uFirstBlock, uint32_t uPrevBlock, 145uint32_t Bu::Nids::createBlock( uint32_t uFirstBlock, uint32_t uPrevBlock,
@@ -171,6 +182,27 @@ int Bu::Nids::getBlockSize()
171{ 182{
172 return iBlockSize; 183 return iBlockSize;
173} 184}
185
186int Bu::Nids::getNumBlocks()
187{
188 return iBlocks;
189}
190
191int Bu::Nids::getNumUsedBlocks()
192{
193 return iUsed;
194}
195
196int Bu::Nids::getBlockStart()
197{
198 return iBlockStart;
199}
200
201int Bu::Nids::getBlockOverhead()
202{
203 return sizeof(Block);
204}
205
174/* 206/*
175void Bu::Nids::extendStream( int iID, int iBlockCount ) 207void Bu::Nids::extendStream( int iID, int iBlockCount )
176{ 208{
@@ -190,6 +222,8 @@ void Bu::Nids::setBlock( uint32_t uIndex, Bu::Nids::Block *pBlock )
190 222
191void Bu::Nids::updateStreamSize( uint32_t uIndex, uint32_t uSize ) 223void Bu::Nids::updateStreamSize( uint32_t uIndex, uint32_t uSize )
192{ 224{
225// if( !sStore.canWrite() )
226// return;
193 sStore.setPos( iBlockStart + (iBlockSize*uIndex)+4*3 ); 227 sStore.setPos( iBlockStart + (iBlockSize*uIndex)+4*3 );
194 sStore.write( &uSize, 4 ); 228 sStore.write( &uSize, 4 );
195} 229}
@@ -206,6 +240,8 @@ uint32_t Bu::Nids::getNextBlock( uint32_t uIndex,
206 sStore.setPos( iBlockStart + (iBlockSize*uIndex)+1*4 ); 240 sStore.setPos( iBlockStart + (iBlockSize*uIndex)+1*4 );
207 sStore.write( &uNew, 4 ); 241 sStore.write( &uNew, 4 );
208 getBlock( uNew, pBlock ); 242 getBlock( uNew, pBlock );
243 printf("Allocated new block (%u) for stream %u.\n",
244 uNew, pBlock->uFirstBlock );
209 } 245 }
210 else 246 else
211 { 247 {