Subject:
Re: [ruby-ffi] ffi-swig generating a :string instead of a :pointer ??
From:
Stephen Bannasch
Date:
12/12/09 10:23 PM
To:
ruby-ffi@googlegroups.com

>I have a C++ function that takes a pointer to a buffer where the function stores a C-string.
>
>This is what I wrote and got working.
>
>  attach_function :GoIO_GetNthAvailableDeviceName, [:pointer, :int, :int, :int, :int], :int
>
>However if I use ffi-swig it generates a :string as the first parameter.
>
>  attach_function :GoIO_GetNthAvailableDeviceName, [ :string, :int, :int, :int, :int ], :int
>
>Is there anyway this could work? The function needs an address to a location to store a C-string.


Looking more closely at ffi-swig it's type_spec.rb does expect:

  char *string;

to be turned into a :string.

I'm not much of a C programmer but is that correct? It seems in this case it should be a pointer.

Types like these are correctly turned into :string

  const char *string;

The choice about turning a pointer into a string appears to be made in ffi-swig at lib/generator/type.rb:74:

  def pointer
    if @declaration.is_pointer? or @is_pointer > 0
      @is_pointer += 1
      if @full_decl.scan(/^p\.(.+)/).flatten[0]
        ffi_type_from(@full_decl.scan(/^p\.(.+)/).flatten[0])
      elsif @full_decl == 'char' and @is_pointer == 2
        ':string'
      else
        ':pointer'
      end
    end       
  end