-
Notifications
You must be signed in to change notification settings - Fork 23
/
read_ascii.go
163 lines (151 loc) · 3.25 KB
/
read_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
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
package main
import (
"bufio"
"encoding/hex"
"fmt"
"io"
"log"
"strconv"
"strings"
)
func grab_value(line string) string {
val := strings.Split(line, " ")[1]
return val
}
func extract_sep(line string) string {
sep := grab_value(line)
sepchar, err := hex.DecodeString(sep[2:])
if err != nil {
log.Panic(err)
}
return string(sepchar)
}
type BroAsciiReader struct {
r io.Reader
br *bufio.Reader
sep string
fields []string
fieldsMap map[string]int
types []string
timeFields map[int]bool
newHeaders bool
}
type ASCIIRecord struct {
line *string
cols *[]string
fields *map[string]int
err error
}
func (r *ASCIIRecord) String() string {
return *r.line
}
func (r *ASCIIRecord) GetString(field string) string {
idx, ok := (*r.fields)[field]
if !ok {
r.err = fmt.Errorf("Invalid field %s", field)
return ""
}
return (*r.cols)[idx]
}
func (r *ASCIIRecord) GetTimestamp(field string) string {
return r.GetString(field)
}
func (r *ASCIIRecord) GetStringList(field string) []string {
raw := r.GetString(field)
spl := strings.Split(raw, ",")
return spl
}
func (r *ASCIIRecord) GetStringByIndex(index int) string {
return (*r.cols)[index]
}
func (r *ASCIIRecord) GetFloat(field string) float64 {
idx, ok := (*r.fields)[field]
if !ok {
r.err = fmt.Errorf("Invalid field %s", field)
return 0.0
}
val := (*r.cols)[idx]
fl, err := strconv.ParseFloat(val, 64)
if err != nil {
panic(err)
}
return fl
}
func (r *ASCIIRecord) GetFloatByIndex(index int) float64 {
val := (*r.cols)[index]
fl, err := strconv.ParseFloat(val, 64)
if err != nil {
panic(err)
}
return fl
}
func (r *ASCIIRecord) IsMissingFieldError() bool {
//TODO: handle here or jsut skip in Next?
return false
}
func (r *ASCIIRecord) Error() error {
if r.err != nil {
return fmt.Errorf("Error parsing %s: %w", r, r.err)
}
return nil
}
func (r *ASCIIRecord) GetFieldIndex(field string) int {
idx, ok := (*r.fields)[field]
if ok {
return idx
}
r.err = fmt.Errorf("Invalid field %s", field)
return -1
}
func NewBroAsciiReader(r io.Reader) *BroAsciiReader {
br := bufio.NewReader(r)
tf := make(map[int]bool)
return &BroAsciiReader{r: r, br: br, timeFields: tf}
}
func (b *BroAsciiReader) Next() (Record, error) {
line, err := b.br.ReadString('\n')
if err == io.EOF {
return nil, nil
}
if err != nil {
return nil, err
}
line = strings.Trim(line, "\n")
if strings.HasPrefix(line, "#") {
b.handleHeader(line)
return b.Next()
}
parts := strings.Split(line, "\t")
rec := ASCIIRecord{
line: &line,
cols: &parts,
fields: &b.fieldsMap,
}
return &rec, nil
}
func (b *BroAsciiReader) handleHeader(line string) error {
b.newHeaders = true
if strings.HasPrefix(line, "#separator") {
b.sep = extract_sep(line)
} else if strings.HasPrefix(line, "#fields") {
b.fields = strings.Split(line, "\t")[1:]
b.fieldsMap = make(map[string]int)
for idx, f := range b.fields {
b.fieldsMap[f] = idx
}
} else if strings.HasPrefix(line, "#types") {
b.types = strings.Split(line, "\t")[1:]
for idx, typ := range b.types {
if typ == "time" {
b.timeFields[idx] = true
}
}
}
return nil
}
func (b *BroAsciiReader) HeadersChanged() bool {
return b.newHeaders
}
func (b *BroAsciiReader) HandledHeaders() {
b.newHeaders = false
}