aboutsummaryrefslogtreecommitdiff
path: root/src/nids.cpp
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/nids.cpp106
1 files changed, 93 insertions, 13 deletions
diff --git a/src/nids.cpp b/src/nids.cpp
index 41c4dc1..d0cb843 100644
--- a/src/nids.cpp
+++ b/src/nids.cpp
@@ -3,9 +3,20 @@
3#include "bu/nidsstream.h" 3#include "bu/nidsstream.h"
4#include <stdio.h> 4#include <stdio.h>
5 5
6#define NIDS_MAGIC_CODE "\xFF\xC3\x99\xBD"
7
8namespace Bu
9{
10 subExceptionDef( NidsException )
11}
12
6Bu::Nids::Nids( Bu::Stream &sStore ) : 13Bu::Nids::Nids( Bu::Stream &sStore ) :
7 sStore( sStore ) 14 sStore( sStore ),
15 iBlockSize( 0 ),
16 iBlocks( 0 ),
17 iBlockStart( -1 )
8{ 18{
19 printf("blockUnused = %u\n", blockUnused );
9 printf("Stream caps:\n" 20 printf("Stream caps:\n"
10 " canRead: %s\n" 21 " canRead: %s\n"
11 " canWrite: %s\n" 22 " canWrite: %s\n"
@@ -25,19 +36,32 @@ Bu::Nids::Nids( Bu::Stream &sStore ) :
25 sStore.isOpen()?"yes":"no" 36 sStore.isOpen()?"yes":"no"
26 ); 37 );
27 printf("sizeof(Block) = %db\n", sizeof(Block) ); 38 printf("sizeof(Block) = %db\n", sizeof(Block) );
39
40
28} 41}
29 42
30Bu::Nids::~Nids() 43Bu::Nids::~Nids()
31{ 44{
32} 45}
33 46
47void Bu::Nids::initialize()
48{
49 char buf[4];
50 sStore.read( buf, 4 );
51 if( memcmp( buf, NIDS_MAGIC_CODE, 4 ) )
52 {
53 throw NidsException(
54 "Stream does not appear to be a valid NIDS format.");
55 }
56}
57
34void Bu::Nids::initialize( int iBlockSize, int iPreAllocate ) 58void Bu::Nids::initialize( int iBlockSize, int iPreAllocate )
35{ 59{
36 char cBuf = 0; 60 char cBuf = 0;
37 int iBuf = 0; 61 int iBuf = 0;
38 62
39 // Magic number 63 // Magic number
40 sStore.write( "\xFF\xC3\x99\xBD", 4 ); 64 sStore.write( NIDS_MAGIC_CODE, 4 );
41 65
42 // Version (0) 66 // Version (0)
43 sStore.write( &cBuf, 1 ); 67 sStore.write( &cBuf, 1 );
@@ -59,56 +83,112 @@ void Bu::Nids::initialize( int iBlockSize, int iPreAllocate )
59 this->iBlockSize = iBlockSize; 83 this->iBlockSize = iBlockSize;
60 this->iBlocks = iPreAllocate; 84 this->iBlocks = iPreAllocate;
61 this->iBlockStart = sStore.tell(); 85 this->iBlockStart = sStore.tell();
86 printf("iBlockStart = %d\n", this->iBlockStart );
62 bsBlockUsed.setSize( iPreAllocate, true ); 87 bsBlockUsed.setSize( iPreAllocate, true );
63 88
64 printf("%d blocks, %db each, %db block offset\n", 89 printf("%d blocks, %db each, %db block offset\n",
65 iBlocks, iBlockSize, iBlockStart ); 90 iBlocks, iBlockSize, iBlockStart );
66 91
67 char *block = new char[iBlockSize]; 92 Block *block = (Block *)new char[iBlockSize];
68 memset( block, 0, iBlockSize ); 93 memset( block, 0, iBlockSize );
94 block->uFirstBlock = block->uNextBlock = block->uPrevBlock = blockUnused;
69 for( int j = 0; j < iPreAllocate; j++ ) 95 for( int j = 0; j < iPreAllocate; j++ )
70 { 96 {
71 sStore.write( block, iBlockSize ); 97 sStore.write( block, iBlockSize );
72 } 98 }
99 delete[] (char *)block;
73} 100}
74 101
75int Bu::Nids::createStream( int iPreAllocate ) 102uint32_t Bu::Nids::createBlock( uint32_t uFirstBlock, uint32_t uPrevBlock,
103 int /*iPreAllocate*/ )
76{ 104{
77 for( int j = 0; j < iBlocks; j++ ) 105 for( int j = 0; j < iBlocks; j++ )
78 { 106 {
79 if( !bsBlockUsed.getBit( j ) ) 107 if( !bsBlockUsed.getBit( j ) )
80 { 108 {
81 Block b = { j, blockUnused, blockUnused, 0, 0 }; 109 Block b = { j, blockUnused, uPrevBlock, 0, 0, { } };
110 if( uFirstBlock != blockUnused )
111 b.uFirstBlock = uFirstBlock;
82 bsBlockUsed.setBit( j ); 112 bsBlockUsed.setBit( j );
83 sStore.setPos( iBlockStart+(iBlockSize*j) ); 113 sStore.setPos( iBlockStart+(iBlockSize*j) );
84 sStore.write( &b, sizeof(b) ); 114 sStore.write( &b, sizeof(b) );
115 return j;
85 } 116 }
86 } 117 }
87 return 0; 118 return blockUnused;
88} 119}
89 120
90void Bu::Nids::deleteStream( int iID ) 121int Bu::Nids::createStream( int iPreAllocate )
122{
123 return createBlock( blockUnused, blockUnused, iPreAllocate );
124}
125
126void Bu::Nids::deleteStream( int /*iID*/ )
91{ 127{
92} 128}
93 129
94Bu::NidsStream Bu::Nids::openStream( int iID ) 130Bu::NidsStream Bu::Nids::openStream( int iID )
95{ 131{
96 return NidsStream( *this ); 132 if( iBlockStart < 0 )
133 {
134 initialize();
135 }
136 return NidsStream( *this, iID );
97} 137}
98 138
99void Bu::Nids::extendStream( int iID, int iBlockCount ) 139int Bu::Nids::getBlockSize()
100{ 140{
141 return iBlockSize;
101} 142}
143/*
144void Bu::Nids::extendStream( int iID, int iBlockCount )
145{
146}*/
102 147
103void Bu::Nids::getBlock( int iIndex, Bu::Nids::Block *pBlock ) 148void Bu::Nids::getBlock( uint32_t uIndex, Bu::Nids::Block *pBlock )
104{ 149{
105 sStore.setPos( iBlockStart + (iBlockSize*iIndex) ); 150 sStore.setPos( iBlockStart + (iBlockSize*uIndex) );
106 sStore.read( pBlock, iBlockSize ); 151 sStore.read( pBlock, iBlockSize );
107} 152}
108 153
109void Bu::Nids::setBlock( int iIndex, Bu::Nids::Block *pBlock ) 154void Bu::Nids::setBlock( uint32_t uIndex, Bu::Nids::Block *pBlock )
110{ 155{
111 sStore.setPos( iBlockStart + (iBlockSize*iIndex) ); 156 sStore.setPos( iBlockStart + (iBlockSize*uIndex) );
112 sStore.write( pBlock, iBlockSize ); 157 sStore.write( pBlock, iBlockSize );
113} 158}
114 159
160void Bu::Nids::updateStreamSize( uint32_t uIndex, uint32_t uSize )
161{
162 sStore.setPos( iBlockStart + (iBlockSize*uIndex)+4*3 );
163 sStore.write( &uSize, 4 );
164}
165
166uint32_t Bu::Nids::getNextBlock( uint32_t uIndex,
167 struct Bu::Nids::Block *pBlock )
168{
169 uint32_t uNew;
170 if( pBlock->uNextBlock == blockUnused )
171 {
172 uNew = createBlock( pBlock->uFirstBlock, uIndex );
173 sStore.setPos( iBlockStart + (iBlockSize*uIndex)+1*4 );
174 sStore.write( &uNew, 4 );
175 getBlock( uNew, pBlock );
176 }
177 else
178 {
179 uNew = pBlock->uNextBlock;
180 getBlock( pBlock->uNextBlock, pBlock );
181 }
182 return uNew;
183}
184
185Bu::Nids::Block *Bu::Nids::newBlock()
186{
187 return (Block *)new char[iBlockSize];
188}
189
190void Bu::Nids::deleteBlock( Block *pBlock )
191{
192 delete[] (char *)pBlock;
193}
194