egra: agg mini code cleanups
[iv.d.git] / balztest / balzeos.d
blob2d6848669a92aeb045c6b25882be86d59e236240
1 module balzeos /*is aliced*/;
3 import iv.alice;
4 import iv.balz;
7 // ////////////////////////////////////////////////////////////////////////// //
8 string n2c (ulong n) {
9 import std.conv : to;
10 string t = to!string(n);
11 if (t.length < 4) return t;
12 string res;
13 while (t.length > 3) {
14 res = ","~t[$-3..$]~res;
15 t = t[0..$-3];
17 if (t.length) res = t~res; else res = res[1..$];
18 return res;
22 // ////////////////////////////////////////////////////////////////////////// //
23 version = show_progress;
25 int main (string[] args) {
26 import core.time;
27 import core.stdc.time;
28 import std.stdio;
30 if (args.length != 4) {
31 write(
32 "BALZ - A ROLZ-based file compressor, v1.20\n"~
33 "\n"~
34 "Usage: BALZ command infile outfile\n"~
35 "\n"~
36 "Commands:\n"~
37 " c|cx Compress (Normal|Maximum)\n"~
38 " d|x Decompress\n"
40 return 1;
43 auto fin = File(args[2]);
44 auto fout = File(args[3], "w");
46 if (args[1][0] == 'c') {
47 Balz bz;
48 writefln("Compressing: %s -> %s", args[2], args[3]);
49 long fsz = fin.size;
50 fout.rawWrite((&fsz)[0..1]);
51 version(show_progress) {
52 auto stt = MonoTime.currTime;
53 long totalRead = 0;
54 long totalWritten = 0;
55 long prevRead = 0, prevWritten = 0;
57 void progress () {
58 if (prevRead == 0 || prevWritten == 0 || totalRead-prevRead >= 1024*1024 || totalWritten-prevWritten >= 1024*1024) {
59 auto ctt = MonoTime.currTime;
60 if ((ctt-stt).total!"seconds" > 0) {
61 stt = ctt;
62 prevRead = totalRead;
63 prevWritten = totalWritten;
64 stdout.write("\r[", totalRead.n2c, "/", fsz.n2c, "] [", totalWritten.n2c, "]");
65 stdout.flush();
70 auto start = clock();
71 bz.compress(
72 // reader
73 (buf) {
74 auto res = fin.rawRead(buf[]);
75 version(show_progress) {
76 totalRead += res.length;
77 progress();
79 return cast(uint)res.length;
81 // writer
82 (buf) {
83 fout.rawWrite(buf[]);
84 version(show_progress) {
85 totalWritten += buf.length;
86 progress();
89 // mode
90 args[1].length > 1 && args[1][1] == 'x'
92 writefln("\r%s -> %s in %.3fs\x1b[K", fin.size.n2c, fout.size.n2c, double(clock()-start)/CLOCKS_PER_SEC);
93 } else if (args[1][0] == 'd' || args[1][0] == 'x') {
94 Unbalz bz;
95 writefln("Decompressing: %s -> %s", args[2], args[3]);
96 long fsz;
97 fin.rawRead((&fsz)[0..1]);
98 auto start = clock();
99 //auto stt = MonoTime.currTime;
100 auto dc = bz.decompress(
101 // reader
102 (buf) { auto res = fin.rawRead(buf[]); return cast(uint)res.length; },
103 // writer
104 (buf) { fout.rawWrite(buf[]); },
106 if (dc != fsz) writeln("INVALID STREAM SIZE: got ", dc, " but expected ", fsz);
107 writefln("%s -> %s in %.3fs", fin.size.n2c, fout.size.n2c, double(clock()-start)/CLOCKS_PER_SEC);
108 } else {
109 writefln("Unknown command: %s", args[1]);
110 return 1;
113 return 0;