http://qs321.pair.com?node_id=1168013


in reply to Re^4: Passing integer pointer in XS? (XS << Perl)
in thread Passing integer pointer in XS?

If the first 4 arguments are for specifying ways to output 2 strings, then you can get simpler than that!

int tdSensor( protocol, protocolLen, model, modelLen, id, dataTypes ) SV * protocol int protocolLen SV * model int modelLen int & id int & dataTypes CODE: { STRLEN n_a; (void) SvPV_force( protocol, n_a ); (void) SvPV_force( model, n_a ); SvGROW( protocol, protocolLen ); SvGROW( model, modelLen ); } RETVAL = tdSensor( SvPV(protocol), protocolLen, SvPV(model), modelLen, &id, &data +Types ); SvCUR_set( protocol, strlen(SvPV(protocol)) ); SvCUR_set( model, strlen(SvPV(model)) ); OUTPUT: RETVAL id dataTypes

But much simpler (and less likely to be buggy) than even that is:

int _tdSensor( protocol, protocolLen, model, modelLen, id, dataTypes ) char * protocol int protocolLen char * model int modelLen int & id int & dataTypes CODE: RETVAL = tdSensor( protocol, protocolLen, model, modelLen, &id, &d +ataTypes ); OUTPUT: RETVAL id dataTypes

Plus

sub tdSensor { my( $protocolLen, $modelLen ) = @_; $protcolLen ||= 1024; $modelLen ||= 1024; my $protocol = '\0' x $protcolLen; my $model = '\0' x $modelLen; my $id = 0; my $dataTypes = 0; my $ret = _tdSensor( $protocol, $protocolLen, $model, $modelLen, $id, $dataTypes ); s/\0.*// for $protocol, $model; return( $ret, $protocol, $model, $id, $dataTypes ); }

Which has the significant advantage of making it much easier to improve the interface without having to futz with the super finicky XS part of the code.

- tye