aboutsummaryrefslogtreecommitdiff
path: root/src/gziplike.nim
diff options
context:
space:
mode:
Diffstat (limited to 'src/gziplike.nim')
-rw-r--r--src/gziplike.nim56
1 files changed, 56 insertions, 0 deletions
diff --git a/src/gziplike.nim b/src/gziplike.nim
new file mode 100644
index 0000000..cf76f5e
--- /dev/null
+++ b/src/gziplike.nim
@@ -0,0 +1,56 @@
1# gzip-like LZSS compressor
2# Copyright (C) 2018 Pacien TRAN-GIRARD
3#
4# This program is free software: you can redistribute it and/or modify
5# it under the terms of the GNU Affero General Public License as
6# published by the Free Software Foundation, either version 3 of the
7# License, or (at your option) any later version.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12# GNU Affero General Public License for more details.
13#
14# You should have received a copy of the GNU Affero General Public License
15# along with this program. If not, see <https://www.gnu.org/licenses/>.
16
17import os, streams, sugar
18import bitio/bitreader, bitio/bitwriter, blocks/streamblock
19
20proc transform*(operation: (BitReader, BitWriter) -> void, input, output: string) =
21 let inputStream = openFileStream(input, fmRead)
22 defer: inputStream.close()
23 let outputStream = openFileStream(output, fmWrite)
24 defer: outputStream.close()
25 operation(inputStream.bitReader(), outputStream.bitWriter())
26
27proc compress*(bitReader: BitReader, bitWriter: BitWriter) =
28 while not bitReader.atEnd():
29 let streamBlock = streamblock.readRaw(bitReader)
30 streamBlock.writeSerialisedTo(bitWriter)
31 bitWriter.flush()
32
33proc decompress*(bitReader: BitReader, bitWriter: BitWriter) =
34 var hasMore = true
35 while hasMore:
36 let streamBlock = streamblock.readSerialised(bitReader)
37 streamBlock.writeRawTo(bitWriter)
38 hasMore = not streamBlock.isLast()
39 bitWriter.flush()
40
41proc printUsage(output: File) =
42 output.writeLine("Usage: " & paramStr(0) & " <compress|decompress> <input file> <output file>")
43
44when isMainModule:
45 if paramCount() != 3:
46 stderr.writeLine("Error: invalid argument count.")
47 printUsage(stderr)
48 quit(1)
49
50 case paramStr(1):
51 of "compress": compress.transform(paramStr(2), paramStr(3))
52 of "decompress": decompress.transform(paramStr(2), paramStr(3))
53 else:
54 stderr.writeLine("Error: invalid operation.")
55 printUsage(stderr)
56 quit(1)