【发布时间】:2016-06-08 03:05:45
【问题描述】:
所以我有这样的记录:
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 等于某个值时,我有没有办法在读取 :name_len 和 :name 后停止读取记录?或者是不是一旦你开始读取带有 Record 的文件,它就必须一直运行?
我知道我可以使用 :onlyif 跳过所有剩余的内容,但是有没有办法将 :onlyif 放在之后的所有内容上?你能在一个选项上加上 :onlyif 吗?
我试过的代码:
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
我还尝试将 :name_len 和 :name 下面的所有内容放在它自己的记录中并这样做:
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 Record 放在上面(它必须是因为 Property 需要知道 PropertyData 是什么)然后它会出错,因为 PropertyData 需要知道 Property 是什么(因为它填充了数组与那种类型)。他们没有办法知道彼此的存在,因为他们都互相使用。
【问题讨论】:
-
当您尝试
:onlyif时发生了什么?您可以在choice中尝试skip。 -
我这样写了 :onlyif:
choice :data, :onlyif => :not_none?, selection: :type do但它错误地说无效的选择“”,因为由于相同的 only:if 而没有定义类型。跳过的问题是我不知道我想跳过这些字节,直到我读完它们,只有当 :name 等于某个值时。 -
请编辑您的问题以包含您尝试过的代码。