だから私はそのようなレコードを持っています:
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 レコードをこの上に配置すると (Property は PropertyData が何であるかを知る必要があるため)、PropertyData が Property が何であるかを知る必要があるため (その型で配列を埋めるため)、エラーが発生するため、これは機能しませんでした。 )。両者はお互いを使用しているため、両者がお互いの存在を知る方法はありません。