Skip to content

Commit 8974a0f

Browse files
committed
feat: Init; #string ...
- Initialize all property - Add #string - Add #to_s to support File#write - Add write support for #array
1 parent 39e4ea4 commit 8974a0f

File tree

1 file changed

+48
-1
lines changed

1 file changed

+48
-1
lines changed

src/binary_parser.cr

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ class BinaryParser
1818
save(io)
1919
end
2020

21+
def to_s(io : IO)
22+
save(io)
23+
end
24+
2125
def save(io : IO)
2226
{% for method in @type.methods %}
2327
{% if method.name.starts_with?("_write_") %}
@@ -38,6 +42,7 @@ class BinaryParser
3842

3943
macro uint32(name)
4044
property! :{{name.id}}
45+
@{{name.id}} = 0u32
4146

4247
def _read_{{name.id}}(io : IO)
4348
@{{name.id}} = io.not_nil!.read_bytes(UInt32).as(UInt32)
@@ -50,6 +55,7 @@ class BinaryParser
5055

5156
macro uint16(name)
5257
property! :{{name.id}}
58+
@{{name.id}} = 0u16
5359

5460
def _read_{{name.id}}(io : IO)
5561
@{{name.id}} = io.not_nil!.read_bytes(UInt16).as(UInt16)
@@ -62,6 +68,7 @@ class BinaryParser
6268

6369
macro uint8(name)
6470
property! :{{name.id}}
71+
@{{name.id}} = 0u8
6572

6673
def _read_{{name.id}}(io : IO)
6774
@{{name.id}} = io.not_nil!.read_bytes(UInt8).as(UInt8)
@@ -74,6 +81,7 @@ class BinaryParser
7481

7582
macro int32(name)
7683
property! :{{name.id}}
84+
@{{name.id}} = 0i32
7785

7886
def _read_{{name.id}}(io : IO)
7987
@{{name.id}} = io.not_nil!.read_bytes(Int32).as(Int32)
@@ -86,6 +94,7 @@ class BinaryParser
8694

8795
macro int16(name)
8896
property! :{{name.id}}
97+
@{{name.id}} = 0i16
8998

9099
def _read_{{name.id}}(io : IO)
91100
@{{name.id}} = io.not_nil!.read_bytes(Int16).as(Int16)
@@ -98,6 +107,7 @@ class BinaryParser
98107

99108
macro int8(name)
100109
property! :{{name.id}}
110+
@{{name.id}} = 0i8
101111

102112
def _read_{{name.id}}(io : IO)
103113
@{{name.id}} = io.not_nil!.read_bytes(Int8).as(Int8)
@@ -110,6 +120,7 @@ class BinaryParser
110120

111121
macro char(name)
112122
property! :{{name.id}}
123+
@{{name.id}} = '\0'
113124

114125
def _read_{{name.id}}(io : IO)
115126
@{{name.id}} = io.not_nil!.read_char
@@ -118,18 +129,24 @@ class BinaryParser
118129

119130
macro type(name, klass)
120131
property! :{{name.id}}
132+
@{{name.id}} = {{klass}}.new
121133

122134
def _read_{{name.id}}(io : IO)
123135
{% raise "Must inhert BinaryParser" if @type >= klass.resolve %}
124136
@{{name.id}} = io.read_bytes({{klass}}).as({{klass}})
125137
end
138+
139+
def _write_{{name.id}}(io : IO)
140+
io.write_bytes(@{{name.id}}.not_nil!)
141+
end
126142
end
127143

128144
macro array(name, opt)
145+
{% raise "Must have count and type" unless opt[:type] && opt[:count] %}
129146
property! :{{name.id}}
147+
@{{name.id}} = [] of {{opt[:type]}}
130148

131149
def _read_{{name.id}}(io : IO)
132-
{% raise "Must have count or type" unless opt[:type] && opt[:count] %}
133150
{% if opt[:count].is_a?(NumberLiteral) %}
134151
@{{name.id}} = Array({{opt[:type]}}).new({{opt[:count]}}) do
135152
io.read_bytes({{opt[:type]}})
@@ -143,5 +160,35 @@ class BinaryParser
143160
# TODO: support :eof
144161
{% end %}
145162
end
163+
164+
def _write_{{name.id}}(io : IO)
165+
@{{name.id}}.not_nil!.each do |item|
166+
io.write_bytes(item)
167+
end
168+
end
169+
end
170+
171+
macro string(name, opt = { count: -1 })
172+
property! :{{name.id}}
173+
@{{name.id}} = ""
174+
175+
def _read_{{name.id}}(io : IO)
176+
{% if opt[:count] != -1 %}
177+
slice = io.read({{opt[:count]}})
178+
@{{name.id}} = String.new(slice)
179+
{% else %}
180+
@{{name.id}} = io.gets('\0')
181+
{% end %}
182+
end
183+
184+
def _write_{{name.id}}(io : IO)
185+
{% if opt[:count] != -1 %}
186+
slice = Slice(UInt8).new({{opt[:count]}})
187+
slice.copy_from(@{{name.id}}.not_nil!.to_slice)
188+
io.write(slice)
189+
{% else %}
190+
io.write(@{{name.id}}.not_nil!.to_slice)
191+
{% end %}
192+
end
146193
end
147194
end

0 commit comments

Comments
 (0)