Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ffi-swig generating a :string instead of a :pointer #4

Open
stepheneb opened this issue Dec 15, 2009 · 0 comments
Open

ffi-swig generating a :string instead of a :pointer #4

stepheneb opened this issue Dec 15, 2009 · 0 comments

Comments

@stepheneb
Copy link

See: http://groups.google.com/group/ruby-ffi/browse_thread/thread/98eaf51ab0386347

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.

Here's the C++ function:

GOIO_DLL_INTERFACE_DECL gtype_int32 GoIO_GetNthAvailableDeviceName(
  char *pBuf,            // [out] ptr to buffer to store device name string.
  gtype_int32 bufSize,   // [in] number of bytes in buffer pointed to by pBuf.
                         // Strlen(pBuf) < bufSize, because the string is NULL terminated.
  gtype_int32 vendorId,  // [in] USB vendor id
  gtype_int32 productId, //[in] USB product id
  gtype_int32 N);        //[in] index into list of known devices, 0 => first device in list.

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant