Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

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]
[erix]: I do some work as a corrector (if that's the right word) and often I have to remove phrases that people think is dutch but that is really literally taken from english (and does not exist in dutch. Although at some point it becomes acceptable, of course)
[erix]: "Do not be dafter than you are" doesn't seem a very good translation
LanX understands corrector but thinks it's editor

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2018-03-19 22:24 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (246 votes). Check out past polls.