-
Notifications
You must be signed in to change notification settings - Fork 0
/
huffer.hs
39 lines (33 loc) · 1.46 KB
/
huffer.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
module Main where
import Encoder (encode)
import Decoder (decode, content)
import System.Environment
main :: IO ()
main = getArgs >>= execute
execute :: [String] -> IO ()
execute input = case input of
("encode":args) -> parseAndRun encode args "output.huf"
("decode":args) -> parseAndRun decode args "."
["content",fileName] -> content fileName
("help":_) -> mapM_ putStrLn helpLines
[] -> showInvalid "No action specified"
(action:_) -> showInvalid $ "Unknown action: " ++ action
parseAndRun :: (([String], String) -> IO()) -> [String] -> String -> IO()
parseAndRun action args defaultOut = case span (/="to") args of
([], _) -> showInvalid "No input file specified"
(inputs, []) -> action (inputs, defaultOut)
(inputs, ["to", out]) -> action (inputs, out)
_ -> showInvalid "You can specify one output file"
helpLines :: [String]
helpLines = [
"huffer is a compressor and decompressor based on canonical Huffman coding\n",
"run with: huffer action [inputs] (to output)\n",
"action can be 'encode', 'decode' or 'content'",
"you have to specify at least one input file (or folder) to encode",
"you can specify only an input file to decode or content",
"you can specify an output file for encoding (or folder for decoding)",
" if you don't, huffer will use 'output.huf' for encoding ('.' for decoding)"
]
showInvalid :: String -> IO ()
showInvalid err = mapM_ putStrLn ["Invalid input!", err,
"run: 'huffer help' for help"]