Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: char ** typemap in perlxs

by rafl (Friar)
on Apr 16, 2008 at 16:21 UTC ( [id://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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2024-04-20 02:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found