Isabelle Henry <[EMAIL PROTECTED]> writes:
>Hi !
>
>I am trying to write an xs interface to an external C library and, since I am
>learning Perl only for
>2 weeks, I have serious problems
Ill be so happy il somebody could help me ! ! ;-)
>
>Here are my main problems (except my english is so bad !)
>
>In the .h (see below), some C structures are defined. In Perl, Ill use hash tables.
>In .xs, Ive
>tried to copy the Perl hash table in C structures in order to call the C function,
>and then to copy
>back the result in hash tables.
Which is fine in theory - but for simple coordinates like these I would
have been tempted to just pass two numbers for input and return a list
of two numbers for output so that perl code would look like:
my ($latitude,$longitude) = StereoToGeoCoordinates($x,$y);
>But, when I print the result, I havent the same results for the parameters I wrote
>in Perl and the
>parameters passed in the C function ! See inside the code for more explanations
.
>
>I hope youll help me and tell what is wrong with my code ! If you dont understand
>my problem, just
>ask me and Ill try to explain better.
As far as I can tell nothing creates the result hash - see below:
>
>void setDouble(HV *hv,char *field, double val) {
This is expecting a valid HV *
> SV **svv = hv_fetch(hv, field, strlen(field),TRUE);
> if (svv){
> sv_setnv(*svv,val);
> }
> else {
> croak("No such arg in hvfetch\n");
> }
>}
>
>MODULE = Util PACKAGE = Util
>
>void
>StereoToGeoCoordinates(rv_stereo,rv_geo)
> SV *rv_stereo
> SV *rv_geo = NO_INIT
>PREINIT:
> StereographicCoordinatesT stereo;
> GeographicCoordinateT geo;
>CODE:
> if (SvROK(rv_stereo) && (SvTYPE(SvRV(rv_stereo)) == SVt_PVHV)) {
>
> HV *hv_stereo = (HV*)SvRV(rv_stereo);
> HV *hv_geo;
As far as I can see that is never initialized I think you want
HV *hv_geo = newHV();
but you could also require caller to pass a hash-ref as for hv_stereo,
(if you remove the NO_INIT above).
>
> /* Copy perl hash to C structure */
>
> stereo.x = getDouble(hv_stereo,"x");
> stereo.y = getDouble(hv_stereo,"y");
> /*Here, when I print the result of getDouble, I have something which looks like
> an address, */
> /*when I print stereo.x, its 0!!!!!*/
>
> /*C function call --will write results in geo structure */
>
> StereoToGeoCoordinates(&stereo,&geo);
>
> /*Copy data back into perl structures*/
>
> setDouble(hv_geo,"latitude",geo.latitude);
> setDouble(hv_geo,"longitude",geo.longitude);
>
> rv_geo = newRV_inc((SV*)hv_geo);
If this is a newHV() above then you probably want newRV_noinc()
> }
> else croak("arg not a hash ref\n");
>OUTPUT:
> rv_geo
--
Nick Ing-Simmons
http://www.ni-s.u-net.com/