-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
98 lines (74 loc) · 1.46 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package main
import (
"fmt"
"os"
"github.com/docopt/docopt-go"
)
const appVersion = "0.2"
const appVersionString = "wavdump " + appVersion
const usage = `
The wavdump tool shows the header structure of WAV or WAVE64 files.
Usage:
wavdump <input_files>...
Options:
-h --help Show this screen.
--version Show version of the program.
`
type cmdArgs struct {
Input_files []string
}
func dataToStr(data []byte) string {
if data == nil {
return ""
}
res := ""
for i, b := range data {
if i > 0 && i%4 == 0 {
res += " "
}
res += fmt.Sprintf("%02x ", b)
}
return res
}
func printLines(lines []Line) error {
for _, line := range lines {
if line.data == nil {
fmt.Println()
continue
}
dataStr := dataToStr(line.data)
value := line.value
if value == nil {
value = ""
}
fmt.Printf("%08x", line.address)
fmt.Printf("%57s", dataStr)
fmt.Printf("%15v", value)
fmt.Printf(" %s\n", line.description)
}
return nil
}
func main() {
args := cmdArgs{}
parser := docopt.Parser{}
opts, _ := parser.ParseArgs(usage, os.Args[1:], appVersionString)
if err := opts.Bind(&args); err != nil {
fmt.Println(err)
parser.HelpHandler(err, usage)
os.Exit(1)
}
res := 0
for _, file := range args.Input_files {
fmt.Println("\n", file)
parser := Parser{}
err := parser.parse(file)
if err == nil {
err = printLines(parser.lines)
}
if err != nil {
fmt.Println(err)
res = 2
}
}
os.Exit(res)
}