# SWIG, Ruby, C and typemaps

**URL:** <https://www.ruby-forum.com/t/swig-ruby-c-and-typemaps/82335>\
**Category:** Ruby\
**Created:** [January 19, 2007, 4:29pm UTC](https://www.ruby-forum.com/t/swig-ruby-c-and-typemaps/82335 "2007-01-19T16:29:17Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jeff\_Sack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7bcc69/32.png) [@Jeff\_Sack](https://www.ruby-forum.com/u/Jeff_Sack)\
**Post date:** [January 19, 2007, 4:29pm UTC](https://www.ruby-forum.com/t/swig-ruby-c-and-typemaps/82335/1 "2007-01-19T16:29:17Z")

</div>

Hello all. I am seeking some advice on using SWIG to wrap some native  
C code provided to me (I don’t have the ability to change it).  
Basically, I have something like this:

The definition of one of the problematic methods I am trying to wrap  
looks something like this. This is in my SWIG interface file (.i  
file):

int open\_file(ResponseObject \*\*resp, const char \*fname);

And the normal way of using this is as follows:

ResponseObject \*reponsObj;  
const char \*filename=“input\_file”;

if(open\_file(&reponsObj, filename)!=0)  
{  
// handle non-zero error code  
}  
else  
{  
// success!  
}

I think the most natural way of accessing this method might be  
something like this:

resp = open\_file(‘input\_file’)

In this scenario, resp is a Ruby object representing ResponseObject,  
and the original int return value is now mapped to a conditionally  
thrown exception. If open\_file returns 0, then no exception would be  
thrown, otherwise, the Ruby method open\_file would throw an exception  
which wraps the int return code.

Thoughts? I know there is some SWIG/typemap magic that can do this,  
but so far I’m a bit stumped. Can someone offer some advice? I can  
provide more details if needed.

Thanks so much in advance for your help. Much appreciated.

-Jeff

---

<div class="post-metadata">

**Author:** ![Jeff\_Sack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7bcc69/32.png) [@Jeff\_Sack](https://www.ruby-forum.com/u/Jeff_Sack)\
**Post date:** [January 19, 2007, 4:30pm UTC](https://www.ruby-forum.com/t/swig-ruby-c-and-typemaps/82335/2 "2007-01-19T16:30:43Z")

</div>

Jeff Sack wrote:

> The definition of one of the problematic methods I am trying to wrap  
> looks something like this. This is in my SWIG interface file (.i  
> file):
> 
> int open\_file(ResponseObject \*\*resp, const char \*fname);

…

> I think the most natural way of accessing this method might be  
> something like this:
> 
> resp = open\_file(‘input\_file’)

You’lll first want a typemap to tell SWIG to convert one ruby input (the  
string filename) into the two parameters C is expecting. Along the lines  
of (untested)

%typemap(in,numinputs=1) (ResponseObject \*\*resp, const char \*fname) {  
$1 = new ResponseObject();  
$2 = rb\_str\_new2(fname);  
}

Then you’ll need another typemap to treat the resp argument as a ruby  
return value, and convert it to a ruby wrapper round the C object.

%typemap(argout) (ResponseObject \*\*resp) {  
$result = SWIG\_NewPointerObj($1, $1\_descriptor, 1);  
}

hth  
alex
