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/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

Instance Method Summary

Macro Summary

Class Method Detail

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

Support for IO#read_bytes

NOTICE: Current not respect to IO::ByteFormat


[View source]

Instance Method Detail

def load(filename : String) #

Load from file with filename


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

Load from an IO object


[View source]
def save(filename : String) #

Save to file with filename


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

Support for IO#write_bytes

NOTICE: Current not respect to IO::ByteFormat


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

Convert to string

File.write("filename", parser)

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

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 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

[View source]
macro char(name) #

[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 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 uint8(name) #

Declare a uint8 field

uint8 :value # name of field

[View source]