diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/main.nim | 40 |
1 files changed, 39 insertions, 1 deletions
diff --git a/src/main.nim b/src/main.nim index dbf331f..8f0536c 100644 --- a/src/main.nim +++ b/src/main.nim | |||
@@ -14,5 +14,43 @@ | |||
14 | # You should have received a copy of the GNU Affero General Public License | 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/>. | 15 | # along with this program. If not, see <https://www.gnu.org/licenses/>. |
16 | 16 | ||
17 | import os, streams, sugar | ||
18 | import bitreader, bitwriter, streamblock | ||
19 | |||
20 | proc 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 | |||
27 | proc compress(bitReader: BitReader, bitWriter: BitWriter) = | ||
28 | while not bitReader.atEnd(): | ||
29 | let streamBlock = streamblock.readRaw(bitReader) | ||
30 | streamBlock.writeSerialisedTo(bitWriter) | ||
31 | bitWriter.flush() | ||
32 | |||
33 | proc 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 | |||
41 | proc printUsage(output: File) = | ||
42 | output.writeLine("Usage: " & paramStr(0) & " <compress|decompress> <input file> <output file>") | ||
43 | |||
17 | when isMainModule: | 44 | when isMainModule: |
18 | echo("Hello, World!") | 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) | ||