diff options
Diffstat (limited to 'src/tools/myriad.cpp')
-rw-r--r-- | src/tools/myriad.cpp | 280 |
1 files changed, 280 insertions, 0 deletions
diff --git a/src/tools/myriad.cpp b/src/tools/myriad.cpp new file mode 100644 index 0000000..1dc73ec --- /dev/null +++ b/src/tools/myriad.cpp | |||
@@ -0,0 +1,280 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2007-2023 Xagasoft, All rights reserved. | ||
3 | * | ||
4 | * This file is part of the libbu++ library and is released under the | ||
5 | * terms of the license contained in the file LICENSE. | ||
6 | */ | ||
7 | |||
8 | #include "bu/sio.h" | ||
9 | #include "bu/file.h" | ||
10 | #include "bu/myriad.h" | ||
11 | #include "bu/myriadstream.h" | ||
12 | #include "bu/optparser.h" | ||
13 | |||
14 | #include <stdlib.h> | ||
15 | |||
16 | using namespace Bu; | ||
17 | |||
18 | enum Mode | ||
19 | { | ||
20 | modeCreate, | ||
21 | modeInfo, | ||
22 | modeStreamNew, | ||
23 | modeStreamDump, | ||
24 | modeStreamPut, | ||
25 | modeStreamGet, | ||
26 | modeBlockMap, | ||
27 | |||
28 | modeNone | ||
29 | }; | ||
30 | |||
31 | class Options : public OptParser | ||
32 | { | ||
33 | public: | ||
34 | Options( int argc, char *argv[] ) : | ||
35 | eMode( modeNone ), | ||
36 | iBlockSize( 64 ), | ||
37 | iPreallocate( 0 ), | ||
38 | iStream( 0 ) | ||
39 | { | ||
40 | addHelpBanner("Mode of operation:"); | ||
41 | addOption( eMode, 'c', "create", | ||
42 | "Create a new Myriad file." ); | ||
43 | addOption( eMode, 'i', "info", | ||
44 | "Display some info about a Myriad file." ); | ||
45 | addOption( eMode, 'n', "new", | ||
46 | "Create a new sub-stream in a Myriad file."); | ||
47 | addOption( eMode, 'd', "dump", | ||
48 | "Display a hexdump of a stream from a Myriad file."); | ||
49 | addOption( eMode, "get", | ||
50 | "Get a file out of a Myriad stream (use --dst)."); | ||
51 | addOption( eMode, "put", | ||
52 | "Put a file into a Myriad stream (usr --src)."); | ||
53 | addOption( eMode, 'm', "block-map", | ||
54 | "Visualize block usage."); | ||
55 | addHelpOption(); | ||
56 | |||
57 | addHelpBanner("\nGeneral options:"); | ||
58 | addOption( iBlockSize, 'b', "block-size", "Set the block size." ); | ||
59 | addOption( iPreallocate, 'p', "preallocate", | ||
60 | "Number of blocks to preallocate." ); | ||
61 | addOption( sFile, 'f', "file", "Set the Myriad filename." ); | ||
62 | addOption( iStream, 's', "stream", "Substream to work with."); | ||
63 | addOption( sSrc, "src", "Source file for copying into a Myriad file."); | ||
64 | addOption( sDst, "dst", | ||
65 | "Destination file for copying out of a Myriad file."); | ||
66 | |||
67 | setOverride( "create", modeCreate ); | ||
68 | setOverride( "info", modeInfo ); | ||
69 | setOverride( "new", modeStreamNew ); | ||
70 | setOverride( "dump", modeStreamDump ); | ||
71 | setOverride( "put", modeStreamPut ); | ||
72 | setOverride( "get", modeStreamGet ); | ||
73 | setOverride( "block-map", modeBlockMap ); | ||
74 | |||
75 | parse( argc, argv ); | ||
76 | } | ||
77 | |||
78 | Mode eMode; | ||
79 | int iBlockSize; | ||
80 | int iPreallocate; | ||
81 | int iStream; | ||
82 | Bu::String sFile; | ||
83 | Bu::String sSrc; | ||
84 | Bu::String sDst; | ||
85 | }; | ||
86 | |||
87 | Bu::Formatter &operator>>( Bu::Formatter &f, Mode & /*e*/ ) | ||
88 | { | ||
89 | sio << "Uh oh, the formatter was called..." << sio.nl; | ||
90 | return f; | ||
91 | } | ||
92 | |||
93 | int main( int argc, char *argv[] ) | ||
94 | { | ||
95 | Options opts( argc, argv ); | ||
96 | |||
97 | switch( opts.eMode ) | ||
98 | { | ||
99 | case modeCreate: | ||
100 | if( !opts.sFile.isSet() ) | ||
101 | { | ||
102 | sio << "Please specify a file to create." << sio.nl; | ||
103 | return 0; | ||
104 | } | ||
105 | else | ||
106 | { | ||
107 | File fOut( opts.sFile, File::WriteNew|File::Read ); | ||
108 | Myriad m( fOut, opts.iBlockSize, opts.iPreallocate ); | ||
109 | } | ||
110 | break; | ||
111 | |||
112 | case modeInfo: | ||
113 | if( !opts.sFile.isSet() ) | ||
114 | { | ||
115 | sio << "Please specify a file to display info about." << sio.nl; | ||
116 | return 0; | ||
117 | } | ||
118 | else | ||
119 | { | ||
120 | File fIn( opts.sFile, File::Read ); | ||
121 | Myriad m( fIn ); | ||
122 | sio << "Myriad info:" << sio.nl | ||
123 | << " Block size: " << m.getBlockSize() << sio.nl | ||
124 | << " Block count: " << m.getTotalBlocks() << sio.nl | ||
125 | << " Blocks used: " << m.getUsedBlocks() << " (" | ||
126 | << m.getUsedBlocks()*100/m.getTotalBlocks() << "%)" | ||
127 | << sio.nl | ||
128 | << " Stream count: " << m.getTotalStreams() << sio.nl | ||
129 | << " Used space: " << m.getTotalUsedBytes() << sio.nl | ||
130 | << " Unused space: " << m.getTotalUnusedBytes() << sio.nl | ||
131 | << " % of files: " << (double)(m.getTotalBlocks()*m.getBlockSize())/(double)(m.getTotalUsedBytes() + m.getTotalUnusedBytes( 4096 ))*100.0 << sio.nl; | ||
132 | /* Bu::Array<int> aStreams = m.getStreamIds(); | ||
133 | sio << " Stream info:" << sio.nl; | ||
134 | for( Bu::Array<int>::iterator i = aStreams.begin(); i; i++ ) | ||
135 | { | ||
136 | sio << " " << Fmt(4) << *i << ") " | ||
137 | << m.getStreamSize( *i ) << "b" << sio.nl; | ||
138 | } */ | ||
139 | } | ||
140 | break; | ||
141 | |||
142 | case modeStreamNew: | ||
143 | if( !opts.sFile.isSet() ) | ||
144 | { | ||
145 | sio << "Please specify a file manipulate." << sio.nl; | ||
146 | return 0; | ||
147 | } | ||
148 | else | ||
149 | { | ||
150 | File fOut( opts.sFile, File::Write|File::Read ); | ||
151 | Myriad m( fOut ); | ||
152 | m.create( Bu::Myriad::WriteNew, opts.iPreallocate ); | ||
153 | } | ||
154 | break; | ||
155 | |||
156 | case modeStreamDump: | ||
157 | if( !opts.sFile.isSet() ) | ||
158 | { | ||
159 | sio << "Please specify a file to manipulate." << sio.nl; | ||
160 | return 0; | ||
161 | } | ||
162 | else | ||
163 | { | ||
164 | File fOut( opts.sFile, File::Read ); | ||
165 | Myriad m( fOut ); | ||
166 | MyriadStream s = m.open( opts.iStream, Bu::Myriad::Read ); | ||
167 | sio << "Stream " << opts.iStream << ":" << sio.nl; | ||
168 | char buf[8]; | ||
169 | int iPos = 0; | ||
170 | while( !s.isEos() ) | ||
171 | { | ||
172 | size_t sAmnt = s.read( buf, 8 ); | ||
173 | sio << Fmt(5) << iPos << ": "; | ||
174 | iPos += sAmnt; | ||
175 | for( size_t j = 0; j < sAmnt; j++ ) | ||
176 | { | ||
177 | sio << Fmt::hex(2) << (int)((unsigned char)buf[j]) | ||
178 | << " "; | ||
179 | } | ||
180 | for( size_t j = sAmnt; j < 8; j++ ) | ||
181 | { | ||
182 | sio << "-- "; | ||
183 | } | ||
184 | sio << "| "; | ||
185 | for( size_t j = 0; j < sAmnt; j++ ) | ||
186 | { | ||
187 | if( buf[j] >= 32 && buf[j] <= 126 ) | ||
188 | sio << buf[j] << " "; | ||
189 | else | ||
190 | sio << " "; | ||
191 | } | ||
192 | sio << sio.nl; | ||
193 | } | ||
194 | sio << "Position: " << s.tell() << ", isEos()=" << s.isEos() | ||
195 | << sio.nl; | ||
196 | } | ||
197 | break; | ||
198 | |||
199 | case modeStreamPut: | ||
200 | if( !opts.sFile.isSet() ) | ||
201 | { | ||
202 | sio << "Please specify a file manipulate." << sio.nl; | ||
203 | return 0; | ||
204 | } | ||
205 | else if( !opts.sSrc.isSet() ) | ||
206 | { | ||
207 | sio << "Please specify a source file to read." << sio.nl; | ||
208 | } | ||
209 | else | ||
210 | { | ||
211 | File fOut( opts.sFile, File::Write|File::Read ); | ||
212 | Myriad m( fOut ); | ||
213 | MyriadStream sOut = m.create( | ||
214 | Bu::Myriad::WriteNew, opts.iPreallocate | ||
215 | ); | ||
216 | File fIn( opts.sSrc, File::Read ); | ||
217 | char buf[1024]; | ||
218 | while( !fIn.isEos() ) | ||
219 | { | ||
220 | sOut.write( buf, fIn.read( buf, 1024 ) ); | ||
221 | } | ||
222 | } | ||
223 | break; | ||
224 | |||
225 | case modeStreamGet: | ||
226 | if( !opts.sFile.isSet() ) | ||
227 | { | ||
228 | sio << "Please specify a file manipulate." << sio.nl; | ||
229 | return 0; | ||
230 | } | ||
231 | else if( !opts.sDst.isSet() ) | ||
232 | { | ||
233 | sio << "Please specify a destination file to write." << sio.nl; | ||
234 | } | ||
235 | else | ||
236 | { | ||
237 | File fIn( opts.sFile, File::Write|File::Read ); | ||
238 | Myriad m( fIn ); | ||
239 | MyriadStream sIn = m.open( opts.iStream, Bu::Myriad::Read ); | ||
240 | File fOut( opts.sDst, File::Write|File::Create|File::Truncate ); | ||
241 | char buf[1024]; | ||
242 | while( !sIn.isEos() ) | ||
243 | { | ||
244 | fOut.write( buf, sIn.read( buf, 1024 ) ); | ||
245 | } | ||
246 | } | ||
247 | break; | ||
248 | |||
249 | case modeBlockMap: | ||
250 | if( !opts.sFile.isSet() ) | ||
251 | { | ||
252 | sio << "Please specify a file manipulate." << sio.nl; | ||
253 | return 0; | ||
254 | } | ||
255 | { | ||
256 | File fIn( opts.sFile, File::Write|File::Read ); | ||
257 | Myriad m( fIn ); | ||
258 | /* Bu::BitString bs = m.getBlocksUsed(); | ||
259 | for( int j = 0; j < bs.getSize(); j++ ) | ||
260 | { | ||
261 | if( j>0 && (j%50) == 0 ) | ||
262 | Bu::println(""); | ||
263 | if( bs.getBit( j ) ) | ||
264 | Bu::print("#"); | ||
265 | else | ||
266 | Bu::print("-"); | ||
267 | }*/ | ||
268 | Bu::println("\n"); | ||
269 | } | ||
270 | break; | ||
271 | |||
272 | case modeNone: | ||
273 | sio << "Please select a mode, for more info, try --help." | ||
274 | << sio.nl << sio.nl; | ||
275 | break; | ||
276 | } | ||
277 | |||
278 | return 0; | ||
279 | } | ||
280 | |||