logo       

Perl typemaps for byte arrays: msg#00174

programming.swig

Subject: Perl typemaps for byte arrays

For the archive, here are an in and an out Perl typemaps for bytearrays where the first int (4 bytes) is the length of the array:

%typemap(in) (unsigned char * x) {
$1 = (unsigned char *)SvPV_nolen($input);
}

%typemap(out) unsigned char * myFunction {
int *iptr;
iptr = (int *)$1;
int len = *iptr;
$result = newSVpv((char *)$1, len);
sv_2mortal($result);
argvi++;
delete [] $1;
}


I had to do a copy of the bytearray into a perl variable on the out, I don't think there's a way to avoid the copy. The in typemap is cleaner with a simple creation of a pointer into the perl variable.

Apologies if this seems straightforward and obvious.

H

_______________________________________________
Swig maillist - Swig@xxxxxxxxxxxxxxx
http://mailman.cs.uchicago.edu/mailman/listinfo/swig



<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise