Module: CoreMIDI::Map

Extended by:
FFI::Library
Defined in:
lib/coremidi/map.rb

Overview

Coremidi C binding

Defined Under Namespace

Modules: CF, HostTime Classes: MIDIPacket, MIDIPacketList, MIDISysexSendRequest

Constant Summary collapse

SnowLeopard =
`uname -r`.scan(/\d*\.\d*/).first.to_f >= 10.6

Class Method Summary collapse

Class Method Details

.get_string(resource, name) ⇒ String

Parameters:

  • resource (FFI::Pointer)

    A pointer to an underlying struct

  • name (String, Symbol)

    The property name to get

Returns:

  • (String)


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/coremidi/map.rb', line 52

def self.get_string(resource, name)
  property = Map::CF.CFStringCreateWithCString(nil, name.to_s, 0)
  begin
    pointer = FFI::MemoryPointer.new(:pointer)
    Map::MIDIObjectGetStringProperty(resource, property, pointer)
    string = pointer.read_pointer
    length = Map::CF.CFStringGetMaximumSizeForEncoding(Map::CF.CFStringGetLength(string), :kCFStringEncodingUTF8)

    bytes = FFI::MemoryPointer.new(length + 1)

    if Map::CF.CFStringGetCString(string, bytes, length + 1, :kCFStringEncodingUTF8)
      bytes.read_string.force_encoding("utf-8")
    end
  ensure
    Map::CF.CFRelease(string) unless string.nil? || string.null?
    Map::CF.CFRelease(property) unless property.null?
  end
end