aboutsummaryrefslogtreecommitdiff
path: root/src/tools/myriad.cpp
blob: 95b4503944763c14796ecfe4beb41b369ec25b46 (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
/*
 * 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,
	modeStreamNew,
	modeStreamRead,

	modeNone
};

class Options : public OptParser
{
public:
	Options( int argc, char *argv[] ) :
		eMode( modeNone ),
		iBlockSize( 64 ),
		iPreallocate( 0 ),
		iStream( 0 )
	{
		addHelpBanner("Mode of operation:");
		addOption( eMode, 'c', "create",
				"Create a new Myriad file." );
		addOption( eMode, 'i', "info",
				"Display some info about a Myriad file." );
		addOption( eMode, 'n', "new",
				"Create a new sub-stream in a Myriad file.");
		addOption( eMode, 'r', "read",
				"Read a stream from a Myriad file.");
		addHelpOption();

		addHelpBanner("\nGeneral options:");
		addOption( iBlockSize, 'b', "block-size", "Set the block size." );
		addOption( iPreallocate, 'p', "preallocate",
			"Number of blocks to preallocate." );
		addOption( sFile, 'f', "file", "Set the Myriad filename." );
		addOption( iStream, 's', "stream", "Substream to work with.");
		addOption( sSrc, "src", "Source file for copying into a Myriad file.");

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

		parse( argc, argv );
	}

	Mode eMode;
	int iBlockSize;
	int iPreallocate;
	int iStream;
	Bu::FString sFile;
	Bu::FString sSrc;
};

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

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

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

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

		case modeStreamNew:
			if( !opts.sFile.isSet() )
			{
				sio << "Please specify a file manipulate." << sio.nl;
				return 0;
			}
			else
			{
				File fOut( opts.sFile, File::Write|File::Read );
				Myriad m( fOut );
				m.initialize();
				m.createStream( opts.iPreallocate );
			}
			break;

		case modeStreamRead:
			if( !opts.sFile.isSet() )
			{
				sio << "Please specify a file manipulate." << sio.nl;
				return 0;
			}
			else
			{
				File fOut( opts.sFile, File::Read );
				Myriad m( fOut );
				m.initialize();
				MyriadStream s = m.openStream( opts.iStream );
				sio << "Stream " << opts.iStream << ":" << sio.nl;
				char buf[8];
				int iPos = 0;
				while( !s.isEos() )
				{
					size_t sAmnt = s.read( buf, 8 );
					sio << Fmt(5) << iPos << ": ";
					iPos += sAmnt;
					for( size_t j = 0; j < sAmnt; j++ )
					{
						sio << Fmt::hex(2) << (int)((unsigned char)buf[j])
							<< " ";
					}
					for( size_t j = sAmnt; j < 8; j++ )
					{
						sio << "-- ";
					}
					sio << "| ";
					for( size_t j = 0; j < sAmnt; j++ )
					{
						if( buf[j] >= 32 && buf[j] <= 126 )
							sio << buf[j] << " ";
						else
							sio << "  ";
					}
					sio << sio.nl;
				}
			}
			break;

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

	return 0;
}