class BinaryParser
- BinaryParser
- Reference
- Object
Overview
BinaryParser for Crystal
class Parser < BinaryParser
uint8 :value
end
io = IO::Memory.new(sizeof(UInt8))
io.write_bytes(42)
io.rewind
parser = Parser.new.load(io)
parser.value # 42
Defined in:
binary_parser.crbinary_parser/byte_size.cr
binary_parser/macros/array.cr
binary_parser/macros/char.cr
binary_parser/macros/int16.cr
binary_parser/macros/int32.cr
binary_parser/macros/int8.cr
binary_parser/macros/string.cr
binary_parser/macros/type.cr
binary_parser/macros/uint16.cr
binary_parser/macros/uint32.cr
binary_parser/macros/uint8.cr
Class Method Summary
-
.from_io(io : IO, format : IO::ByteFormat)
Support for
IO#read_bytes
Instance Method Summary
-
#load(filename : String)
Load from file with
filename
-
#load(io : IO)
Load from an IO object
-
#save(filename : String)
Save to file with
filename
-
#to_io(io : IO, format : IO::ByteFormat)
Support for
IO#write_bytes
-
#to_s(io : IO)
Convert to string
-
#write(io : IO)
Write to an IO object
Macro Summary
-
array(name, opt)
Declare an array field
- char(name)
-
int16(name)
Declare a int16 field
-
int32(name)
Declare a int32 field
-
int8(name)
Declare a int8 field
-
string(name, opt = {count: -1})
Declare a string field
-
type(name, klass)
Nested BinaryParser
-
uint16(name)
Declare a uint16 field
-
uint32(name)
Declare a uint32 field
-
uint8(name)
Declare a uint8 field
Class Method Detail
def self.from_io(io : IO, format : IO::ByteFormat)
#
Support for IO#read_bytes
NOTICE: Current not respect to IO::ByteFormat
Instance Method Detail
def to_io(io : IO, format : IO::ByteFormat)
#
Support for IO#write_bytes
NOTICE: Current not respect to IO::ByteFormat
Macro Detail
macro array(name, opt)
#
Declare an array field
Argument:
- name: Field name
- opt: Options
:type
: Element type, must implement.from_io
:count
: Element size, can be a number for fixed size, or a symbol for variable size
Example:
# Fixed size
class Parser < BinaryParser
array :arr, { type: UInt8, count: 10 } # Array of 10 UInt8
end
# Variable size
class Parser < BinaryParser
uint32 :size
array :arr, { type: UInt8, count: :size }
end
macro string(name, opt = {count: -1})
#
Declare a string field
Argument:
- name: Field name
- opt: Options
:count
: Sting length, can be a number for fixed size,-1
for zero terminated, or a symbol for variable size
Example:
# Fixed size
class Parser < BinaryParser
string :str, { count: 10 } # Array of 10 UInt8
end
# Variable size
class Parser < BinaryParser
uint32 :size
string :str, { count: :size }
end
macro type(name, klass)
#
Nested BinaryParser
Argument:
- name: Field name
- klass: Another BinaryParser
Example:
class InnerParser < BinaryParser
uint8 :foo
end
class Parser < BinaryParser
type :inner, InnerParser
end