-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
45 lines (37 loc) · 1.01 KB
/
main.go
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
package main
import (
"flag"
"github.com/absolutezero000/encoding/huffman"
"log"
"os"
)
func main() {
inputFile := flag.String("i", "", "Input file name")
outputFile := flag.String("o", "", "Output file name")
verbose := flag.Bool("v", false, "Enable verbose mode")
decode := flag.Bool("d", false, "Decode mode")
flag.Parse()
data, err := os.ReadFile(*inputFile)
if err != nil {
log.Fatalf("Failed to read file %s: %v", *inputFile, err)
}
var outputData []byte
if *decode {
decodedData, err := huffman.DeserializeData(data, *verbose)
if err != nil {
log.Fatalf("Failed to deserialize data: %v", err)
}
os.Stdout.Write([]byte(decodedData))
outputData = []byte(decodedData)
} else {
outputData, err = huffman.SerializeData(string(data), *verbose)
if err != nil {
log.Fatalf("Failed to serialize data: %v", err)
}
}
if *outputFile != "" {
if err := os.WriteFile(*outputFile, outputData, 0600); err != nil {
log.Printf("Error writing to output file %s: %v", *outputFile, err)
}
}
}