-
Notifications
You must be signed in to change notification settings - Fork 11
/
rawx12file.go
85 lines (79 loc) · 1.7 KB
/
rawx12file.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
package gox12
import (
"bufio"
//"bytes"
//"fmt"
"io"
"log"
"strings"
)
// X12 line reader and part delimiters
type rawX12FileReader struct {
reader *bufio.Reader
segmentTerm byte
elementTerm byte
subelementTerm byte
repetitionTerm byte
icvn string
}
type RawSegment struct {
Segment Segment
LineCount int
}
func NewRawX12FileReader(inFile io.Reader) (*rawX12FileReader, error) {
const isaLength = 106
r := new(rawX12FileReader)
r.reader = bufio.NewReader(inFile)
//buffer := bytes.NewBuffer(make([]byte, 0))
first, err := r.reader.Peek(isaLength)
if err != nil {
log.Fatal(err)
return nil, err
}
isa := string(first)
segTerm, eleTerm, subeleTerm, repTerm, icvn := getDelimiters(isa)
r.segmentTerm = segTerm
r.elementTerm = eleTerm
r.subelementTerm = subeleTerm
r.repetitionTerm = repTerm
r.icvn = icvn
return r, nil
}
func (r *rawX12FileReader) GetSegments() <-chan RawSegment {
ch := make(chan RawSegment)
ct := 0
go func() {
for {
row, err := r.reader.ReadString(r.segmentTerm)
if err == io.EOF {
break
} else if err != nil {
panic(err)
}
if strings.HasSuffix(row, string(r.segmentTerm)) {
row = row[:len(row)-1]
}
row = strings.Trim(row, "\r\n")
mySeg := NewSegment(row, r.elementTerm, r.subelementTerm, r.repetitionTerm)
ct++
seg := RawSegment{
mySeg,
ct,
}
ch <- seg
}
close(ch)
}()
return ch
}
// Get the X12 delimiters specified in the ISA segment
func getDelimiters(isa string) (segTerm byte, eleTerm byte, subeleTerm byte, repTerm byte, icvn string) {
segTerm = isa[len(isa)-1]
eleTerm = isa[3]
subeleTerm = isa[len(isa)-2]
icvn = isa[84:89]
if icvn == "00501" {
repTerm = isa[82]
}
return
}