Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Re: char ** typemap in perlxs

by rafl (Friar)
on Apr 16, 2008 at 16:21 UTC ( #680849=note: print w/ replies, xml ) Need Help??

in reply to char ** typemap in perlxs

The default typemap maps char ** to T_PACKEDARRAY. The input typemap for that looks like this:

T_PACKEDARRAY $var = XS_unpack_$ntype($arg)

so to convert the perl argument to a char ** value it will call XS_unpackcharPtrPtr. However implementing this function is something you will need to do as xsubpp can't know what the meaning of the data is.

The following (untested) code might be useful if you want to pass an array reference from perl and your c code expects a NULL terminated list of strings:

char ** XS_unpack_charPtrPtr (SV *arg) { char **ret; AV *av; I32 i; if (!arg || !SvOK (arg) || !SvROK (arg) || (SvTYPE (SvRV (arg)) != + SVt_AV)) { croak ("array reference expected"); } av = (AV *)SvRV (arg); ret = (char **)malloc (av_len (av) + 1); for (i = 0; i <= av_len (av); i++) { SV **elem = av_fetch (av, i, 0); if (!elem || !*elem) { croak ("foo"): } ret[i] = SvPV_nolen (*elem); } ret[i + 1] = NULL; return ret; }

As this function allocates memory you will need to free the return value when you don't need it anymore. The CLEANUP section of an xsub might be a good place for that.

Comment on Re: char ** typemap in perlxs
Select or Download Code

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://680849]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (7)
As of 2015-11-28 21:05 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (745 votes), past polls