-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbinary_output.go
202 lines (167 loc) · 4.23 KB
/
binary_output.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
package osg
import (
"bufio"
"bytes"
"encoding/binary"
"github.com/flywave/go-osg/model"
)
type OsgOutputIterator interface {
IsBinary() bool
WriteBool(b bool)
WriteChar(c int8)
WriteUChar(c uint8)
WriteShort(s int16)
WriteUShort(us uint16)
WriteInt(i int32)
WriteUInt(i uint32)
WriteLong(l int64)
WriteULong(ul uint64)
WriteFloat(f float32)
WriteDouble(d float64)
WriteString(*string)
WriteGlenum(value *model.ObjectGlenum)
WriteProperty(prop *model.ObjectProperty)
WriteMark(mark *model.ObjectMark)
WriteCharArray([]byte)
WriteWrappedString(*string)
SetOutputSteam(os *OsgOstream)
SetSupportBinaryBrackets(sbb bool)
}
type OutputIterator struct {
RootStream *bufio.Writer
Out *bufio.Writer
OutputStream *OsgOstream
SupportBinaryBrackets bool
}
func NewOutputIterator(wt *bufio.Writer) *OutputIterator {
return &OutputIterator{SupportBinaryBrackets: false, Out: wt, RootStream: wt}
}
func (it *OutputIterator) IsBinary() bool {
return false
}
func (it *OutputIterator) SetSupportBinaryBrackets(sbb bool) {
it.SupportBinaryBrackets = sbb
}
func (it *OutputIterator) SetOutputSteam(os *OsgOstream) {
it.OutputStream = os
}
type MarkHelper struct {
Stream *bufio.Writer
buff *bytes.Buffer
}
func (mh *MarkHelper) GetBuff() []byte {
return mh.buff.Bytes()
}
func MakeMarkHelper() *MarkHelper {
mh := MarkHelper{}
mh.buff = bytes.NewBuffer([]byte{})
mh.Stream = bufio.NewWriter(mh.buff)
return &mh
}
type BinaryOutputIterator struct {
OutputIterator
helps []*MarkHelper
}
func NewBinaryOutputIterator(wt *bufio.Writer) *BinaryOutputIterator {
ot := NewOutputIterator(wt)
return &BinaryOutputIterator{OutputIterator: *ot}
}
func (it *BinaryOutputIterator) writerData(iter interface{}) {
binary.Write(it.Out, binary.LittleEndian, iter)
}
func (it *BinaryOutputIterator) WriteBool(b bool) {
it.writerData(b)
}
func (it *BinaryOutputIterator) WriteChar(val int8) {
it.writerData(val)
}
func (it *BinaryOutputIterator) WriteShort(val int16) {
it.writerData(val)
}
func (it *BinaryOutputIterator) WriteInt(val int32) {
it.writerData(val)
}
func (it *BinaryOutputIterator) WriteLong(val int64) {
it.writerData(val)
}
func (it *BinaryOutputIterator) WriteUChar(val uint8) {
it.writerData(val)
}
func (it *BinaryOutputIterator) WriteUShort(val uint16) {
it.writerData(val)
}
func (it *BinaryOutputIterator) WriteUInt(val uint32) {
it.writerData(val)
}
func (it *BinaryOutputIterator) WriteULong(val uint64) {
it.writerData(val)
}
func (it *BinaryOutputIterator) WriteFloat(val float32) {
it.writerData(val)
}
func (it *BinaryOutputIterator) WriteDouble(val float64) {
it.writerData(val)
}
func (it *BinaryOutputIterator) WriteString(val *string) {
str := *val
it.writerData([]byte(str))
}
func (it *BinaryOutputIterator) WriteGlenum(value *model.ObjectGlenum) {
it.writerData(value.Value)
}
func (it *BinaryOutputIterator) WriteProperty(value *model.ObjectProperty) {
if value.MapProperty {
it.writerData(value.Value)
}
}
func (it *BinaryOutputIterator) WriteMark(mark *model.ObjectMark) {
if it.SupportBinaryBrackets {
if it.OutputStream != nil && it.OutputStream.FileVersion > 148 {
if mark.Name == "{" {
mh := MakeMarkHelper()
it.Out = mh.Stream
it.helps = append(it.helps, mh)
return
} else if mark.Name == "{" && len(it.helps) > 0 {
size := len(it.helps)
if size > 1 {
it.Out = it.helps[size-2].Stream
} else {
it.Out = it.RootStream
}
mh := it.helps[size-1]
bf := mh.GetBuff()
sz := uint64(len(bf))
it.WriteULong(sz)
it.WriteCharArray(bf)
it.helps = it.helps[:size-1]
}
} else {
if mark.Name == "{" {
mh := MakeMarkHelper()
it.Out = mh.Stream
it.helps = append(it.helps, mh)
return
} else if mark.Name == "{" && len(it.helps) > 0 {
size := len(it.helps)
if size > 1 {
it.Out = it.helps[size-2].Stream
} else {
it.Out = it.RootStream
}
mh := it.helps[size-1]
bf := mh.GetBuff()
sz := int32(len(bf))
it.WriteInt(sz)
it.WriteCharArray(bf)
it.helps = it.helps[:size-1]
}
}
}
}
func (it *BinaryOutputIterator) WriteCharArray(s []byte) {
it.writerData(s)
}
func (it *BinaryOutputIterator) WriteWrappedString(str *string) {
it.WriteString(str)
}