package gps import ( "errors" "strconv" ) type Parser struct { fields []string EmptyField bool Err error } func (parser *Parser) setError(text string) { if parser.Err == nil { parser.Err = errors.New(text) } } func (parser *Parser) updateError(err error) { if err != nil { parser.Err = err } } func (parser *Parser) get(idx int, fallback string) string { if idx >= len(parser.fields) { parser.setError("Field out of range.") return fallback } else { value := parser.fields[idx] if value == "" { parser.EmptyField = true return fallback } else { return value } } } func (parser *Parser) parseInt(value string) int { f, err := strconv.ParseInt(value, 0, 0) parser.updateError(err) return int(f) } func (parser *Parser) parseFloat(value string) float64 { f, err := strconv.ParseFloat(value, 64) parser.updateError(err) return f } func (parser *Parser) parseFloat32(value string) float32 { f, err := strconv.ParseFloat(value, 32) parser.updateError(err) return float32(f) } func (parser *Parser) GetString(idx int) string { return parser.get(idx, "") } func (parser *Parser) GetInt(idx int) int { return parser.parseInt(parser.get(idx, "0")) } func (parser *Parser) GetFloat(idx int) float64 { value := parser.get(idx, "0") f, err := strconv.ParseFloat(value, 64) parser.updateError(err) return f } func (parser *Parser) GetFloat32(idx int) float32 { value := parser.get(idx, "0") f, err := strconv.ParseFloat(value, 32) parser.updateError(err) return float32(f) } func (parser *Parser) GetTime(idx int) float64 { value := parser.parseFloat(parser.GetString(idx)) hhmm := int(value / 100) hours, minutes := hhmm/100, hhmm%100 seconds := value - float64(hhmm*100) return float64(hours*3600+minutes*60) + seconds } func (parser *Parser) GetLatLong(idx int) float64 { value := parser.GetFloat(idx) dd := int(value / 100) mm := value - float64(dd*100) decimal := float64(dd) + mm/60 direction := parser.get(idx+1, "W") if direction == "W" || direction == "S" { decimal = -decimal } return decimal }