aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpacien2018-11-24 01:03:43 +0100
committerpacien2018-11-24 01:03:43 +0100
commit643d2d72fab23df30d29c10614bfa89648cd3655 (patch)
tree2894c609271737576e382055b399bc59580fbc8b
parent7c48f80fd8d81ec7c0b9e504174b05892248380a (diff)
downloadgziplike-643d2d72fab23df30d29c10614bfa89648cd3655.tar.gz
Implement program entry point
-rw-r--r--readme.md7
-rw-r--r--src/main.nim40
2 files changed, 44 insertions, 3 deletions
diff --git a/readme.md b/readme.md
index 55d0546..be2587c 100644
--- a/readme.md
+++ b/readme.md
@@ -17,10 +17,13 @@ Building this project requires Nim and Nimble version 0.19.0 or later.
17Usage 17Usage
18----- 18-----
19 19
20```
21Usage: gziplike <compress|decompress> <input file> <output file>
22```
23
20Compression and decompression are done using the `compress` and `decompress` commands repsectively. 24Compression and decompression are done using the `compress` and `decompress` commands repsectively.
21 25
22The input stream is read from the standard input. 26Paths to the input and output files are given as second and third arguments respectively.
23The result is outputted to the standard output.
24 27
25 28
26License 29License
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
17import os, streams, sugar
18import bitreader, bitwriter, 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
17when isMainModule: 44when 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)