使用BinData读取时跳过字节 [英] Skipping Bytes when reading with BinData

查看:103
本文介绍了使用BinData读取时跳过字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个这样的记录:

So I have a Record like such:

class Property < BinData::Record
    endian :little

    int32  :name_len
    string :name, read_length: :name_len

    # want to quit here.

    int32 :type_len
    string :type, read_length: :type_len
end

只有当:name等于某个值时,我有没有办法停止从Record读取:name_len和:name之后读取?或者是一旦您开始读取带有记录的文件,它就必须一直贯穿整个过程?

Is there a way for me to stop reading from the Record after it reads :name_len and :name, only if :name is equal to a certain value? Or is it the case that once you begin to read a file with a Record it must run all the way through?

我知道我可以使用:onlyif来跳过所有剩余的内容,但是有没有办法将:onlyif放在后面的所有内容上?您可以在选择中放入:onlyif吗?

I know I can use :onlyif to skip all the remaining, but is there a way to put the :onlyif on everything after? Can you put an :onlyif on a choice?

我尝试的代码:

class Property < BinData::Record
    endian :little

    int32  :name_len
    string :name, read_length: :name_len

    int32 :type_len, :onlyif => :not_none?
    string :type, read_length: :type_len, :onlyif => :not_none?

    int64 :data_len, :onlyif => :not_none?    
    choice :data, :onlyif => :not_none?, selection: :type do
        int32 "IntProperty\x00"

        float "FloatProperty\x00"

        struct "StrProperty\x00" do
            int32 :len
            string :data, read_length: :len
        end 

        struct "NameProperty\x00" do
            int32 :len
            string :data, read_length: :len
        end

        struct "ArrayProperty\x00" do
            int32 :num_items
            array :properties, type: :property, initial_length: :num_items
        end
    end

    def not_none?
        name != 'None\x00'
    end
end

我还尝试将所有内容放在自己的Record中的:name_len和:name下面,并执行以下操作:

I also tried putting everything below the :name_len and :name in it's own Record and doing this:

class Property < BinData::Record
    endian :little

    string_record :name

    property_data :p_data, :onlyif => :not_none?

    def not_none?
        name.data != 'None\x00'
    end 
end

但是这没有用,因为如果我将PropertyData记录放在其上方(这必须是因为Property需要知道什么PropertyData),然后它会出错,因为PropertyData需要知道什么是Property(因为它填充了数组)使用该类型).他们俩彼此使用,因此无法彼此知道对方的存在.

But this didn't work because if I put the PropertyData Record above this (which it has to be because Property needs to know what PropertyData is) then it errors because PropertyData needs to know what Property is (because it fills the array with that type). There is no way for them to both know each other exist since they both use one another.

推荐答案

[T]无效,因为如果我将PropertyData记录放在其上方(这必须是因为Property需要知道什么是PropertyData),否则它将出错,因为PropertyData需要知道什么是Property(因为它填充了)具有该类型的数组).

[T]his didn't work because if I put the PropertyData Record above this (which it has to be because Property needs to know what PropertyData is) then it errors because PropertyData needs to know what Property is (because it fills the array with that type).

您可以通过在Property之前声明PropertyData类来解决此问题,但是在之后进行填充:

You can solve this by declaring the PropertyData class before Property, but filling it out after:

class PropertyData < BinData::Record; end # Forward declaration

class Property < BinData::Record
  endian :little

  # ...
end

class PropertyData < BinData::Record
  endian :little

  # ...
end

您可以在 list.rb示例.

You can see the same thing happening in the list.rb example.

这篇关于使用BinData读取时跳过字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆