Lähettäjä: Jon McClintock <jammer@weak.org>
Aihe: A couple of Ruby API questions
Hi Rubyists,
I'm working on wrapping a C library, and I've got a couple of things I
can't quite figure out how to manage:
1) Wrapping functions that take variable-length arguments (varargs). Is
there are standard way to do this? The best I've found is libffi,
which lets you construct the stack frames you need to do this, but it
seems like this would be a problem someone would have solved a while
ago.
Wrapping how? Should these be callable from Ruby? Ruby can define a
method to take variable arguments either as a RArray or as a C array,
so you'll pick one, construct a wrapper function based your selected
way and have that function extract the values and feed them to your
'actual' function.
2) Wrapping structures with flexible arrays. How do I realloc the
structure held by a VALUE? Or do I have to create an entirely new
Ruby object each time I need to resize the structure?
Just wrap a pointer to the struct instead if you already have the C
to manage the arrays. Whenever Ruby needs those values, have a C
function extract them. Alternatively, you could use a RArray.
It's not quite that easy. Ruby lets you define a method that takes a
variable number of arguments, but then how do you actually pass them to
a varargs function? Think about printf(), for example. How can I call
printf() using the contents of an arbitrary length array (without
resorting to using vprintf())?
Because that'll just pass the address of the array to the function.
You can't do:
printf(format, args[0], args[1], args[2], ...);
Unless you build some ungodly switch statement on the number of
arguments. Surely I'm not the first person to run into this?
-Jon
···
On Wed, Feb 09, 2005 at 04:00:57PM +0900, E S wrote:
> 1) Wrapping functions that take variable-length arguments (varargs). Is
> there are standard way to do this? The best I've found is libffi,
> which lets you construct the stack frames you need to do this, but it
> seems like this would be a problem someone would have solved a while
> ago.
Wrapping how? Should these be callable from Ruby? Ruby can define a
method to take variable arguments either as a RArray or as a C array,
so you'll pick one, construct a wrapper function based your selected
way and have that function extract the values and feed them to your
'actual' function.
You're not the first person to run into this, but most people punt on
trying to find some general purpose solution to the problem (which
seems to be what you're asking for) and instead look for a way to wrap
the *specific* varargs function that makes sense for the targeted
scripting language.
There's a pretty good discussion of this issue in the SWIG documentation, here:
It includes some strategies (including the use of libffi) for dealing
with these kinds of functions.
Hope this helps,
Lyle
···
On Thu, 10 Feb 2005 00:00:33 +0900, Jon McClintock <jammer@weak.org> wrote:
It's not quite that easy. Ruby lets you define a method that takes a
variable number of arguments, but then how do you actually pass them to
a varargs function? ...
Unless you build some ungodly switch statement on the number of
arguments. Surely I'm not the first person to run into this?
Seems like there are a fair number of people out there crying for this..
I haven't found anything ideal either.. there is sybase-ctlib which connects
through sybase's proprietary C access library (open connect???)..
I'm told that works but I wanted something more "free"..
Also SQL-Relay should offer an option (use Ruby's SQL-Relay driver and
connect to a SQL-Relay that goes to Sybase).. but I haven't tried that
either..
In theory using GNOME-DB might work as well (haven't looked into that one at
all).. but they are all hacks compared to a good native ruby library or
something based on freeTDS.
···
On Wednesday 09 February 2005 11:34, Kingsley Hendrickse wrote:
Hi
is anyone currently working on any Ruby Sybase connection code?
I really want to connect to a sybase db via Ruby and all the packages I
found so far are outdated and broken and use non-free stuff.
If there's no-one working on it would anyone like to help me with one?