-
Notifications
You must be signed in to change notification settings - Fork 7
/
compress.oc
63 lines (55 loc) · 1.69 KB
/
compress.oc
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import std::shift_args
import std::fs
import std::huffman
enum Mode {
Compress
Decompress
None
}
def usage(code: i32, full: bool) {
println("Usage:")
println(" ./huffman <-c|-d> -i <path> -o <path>")
if not full then std::exit(code)
println("--------------------------------------------------------")
println("Compile Options:")
println(" -i path Input path")
println(" -o path Output path")
println(" -c Compress")
println(" -d Decompress")
println(" -h Display this information")
std::exit(code)
}
let mode: Mode = None
let input: str = "/dev/stdin"
let output: str = "/dev/stdout"
def parse_args(argc: &i32, argv: &&str) {
while *argc > 0 {
let arg = shift_args(argc, argv)
match arg {
"-h" | "--help" => usage(code: 0, true)
"-i" | "--input" => input = shift_args(argc, argv)
"-o" | "--output" => output = shift_args(argc, argv)
"-c" | "--compress" => mode = Compress
"-d" | "--decompress" => mode = Decompress
else => {
println("Unknown option/argument: '%s'", arg)
usage(code: 1, true)
}
}
}
if mode == None {
println("Mode was not selected. Please choose -c or -d")
usage(code: 1, true)
}
}
def main(argc: i32, argv: &str) {
shift_args(&argc, &argv)
parse_args(&argc, &argv)
let file = fs::read_file_inc(input)
let res = match mode {
Compress => huffman::compress(&file)
Decompress => huffman::decompress(&file)
None => std::panic("unreachable")
}
fs::write_file(output, res)
}