diff options
author | pacien | 2018-11-24 01:03:43 +0100 |
---|---|---|
committer | pacien | 2018-11-24 01:03:43 +0100 |
commit | 643d2d72fab23df30d29c10614bfa89648cd3655 (patch) | |
tree | 2894c609271737576e382055b399bc59580fbc8b | |
parent | 7c48f80fd8d81ec7c0b9e504174b05892248380a (diff) | |
download | gziplike-643d2d72fab23df30d29c10614bfa89648cd3655.tar.gz |
Implement program entry point
-rw-r--r-- | readme.md | 7 | ||||
-rw-r--r-- | src/main.nim | 40 |
2 files changed, 44 insertions, 3 deletions
@@ -17,10 +17,13 @@ Building this project requires Nim and Nimble version 0.19.0 or later. | |||
17 | Usage | 17 | Usage |
18 | ----- | 18 | ----- |
19 | 19 | ||
20 | ``` | ||
21 | Usage: gziplike <compress|decompress> <input file> <output file> | ||
22 | ``` | ||
23 | |||
20 | Compression and decompression are done using the `compress` and `decompress` commands repsectively. | 24 | Compression and decompression are done using the `compress` and `decompress` commands repsectively. |
21 | 25 | ||
22 | The input stream is read from the standard input. | 26 | Paths to the input and output files are given as second and third arguments respectively. |
23 | The result is outputted to the standard output. | ||
24 | 27 | ||
25 | 28 | ||
26 | License | 29 | License |
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) | ||