I am familiar with Ruby / DL, but not sure how to use C function calls with pointers for returned parameters

I have this function in this module

require 'dl'
require 'dl/import'

module LibCalendars
  extend DL::Importer
  dlload './cal2jd.o'

  extern 'int iauCal2jd(int, int, int, double *, double *)'
end

How to set this in a module to access pointers?

What do I need in part there? I'm just not sure. How is this done correctly? The exact code is at http://www.iausofa.org/2013_1202_C/sofa/cal2jd.c I need to know how to access these address pointers. Validation testing here http://www.iausofa.org/2013_1202_C/sofa/t_sofa_c.c

An almost equivalent core C would look something like this following code. I just made a copy of cal2jd.c and applied the main function at the bottom.

#include <stdio.h>
int
main()
{
 int y = 2003, m = 6, d = 1;
 int j;
 double djm0, djm;
 double *pdjm0 = &djm0;
 double *pdjm  = &djm;  

 printf("values are: y == %d, m == %d, d == %d\n", y, m, d);

 j = iauCal2jd(y, m, d, &djm0, &djm);

 printf("j == %d\n", j);
 printf("address of &djm0 == %p, size of pointer == %d\n", &djm0, sizeof(*pdjm0));
 printf("address of &djm  == %p, size of pointer == %d\n", &djm, sizeof(*pdjm0));
 printf("value from &djm0 == %.20g, size of djm0 == %d\n", *pdjm0, sizeof(djm0));
 printf("value from &djm  == %.20g, size of djm  == %d\n", *pdjm, sizeof(djm));
 printf("value from &djm0 == %.3f\n", (float)*pdjm0);
 printf("value from &djm  == %.3f\n", (float)*pdjm);

 return 0;
 }

I will compile it with> gcc -o cal2jd cal2jd.c

Here is my conclusion

$ cal2jd
values are: y == 2003, m == 6, d == 1
j == 0
address of &djm0 == 0022FF30, size of pointer == 8
address of &djm  == 0022FF28, size of pointer == 8
value from &djm0 == 2400000.5, size of djm0 == 8
value from &djm  == 52791, size of djm  == 8
value from &djm0 == 2400000.500
value from &djm  == 52791.000

, djm0 sofam.h 2400000.5 , . , djm.

. C SOFA Ruby. ( , JRuby). C, DL FFI .

ffi. .

require 'ffi'

module MyLibrary
  extend FFI::Library
  ffi_lib "cal2jd.so"
  attach_function :iauCal2jd, [:int, :int, :int, :pointer, :pointer], :int
end

JRuby. , . ?   .

+4
2

- ruby ​​ jruby, C, : cfunction - .

, f, :

void f(void (*)(int , int , double *));

f :

f(cfunction);
+1

, . , - . C . C, . cal2jd.so.

require 'ffi'

module Calendars
  extend FFI::Library

  ffi_lib 'cal2jd.so'

  attach_function :iauCal2jd, [:int, :int, :int, :pointer, :pointer ], :int

end

mp1 = FFI::MemoryPointer.new(:double, 8)
mp2 = FFI::MemoryPointer.new(:double, 8)
cal = Calendars.iauCal2jd(2014, 4, 6, mp1, mp2 )
puts cal

puts mp1.get_double, mp2.get_double

0 # return result good
2400000.5
56753.0  # pretty soon we're gonna have a straight 56789 ;-)

, - . , .

+1

Source: https://habr.com/ru/post/1534713/


All Articles