Re: question on implementing C function with binary string as input and output

[email protected] (Perf Tech) Sun, 6 Apr 2014 20:08:44 -0500
Newsgroups perl.inline
Message-ID <CAL27a15dga9mrD54Y357Yx3dYf6POkTOwbRq-4RkHN503ZMVkw@mail.gmail.com>
Thanks Rob a lot for the great example!  Wish it's included in the
cookbook.




On Sun, Apr 6, 2014 at 7:53 PM, <[email protected]> wrote:

>
>
> -----Original Message----- From: [email protected]
>
>  I don't know (off the top of my head) how to concatenate binary strings
>> in C ...
>>
>
> Thankfully, perl's API provides a simple solution:
>
>
> ##############################
>
> use warnings;
> use strict;
> use Devel::Peek;
>
> use Inline C => Config =>
> BUILD_NOISY => 1,
> ;
>
> use Inline C => <<'EOC';
>
> SV * foo(SV * in) {
>  SV * ret;
>
>  STRLEN len;
>  char *tmp = SvPV(in, len);
>  ret = newSVpv(tmp, len);
>  sv_catpvn(ret, tmp, len);
>  return ret;
>
> }
>
> EOC
>
> my $in = 'hello' . "\x00" . 'world';
> my $ret = foo($in);
>
> $ret eq $in . $in ? print "\nok 1\n\n"
>
>                  : print "\nnot ok 1\n\n";
>
> Dump($in);
> print "\n";
> Dump ($ret);
> ##############################
>
> Cheers,
> Rob
>
>