summaryrefslogtreecommitdiff
path: root/src/nids.cpp
blob: d0cb843fc79ff03741e711522452b1c1a107f7cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include "bu/nids.h"
#include "bu/stream.h"
#include "bu/nidsstream.h"
#include <stdio.h>

#define NIDS_MAGIC_CODE		"\xFF\xC3\x99\xBD"

namespace Bu
{
	subExceptionDef( NidsException )
}

Bu::Nids::Nids( Bu::Stream &sStore ) :
	sStore( sStore ),
	iBlockSize( 0 ),
	iBlocks( 0 ),
	iBlockStart( -1 )
{
	printf("blockUnused = %u\n", blockUnused );
	printf("Stream caps:\n"
		"  canRead:    %s\n"
		"  canWrite:   %s\n"
		"  isReadable: %s\n"
		"  isWritable: %s\n"
		"  isSeekable: %s\n"
		"  isBlocking: %s\n"
		"  isEOS:      %s\n"
		"  isOpen:     %s\n",
		sStore.canRead()?"yes":"no",
		sStore.canWrite()?"yes":"no",
		sStore.isReadable()?"yes":"no",
		sStore.isWritable()?"yes":"no",
		sStore.isSeekable()?"yes":"no",
		sStore.isBlocking()?"yes":"no",
		sStore.isEOS()?"yes":"no",
		sStore.isOpen()?"yes":"no"
		);
	printf("sizeof(Block) = %db\n", sizeof(Block) );


}

Bu::Nids::~Nids()
{
}

void Bu::Nids::initialize()
{
	char buf[4];
	sStore.read( buf, 4 );
	if( memcmp( buf, NIDS_MAGIC_CODE, 4 ) )
	{
		throw NidsException(
			"Stream does not appear to be a valid NIDS format.");
	}
}

void Bu::Nids::initialize( int iBlockSize, int iPreAllocate )
{
	char cBuf = 0;
	int iBuf = 0;

	// Magic number
	sStore.write( NIDS_MAGIC_CODE, 4 );

	// Version (0)
	sStore.write( &cBuf, 1 );

	// Bytes per int
	cBuf = 4;
	sStore.write( &cBuf, 1 );

	// The size of each block and the number of blocks we're pre-allocating
	sStore.write( &iBlockSize, 4 );
	sStore.write( &iPreAllocate, 4 );

	// The number of used blocks
	sStore.write( &iBuf, 4 );

	// Reserved space
	sStore.write( "\x00\x00\x00\x00\x00\x00\x00", 7 );

	this->iBlockSize = iBlockSize;
	this->iBlocks = iPreAllocate;
	this->iBlockStart = sStore.tell();
	printf("iBlockStart = %d\n", this->iBlockStart );
	bsBlockUsed.setSize( iPreAllocate, true );

	printf("%d blocks, %db each, %db block offset\n",
		iBlocks, iBlockSize, iBlockStart );

	Block *block = (Block *)new char[iBlockSize];
	memset( block, 0, iBlockSize );
	block->uFirstBlock = block->uNextBlock = block->uPrevBlock = blockUnused;
	for( int j = 0; j < iPreAllocate; j++ )
	{
		sStore.write( block, iBlockSize );
	}
	delete[] (char *)block;
}

uint32_t Bu::Nids::createBlock( uint32_t uFirstBlock, uint32_t uPrevBlock,
	int /*iPreAllocate*/ )
{
	for( int j = 0; j < iBlocks; j++ )
	{
		if( !bsBlockUsed.getBit( j ) )
		{
			Block b = { j, blockUnused, uPrevBlock, 0, 0, { } };
			if( uFirstBlock != blockUnused )
				b.uFirstBlock = uFirstBlock;
			bsBlockUsed.setBit( j );
			sStore.setPos( iBlockStart+(iBlockSize*j) );
			sStore.write( &b, sizeof(b) );
			return j;
		}
	}
	return blockUnused;
}

int Bu::Nids::createStream( int iPreAllocate )
{
	return createBlock( blockUnused, blockUnused, iPreAllocate );
}

void Bu::Nids::deleteStream( int /*iID*/ )
{
}

Bu::NidsStream Bu::Nids::openStream( int iID )
{
	if( iBlockStart < 0 )
	{
		initialize();
	}
	return NidsStream( *this, iID );
}

int Bu::Nids::getBlockSize()
{
	return iBlockSize;
}
/*
void Bu::Nids::extendStream( int iID, int iBlockCount )
{
}*/

void Bu::Nids::getBlock( uint32_t uIndex, Bu::Nids::Block *pBlock )
{
	sStore.setPos( iBlockStart + (iBlockSize*uIndex) );
	sStore.read( pBlock, iBlockSize );
}

void Bu::Nids::setBlock( uint32_t uIndex, Bu::Nids::Block *pBlock )
{
	sStore.setPos( iBlockStart + (iBlockSize*uIndex) );
	sStore.write( pBlock, iBlockSize );
}

void Bu::Nids::updateStreamSize( uint32_t uIndex, uint32_t uSize )
{
	sStore.setPos( iBlockStart + (iBlockSize*uIndex)+4*3 );
	sStore.write( &uSize, 4 );
}

uint32_t Bu::Nids::getNextBlock( uint32_t uIndex,
	struct Bu::Nids::Block *pBlock )
{
	uint32_t uNew;
	if( pBlock->uNextBlock == blockUnused )
	{
		uNew = createBlock( pBlock->uFirstBlock, uIndex );
		sStore.setPos( iBlockStart + (iBlockSize*uIndex)+1*4 );
		sStore.write( &uNew, 4 );
		getBlock( uNew, pBlock );
	}
	else
	{
		uNew = pBlock->uNextBlock;
		getBlock( pBlock->uNextBlock, pBlock );
	}
	return uNew;
}

Bu::Nids::Block *Bu::Nids::newBlock()
{
	return (Block *)new char[iBlockSize];
}

void Bu::Nids::deleteBlock( Block *pBlock )
{
	delete[] (char *)pBlock;
}