Class CStruct
In: lib/cstruct/utils.rb
lib/cstruct/field.rb
lib/cstruct/cstruct.rb
Parent: Object

Description

CStruct is a simulation of the C language‘s struct. Its main purpose is to manipulate binary-data conveniently in Ruby.

Supported primitive types in CStruct

  • signed types: char,int8,int16,int32,int64,float,double
  • unsigned types: uchar,uint8,uint16,uint32,uint64

More infomantion

Methods

<<   __data__   __data__=   __reset__   align   data   data=   endian   fields   options   reset   size   struct   union  

Constants

VERSION = "1.0.1"   version

External Aliases

size -> __size__
unsigned_8byte -> uint64
unsigned_4byte -> uint32
unsigned_2byte -> uint16
unsigned_1byte -> uint8
unsigned_1byte -> uchar
signed_8byte -> int64
signed_4byte -> int32
signed_2byte -> int16
signed_1byte -> int8
signed_1byte -> char

Attributes

owner  [RW] 

Public Class methods

Return the align of a struct;the default is 1.

Return the endian of a struct;the default is :little.

Return the fields of a struct.

Return the size of a struct;size similar to sizeof in C language.

Example:

  class Point < CStruct
    int32:x
    int32:y
  end

  puts Point.size # or Point.__size__

Define a anonymous struct field.

Example:

in C:

  struct Window
  {
     int style;
     struct{
       int x;
       int y;
     }position;
  };

use CStruct in Ruby:

  class Window < CStruct
    int32:style
    struct :position do
      int32:x
      int32:y
    end
  end

Define a anonymous union field.

Example: in C:

  struct U
  {
     union{
       int x;
       int y;
     }value; /* values is anonymous union's instance */

};

use CStruct in Ruby:

  class U < CStruct
    union:value do
      int32:x
      int32:y
    end
  end

Public Instance methods

Assign to CStruct‘s instance.

__data__()

Alias for data

__data__=(bindata)

Alias for data=

__reset__()

Alias for reset

Return the data buffer of a CStruct‘s instance.

Assign to CStruct‘s instance.

Fill the date buffer of a CStruct‘s instance with zero.

[Validate]