-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_read.go
69 lines (57 loc) · 1.54 KB
/
file_read.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 mpq
import (
"fmt"
"io"
"github.com/Gophercraft/mpq/info"
)
var (
ErrReadPatchFile = fmt.Errorf("mpq: patch files not yet implemented")
)
func (file *File) Read(b []byte) (n int, err error) {
// cannot currently read patch files
if file.has_flag(info.FilePatchFile) {
err = ErrReadPatchFile
return
}
if len(b) == 0 {
return
}
for {
// If there is no reader, get the next sector
if file.sector_reader == nil {
file.sector_reader, err = file.read_next_sector()
if err != nil {
return
}
}
// read from the current sector
n, err = file.sector_reader.Read(b)
file.bytes_read += uint64(n)
if err == io.EOF {
// if the current sector has been read completely
// move to next sector
file.sector_reader = nil
file.sector_index++
// If this is the last sector offset, then we're truly out of data to read
if file.sector_index == file.sector_count {
// if we've reached the end of the file,
// the number of bytes read must be equal to the uncompressed file size
// if not, a serious error has occurred
if file.bytes_read != file.size {
err = fmt.Errorf("mpq: reached end of file '%s', but # of bytes read (%d) mismatches the size of the file (%d)", file.name, file.bytes_read, file.size)
}
return
}
// otherwise, we have more data
// so no error
err = nil
// Nop if no bytes were read, act as if nothing happened!
if n == 0 {
continue
}
// return n, nil
return
}
return
}
}