-
Notifications
You must be signed in to change notification settings - Fork 46
/
messageScanner.go
53 lines (47 loc) · 923 Bytes
/
messageScanner.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
package golevel7
import (
"bufio"
"golevel7/commons"
"io"
)
type MessageScanner struct {
r io.Reader
b *bufio.Scanner
thisMsg *Message
err error
}
// NewMessageScanner returns a new scanner that returns
// hl7 messages from an io.Reader
func NewMessageScanner(r io.Reader) *MessageScanner {
ms := &MessageScanner{
r: r,
b: commons.NewBufScanner(r),
}
return ms
}
func (ms *MessageScanner) Scan() (gotOne bool) {
if scan := ms.b.Scan(); scan {
if ms.err = ms.b.Err(); ms.err != nil || len(ms.b.Bytes()) < 5 {
if ms.b.Bytes() != nil && !(len(ms.b.Bytes()) < 5) {
gotOne = true
}
} else {
gotOne = true
}
if gotOne {
ms.thisMsg = NewMessage(ms.b.Bytes())
} else {
ms.thisMsg = nil
}
}
if !gotOne {
ms.b = nil
}
return gotOne
}
func (ms *MessageScanner) Message() *Message {
return ms.thisMsg
}
func (ms *MessageScanner) Err() error {
return ms.err
}