On 28/01/03 22:28 +0000, Miah Gregory wrote: > [subscribed to list] > > Hi all, > > Firstly, my apologies if this has been covered before, I've had a look > through the archive, but I couldn't find anything relevant. > > I'm trying to re-implement a fairly simple perl subroutine in C, and > inline it. Problem is, though, it takes as an argument, a 'string' of > data, and that data may contain NUL's. This means I can't simply use a > char * as the type. > > Another problem is how I should return the two results. It would be nice > if I could return them in the same way as the perl subroutine. Looks as if > I need to set the return type as 'void', and use Inline_Stack_*, but > again, how do I pass back a 'string' that contains NUL's. I'm also not > sure about how to allocate the memory for these. I saw newSVpvf mentioned > on the mailing list, but it doesn't appear to take a length. > > Where can I find out more about things like newSVpvf and the rest of these > macros/functions? > > I'll paste the perl code here, just for reference, so the above might seem > clearer. > > sub splitatoffset > { > my ($data, $length) = @_; > my ($a, $b); > > return ($data, "") if ($length > length($data)); > $a = substr($data, 0, $length); > $b = substr($data, $length); > > return ($a, $b); > }
Perl SV-s contain a string pointer and a length. So I would use a SV* for input and output, and use the 'SvPV' macro to get the pointer and length. SV* splitatoffset(SV* string) { char* str_ptr; STRLEN str_len; str_ptr = SvPV(string, str_len); printf("The length of '%s' is %d\n", str_ptr, str_len); } I'll let you take it from there. Cheers, Brian > Before I get flamed, I'm not looking for a complete solution (although if > someone does that, I'll be the last to complain), but just enough to get > the perl glue in place and working. The C itself should be simple enough. BTW, we keep the burner pretty low around here :)