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

2014-04-07 Thread Perf Tech
Thanks Rob a lot for the great example!  Wish it's included in the
cookbook.




On Sun, Apr 6, 2014 at 7:53 PM, sisyph...@optusnet.com.au wrote:



 -Original Message- From: sisyph...@optusnet.com.au

  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




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

2014-04-06 Thread sisyphus1


From: Perf Tech

In another word, the C function should have the similar syntax as the 
following perl function.


sub myRepeat {
my $str = shift;
return $str . $str;
}


I don't know (off the top of my head) how to concatenate binary strings in 
C, but the following demonstrates one way of successfully passing binary 
strings between perl and C:


##
use warnings;
use strict;
use Devel::Peek;

use Inline C = Config =
BUILD_NOISY = 1,
;

use Inline C = 'EOC';

SV * foo(SV * in) {
 STRLEN len;
 char *tmp = SvPV(in, len);
 return newSVpv(tmp, len);
}

EOC

my $in = 'hello' . \x00 . 'world';
my $ret = foo($in);

$ret eq $in ? print \nok 1\n\n
   : print \nnot ok 1\n\n;

Dump($in);
print \n;
Dump ($ret);
##

Let us know if you need additional help with the concatenation aspect (or 
anything else, for that matter ;-)


Cheers,
Rob




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

2014-04-06 Thread sisyphus1



-Original Message- 
From: sisyph...@optusnet.com.au


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