Class: Array

Inherits:
Object
  • Object
show all
Defined in:
lib/mtp.rb

Overview

overrides Array.pack to support MTP datatypes

Constant Summary collapse

@@iconv =
Iconv.new("UCS2", "UTF8")

Instance Method Summary collapse

Instance Method Details

#pack_with_mtp(format) ⇒ Object Also known as: pack



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/mtp.rb', line 108

def pack_with_mtp(format)
  str, ary = "", self
  formats = format.split(/(K\+?|D|J|Y|\w\+)/)

  while f = formats.shift
    next if f.empty?
    
    # check for array
    if f.match(/\*/)
      str << ary.pack_without_mtp(f)
    elsif f[0,1] == "K" and f[1,1] == "+"
      str << ([ ary.first.size ] + ary.first).pack_without_mtp("VS*")
      ary.shift
    elsif f[0,1] == "K"
      str << ary.pack_without_mtp("S")
      ary.shift
    elsif f[0,1] == "Y"
      str << [ ary.first & 0xffffffffffffffff, ary[1] >> 32 ].pack_without_mtp("QQ")
      ary.shift
      ary.shift
    elsif f[1,1] == '+'
      str << ([ ary.first.size ] + ary.first).pack_without_mtp("V#{f[0,1]}*")
      ary.shift
    elsif f == "J"
      string = @@iconv.iconv(ary.first) + "\000\000"
      len = string.length / 2
      str << [ len, string ].pack_without_mtp("Ca*")
      ary.shift
    elsif f == "D"
      if ary.first.nil?
        string = "\000\000"
      else
        date = ary.first.xmlschema
        string = @@iconv.iconv(date) + "\000\000"
      end
      len = string.length / 2
      string = [ len, string ].pack_without_mtp("Ca*")
      str << [ len, string ].pack_without_mtp("Ca*")
      ary.shift
    else
      str << ary.pack_without_mtp("#{f}")
      (0...f.length).each { |i| ary.shift }
    end
  end
  str
end