Declarative pure Ruby equivalent of C/C++ structs.
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 unnecessary freezing removed Mar 3, 2011
.document rakefile, gemfile and jeweler files Mar 2, 2011
.gitignore
Gemfile
Gemfile.lock
LICENSE.txt initial commit Mar 1, 2011
README.md
Rakefile rakefile, gemfile and jeweler files Mar 2, 2011
VERSION
struct-fx.gemspec Regenerate gemspec for version 0.1.1 Mar 9, 2011
test
test.gif

README.md

StructFx

struct-fx provides declarative pure Ruby equivalent of C++ structs. Declarations are simple:

gif = StructFx::new(data) do
    string (:format) {3}
    string (:version) {3}
    uint16 :width
    uint16 :height
end

See whole real live example:

require "struct-fx"

# Read and analyze file

File.open("./test.gif", "rb") do |io|
    data = io.read(10)
end

gif = StructFx::new(data) do
    string (:format) {3}
    string (:version) {3}
    uint16 :width
    uint16 :height
end

# Get results

p gif.length        # declared struct length is really 10 bytes

p gif.data.format   # "GIF"
p gif.data.version  # "89a"
p gif.data.width    # 32
p gif.data.height   # 32    # it's square :-)

# Change the values

gif.data.version = "87a"
gif.data.width = 128
gif.data.height = 128

gif.data.to_s       # "GIF87a\x80\x00\x80\x00"

Supported data types are:

  • signed and unsigned integers (1 to 8 bytes),
  • fixed-size strings (specified length),
  • floats and doubles (4 and 8 bytes),
  • single chars (1 byte),
  • packed bytes and flag arrays (1 byte),
  • skipped data blocks.

More detailed documentation is available in StructFx#declare method documentation and documentation of the BitPacker library.

Contributing

  1. Fork it.
  2. Create a branch (git checkout -b 20101220-my-change).
  3. Commit your changes (git commit -am "Added something").
  4. Push to the branch (git push origin 20101220-my-change).
  5. Create an Issue with a link to your branch.
  6. Enjoy a refreshing Diet Coke and wait.

Copyright

Copyright © 2011 Martin Kozák. See LICENSE.txt for further details.