Sha256: 6cac0b3b68eb11ca2c567411a1f0e35aa112b6330934bc87a5d47a97a270f080

Contents?: true

Size: 950 Bytes

Versions: 2

Compression:

Stored size: 950 Bytes

Contents

# frozen_string_literal: true

module DspBlueprintParser
  # class to manage the position of byte array while parsing data out of it
  class BinaryReader
    # @param data [Array<byte>]
    def initialize(data)
      @data = data
      @position = 0
    end

    def read_i32
      get_integer(4, 'l<')
    end

    def read_i16
      get_integer(2, 's<')
    end

    def read_i8
      get_integer(1, 'c')
    end

    def read_single
      get_integer(4, 'e')
    end

    private

    # @param byte_count [Integer]
    # @return [String]
    def get_string(byte_count)
      value = @data[@position..@position + byte_count].pack('C*').force_encoding('UTF-8')
      @position += byte_count

      value
    end

    # @param byte_count [Integer]
    # @param format [String]
    # @return [Integer]
    def get_integer(byte_count, format)
      get_string(byte_count).unpack1(format)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dsp_blueprint_parser-0.1.1 lib/dsp_blueprint_parser/binary_reader.rb
dsp_blueprint_parser-0.1.0 lib/dsp_blueprint_parser/binary_reader.rb