File: utils.rb

package info (click to toggle)
ruby-cstruct 1.0.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 412 kB
  • sloc: ruby: 1,008; makefile: 7
file content (53 lines) | stat: -rwxr-xr-x 1,839 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
require 'enumerator'

class CStruct
  module Utils #:nodoc: all
    UnpackFormat =
    {
      :little => { 1=>'C',2=>'v',4=>'V',8=>'Q',:float=>'e',:double=>'E'},
      :big    => { 1=>'C',2=>'n',4=>'N',8=>'Q',:float=>'g',:double=>'G'}, #8=>'Q'? 'Q' is native endian
    }
    SigedMaxValue    = {  1 => 0x7F, 2 => 0x7FFF, 4 => 0x7FFFFFFF, 8 => 0x7FFFFFFFFFFFFFFF }
    UnsigedMaxValue  = {  1 => 0xFF, 2 => 0xFFFF, 4 => 0xFFFFFFFF, 8 => 0xFFFFFFFFFFFFFFFF }
    
    class << self
      # buffer is a String's object
      def unpack(buffer,struct_endian,fsize,fsign)
        format_index = (fsign==:float or fsign ==:double) ? (fsign):(fsize)
        format = UnpackFormat[struct_endian][format_index]
        value  = buffer.unpack(format).first
       
        if  fsign == :signed
          value = unsigned_to_signed value,fsize
        end  
        value
      end
      
      # buffer is a Array's object
      def pack(buffer,struct_endian,fsize,fsign)
        format_index = (fsign==:float or fsign ==:double) ? (fsign):(fsize)
        format = UnpackFormat[struct_endian][format_index] 
        buffer.pack format
      end

      def string_setbyte(string,index,value)
        RUBY_VERSION < "1.9" ? (string[index] = value):(string.setbyte index,value)       
      end

      def string_getbyte(string,index)
        RUBY_VERSION < "1.9" ? (string[index]):(string.getbyte index)
      end
      
      def unsigned_to_signed(value,value_size)
        value > SigedMaxValue[value_size] ? (value - UnsigedMaxValue[value_size]-1):(value)  
      end

      def buffer_setbytes(target,source,target_index)
          source.enum_for(:each_byte).each_with_index do |byte,index|
          string_setbyte(target,target_index + index,byte)
        end
      end

    end
  end
end