Subject: [ruby-ffi] Re: **char issues |
From: Bryan Kearney |
Date: 10/30/09 9:46 AM |
To: ruby-ffi@googlegroups.com |
Thank you.. that worked. Next one is a ***char issue. the augeas API returns an array of Strings:
2009/10/29 bk<bkearney@redhat.com>:
I am attempting to convert the augeas bindings to ffi. I did this with
JNA, and it was very helpful. The method in question is:
int aug_get(const augeas *aug, const char *path, const char **value);
where the last paraemeter returns the result of the get. I have
attempted to wrap this with
attach_function :aug_get, [:pointer, :string, :pointer], :int
and then call this with:
def get(path)
ptr = FFI::MemoryPointer.new(:pointer, 1)
AugeasLib.aug_get(@aug, path, ptr)
strPtr = ptr.read_pointer()
return strPtr.read_string()
end
This works fine if the get returns something, but if the get ruturns a
null string I fail with:
FFI::NullPointerError: invalid memory read at address=(nil)
You need to test strPtr for null
e.g.
if !((strPtr = ptr.read_pointer).null?
strPtr.read_string
else
"" # Or whatever end value you want
end
|x|