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/_primary_type.cr
binary_parser/macros/array.cr
binary_parser/macros/endian.cr
binary_parser/macros/int16.cr
binary_parser/macros/int32.cr
binary_parser/macros/int64.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/uint64.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, format : IO::ByteFormat = IO::ByteFormat::SystemEndian)
Load from file with
filename
-
#load(io : IO, format : IO::ByteFormat = IO::ByteFormat::SystemEndian)
Load from an IO object
-
#save(filename : String, format : IO::ByteFormat = IO::ByteFormat::SystemEndian)
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, format : IO::ByteFormat = IO::ByteFormat::SystemEndian)
Write to an IO object
Macro Summary
-
array(name, opt)
Declare an array field
-
endian(type)
Define what endian to use.
-
int16(name)
Declare a int16 field
-
int32(name)
Declare a int32 field
-
int64(name)
Declare a int64 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
-
uint64(name)
Declare a uint64 field
-
uint8(name)
Declare a uint8 field
Class Method Detail
Instance Method Detail
Load from file with filename
Load from an IO object
Save to file with filename
Write to an IO object
Macro Detail
Declare an array field
Argument:
- name: Field name
- opt: Options
:type
: Element type, must respond to.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
Define what endian to use. endian
can be either :little
or :big
This macro should be place at the top of class, and can only call zero or one time.
endian
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
Nested BinaryParser
Argument:
- name: Field name
- klass: Another BinaryParser
Example:
class InnerParser < BinaryParser
uint8 :foo
end
class Parser < BinaryParser
type :inner, InnerParser
end