class GDAL::Options

A wrapper for the way GDAL does key/value pair options for methods.

Public Class Methods

pointer(hash) click to toggle source

Shortcut for if you just want to build the options and get the pointer to them.

@param hash [Hash] @return [FFI::MemoryPointer, nil]

# File lib/gdal/options.rb, line 13
def self.pointer(hash)
  return if hash.empty?

  options_ptr = FFI::MemoryPointer.new(:pointer)
  options_ptr.autorelease = false

  hash.each do |key, value|
    # Note, we started off with a MemoryPointer above, but this returns a
    # new FFI::Pointer.
    options_ptr = FFI::CPL::String.CSLAddNameValue(options_ptr, key.to_s.upcase, value.to_s)
    options_ptr.autorelease = false
  end

  FFI::AutoPointer.new(options_ptr, Options.method(:release))
end
release(pointer) click to toggle source

@param pointer [FFI::Pointer]

# File lib/gdal/options.rb, line 48
def self.release(pointer)
  return if pointer.nil? || pointer.null?

  FFI::CPL::String.CSLDestroy(pointer)
end
to_hash(pointer) click to toggle source

Takes a GDAL options pointer and turns it into a Ruby Hash.

@param pointer [FFI::Pointer] @return [Hash]

# File lib/gdal/options.rb, line 33
def self.to_hash(pointer)
  # Docs say passing a null pointer here is ok; will result in 0.
  count = FFI::CPL::String.CSLCount(pointer)

  count.times.each_with_object({}) do |i, o|
    # Docs say that the pointer returned from CSLGetField shouldn't be freed.
    key_and_value, ptr = FFI::CPL::String.CSLGetField(pointer, i)
    ptr.autorelease = false

    key, value = key_and_value.split("=")
    o[key.downcase.to_sym] = value
  end
end