Hello Muppet!
newSVpv() copies a character array into a scalar; but it doesn't have to be a character string.
SV * sv = newSVpv ((char*) data, n*sizeof(data[0]));
that will allocate a scalar with n*sizeof(data[0]) bytes of storage, and copy that many bytes of the contents of data into it. in perl, you can unpack that normally. this also works for structs and pretty much anything else; the key is to specify the length explicitly, rather than letting newSVpv() find the length with strlen().
thank you! :))
Now my xs work fine:
SV * getnum(char *data,int l,int k)
INIT: CODE:
HV* hash = newHV();
AV* array = newAV();
av_push(array, newSVnv(34)); #for loop
av_push(array, newSVnv(77)); # for loop
hv_store(hash, "unsigned_char",3, newSVpv ((char*) data, l*sizeof(data[0])), 0);
hv_store(hash, "array",3,(SV*)array, 0);
RETVAL = newRV( (SV *) hash ); /* return a ref to the hash */
OUTPUT: RETVAL
and return hash with two elements: array and packed data $pixels1.
perl:
my $txt=pack "C",255;
my $hash_ref = Hval::getnum($txt,length $txt,10);
foreach my $key (keys %$hash_ref) { print "$key => "; print join " | "=> @{$hash_ref->{$key}} if $key ne 'uns'; print "\n\n"; print "$key => ".unpack "C",$hash_ref->{$key} if $key eq 'uns'; print "\n"; }
SV * gdk_pixbuf_get_pixels (pixbuf) GdkPixbuf *pixbuf PREINIT: guchar * pixels; CODE: pixels = gdk_pixbuf_get_pixels (pixbuf); RETVAL = newSVpv ((gchar *) pixels, gdk_pixbuf_get_height (pixbuf) * gdk_pixbuf_get_rowstride (pixbuf)); OUTPUT: RETVAL
yeah... this code from
http://cvs.sourceforge.net/viewcvs.py/gtk2-perl/gtk2-perl-xs/Gtk2/xs/GdkPixbuf.xs?rev=1.32&view=auto
heh.
Thank you!
dmitriy