call function does not exist in version 6. A gateway has been created. 79/17279/2
Adeline CARNIS [Thu, 1 Oct 2015 15:07:25 +0000 (17:07 +0200)]
To test: Demo GUI > Introduction: Getting started with Scilab > Call an
external routine
Demo GUI > Dynamic link > Call a C function
Demo GUI > Dynamic link > Call a Fortran subroutine

Change-Id: I610b7b4742c99153581ddf5aef24f14b5a64012f

scilab/modules/core/demos/intro/external_routine.dem.sce
scilab/modules/dynamic_link/demos/call_c.sce
scilab/modules/dynamic_link/demos/call_fortran.sce

index 356174b..f79a7cb 100644 (file)
@@ -10,20 +10,85 @@ mode(7)
 if haveacompiler() then
     //           CALLING EXTERNAL ROUTINE
 
-    foo=["void foo(double *a,double *b,double *c)";
+    f1=["void foo(double *a,double *b,double *c)";
     "{ *c = *a + *b; }"  ];
+    
+    i=["#include <stdlib.h>"
+       "#include <api_scilab.h>"
+       "#include <Scierror.h>"
+       "#include <localization.h>"
+       ""
+       "extern int foo(double *x, double *y, double *z);"
+       ""
+       "int sci_foo(char *fname, void* pvApiCtx)"
+       "{"
+       "  SciErr sciErr;"
+       ""
+       "  int m1 = 0, n1 = 0;"
+       "  double *pdVarOne = NULL;"
+       "  int *piAddressVarOne = NULL;"
+       "  int m2 = 0, n2 = 0;"
+       "  double *pdVarTwo = NULL;"
+       "  int *piAddressVarTwo = NULL;"
+       "  double *pdblOut = NULL;"
+       ""
+       "  CheckInputArgument(pvApiCtx, 1, 2);"
+       "  CheckOutputArgument(pvApiCtx, 0, 1);"
+       ""
+       "  sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddressVarOne);"
+       "  if(sciErr.iErr)"
+       "  {"
+       "    printError(&sciErr, 0);"
+       "    return 0;"
+       "  }"
+       ""
+       "  sciErr = getMatrixOfDouble(pvApiCtx, piAddressVarOne, &m1, &n1, &pdVarOne);"
+       "  if(sciErr.iErr)"
+       "  {"
+       "    printError(&sciErr, 0);"
+       "    return 0;"
+       "  }"
+       ""
+       "  sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddressVarTwo);"
+       "  if(sciErr.iErr)"
+       "  {"
+       "    printError(&sciErr, 0);"
+       "    return 0;"
+       "  }"
+       ""
+       "  sciErr = getMatrixOfDouble(pvApiCtx, piAddressVarTwo, &m2, &n2, &pdVarTwo);"
+       "  if(sciErr.iErr)"
+       "  {"
+       "    printError(&sciErr, 0);"
+       "    return 0;"
+       "  }"
+       ""
+       "  sciErr = allocMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 1, 1, 1, &pdblOut);"
+       "  if (sciErr.iErr)"
+       "  {"
+       "    printError(&sciErr, 0);"
+       "    return 0;"
+       "  }"
+       ""
+       "  foo(pdVarOne, pdVarTwo, pdblOut);"
+       ""
+       "  AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;"
+       "  ReturnArguments(pvApiCtx);"
+       "  return 0;"
+   "}"]
 
     // we use TMPDIR for compilation
 
     if ~c_link("foo") then
         path = pwd();
         chdir(TMPDIR);
-        mputl(foo,"foo.c");
-        ilib_for_link(["foo"],"foo.c",[],"c");
+        mputl(i, "sci_foo.c");
+        mputl(f1,"foo.c");
+        ilib_build(["gw_foo_c"],["foo", "sci_foo"],["foo.c", "sci_foo.c"],[]);
         exec loader.sce
         chdir(path)
     end
 
     //5+7 by C function
-    call("foo",5,1,"d",7,2,"d","out",[1,1],3,"d")
+    foo(5,7)
 end
index 270effb..a102248 100644 (file)
@@ -12,24 +12,91 @@ if haveacompiler() then
 
     // we use TMPDIR for compilation
 
-    foo=["void foo(double *a,double *b,double *c)";
+    f1=["void foo(double *a,double *b,double *c)";
     "{ *c = *a + *b; }"  ];
+    
+    i=["#include <stdlib.h>"
+       "#include <api_scilab.h>"
+       "#include <Scierror.h>"
+       "#include <localization.h>"
+       ""
+       "extern int foo(double *x, double *y, double *z);"
+       ""
+       "int sci_foo(char *fname, void* pvApiCtx)"
+       "{"
+       "  SciErr sciErr;"
+       ""
+       "  int m1 = 0, n1 = 0;"
+       "  double *pdVarOne = NULL;"
+       "  int *piAddressVarOne = NULL;"
+       "  int m2 = 0, n2 = 0;"
+       "  double *pdVarTwo = NULL;"
+       "  int *piAddressVarTwo = NULL;"
+       "  double *pdblOut = NULL;"
+       ""
+       "  CheckInputArgument(pvApiCtx, 2, 2);"
+       "  CheckOutputArgument(pvApiCtx, 0, 1);"
+       ""
+       "  sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddressVarOne);"
+       "  if(sciErr.iErr)"
+       "  {"
+       "    printError(&sciErr, 0);"
+       "    return 0;"
+       "  }"
+       ""
+       "  sciErr = getMatrixOfDouble(pvApiCtx, piAddressVarOne, &m1, &n1, &pdVarOne);"
+       "  if(sciErr.iErr)"
+       "  {"
+       "    printError(&sciErr, 0);"
+       "    return 0;"
+       "  }"
+       ""
+       "  sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddressVarTwo);"
+       "  if(sciErr.iErr)"
+       "  {"
+       "    printError(&sciErr, 0);"
+       "    return 0;"
+       "  }"
+       ""
+       "  sciErr = getMatrixOfDouble(pvApiCtx, piAddressVarTwo, &m2, &n2, &pdVarTwo);"
+       "  if(sciErr.iErr)"
+       "  {"
+       "    printError(&sciErr, 0);"
+       "    return 0;"
+       "  }"
+       ""
+       "  sciErr = allocMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 1, 1, 1, &pdblOut);"
+       "  if (sciErr.iErr)"
+       "  {"
+       "    printError(&sciErr, 0);"
+       "    return 0;"
+       "  }"
+       ""
+       "  foo(pdVarOne, pdVarTwo, pdblOut);"
+       ""
+       "  AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;"
+       "  ReturnArguments(pvApiCtx);"
+       "  return 0;"
+   "}"]
 
     mprintf("\n");
     mprintf(gettext("Calling a C function from Scilab.\n"));
 
-    disp(foo);
+    disp(f1);
 
     if ~c_link("foo") then
         cur_ilib_verbose = ilib_verbose();
         ilib_verbose(1);
         path = pwd();
         chdir(TMPDIR);
-        mputl(foo,"foo.c");
+        mputl(f1,"foo.c");
+        mputl(i,"sci_foo.c");
 
         mprintf("\n");
         mprintf(gettext("Calling ilib_for_link to build C function.\n"));
-        ilib_for_link(["foo"],"foo.c",[],"c");
+        lib_ = ilib_for_link(["foo"], "foo.c", [],"c");
+        link(lib_, "foo", "c");
+        ilib_build("gw_foo",["foo" "sci_foo"],"sci_foo.c",basename(lib_));
         exec loader.sce ;
         chdir(path);
         ilib_verbose(cur_ilib_verbose);
@@ -45,10 +112,10 @@ if haveacompiler() then
     mprintf("\n");
     mprintf(gettext("with Y = %d"), Y);
     mprintf("\n");
-    mprintf("Z = call(''foo'', X, 1, ''d'', Y, 2, ''d'', ''out'', [1,1], 3, ''d'');");
+    mprintf("Z = foo(X, Y);");
     mprintf("\n");
-    Z = call("foo", X, 1, "d", Y, 2, "d", "out", [1,1], 3, "d");
+    Z = foo(X, Y);
     mprintf(gettext("Result Z = %d"), Z);
     mprintf("\n");
 
-end
\ No newline at end of file
+end
index d310dfb..616f2db 100644 (file)
@@ -9,27 +9,94 @@ if haveacompiler() then
 
     //           CALLING EXTERNAL FORTRAN SUBROUTINE
 
-    foof = ["      subroutine foof(a,b,c)";
+    f1f = ["      subroutine foof(a,b,c)";
     "      double precision  a,b,c";
     "      c=a+b ";
     "      return"; "       end"];
+    
+    i=["#include <stdlib.h>"
+       "#include <api_scilab.h>"
+       "#include <Scierror.h>"
+       "#include <localization.h>"
+       ""
+       "extern int C2F(foof)(double *x, double *y, double *z);"
+       ""
+       "int sci_foof(char *fname, void* pvApiCtx)"
+       "{"
+       "  SciErr sciErr;"
+       ""
+       "  int m1 = 0, n1 = 0;"
+       "  double *pdVarOne = NULL;"
+       "  int *piAddressVarOne = NULL;"
+       "  int m2 = 0, n2 = 0;"
+       "  double *pdVarTwo = NULL;"
+       "  int *piAddressVarTwo = NULL;"
+       "  double *pdblOut = NULL;"
+       ""
+       "  CheckInputArgument(pvApiCtx, 2, 2);"
+       "  CheckOutputArgument(pvApiCtx, 0, 1);"
+       ""
+       "  sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddressVarOne);"
+       "  if(sciErr.iErr)"
+       "  {"
+       "    printError(&sciErr, 0);"
+       "    return 0;"
+       "  }"
+       ""
+       "  sciErr = getMatrixOfDouble(pvApiCtx, piAddressVarOne, &m1, &n1, &pdVarOne);"
+       "  if(sciErr.iErr)"
+       "  {"
+       "    printError(&sciErr, 0);"
+       "    return 0;"
+       "  }"
+       ""
+       "  sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddressVarTwo);"
+       "  if(sciErr.iErr)"
+       "  {"
+       "    printError(&sciErr, 0);"
+       "    return 0;"
+       "  }"
+       ""
+       "  sciErr = getMatrixOfDouble(pvApiCtx, piAddressVarTwo, &m2, &n2, &pdVarTwo);"
+       "  if(sciErr.iErr)"
+       "  {"
+       "    printError(&sciErr, 0);"
+       "    return 0;"
+       "  }"
+       ""
+       "  sciErr = allocMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 1, 1, 1, &pdblOut);"
+       "  if (sciErr.iErr)"
+       "  {"
+       "    printError(&sciErr, 0);"
+       "    return 0;"
+       "  }"
+       ""
+       "  C2F(foof)(pdVarOne, pdVarTwo, pdblOut);"
+       ""
+       "  AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;"
+       "  ReturnArguments(pvApiCtx);"
+       "  return 0;"
+   "}"]
 
     mprintf("\n");
     mprintf(gettext("Calling a Fortran subroutine from Scilab.\n"));
 
-    disp(foof);
+    disp(f1f);
 
     // we use TMPDIR for compilation
 
     if ~c_link("foof") then
         path = pwd();
         chdir(TMPDIR);
-        mputl(foof,"foof.f");
+        mputl(f1f,"foof.f");
+        mputl(i,"sci_foof.c");
 
         mprintf("\n");
         mprintf(gettext("Calling ilib_for_link to build a Fortran subroutine.\n"));
 
-        ilib_for_link(["foof"],"foof.f",[],"f");
+        lib_ = ilib_for_link(["foof"], "foof.f", [],"f");
+        link(lib_, "foof", "f");
+        ilib_build("gw_foof",["foof" "sci_foof"],"sci_foof.c",basename(lib_));
         exec loader.sce ;
         chdir(path)
     end
@@ -45,10 +112,10 @@ if haveacompiler() then
     mprintf("\n");
     mprintf(gettext("with Y = %d"), Y);
     mprintf("\n");
-    mprintf("Z = call(''foof'', X, 1, ''d'', Y, 2, ''d'', ''out'', [1,1], 3, ''d'');");
+    mprintf("Z = foof(X, Y);");
     mprintf("\n");
-    Z = call("foof", X, 1, "d", Y, 2, "d", "out", [1,1], 3, "d");
+    Z = foof(X, Y);
     mprintf(gettext("Result Z = %d"), Z);
     mprintf("\n");
 
-end
\ No newline at end of file
+end