aboutsummaryrefslogtreecommitdiff
path: root/src/tests/nidstool.cpp
blob: 546534eaf71b94b43180a56221d859acf787da60 (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
#include "bu/file.h"
#include "bu/nids.h"
#include "bu/nidsstream.h"
#include "bu/paramproc.h"

#include <stdlib.h>

class Param : public Bu::ParamProc
{
public:
	Param( int argc, char *argv[] )
	{
		addHelpBanner("nidstool - Do stuff with nids files.\n\n");
		addParam("info", 'i', mkproc(Param::procInfo),
			"Print some info about the file.");
		addParam("dump", 'd', mkproc(Param::procDump),
			"Dump a stream to a file");
		addParam("help", 'h', mkproc(Bu::ParamProc::help), "This help.");
		process( argc, argv );
	}

	virtual ~Param()
	{
	}

	int procInfo( int argc, char *argv[] )
	{
		if( argc < 1 )
		{
			printf("You must provide a file name.\n");
			exit( 1 );
		}

		Bu::File fIn( argv[0], Bu::File::Read );
		Bu::Nids n( fIn );
		n.initialize();

		printf("Block size:     %db\n", n.getBlockSize() );
		printf("Block count:    %d\n", n.getNumBlocks() );
		printf("Blocks used:    %d (%d%%)\n", n.getNumUsedBlocks(),
			n.getNumUsedBlocks()*100/n.getNumBlocks() );
		printf("Block start:    %db\n", n.getBlockStart() );
		printf("Block overhead: %db\n", n.getBlockOverhead() );
		printf("Block storage:  %db (%d%%)\n",
			n.getBlockSize()-n.getBlockOverhead(),
			(n.getBlockSize()-n.getBlockOverhead())*100/n.getBlockSize() );

		if( argc >= 2 )
		{
			typedef struct Block
			{
				uint32_t uFirstBlock;
				uint32_t uNextBlock;
				uint32_t uPrevBlock;
				uint32_t uBytesUsed;
				uint32_t uReserved;
			} Block;

			uint32_t uStream = strtoul( argv[1], NULL, 0 );
			uint32_t uBlock = uStream;

			Block b;

			for(;;)
			{
				fIn.setPos( n.getBlockStart()+n.getBlockSize()*uBlock );
				fIn.read( &b, sizeof(Block) );
				printf("Stream %u:  block %u, next %u, prev %u, %ub used.\n",
					uStream, uBlock, b.uNextBlock, b.uPrevBlock, b.uBytesUsed
					);
				if( b.uNextBlock == 0xFFFFFFFFUL )
					break;
				uBlock = b.uNextBlock;
			}
			printf("Stream End.\n");

			return 2;
		}

		return 1;
	}

	int procDump( int argc, char *argv[] )
	{
		if( argc < 3 )
		{
			printf("You must provide a nids file, a stream id, and an output "
				"file.\n");
			exit( 1 );
		}

		Bu::File fIn( argv[0], Bu::File::Read );
		Bu::Nids n( fIn );
		n.initialize();

		int iStream = strtol( argv[1], NULL, 0 );
		Bu::NidsStream sIn = n.openStream( iStream );

		Bu::File fOut( argv[2], Bu::File::Write|Bu::File::Create );
		int iTotal = 0;
		char buf[100];
		for(;;)
		{
			int iRead = sIn.read( buf, 100 );
			iTotal += fOut.write( buf, iRead );
			if( iRead < 100 )
				break;
		}

		printf("Wrote %db from stream %d in %s to %s.\n",
			iTotal, iStream, argv[0], argv[2] );
		return 3;
	}

};


int main( int argc, char *argv[] )
{
	Param p( argc, argv );

	return 0;
}