blob: 9d852b3b8e79eb5b7004d3989be6118f6194b7e0 [file] [log] [blame]
Leo Repp58b9f112021-11-22 11:57:47 +01001#!/usr/bin/env node
2
3var program = require('commander');
4var Bunzip = require('../');
5var fs = require('fs');
6
7program
8 .version(Bunzip.version)
9 .usage('[infile]')
10 .option('-m, --multistream',
11 'Read a multistream bzip2 file');
12program.on('--help', function() {
13 console.log(' If <infile> is omitted, reads from stdin.');
14});
15program.parse(process.argv);
16
17var makeInStream = function(in_fd) {
18 var stat = fs.fstatSync(in_fd);
19 var stream = {
20 buffer: new Buffer(4096),
21 totalPos: 0,
22 pos: 0,
23 end: 0,
24 _fillBuffer: function() {
25 this.end = fs.readSync(in_fd, this.buffer, 0, this.buffer.length);
26 this.pos = 0;
27 },
28 readByte: function() {
29 if (this.pos >= this.end) { this._fillBuffer(); }
30 if (this.pos < this.end) {
31 this.totalPos++;
32 return this.buffer[this.pos++];
33 }
34 return -1;
35 },
36 read: function(buffer, bufOffset, length) {
37 if (this.pos >= this.end) { this._fillBuffer(); }
38 var bytesRead = 0;
39 while (bytesRead < length && this.pos < this.end) {
40 buffer[bufOffset++] = this.buffer[this.pos++];
41 bytesRead++;
42 }
43 this.totalPos += bytesRead;
44 return bytesRead;
45 },
46 eof: function() {
47 if (this.pos >= this.end) { this._fillBuffer(); }
48 return !(this.pos < this.end);
49 }
50 };
51 if (stat.size) {
52 stream.size = stat.size;
53 }
54 return stream;
55};
56
57var in_fd = 0, close_in = function(){};
58if (program.args.length > 0) {
59 in_fd = fs.openSync(program.args.shift(), 'r');
60 close_in = function() { fs.closeSync(in_fd); };
61}
62
63var inStream = makeInStream(in_fd);
64
65var report = function(position, blocksize) {
66 console.log(position+'\t'+blocksize);
67};
68
69Bunzip.table(inStream, report, program.multistream);
70close_in();
71return 0;