DataFormat is an (experimental) ruby DSL to describe binary data formats.
Ruby
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
lib
spec
LICENSE
README
Rakefile

README

== data-format

With the DataFormat DSL you can describe binary data formats:

it "should read an array" do
	stream = StringStream.create("str\0",2,4363,0.5,66584,-1677.5)

	d = DataFormat.description do
		string :name
		int :arr_length

		array(:things,length: arr_length) do
			int :thing_id
			float :value
		end
	end

	data = d.read_from(stream)

	data.name.should == "str"

	data.arr_length.should == 2
	data.things[0].thing_id.should == 4363
	data.things[0].value.should == 0.5

	data.things[1].thing_id.should == 66584
	data.things[1].value.should == -1677.5
end

The name string is null-terminated.

You can also do validation by specifying ranges:

it "simple format with options" do
	stream = StringStream.create("asdfghjk",2,0.225)

	d = DataFormat.description do
		string	:name, length: 8.bytes
		int			:int_value, range: 1..100
		float		:float_value, range: 0.0..1.0
	end

	data = d.read_from(stream)
	data.name.should == "asdfghjk"
	data.int_value.should == 2
	data.float_value.should == [0.225].pack("g").unpack("g").first
end