forked from adrianmo/go-nmea
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.go
167 lines (153 loc) · 3.57 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package nmea
import (
"fmt"
"strconv"
)
// parser provides a simple way of accessing and parsing
// sentence fields
type parser struct {
BaseSentence
prefix string
err error
}
// newParser constructor
func newParser(s BaseSentence, prefix string) *parser {
p := &parser{BaseSentence: s, prefix: prefix}
if p.Type != prefix {
p.SetErr("prefix", p.Type)
}
return p
}
// Err returns the first error encountered during the parser's usage.
func (p *parser) Err() error {
return p.err
}
// SetErr assigns an error. Calling this method has no
// effect if there is already an error.
func (p *parser) SetErr(context, value string) {
if p.err == nil {
p.err = fmt.Errorf("nmea: %s invalid %s: %s", p.prefix, context, value)
}
}
// String returns the field value at the specified index.
func (p *parser) String(i int, context string) string {
if p.err != nil {
return ""
}
if i < 0 || i >= len(p.Fields) {
p.SetErr(context, "index out of range")
return ""
}
return p.Fields[i]
}
// EnumString returns the field value at the specified index.
// An error occurs if the value is not one of the options and not empty.
func (p *parser) EnumString(i int, context string, options ...string) string {
s := p.String(i, context)
if p.err != nil || s == "" {
return ""
}
for _, o := range options {
if o == s {
return s
}
}
p.SetErr(context, s)
return ""
}
// EnumChars returns an array of strings that are matched in the Mode field.
// It will only match the number of characters that are in the Mode field.
// If the value is empty, it will return an empty array
func (p *parser) EnumChars(i int, context string, options ...string) []string {
s := p.String(i, context)
if p.err != nil || s == "" {
return []string{}
}
strs := []string{}
for _, r := range s {
rs := string(r)
for _, o := range options {
if o == rs {
strs = append(strs, o)
break
}
}
}
if len(strs) != len(s) {
p.SetErr(context, s)
return []string{}
}
return strs
}
// Int64 returns the int64 value at the specified index.
// If the value is an empty string, 0 is returned.
func (p *parser) Int64(i int, context string) int64 {
s := p.String(i, context)
if p.err != nil {
return 0
}
if s == "" {
return 0
}
v, err := strconv.ParseInt(s, 10, 64)
if err != nil {
p.SetErr(context, s)
}
return v
}
// Float64 returns the float64 value at the specified index.
// If the value is an empty string, 0 is returned.
func (p *parser) Float64(i int, context string) float64 {
s := p.String(i, context)
if p.err != nil {
return 0
}
if s == "" {
return 0
}
v, err := strconv.ParseFloat(s, 64)
if err != nil {
p.SetErr(context, s)
}
return v
}
// Time returns the Time value at the specified index.
// If the value is empty, the Time is marked as invalid.
func (p *parser) Time(i int, context string) Time {
s := p.String(i, context)
if p.err != nil {
return Time{}
}
v, err := ParseTime(s)
if err != nil {
p.SetErr(context, s)
}
return v
}
// Date returns the Date value at the specified index.
// If the value is empty, the Date is marked as invalid.
func (p *parser) Date(i int, context string) Date {
s := p.String(i, context)
if p.err != nil {
return Date{}
}
v, err := ParseDate(s)
if err != nil {
p.SetErr(context, s)
}
return v
}
// LatLong returns the coordinate value of the specified fields.
func (p *parser) LatLong(i, j int, context string) float64 {
a := p.String(i, context)
b := p.String(j, context)
if p.err != nil {
return 0
}
s := fmt.Sprintf("%s %s", a, b)
v, err := ParseLatLong(s)
if err != nil {
p.SetErr(context, err.Error())
}
return v
}