forked from adrianmo/go-nmea
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
232 lines (206 loc) · 5.05 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package nmea
import (
"fmt"
"strconv"
)
// Parser provides a simple way of accessing and parsing
// sentence fields
type Parser struct {
BaseSentence
err error
}
// NewParser constructor
func NewParser(s BaseSentence) *Parser {
return &Parser{BaseSentence: s}
}
// AssertType makes sure the sentence's type matches the provided one.
func (p *Parser) AssertType(typ string) {
if p.Type != typ {
p.SetErr("type", p.Type)
}
}
// 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]
}
// ListString returns a list of all fields from the given start index.
// An error occurs if there is no fields after the given start index.
func (p *Parser) ListString(from int, context string) (list []string) {
if p.err != nil {
return []string{}
}
if from < 0 || from >= len(p.Fields) {
p.SetErr(context, "index out of range")
return []string{}
}
return append(list, p.Fields[from:]...)
}
// 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())
}
if (b == North || b == South) && (v < -90.0 || 90.0 < v) {
p.SetErr(context, "latitude is not in range (-90, 90)")
return 0
} else if (b == West || b == East) && (v < -180.0 || 180.0 < v) {
p.SetErr(context, "longitude is not in range (-180, 180)")
return 0
}
return v
}
// SixBitASCIIArmour decodes the 6-bit ascii armor used for VDM and VDO messages
func (p *Parser) SixBitASCIIArmour(i int, fillBits int, context string) []byte {
if p.err != nil {
return nil
}
if fillBits < 0 || fillBits >= 6 {
p.SetErr(context, "fill bits")
return nil
}
payload := []byte(p.String(i, "encoded payload"))
numBits := len(payload)*6 - fillBits
if numBits < 0 {
p.SetErr(context, "num bits")
return nil
}
result := make([]byte, numBits)
resultIndex := 0
for _, v := range payload {
if v < 48 || v >= 120 {
p.SetErr(context, "data byte")
return nil
}
d := v - 48
if d > 40 {
d -= 8
}
for i := 5; i >= 0 && resultIndex < len(result); i-- {
result[resultIndex] = (d >> uint(i)) & 1
resultIndex++
}
}
return result
}