/
abtfile.go
69 lines (50 loc) · 1.07 KB
/
abtfile.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
package abt
import (
bencode "code.google.com/p/bencode-go"
"io"
"os"
)
type ABTFile struct {
Sections SectionList
Source ABTFileSource
}
type SectionList []SectionData
type SectionData struct {
Path string
Origin string
Start int64
Size int64
Filesize int64
}
type ABTFileSource interface {
io.Reader
io.ReaderAt
}
func getSectionList(input io.Reader) (list SectionList, err error) {
list = make(SectionList, 0, 5)
err = bencode.Unmarshal(input, &list)
return
}
func NewABTFile(source ABTFileSource) (abtfile *ABTFile, err error) {
sectionList, err := getSectionList(source)
if err != nil {
return
}
abtfile = &ABTFile{sectionList, source}
return
}
func OpenABTFile(path *string) (abtfile *ABTFile, err error) {
file, err := os.Open(*path)
if err != nil {
return
}
abtfile, err = NewABTFile(file)
return
}
func (sections *SectionList) Write(target io.Writer) (err error) {
return bencode.Marshal(target, *sections)
}
func (abtfile *ABTFile) Write(target io.Writer) (err error) {
err = abtfile.Sections.Write(target)
return
}