class BinaryParser

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.cr
binary_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

Instance Method Summary

Macro Summary

Class Method Detail

def self.from_io(io : IO, format : IO::ByteFormat) #

Support for IO#read_bytes


[View source]

Instance Method Detail

def load(filename : String, format : IO::ByteFormat = IO::ByteFormat::SystemEndian) #

Load from file with filename


[View source]
def load(io : IO, format : IO::ByteFormat = IO::ByteFormat::SystemEndian) #

Load from an IO object


[View source]
def save(filename : String, format : IO::ByteFormat = IO::ByteFormat::SystemEndian) #

Save to file with filename


[View source]
def to_io(io : IO, format : IO::ByteFormat) #

Support for IO#write_bytes


[View source]
def to_s(io : IO) #

Convert to string

File.write("filename", parser)

[View source]
def write(io : IO, format : IO::ByteFormat = IO::ByteFormat::SystemEndian) #

Write to an IO object


[View source]

Macro Detail

macro array(name, opt) #

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

[View source]
macro endian(type) #

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

[View source]
macro int16(name) #

Declare a int16 field

int16 :value # name of field

[View source]
macro int32(name) #

Declare a int32 field

int32 :value # name of field

[View source]
macro int64(name) #

Declare a int64 field

int64 :value # name of field

[View source]
macro int8(name) #

Declare a int8 field

int8 :value # name of field

[View source]
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

[View source]
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

[View source]
macro uint16(name) #

Declare a uint16 field

uint16 :value # name of field

[View source]
macro uint32(name) #

Declare a uint32 field

uint32 :value # name of field

[View source]
macro uint64(name) #

Declare a uint64 field

uint64 :value # name of field

[View source]
macro uint8(name) #

Declare a uint8 field

uint8 :value # name of field

[View source]