Class: CStruct
- Inherits:
-
Object
- Object
- CStruct
- Defined in:
- lib/cstruct.rb
Direct Known Subclasses
Defined Under Namespace
Modules: Utils
Constant Summary collapse
- VERSION =
'1.0.0'
Instance Attribute Summary collapse
-
#owner ⇒ Object
instance methods.
Class Method Summary collapse
- .align ⇒ Object
-
.double(*args) ⇒ Object
|symbol,dimension=nil|.
- .endian ⇒ Object
- .field_hash ⇒ Object
-
.float(*args) ⇒ Object
|symbol,dimension=nil|.
- .options(opt_hash) ⇒ Object
- .size ⇒ Object (also: __size__)
- .struct(symbol, &block) ⇒ Object
- .union(symbol, &block) ⇒ Object
Instance Method Summary collapse
- #<<(bindata) ⇒ Object
- #data ⇒ Object (also: #__data__)
- #data=(bindata) ⇒ Object (also: #__data__=)
-
#initialize {|_self| ... } ⇒ CStruct
constructor
A new instance of CStruct.
- #reset ⇒ Object (also: #__reset__)
- #sync_to_owner ⇒ Object
Constructor Details
#initialize {|_self| ... } ⇒ CStruct
Returns a new instance of CStruct.
309 310 311 312 313 314 315 |
# File 'lib/cstruct.rb', line 309 def initialize @data = "\0"*self.class.size @owner = [] @data.encode!("BINARY") if RUBY_VERSION >= '1.9' yield self if block_given? end |
Instance Attribute Details
#owner ⇒ Object
instance methods
308 309 310 |
# File 'lib/cstruct.rb', line 308 def owner @owner end |
Class Method Details
.align ⇒ Object
49 50 51 |
# File 'lib/cstruct.rb', line 49 def self.align @align end |
.double(*args) ⇒ Object
|symbol,dimension=nil|
218 219 220 221 |
# File 'lib/cstruct.rb', line 218 def double(*args) # |symbol,dimension=nil| symbol,dimension = args field symbol,8,:double,dimension end |
.endian ⇒ Object
45 46 47 |
# File 'lib/cstruct.rb', line 45 def self.endian @endian end |
.field_hash ⇒ Object
53 54 55 |
# File 'lib/cstruct.rb', line 53 def self.field_hash @fields end |
.float(*args) ⇒ Object
|symbol,dimension=nil|
214 215 216 217 |
# File 'lib/cstruct.rb', line 214 def float(*args) # |symbol,dimension=nil| symbol,dimension = args field symbol,4,:float,dimension end |
.options(opt_hash) ⇒ Object
38 39 40 41 42 43 |
# File 'lib/cstruct.rb', line 38 def self. opt_hash raise 'Data Type Error!' unless opt_hash.is_a? Hash @endian = opt_hash[:endian] if opt_hash.has_key?:endian raise 'Unsupported Endian!' if @endian!=:little and @endian!=:big @align = opt_hash[:align] if opt_hash.has_key?:align end |
.size ⇒ Object Also known as: __size__
196 197 198 |
# File 'lib/cstruct.rb', line 196 def self.size @size end |
.struct(symbol, &block) ⇒ Object
517 518 519 520 521 522 |
# File 'lib/cstruct.rb', line 517 def CStruct.struct symbol,&block struct_super = self.ancestors[1] struct_class = Class.new(struct_super) struct_class.instance_eval(&block) do_structfield symbol,struct_class,struct_class.size end |
.union(symbol, &block) ⇒ Object
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 |
# File 'lib/cstruct.rb', line 494 def CStruct.union symbol,&block union_super = self.ancestors[1] union_class = Class.new(union_super) do def self.change_to_union @fields.each do|k,v| v[1] = 0 @fields[k] = v end max_field_size = @fields.values.inject(0)do |max,v| dimension = v[3] dimension_product = 1 dimension_product = dimension.inject(1){|m,d| m *= d } if dimension.is_a? Array field_size = v[0]* dimension_product max = (field_size> max ? field_size : max) end @size = max_field_size end end union_class.instance_eval(&block) union_class.instance_eval{change_to_union} do_structfield symbol,union_class,union_class.size end |
Instance Method Details
#<<(bindata) ⇒ Object
333 334 335 336 337 338 |
# File 'lib/cstruct.rb', line 333 def << bindata count = @data.size < bindata.size ? @data.size : bindata.size (0...count).each do |i| CStruct::Utils.string_setbyte @data,i, CStruct::Utils.string_getbyte(bindata,i) end end |
#data ⇒ Object Also known as: __data__
317 318 319 |
# File 'lib/cstruct.rb', line 317 def data @data end |
#data=(bindata) ⇒ Object Also known as: __data__=
328 329 330 331 |
# File 'lib/cstruct.rb', line 328 def data= bindata raise 'Data Type Error!' unless bindata.is_a? String self << bindata end |
#reset ⇒ Object Also known as: __reset__
321 322 323 324 325 326 |
# File 'lib/cstruct.rb', line 321 def reset (0...self.class.size).each do |i| CStruct::Utils.string_setbyte @data,i, 0 end sync_to_owner end |
#sync_to_owner ⇒ Object
340 341 342 343 344 345 346 347 348 |
# File 'lib/cstruct.rb', line 340 def sync_to_owner return if @owner.empty? final_offset = @owner.inject(0) do |sum,owner_value| _,foffset,_ = owner_value sum+= foffset end onwerdata,_,_ = @owner.last CStruct::Utils.buffer_setbytes onwerdata,@data,final_offset end |