Sha256: 4f2a94d772dfac2e3748353b989cb99a7d3aa1f992ca0c49b68e772ab2cf6a88

Contents?: true

Size: 1.62 KB

Versions: 9

Compression:

Stored size: 1.62 KB

Contents

# frozen_string_literal: true

require 'ffi'

module GDAL
  # A wrapper for the way GDAL does key/value pair options for methods.
  class Options < Hash
    # Shortcut for if you just want to build the options and get the pointer to
    # them.
    #
    # @param hash [Hash]
    # @param nil_on_empty [Boolean] When +true+, if +hash+ is empty, return
    #   +nil+.  If +false+, creates a 0-size pointer.
    # @return [FFI::MemoryPointer, nil]
    def self.pointer(hash, nil_on_empty: true)
      return if nil_on_empty && hash.empty?

      new(hash).c_pointer
    end

    # Takes a GDAL options pointer and turns it into a Ruby Hash.
    #
    # @param pointer [FFI::Pointer]
    # @return [Hash]
    def self.to_hash(pointer)
      FFI::CPL::String.CSLCount(pointer).times.each_with_object({}) do |i, o|
        key_and_value = FFI::CPL::String.CSLGetField(pointer, i)
        key, value = key_and_value.split('=')
        o[key.downcase.to_sym] = value
      end
    end

    # @param hash [Hash] The hash of options to turn into a CPL key/value pair
    #   set.
    def initialize(hash = {})
      super()
      capitalize_keys!(hash)
    end

    # @return [FFI::MemoryPointer] The double-char pointer that contains the
    #   options for GDAL to use.
    def c_pointer
      options_ptr = FFI::MemoryPointer.new(:pointer, size)

      each do |key, value|
        options_ptr = FFI::CPL::String.CSLSetNameValue(options_ptr, key, value.to_s)
      end

      options_ptr
    end

    private

    def capitalize_keys!(hash)
      hash.each_with_object(self) do |(key, value), obj|
        obj[key.to_s.upcase] = value
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
ffi-gdal-1.0.0.beta16 lib/gdal/options.rb
ffi-gdal-1.0.0.beta15 lib/gdal/options.rb
ffi-gdal-1.0.0.beta14 lib/gdal/options.rb
ffi-gdal-1.0.0.beta13 lib/gdal/options.rb
ffi-gdal-1.0.0.beta12 lib/gdal/options.rb
ffi-gdal-1.0.0.beta11 lib/gdal/options.rb
ffi-gdal-1.0.0.beta10 lib/gdal/options.rb
ffi-gdal-1.0.0.beta9 lib/gdal/options.rb
ffi-gdal-1.0.0.beta8 lib/gdal/options.rb