Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://680849]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (1)
As of 2017-09-21 23:19 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (254 votes). Check out past polls.