-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.go
55 lines (47 loc) · 936 Bytes
/
parse.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
package betterpath
import (
"bufio"
"encoding/csv"
"errors"
"io"
"os"
"strconv"
)
// Parse checks the data format and stores points
func (points *Points) Parse() error {
if len(os.Args) < 2 {
return errors.New("No file provided")
}
csvFile, err := os.Open(os.Args[1])
if err != nil {
return errors.New("Open error")
}
reader := csv.NewReader(bufio.NewReader(csvFile))
for {
line, err := reader.Read()
if err == io.EOF {
break
} else if err != nil {
return errors.New("Read error")
}
a, err := strconv.ParseFloat(line[0], 64)
if err != nil {
return errors.New("Wrong data format")
}
b, err := strconv.ParseFloat(line[1], 64)
if err != nil {
return errors.New("Wrong data format")
}
c, err := strconv.ParseInt(line[2], 10, 32)
if err != nil {
return errors.New("Wrong data format")
}
*points = append(*points, Point{
x: a,
y: b,
t: c,
},
)
}
return nil
}