-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.go
135 lines (112 loc) · 2.52 KB
/
parser.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package main
import (
"encoding/binary"
"fmt"
"io"
"os"
)
type Line struct {
address int64
data []byte
value interface{}
description string
}
type Parser struct {
file *os.File
lines []Line
}
func NewParser(file *os.File) (Parser, error) {
res := Parser{
file: file,
lines: []Line{},
}
return res, nil
}
func (p *Parser) parse(fileName string) (err error) {
defer func() {
if e := recover(); e != nil {
err = fmt.Errorf("%v", e)
}
}()
p.file, err = os.Open(fileName)
if err != nil {
return err
}
defer p.file.Close()
magic := readBytes(p.file, 4)
_, err = p.file.Seek(0, io.SeekStart)
if err != nil {
return err
}
switch string(magic) {
case "RIFF":
p.readWav()
case "riff":
p.readWave64()
default:
return fmt.Errorf("file \"%s\" is corrupted or has an unsupported format", fileName)
}
return nil
}
func filePos(file *os.File) int64 {
res, err := file.Seek(0, io.SeekCurrent)
if err != nil {
panic(err)
}
return res
}
func readBytes(file *os.File, size uint) []byte {
var res = make([]byte, size)
_, err := file.Read(res)
if err != nil {
panic(err)
}
return res
}
func (p *Parser) addEmptyLine() {
p.lines = append(p.lines, Line{})
}
func (p *Parser) readFourCC(description string) string {
res := Line{}
res.address = filePos(p.file)
res.description = description
res.data = readBytes(p.file, 4)
res.value = string(res.data)
p.lines = append(p.lines, res)
return string(res.data)
}
func (p *Parser) readUInt16(description string) {
res := Line{}
res.address = filePos(p.file)
res.description = description
res.data = readBytes(p.file, 2)
res.value = binary.LittleEndian.Uint16(res.data)
p.lines = append(p.lines, res)
}
func (p *Parser) readUInt32(description string) uint32 {
res := Line{}
res.address = filePos(p.file)
res.description = description
res.data = readBytes(p.file, 4)
res.value = binary.LittleEndian.Uint32(res.data)
p.lines = append(p.lines, res)
return binary.LittleEndian.Uint32(res.data)
}
func (p *Parser) readUInt64(description string) uint64 {
res := Line{}
res.address = filePos(p.file)
res.description = description
res.data = readBytes(p.file, 8)
res.value = binary.LittleEndian.Uint64(res.data)
p.lines = append(p.lines, res)
return binary.LittleEndian.Uint64(res.data)
}
func (p *Parser) readBytes(size uint, description string) []byte {
res := Line{}
res.address = filePos(p.file)
res.description = description
res.data = readBytes(p.file, size)
res.value = nil
p.lines = append(p.lines, res)
return res.data
}