some updates
[iv.d.git] / sr3csample / sr3ctest.d
blobb951ae90d8248c3455ef01f5b792c7e10ff1ff1c
1 /* SR3C, a symbol ranking data compressor.
3 * This file implements a fast and effective data compressor.
4 * The compression is on par (k8: i guess ;-) to gzip -7.
5 * bzip2 -2 compresses slightly better than SR3C, but takes almost
6 * three times as long. Furthermore, since bzip2 is based on
7 * Burrows-Wheeler block sorting, it can't be used in on-line
8 * compression tasks.
9 * Memory consumption of SR3C is currently around 4.5 MB per ongoing
10 * compression and decompression.
12 * Author: Kenneth Oksanen <cessu@iki.fi>, 2008.
13 * Copyright (C) Helsinki University of Technology.
14 * D conversion by Ketmar // Invisible Vector <ketmar@ketmar.no-ip.org>
16 * This code borrows many ideas and some paragraphs of comments from
17 * Matt Mahoney's s symbol ranking compression program SR2 and Peter
18 * Fenwicks SRANK, but otherwise all code has been implemented from
19 * scratch.
21 * This file is distributed under the following license:
23 * The MIT License
24 * Copyright (c) 2008 Helsinki University of Technology
25 * Permission is hereby granted, free of charge, to any person obtaining a copy
26 * of this software and associated documentation files (the "Software"), to deal
27 * in the Software without restriction, including without limitation the rights
28 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
29 * copies of the Software, and to permit persons to whom the Software is
30 * furnished to do so, subject to the following conditions:
31 * The above copyright notice and this permission notice shall be included in
32 * all copies or substantial portions of the Software.
33 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
39 * THE SOFTWARE.
41 import std.stdio;
42 import iv.sr3c;
45 void main (string[] args) {
46 assert(args.length == 4);
47 SR3C ctx;
49 ubyte[] idata;
52 auto fi = File(args[2]);
53 idata = new ubyte[](cast(uint)fi.size);
54 fi.rawRead(idata[]);
57 if (args[1] == "p") {
58 auto fo = File(args[3], "w");
59 ctx = new SR3C(
60 (const(void)[] bytes, bool flush) {
61 fo.rawWrite(bytes[]);
62 return 0;
63 });
64 auto res = ctx.compress(idata[]);
65 assert(res == 0);
66 res = ctx.flush();
67 } else if (args[1] == "u") {
68 auto fo = File(args[3], "w");
69 ctx = new SR3C(
70 (const(void)[] bytes, bool flush) {
71 fo.rawWrite(bytes[]);
72 return 0;
73 });
74 auto res = ctx.uncompress(idata[]);
75 assert(res == 0);