|
(no subject): msg#00135programming.swig
Hi, first of all I apologize in advance if the problems I encountered has already been discussed here. 1 .I have a problem when I try to work with C-structures using SWIG-wrapped C-functions in Perl. In a nutshell, why it makes a difference if I call: # prints actual content of the structure SwigTest::print_string("$Cstruct::cstruct->{key}"); or # prints nothing! SwigTest::print_string($Cstruct::cstruct->{key}); In the latter case a null(!) pointer is passed to the function. The function prototype is void print_string(const char * str); and the struct is defined as typedef struct cstruct { char *key; } cstruct_t; The argument passed to the function will be cast in char * pointer. So, whatever it is, it will not be zero after casting. But if it is zero, how comes that "quoting" the argument converts it to a nonempty string ? If instead of C-structure a Perl hash is used, there problem disappear. Environment: SunOS 5.8 Generic_117000-01 sun4u sparc, perl v5.8.5 built for sun4-solaris, SWIG Version 1.3.22. Compiled with g++ [sparc-sun-solaris2.8], gcc version 3.4.1. To run the example: swig -perl -shadow SwigTest.i; gcc -D_GNU_SOURCE -g -fPIC -mtune=ultrasparc -O3 -I/usr/gnu/install/perl-5.8.5/lib/5.8.5/sun4-sol aris/CORE -c SwigTest_wrap.c SwigTest.c; ld -G SwigTest.o SwigTest_wrap.o -o SwigTest.so; ./Swigtest.pl; You will have to adjust the path to perl executable and to perl CORE directory. Here is the full source: //--------------SwigTest.c--------------// #include <stdio.h> #include "SwigTest.h" void print_string(const char * str) { if(0==str) printf("ZERO POINTER!\n", str); else if(0==*str) printf("Empty string!\n", str); else printf("%s\n", str); } cstruct_t *cstruct_make() { cstruct_t *cstruct; cstruct=(cstruct_t *)malloc(sizeof(cstruct_t)); return(cstruct); } //--------------------------------------// //--------------SwigTest.h--------------// typedef struct cstruct { char *key; } cstruct_t; void print_string(const char * str); cstruct_t * cstruct_make(); //--------------------------------------// //--------------SwigTest.i--------------// %module SwigTest %{ #include <stdio.h> #include "SwigTest.h" %} typedef struct cstruct { char *key; } cstruct_t; %name(print_string) extern void print_string(const char *str); %name(cstruct_make) extern cstruct_t * cstruct_make(void); //--------------------------------------// //--------------Cstruct.pm--------------// package Cstruct; use strict; use SwigTest; use vars qw($hr $cstruct); # This package provides access to two variables # One is an ordinary perl hash, with one element # The other is a hash tied via SWIG to a C structure containing one element my %hash; $hr =\%hash; print "$hr\n"; $hr->{key} = "Contents of hash element with key \"key\""; $cstruct=SwigTest::cstruct_make() or die; $cstruct->{key}="Contents of hash element linked via Swig to C structure element"; print "[Loaded: ", __FILE__, "]\n"; 1; //--------------------------------------// //-----------Swigtest.pl----------------// #!/usr/gnu/bin/perl -w use strict; use lib ("."); use Cstruct; use SwigTest; print "\n\n"; print "\$Cstruct::hr is a $Cstruct::hr\n"; print "\$Cstruct::cstruct is a $Cstruct::cstruct\n"; print "\n"; print "\$Cstruct::hr->{key} contains: "; print $Cstruct::hr->{key},"\n"; print "\$Cstruct::cstrcut->{key} contains: "; print $Cstruct::cstruct->{key},"\n"; print "\n"; print "When \$Cstruct::hr->{key} is passed to C it contains: "; SwigTest::print_string($Cstruct::hr->{key}); print "When \$Cstruct::cstruct->{key} is passed to C it contains: "; SwigTest::print_string($Cstruct::cstruct->{key}); print "When \"\$Cstruct::cstruct->{key}\" is passed to C it contains: "; SwigTest::print_string("$Cstruct::cstruct->{key}"); print "\n"; //--------------------------------------// __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com _______________________________________________ Swig maillist - Swig@xxxxxxxxxxxxxxx http://mailman.cs.uchicago.edu/mailman/listinfo/swig |
|
| <Prev in Thread] | Current Thread | [Next in Thread> |
|---|---|---|
| Previous by Date: | Re: Preventing a user to refer to non-existent struct members: 00135, Robert Lupton |
|---|---|
| Next by Date: | How can I map "C" unsigned char[] to byte[] in Java ?: 00135, Tom Lueders (MT641) |
| Previous by Thread: | (no subject)i: 00135, Bryga 66 |
| Next by Thread: | How can I map "C" unsigned char[] to byte[] in Java ?: 00135, Tom Lueders (MT641) |
| Indexes: | [Date] [Thread] [Top] [All Lists] |
| News | FAQ | advertise |