aboutsummaryrefslogtreecommitdiff
path: root/src/tools/myriad.cpp
blob: b3067d2a3a2fdf2b211510a760ba4507854b87b4 (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-2010 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.
 */

#include "bu/sio.h"
#include "bu/file.h"
#include "bu/myriad.h"
#include "bu/myriadstream.h"
#include "bu/optparser.h"

#include <stdlib.h>

using namespace Bu;

enum Mode
{
	modeCreate,
	modeInfo,

	modeNone
};

class Options : public OptParser
{
public:
	Options( int argc, char *argv[] ) :
		eMode( modeNone ),
		iBlockSize( 64 ),
		iPreallocate( 0 )
	{
		addHelpBanner("Mode of operation:");
		addOption( eMode, 'c', "create", "Create a new NIDS file." );
		addOption( eMode, "info", "Display some info about a NIDS file." );
		addHelpOption();

		addHelpBanner("\nGeneral options:");
		addOption( iBlockSize, 'b', "block-size", "Set the block size." );
		addOption( iPreallocate, 'p', "preallocate",
			"Number of blocks to preallocate." );
		addOption( sOutput, 'o', "output", "Set the output filename." );
		addOption( sInput, 'i', "input", "Set the input filename." );

		setOverride( "create", "create" );
		setOverride( "info", "info" );

		parse( argc, argv );
	}

	Mode eMode;
	int iBlockSize;
	int iPreallocate;
	Bu::FString sOutput;
	Bu::FString sInput;
};

Bu::Formatter &operator>>( Bu::Formatter &f, Mode &m )
{
	Bu::FString sTok = f.readToken();
	if( sTok == "create" || sTok == "c" )
		m = modeCreate;
	else if( sTok == "info" )
		m = modeInfo;
	else
		m = modeNone;
	return f;
}

int main( int argc, char *argv[] )
{
	Options opts( argc, argv );

	switch( opts.eMode )
	{
		case modeCreate:
			if( !opts.sOutput.isSet() )
			{
				sio << "Please specify an output file to create a stream for."
					<< sio.nl;
				return 0;
			}
			else
			{
				File fOut( opts.sOutput, File::WriteNew );
				Myriad n( fOut );
				n.initialize( opts.iBlockSize, opts.iPreallocate );
			}
			break;

		case modeInfo:
			if( !opts.sInput.isSet() )
			{
				sio << "Please specify an input file to display info about."
					<< sio.nl;
				return 0;
			}
			else
			{
				File fIn( opts.sInput, File::Read );
				Myriad n( fIn );
				n.initialize();
			}
			break;

		case modeNone:
			sio << "Please select a mode, for more info, try --help."
				<< sio.nl << sio.nl;
			break;
	}

	return 0;
}