Module: HidApi::Util::WCHAR

Defined in:
lib/hid_api/util/wchar.rb

Constant Summary collapse

WCHAR_T_WIDTH =
RUBY_PLATFORM =~ /mswin/i ? 2 : 4
FORMATS =

Maps a WCHAR_T_WIDTH to the corresponding Array#pack character width

{
  2 => 'S',
  4 => 'L'
}
ENCODINGS =

Maps a WCHAR_T_WIDTH to the corresponding string encoding

{
  2 => 'utf-16le',
  4 => 'utf-32le'
}

Instance Method Summary collapse

Instance Method Details

#read_wchar_string(max_chars = nil) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/hid_api/util/wchar.rb', line 18

def read_wchar_string max_chars=nil
  buffer = []
  offset = 0
  loop do
    pointer = self + (offset * WCHAR_T_WIDTH)
    char = pointer.send("read_uint#{WCHAR_T_WIDTH * 8}")
    break if char.zero?
    buffer << char
    offset += 1
  end
  buffer.pack("#{FORMATS[WCHAR_T_WIDTH]}*").force_encoding(ENCODINGS[WCHAR_T_WIDTH]).encode('utf-8')
end