aboutsummaryrefslogtreecommitdiff
path: root/src/nids.h
blob: 63d7061fae3ee9c2918d102b31a95e014f117162 (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
/*
 * Copyright (C) 2007-2008 Xagasoft, All rights reserved.
 *
 * This file is part of the libbu++ library and is released under the
 * terms of the license contained in the file LICENSE.
 */

#ifndef BU_NIDS_H
#define BU_NIDS_H

#include <stdint.h>
#include "bu/bitstring.h"
#include "bu/exceptionbase.h"

namespace Bu
{
	class Stream;
	class NidsStream;

	subExceptionDecl( NidsException )

	/**
	 * Numerically Indexed Data Streams.  This is a working name so I can
	 * actually get some code written instead of agonizing over the name.
	 *
	 * This is a system for creating streams that contain other streams in
	 * a flexible block-allocated system.
	 */
	class Nids
	{
		friend class NidsStream;
	public:
		Nids( Bu::Stream &sStore );
		virtual ~Nids();

		/**
		 * Initialize this object based on the data already in the assosiated
		 * stream.  This will be called automatically for you if you forget,
		 * but if you want to pre-initialize for some reason, just call this
		 * once before you actually start doing anything with your Nids.
		 */
		void initialize();

		/**
		 * Create a new Nids system in the assosiated stream.  This should be
		 * used carefully, it will destroy all data already within the stream.
		 * More options will probably be added soon.
		 */
		void initialize( int iBlockSize, int iPreAllocate=1 );

		/**
		 * Create a new stream within the Nids system.  The ID of the new stream
		 * is returned.
		 */
		int createStream( int iPreAllocate=1 );

		/**
		 * Delete a stream that's already within the Nids.
		 */
		void deleteStream( int iID );

		/**
		 * Return a new Stream object assosiated with the given stream ID.
		 */
		NidsStream openStream( int iID );

		int getBlockSize();
		int getNumBlocks();
		int getNumUsedBlocks();
		int getBlockStart();
		int getBlockOverhead();

	private:
		typedef struct Block
		{
			uint32_t uFirstBlock;
			uint32_t uNextBlock;
			uint32_t uPrevBlock;
			uint32_t uBytesUsed;
			uint32_t uReserved;
			unsigned char pData[0];
		} Block;

		enum
		{
			blockUnused	=	0xFFFFFFFFUL
		};

		void initBlock( uint32_t uPos, uint32_t uFirstBlock,
			uint32_t uPrevBlock, bool bNew=false );
		uint32_t createBlock( uint32_t uFirstBlock, uint32_t uPrevBlock,
			int iPreAllocate=1 );
		void getBlock( uint32_t uIndex, struct Nids::Block *pBlock );
		void setBlock( uint32_t uIndex, struct Nids::Block *pBlock );
		void updateStreamSize( uint32_t uIndex, uint32_t uSize );
		uint32_t getNextBlock( uint32_t uIndex, struct Nids::Block *pBlock,
			bool bCreate=true);

		void updateHeader();

		// Block allocation routines
		Block *newBlock();
		void deleteBlock( Block *pBlock );

	private:
		Bu::Stream &sStore;
		int iBlockSize;
		int iBlocks;
		int iBlockStart;
		int iUsed;
		Bu::BitString bsBlockUsed;
	};
};

#endif