-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathreader_list_ascii.go
60 lines (49 loc) · 1.27 KB
/
reader_list_ascii.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
package ply
import (
"errors"
"strconv"
)
type listAsciiPropertyReader struct {
property ListProperty
lastReadListSize int32
buf []string
}
func (lpr *listAsciiPropertyReader) Read(line []string) (offset int, err error) {
v, err := strconv.ParseInt(line[0], 10, 32)
if err != nil {
return -1, err
}
lpr.lastReadListSize = int32(v)
// Resize to fit contents
if len(lpr.buf) < int(lpr.lastReadListSize) {
lpr.buf = make([]string, lpr.lastReadListSize)
}
copy(lpr.buf, line[1:lpr.lastReadListSize+1])
return int(lpr.lastReadListSize) + 1, err
}
func (lpr listAsciiPropertyReader) Float64(out []float64) (err error) {
if len(out) < int(lpr.lastReadListSize) {
return errors.New("can't fit property reader data in provided out slice")
}
for i := 0; i < int(lpr.lastReadListSize); i++ {
v, err := strconv.ParseFloat(lpr.buf[i], 64)
if err != nil {
return err
}
out[i] = v
}
return nil
}
func (lpr listAsciiPropertyReader) Int(out []int) error {
if len(out) < int(lpr.lastReadListSize) {
return errors.New("can't fit property reader data in provided out slice")
}
for i := 0; i < int(lpr.lastReadListSize); i++ {
v, err := strconv.ParseInt(lpr.buf[i], 10, 32)
if err != nil {
return err
}
out[i] = int(v)
}
return nil
}