Class: String

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

Overview

overrides String.unpack to support MTP datatypes

Constant Summary collapse

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

Instance Method Summary collapse

Instance Method Details

#unicodeObject



162
163
164
# File 'lib/mtp.rb', line 162

def unicode
  @@iconv.iconv(self)
end

#unpack_with_mtp(format) ⇒ Object Also known as: unpack

J is a string, use + for an array O is an MTP code



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/mtp.rb', line 168

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

  while f = formats.shift
    next if f.empty?
    
    # check for array
    if f.match(/\*/)
      ary.concat(str.unpack_without_mtp(f))
    elsif f[0,1] == "K" and f[1,1] == "+"
      size, str = str.unpack_without_mtp("Va*")
      n = str.unpack_without_mtp("S#{size}a*")
      str = n.pop
      ary << n.map { |k| MTP::Datacode.new(k) }
    elsif f[0,1] == "K"
      n, str = str.unpack_without_mtp("Sa*")
      ary << MTP::Datacode.new(n)
    elsif f[0,1] == "Y"
      n1, n2, str = str.unpack_without_mtp("QQa*")
      ary << ((n2 << 32) + n1)
    elsif f[1,1] == '+'
      size, str = str.unpack_without_mtp("Va*")
      n = str.unpack_without_mtp("#{f[0,1]}#{size}a*")
      str = n.pop
      ary << n
    elsif f == "J"
      len = str.unpack_without_mtp("C").first.to_i * 2 
      len, string, str = str.unpack_without_mtp("Ca#{len}a*")
      if len > 0
        ary << string[0, string.length - 2].unicode
      else
        ary << ""
      end
    elsif f == "D"
      len = str.unpack_without_mtp("C").first.to_i * 2 
      len, string, str = str.unpack_without_mtp("Ca#{len}a*")
      if len > 0
        string = string[0, string.length - 2].unicode
        if string.match(/(\d{4})(\d{2})(\d{2})T(\d{2})(\d{2})(\d{2})?(\.(\d+))?$/)
          t = Time.xmlschema(string)
        end
      end
      ary << t
    else
      n = str.unpack_without_mtp("#{f}a*")
      str = n.pop
      ary.concat(n)
    end
  end
  ary
end