Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (9)
As of 2014-12-26 06:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (167 votes), past polls