From 4e9cbd8a7c3592a1caef5e84fb19792e86caccb0 Mon Sep 17 00:00:00 2001 From: Baozeng Ding Date: Sat, 13 Jun 2009 12:18:35 +0000 Subject: [PATCH] add support for constants git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/branches/gsoc2009-sploving@11251 626c5289-ae23-0410-ae9c-e8d60b6d4f22 --- Doc/Manual/Scilab.html | 81 ++++++++++-- Examples/Makefile.in | 25 ++++ Examples/scilab/constants/example.i | 27 ++++ Examples/scilab/constants/makefile | 15 +++ Examples/scilab/constants/runme.sci | 32 +++++ Examples/scilab/simple/makefile | 15 +++ Examples/scilab/simple/runme.sci | 5 +- Examples/scilab/variables/example.c | 46 +++++++ Examples/scilab/variables/example.i | 27 ++++ Examples/scilab/variables/makefile | 15 +++ Examples/scilab/variables/runme.sci | 45 +++++++ Lib/scilab/scilab.swg | 2 +- Lib/scilab/sciprimtypes.swg | 1 + Lib/scilab/scitypemaps.swg | 194 +++++++++++++--------------- Source/Modules/scilab.cxx | 82 ++++++++---- 15 files changed, 470 insertions(+), 142 deletions(-) create mode 100644 Examples/scilab/constants/example.i create mode 100644 Examples/scilab/constants/makefile create mode 100644 Examples/scilab/constants/runme.sci create mode 100644 Examples/scilab/simple/makefile create mode 100644 Examples/scilab/variables/example.c create mode 100644 Examples/scilab/variables/example.i create mode 100644 Examples/scilab/variables/makefile create mode 100644 Examples/scilab/variables/runme.sci create mode 100644 Lib/scilab/sciprimtypes.swg diff --git a/Doc/Manual/Scilab.html b/Doc/Manual/Scilab.html index e9347e810..cc6af72f0 100644 --- a/Doc/Manual/Scilab.html +++ b/Doc/Manual/Scilab.html @@ -23,6 +23,7 @@
  • Modules
  • Functions
  • Global variables +
  • Constants @@ -66,7 +67,7 @@ To build an Scilab module, run SWIG using the -scilab option.
    $ swig -scilab example.i 

    -This creates a C source file example_wrap.cand a interface file builder.sce. The generated C source file contains the low-level wrappers that need to be compiled and linked with the rest of your C application (in this case, the gcd implementation) to create an extension module. And the builder.sce is used to generate the *.so file. +This creates a C source file example_wrap.c and a interface file builder.sce. The generated C source file contains the low-level wrappers that need to be compiled and linked with the rest of your C application (in this case, the gcd implementation) to create an extension module. And the builder.sce is used to generate the *.so file.

    @@ -89,7 +90,7 @@ $ ./scilab

    where builder.sce is the interface file generated by the swig. It looks like the following:

    -
    +
     ilib_name = "examplelib";
     files = ["example_wrap.c","example.o"];
     libs = [];
    @@ -97,16 +98,21 @@ table = ["gcd","_wrap_gcd";"Foo_set","_wrap_Foo_set";"Foo_get","_wrap_Foo_get";]
     ilib_build(ilib_name,table,files,libs);
     
    - -

    -"ilib_name" is the name of the lib we want to build. "table" contains the name of the C file and its wrapper file. "files" represent the .o file we want to compile, and"libs" is other libs we want to use. +

    ilib_build(lib_name,table,files,libs) is used to create shared libraries and to generate a loader file which can be used to dynamically load the shared library into Scilab with addinter.

    +
      +
    • ilib_name: a character string, the generic name of the library without path and extension.
    • +
    • files: string matrix giving objects files needed for shared library creation.
    • +
    • libs: string matrix giving extra libraries needed for shred library creation.
    • +
    • table: two column string matrix giving the table of pairs 'scilab-name', 'interface name'.
    • +
    +

    "exec builder.sce" will produce *.so,and a file called "loader.sce" which contains how to load the module. Loading it into Scilab is then a matter of invoking

    -
    Scilab:1> exec loader.sce
    +
    Scilab:1> exec loader.sce

    36.2.2 Using your module

    @@ -119,9 +125,12 @@ Assuming all goes well, you will be able to do this:
     Scilab:2>gcd(4,6)
     ans =  2
    +
     Scilab:3>Foo_get
     ans =  3
    +
     Scilab:4>Foo_set(4);
    +
     Scilab:5>Foo_get
     ans =  4 
    @@ -135,7 +144,7 @@ ans = 4
    The SWIG module directive specifies the name of the Scilab module. If you want to load the module, you'll need a file called "loader.sce" which is usually generated by the command "exec builder.sce". The loader.sce looks as following:

    -
    +    
     // ------------------------------------------------------
     // generated by builder.sce: Please do not edit this file
     // ------------------------------------------------------
    @@ -153,6 +162,15 @@ clear get_file_path;
     // ------------------------------------------------------
     
     
    +

    addinter (files,spname,fcts) performs incremental linking of a compiled C new Scilab interface routine. +

    +
      +
    • files: a character string or a vector of character string contain object files used to define the new Scilab interface routine (interface code, user routines or libraries, system libraries).
    • +
    • spname: a character string. Name of interface routine entry point.
    • +
    • fcts: vector of character strings. The name of new Scilab function implemented in the new interface.
    • +
    + +

    After you run the command "exec loader.sce", you could use the module.

    @@ -176,16 +194,55 @@ ant=24

    27.3.3 Global variables

    - To expose variables, SWIG actually generates two functions, to get and set the value. In this case, Foo_set and Foo_set would be generated. SWIG then automatically calls these functions when you get and set the variable-- in the former case creating a local copy in the interpreter of the C variables, and in the latter case copying an interpreter variables onto the C variable. + To expose variables, SWIG actually generates two functions, to get and set the value. In this case, Foo_set and Foo_get would be generated. SWIG then automatically calls these functions when you get and set the variable-- in the former case creating a local copy in the interpreter of the C variables, and in the latter case copying an interpreter variables onto the C variable.

    scilab:1> exec loader.sce;
    -scilab:2> c=Foo
    -c =  3
    -scilab:3> Foo=4;
    +scilab:2> c=Foo_get();
    +
    +scilab:3> Foo_set(4);
    +
     scilab:4> c
     c =  3
    -scilab:5> Foo
    +
    +scilab:5> Foo_get()
     ans =  4
    +

    27.3.4 Constants

    + + +

    + C constants are not really constant in Scilab. They are actually just a copy of the value into the Scilab interpreter. Therefore they can be changed just as any other value. For example given some constants: +

    + +
    %module example
    +%constant int ICONST=42;
    +#define    SCONST      "Hello World"
    +
    + +

    + This is 'effectively' converted into the following code in the wrapper file: +

    + +
    ....
    +const int ICONST=42;
    +const char * SCONST="Hello World";
    +....
    +int ICONST_get (char *fname,unsigned long fname_len) {..}
    +int SCONST_get (char *fname,unsigned long fname_len) {..}
    +.... 
    +

    It is easy to use the C constants as global variables:

    + +
    +scilab:1> ICONST
    +ant = 42
    +
    +scilab:2> SCONST
    +ant= Hello world
    +
    +scilab:3> c=SCONST()
    +c = Hello World  
    +
    + + diff --git a/Examples/Makefile.in b/Examples/Makefile.in index d6dbfdeeb..e1b4c2108 100644 --- a/Examples/Makefile.in +++ b/Examples/Makefile.in @@ -1135,3 +1135,28 @@ r_clean: rm -f *.@OBJEXT@ *@SO@ NAMESPACE rm -f $(RRSRC) runme.Rout .RData +################################################################## +##### SCILAB ###### +################################################################## + +# Make sure these locate your Octave installation +SCILAB_INCLUDE= $(DEFS) @SCILABINCLUDE@ +SCILAB_LIB = @SCILABLIB@ +SCILAB = @SCILAB@ + + +# ---------------------------------------------------------------- +# Build a C dynamically loadable module +# ---------------------------------------------------------------- + +scilab: $(SRCS) + $(SWIG) -scilab $(SWIGOPT) $(INTERFACEPATH) + + +# ----------------------------------------------------------------- +# Cleaning the scilab examples +# ----------------------------------------------------------------- + +scilab_clean: + rm -f *_wrap* + diff --git a/Examples/scilab/constants/example.i b/Examples/scilab/constants/example.i new file mode 100644 index 000000000..4f7b1a4d7 --- /dev/null +++ b/Examples/scilab/constants/example.i @@ -0,0 +1,27 @@ +/* File : example.i */ +%module example + +/* A few preprocessor macros */ + +#define ICONST 42 +#define FCONST 2.1828 +#define CCONST 'x' +#define CCONST2 '\n' +#define SCONST "Hello World" +#define SCONST2 "\"Hello World\"" + +/* This should work just fine */ +#define EXPR ICONST + 3*(FCONST) + +/* This shouldn't do anything */ +#define EXTERN extern + +/* Neither should this (BAR isn't defined) */ +#define FOO (ICONST + BAR) + +/* The following directives also produce constants */ + +%constant int iconst = 37; +%constant double fconst = 3.14; + + diff --git a/Examples/scilab/constants/makefile b/Examples/scilab/constants/makefile new file mode 100644 index 000000000..f35cee60e --- /dev/null +++ b/Examples/scilab/constants/makefile @@ -0,0 +1,15 @@ +TOP = ../.. +SWIG = $(TOP)/../preinst-swig +SRCS = example.i +TARGET = example +INTERFACE = example.i + +all:: + $(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \ + TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' scilab + +clean:: + $(MAKE) -f $(TOP)/Makefile scilab_clean + rm -f *.sce *.so lib*lib.c + +check: all diff --git a/Examples/scilab/constants/runme.sci b/Examples/scilab/constants/runme.sci new file mode 100644 index 000000000..e88018958 --- /dev/null +++ b/Examples/scilab/constants/runme.sci @@ -0,0 +1,32 @@ +// builder the *.so +exec builder.sce; + +//loader the *.so +exec loader.sce; + +printf("ICONST = %i (should be 42)\n", ICONST()); +printf("FCONST = %f (should be 2.1828)\n", FCONST()); +printf("CCONST = %c (should be x)\n", CCONST()); +printf("CCONST2 = %s (this should be on a new line)\n", CCONST2()); +printf("SCONST = %s (should be Hello World)\n", SCONST()); +printf("SCONST2 = %s (should be Hello World)\n", SCONST2()); +printf("EXPR = %f (should be 48.5484)\n", EXPR()); +printf("iconst = %i (should be 37)\n", iconst()); +printf("fconst = %f (should be 3.14)\n", fconst()); + +try + printf("EXTERN = %s (Arg! This should not printf(anything)\n", EXTERN()); +catch + printf("EXTERN is not defined (good)\n"); + +try + printf("FOO = %i (Arg! This should not printf(anything)\n", FOO()); +catch + printf("FOO is not defined (good)\n"); + + + + + + + diff --git a/Examples/scilab/simple/makefile b/Examples/scilab/simple/makefile new file mode 100644 index 000000000..e9a70a676 --- /dev/null +++ b/Examples/scilab/simple/makefile @@ -0,0 +1,15 @@ +TOP = ../.. +SWIG = $(TOP)/../preinst-swig +SRCS = example.c +TARGET = example +INTERFACE = example.i + +all:: + $(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \ + TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' scilab + +clean:: + $(MAKE) -f $(TOP)/Makefile scilab_clean + rm -f *.sce *.so lib*lib.c + +check: all diff --git a/Examples/scilab/simple/runme.sci b/Examples/scilab/simple/runme.sci index 1777a7750..57655a2b0 100644 --- a/Examples/scilab/simple/runme.sci +++ b/Examples/scilab/simple/runme.sci @@ -5,6 +5,7 @@ exec builder.sce; exec loader.sce; // Call our gcd() function + x = 42; y = 105; g = gcd(x,y); @@ -16,8 +17,8 @@ printf("The gcd of %d and %d is %d\n",x,y,g); Foo_get() // Change its value -Foo_set = 3.1415926 +Foo_set(3.1415926) //See if the change took effect -Foo_get +Foo_get() diff --git a/Examples/scilab/variables/example.c b/Examples/scilab/variables/example.c new file mode 100644 index 000000000..2dd388706 --- /dev/null +++ b/Examples/scilab/variables/example.c @@ -0,0 +1,46 @@ +/* File : example.c */ + +/* I'm a file containing some C global variables */ + +/* Deal with Microsoft's attempt at deprecating C standard runtime functions */ +#if !defined(SWIG_NO_CRT_SECURE_NO_DEPRECATE) && defined(_MSC_VER) +# define _CRT_SECURE_NO_DEPRECATE +#endif + +#include +#include +#include "sciprint.h" + +int ivar = 0; +short svar = 0; +long lvar = 0; +unsigned int uivar = 0; +unsigned short usvar = 0; +unsigned long ulvar = 0; +signed char scvar = 0; +unsigned char ucvar = 0; +char cvar = 0; +float fvar = 0; +double dvar = 0; +char *strvar=0; + + +/* A debugging function to print out their values */ + +void print_vars() { + sciprint("ivar = %d\n", ivar); + sciprint("svar = %d\n", svar); + sciprint("lvar = %ld\n", lvar); + sciprint("uivar = %u\n", uivar); + sciprint("usvar = %u\n", usvar); + sciprint("ulvar = %lu\n", ulvar); + sciprint("scvar = %d\n", scvar); + sciprint("ucvar = %u\n", ucvar); + sciprint("fvar = %g\n", fvar); + sciprint("dvar = %g\n", dvar); + sciprint("cvar = %c\n", cvar); + sciprint("strvar = %s\n",strvar); +} + + + diff --git a/Examples/scilab/variables/example.i b/Examples/scilab/variables/example.i new file mode 100644 index 000000000..a7c198f40 --- /dev/null +++ b/Examples/scilab/variables/example.i @@ -0,0 +1,27 @@ +/* File : example.i */ +%module example + +#pragma SWIG nowarn=SWIGWARN_TYPEMAP_SWIGTYPELEAK + +/* Some global variable declarations */ +%inline %{ + extern int ivar; + extern short svar; + extern long lvar; + extern unsigned int uivar; + extern unsigned short usvar; + extern unsigned long ulvar; + extern signed char scvar; + extern unsigned char ucvar; + extern char cvar; + extern float fvar; + extern double dvar; + extern char *strvar; +%} + + +/* Some helper functions to make it easier to test */ +%inline %{ +extern void print_vars(); +%} + diff --git a/Examples/scilab/variables/makefile b/Examples/scilab/variables/makefile new file mode 100644 index 000000000..e9a70a676 --- /dev/null +++ b/Examples/scilab/variables/makefile @@ -0,0 +1,15 @@ +TOP = ../.. +SWIG = $(TOP)/../preinst-swig +SRCS = example.c +TARGET = example +INTERFACE = example.i + +all:: + $(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \ + TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' scilab + +clean:: + $(MAKE) -f $(TOP)/Makefile scilab_clean + rm -f *.sce *.so lib*lib.c + +check: all diff --git a/Examples/scilab/variables/runme.sci b/Examples/scilab/variables/runme.sci new file mode 100644 index 000000000..cf42539f0 --- /dev/null +++ b/Examples/scilab/variables/runme.sci @@ -0,0 +1,45 @@ +// builder the *.so +exec builder.sce + +//loader the *.so +exec loader.sce + +// Try to set the values of some global variables + +ivar_set (42); +svar_set (31000); +lvar_set (65537); +uivar_set (123456); +usvar_set (61000); +ulvar_set (654321); +scvar_set (-13); +ucvar_set (251); +cvar_set ("S"); +fvar_set (3.14159); +dvar_set (2.1828); +strvar_set("Hello World"); + +// Now print out the values of the variables + +printf("Variables (values printed from Scilab)\n"); + +printf("ivar = %i\n", ivar_get()); +printf("svar = %i\n", svar_get()); +printf("lvar = %i\n", lvar_get()); +printf("uivar = %i\n", uivar_get()); +printf("usvar = %i\n", usvar_get()); +printf("ulvar = %i\n", ulvar_get()); +printf("scvar = %i\n", scvar_get()); +printf("ucvar = %i\n", ucvar_get()); +printf("fvar = %f\n", fvar_get()); +printf("dvar = %f\n", dvar_get()); +printf("cvar = %s\n", cvar_get()); +printf("strvar = %s\n", strvar_get()); + +printf("\nVariables (values printed from C)\n"); + +print_vars() + + + + diff --git a/Lib/scilab/scilab.swg b/Lib/scilab/scilab.swg index ec101d74e..b02ccc409 100644 --- a/Lib/scilab/scilab.swg +++ b/Lib/scilab/scilab.swg @@ -1,5 +1,5 @@ %include %include -%include +%include %include diff --git a/Lib/scilab/sciprimtypes.swg b/Lib/scilab/sciprimtypes.swg new file mode 100644 index 000000000..8b1378917 --- /dev/null +++ b/Lib/scilab/sciprimtypes.swg @@ -0,0 +1 @@ + diff --git a/Lib/scilab/scitypemaps.swg b/Lib/scilab/scitypemaps.swg index 539969bc3..859a34e9e 100644 --- a/Lib/scilab/scitypemaps.swg +++ b/Lib/scilab/scitypemaps.swg @@ -12,131 +12,115 @@ //%include -%typemap(in) char (int *piAddrVar, int iRows, int iCols), - signed char (int *piAddrVar, int iRows, int iCols), - unsigned char(int *piAddrVar, int iRows, int iCols) -{ - char* _piData8; - getVarAddressFromNumber($argnum, &piAddrVar); - getVarDimension(piAddrVar, &iRows, &iCols); - if(getVarType(piAddrVar) != sci_matrix || iRows1 != 1 || iCols1 != 1 || isVarComplex(piAddrVar)) - { - Scierror(999,_("%s: Wrong type for input argument #%d: Real scalar expected.\n"), fname, 1); - } - getMatrixOfInteger8(piAddrVar, &iRows, &iCols, &_piData8); - $1=_piData8[0]; +%typemap(in) signed char (int *piAddrVar, int iRows, int iCols), + unsigned char (int *piAddrVar, int iRows, int iCols), + short (int *piAddrVar, int iRows, int iCols), + unsigned short (int *piAddrVar, int iRows, int iCols), + int (int *piAddrVar, int iRows, int iCols), + unsigned int (int *piAddrVar, int iRows, int iCols), + long (int *piAddrVar, int iRows, int iCols), + unsigned long (int *piAddrVar, int iRows, int iCols), + float (int *piAddrVar, int iRows, int iCols), + double (int *piAddrVar, int iRows, int iCols) { + double* _piData; + getVarAddressFromNumber($argnum, &piAddrVar); + getVarDimension(piAddrVar, &iRows, &iCols); + + if (getVarType(piAddrVar) != sci_matrix || iRows != 1 || iCols != 1 || isVarComplex(piAddrVar)) { + Scierror(999, _("%s: Wrong type for input argument #%d: Real scalar expected.\n"), fname, 1); + } + getMatrixOfDouble(piAddrVar, &iRows, &iCols, &_piData); + $1=($1_ltype)*_piData; } -%typemap(in) short (int *piAddrVar, int iRows, int iCols), - unsigned short (int *piAddrVar, int iRows, int iCols) - -{ short* _piData16; - getVarAddressFromNumber($argnum, &piAddrVar); - getVarDimension(piAddrVar, &iRows, &iCols); - if(getVarType(piAddrVar) != sci_matrix || iRows1 != 1 || iCols1 != 1 || isVarComplex(piAddrVar)) - { - Scierror(999,_("%s: Wrong type for input argument #%d: Real scalar expected.\n"), fname, 1); - } - getMatrixOfInteger16(piAddrVar, &iRows, &iCols, &_piData16); - $1= _piData16[0]; +%typemap(in) char (int *piAddrVar, int iRows, int iCols) { + char* _pstStrings; + int _piLength; + getVarAddressFromNumber($argnum, &piAddrVar); + getVarDimension(piAddrVar, &iRows, &iCols); + + if (getVarType(piAddrVar) != sci_strings || iRows != 1 || iCols != 1 || isVarComplex(piAddrVar)) { + Scierror(999, _("%s: Wrong type for input argument #%d: Real scalar expected.\n"), fname, 1); + } + getMatrixOfString(piAddrVar, &iRows, &iCols,&_piLength, &_pstStrings); + $1=($1_ltype)*_pstStrings; } -%typemap(in) int (int *piAddrVar, int iRows, int iCols), - unsigned int (int *piAddrVar, int iRows, int iCols) - -{ int* _piData32; - getVarAddressFromNumber($argnum, &piAddrVar); - getVarDimension(piAddrVar, &iRows, &iCols); - if(getVarType(piAddrVar) != sci_matrix || iRows1 != 1 || iCols1 != 1 || isVarComplex(piAddrVar)) - { - Scierror(999,_("%s: Wrong type for input argument #%d: Real scalar expected.\n"), fname, 1); - } - getMatrixOfInteger16(piAddrVar, &iRows, &iCols, &_piData32); - $1= _piData32[0]; - -} - -%typemap(in) long(int* piAddrVar,int iRows,int iCols), - unsigned long(int* piAddrVar,int iRows,int iCols), - double(int* piAddrVar,int iRows,int iCols), - float (int* piAddrVar,int iRows,int iCols) - -{ double *pdblReal; - getVarAddressFromNumber($argnum, &piAddrVar); - getVarDimension(piAddrVar, &iRows, &iCols); - if(getVarType(piAddrVar) != sci_matrix || iRows1 != 1 || iCols1 != 1 || isVarComplex(piAddrVar)) - { - Scierror(999,_("%s: Wrong type for input argument #%d: Real scalar expected.\n"), fname, 1); - } - getMatrixOfDouble(piAddrVar, &iRows, &iCols, &pdblReal); - $1= pdblReal[0]; - -} - -/* -%typemap(in) char *(int m,int n,int l) -{ - if (GetType($argnum) == sci_strings) - { - GetRhsVar($argnum,STRING_DATATYPE,&m,&n,&l); - $1=($1_ltype)strdup(cstk(l)); - } - else - Scierror(999,"error ...\n"); - -}*/ - - -%typemap(out) char (int iRowsOut,int iColsOut,int* _piAddress), - signed char (int iRowsOut,int iColsOut,int* _piAddress), - unsigned char(int iRowsOut,int iColsOut,int* _piAddress) -{ - iRowsOut=1; - iColsOut=1; - createMatrixOfInteger8(Rhs+1, iRowsOut, iColsOut, &$1, &_piAddress); - LhsVar(1)=Rhs+1; - PutLhsVar(); +%typemap(out) signed char (int iRowsOut,int iColsOut,int* _piAddress) { + char temp; + temp=(char)$1; + iRowsOut=1; + iColsOut=1; + createMatrixOfInteger8(Rhs+1, iRowsOut, iColsOut, &temp, &_piAddress); + LhsVar(1)=Rhs+1; } %typemap(out) short (int iRowsOut,int iColsOut,int* _piAddress), - unsigned short(int iRowsOut,int iColsOut,int* _piAddress) - -{ - iRowsOut=1; - iColsOut=1; - createMatrixOfInteger16(Rhs+1, iRowsOut, iColsOut, &$1, &_piAddress); - LhsVar(1)=Rhs+1; - PutLhsVar(); + unsigned char (int iRowsOut,int iColsOut,int* _piAddress) { + short temp; + temp=(short)$1; + iRowsOut=1; + iColsOut=1; + createMatrixOfInteger16(Rhs+1, iRowsOut, iColsOut, &temp, &_piAddress); + LhsVar(1)=Rhs+1; } %typemap(out) int (int iRowsOut,int iColsOut,int* _piAddress), - unsigned int(int iRowsOut,int iColsOut,int* _piAddress) - -{ + unsigned int (int iRowsOut,int iColsOut,int* _piAddress), + unsigned short (int iRowsOut,int iColsOut,int* _piAddress), + unsigned long (int iRowsOut,int iColsOut,int* _piAddress), + long (int iRowsOut,int iColsOut,int* _piAddress) { + int temp; + temp=(int)$1; iRowsOut=1; iColsOut=1; - createMatrixOfInteger32(Rhs+1, iRowsOut, iColsOut, &$1, &_piAddress); + createMatrixOfInteger32(Rhs+1, iRowsOut, iColsOut, &temp, &_piAddress); LhsVar(1)=Rhs+1; - PutLhsVar(); } -%typemap(out) long(int iRowsOut,int iColsOut,int* _piAddress), - unsigned long(int iRowsOut,int iColsOut,int* _piAddress), - double(int iRowsOut,int iColsOut,int* _piAddress), - float(int iRowsOut,int iColsOut,int* _piAddress) -{ - iRowsOut=1; - iColsOut=1; - createMatrixDouble(Rhs+1, iRowsOut, iColsOut, &$1, &_piAddress); - LhsVar(1)=Rhs+1; - PutLhsVar(); +%typemap(out) double (int iRowsOut,int iColsOut,int* _piAddress), + float (int iRowsOut,int iColsOut,int* _piAddress) { + double temp; + temp=(double)$1; + iRowsOut=1; + iColsOut=1; + createMatrixOfDouble(Rhs+1, iRowsOut, iColsOut, &temp, &_piAddress); + LhsVar(1)=Rhs+1; +} + +%typemap(out) char (int iRowsOut,int iColsOut,int* _piAddress) { + char* temp; + temp=(char*)&$1; + iRowsOut=1; + iColsOut=1; + createMatrixOfString(Rhs+1, iRowsOut, iColsOut, &temp, &_piAddress); + LhsVar(1)=Rhs+1; } -%typemap(out,noblock=1) void -{ +%typemap(out,noblock=1) void { +} + +%typemap(in) char *(int *piAddrVar, int iRows, int iCols) { + char* _pstStrings; + int _piLength; + getVarAddressFromNumber($argnum, &piAddrVar); + getVarDimension(piAddrVar, &iRows, &iCols); + + if (getVarType(piAddrVar) != sci_strings || iRows != 1 || iCols != 1 || isVarComplex(piAddrVar)) { + Scierror(999, _("%s: Wrong type for input argument #%d: Real scalar expected.\n"), fname, 1); + } + getMatrixOfString(piAddrVar, &iRows, &iCols,&_piLength, &_pstStrings); + $1=strdup(_pstStrings); +} + +%typemap(out) char *(int iRowsOut,int iColsOut,int* _piAddress){ + iRowsOut=1; + iColsOut=1; + createMatrixOfString(Rhs+1, iRowsOut, iColsOut, &$1, &_piAddress); + LhsVar(1)=Rhs+1; } diff --git a/Source/Modules/scilab.cxx b/Source/Modules/scilab.cxx index 106c688ba..fd2a0f162 100644 --- a/Source/Modules/scilab.cxx +++ b/Source/Modules/scilab.cxx @@ -72,7 +72,7 @@ class SCILAB:public Language { f_begin = NewFile(outfile, "w", SWIG_output_files()); /*Another output file to generate the .so or .dll */ - String *builder = NewString("builder.sce"); + String *builder = NewStringf("%sbuilder.sce",SWIG_output_directory()); f_builder=NewFile(builder,"w",SWIG_output_files()); /* Initialize all of the output files */ @@ -99,6 +99,9 @@ class SCILAB:public Language { Printf(f_runtime, "#include \"stack-c.h\"\n"); Printf(f_runtime, "#include \"sciprint.h\"\n"); Printf(f_runtime, "#include \"Scierror.h\"\n"); + Printf(f_runtime, "#include \"variable_api.h\"\n"); + Printf(f_runtime, "#include \"localization.h\"\n"); + /*Initialize the builder.sce file*/ Printf(f_builder,"ilib_name = \"%slib\";\n",module); @@ -166,7 +169,7 @@ class SCILAB:public Language { if (overloaded) Append(overname, Getattr(n, "sym:overname")); - Printv(f->def, "int ", overname, " (char *fname){", NIL); + Printv(f->def, "int ", overname, " (char *fname,unsigned long fname_len) {", NIL); // Emit all of the local variables for holding arguments emit_parameter_variables(l, f); @@ -209,8 +212,8 @@ class SCILAB:public Language { } String *getargs = NewString(""); Printv(getargs, tm, NIL); - Printv(f->code, getargs, "\n", NIL); - Delete(getargs); + Printv(f->code, getargs, "\n", NIL); + Delete(getargs); p = Getattr(p, "tmap:in:next"); continue; } else { @@ -228,22 +231,19 @@ class SCILAB:public Language { //Insert the return variable emit_return_variable(n, d, f); - if ((tm = Swig_typemap_lookup_out("out", n, "result", f, actioncode))) { + if ((tm = Swig_typemap_lookup_out("out", n, "result", f, actioncode))) { Printf(f->code, "%s\n", tm); } - else { - Swig_warning(WARN_TYPEMAP_OUT_UNDEF, input_file, line_number, "Unable to use return type %s in function %s.\n", SwigType_str(d, 0), iname); + else { + Swig_warning(WARN_TYPEMAP_OUT_UNDEF, input_file, line_number, "Unable to use return type %s in function %s.\n", SwigType_str(d, 0), iname); } /* Insert argument output code */ String *outarg = NewString(""); for (p = l; p;) { if ((tm = Getattr(p, "tmap:argout"))) { - Replaceall(tm, "$result", "_outp"); - //Replaceall(tm, "$arg", Getattr(p, "emit:input")); - //Replaceall(tm, "$input", Getattr(p, "emit:input")); Printv(outarg, tm, "\n", NIL); p = Getattr(p, "tmap:argout:next"); } else { @@ -290,13 +290,12 @@ class SCILAB:public Language { String *getname = Swig_name_get(iname); String *setname = Swig_name_set(iname); - Printv(setf->def, "int ", setname, " (char *fname){", NIL); + Printv(setf->def, "int ", setname, " (char *fname,unsigned long fname_len) {", NIL); Wrapper_add_local(setf, "piAddrVar", "int *piAddrVar"); Wrapper_add_local(setf, "iRows", "int iRows"); Wrapper_add_local(setf, "iCols", "int iCols"); - // Wrapper_add_local(setf, "pdblReal", "double *pdblReal"); - + if (is_assignable(n)) { Setattr(n, "wrap:name", setname); @@ -304,7 +303,7 @@ class SCILAB:public Language { Replaceall(tm, "$source", "args(0)"); Replaceall(tm, "$target", name); Replaceall(tm, "$input", "args(0)"); - Replaceall(tm, "$argnum", "1"); + Replaceall(tm, "$argnum", "1"); //if (Getattr(n, "tmap:varin:implicitconv")) { //Replaceall(tm, "$implicitconv", get_implicitconv_flag(n)); //} @@ -319,11 +318,11 @@ class SCILAB:public Language { } Append(setf->code, "}\n"); Wrapper_print(setf, f_wrappers); - Printf(f_builder, "\"%s\",\"%s\";",iname,setname); + Printf(f_builder, "\"%s\",\"%s\";",setname,setname); Setattr(n, "wrap:name", getname); int addfail = 0; - Printv(getf->def, "int ", getname, " (char *fname){", NIL); + Printv(getf->def, "int ", getname, " (char *fname,unsigned long fname_len){", NIL); Wrapper_add_local(getf, "piAddrOut", "int* _piAddress"); Wrapper_add_local(getf, "iRows", "int iRowsOut"); @@ -338,20 +337,59 @@ class SCILAB:public Language { } else { Swig_warning(WARN_TYPEMAP_VAROUT_UNDEF, input_file, line_number, "Unable to read variable of type %s\n", SwigType_str(t, 0)); } - //Append(getf->code, " return obj;\n"); - //if (addfail) { - //Append(getf->code, "fail:\n"); - //Append(getf->code, " return octave_value_list();\n"); - //} + Append(getf->code, "}\n"); Wrapper_print(getf, f_wrappers); - Printf(f_builder, "\"%s\",\"%s\";",iname,getname); + Printf(f_builder, "\"%s\",\"%s\";",getname,getname); return SWIG_OK; } + /* ----------------------------------------------------------------------- + * constantWrapper() + * ----------------------------------------------------------------------- */ + + virtual int constantWrapper(Node *n) { + String *name = Getattr(n, "name"); + String *iname = Getattr(n, "sym:name"); + SwigType *type = Getattr(n, "type"); + String *rawval = Getattr(n, "rawval"); + String *value = rawval ? rawval : Getattr(n, "value"); + String *tm; + + if (!addSymbol(iname, n)) + return SWIG_ERROR; + + Wrapper *getf = NewWrapper(); + String *getname = Swig_name_get(iname); + + Setattr(n, "wrap:name", getname); + Printv(getf->def, "int ", getname, " (char *fname,unsigned long fname_len) {", NIL); + + Wrapper_add_local(getf, "piAddrOut", "int* _piAddress"); + Wrapper_add_local(getf, "iRows", "int iRowsOut"); + Wrapper_add_local(getf, "iColsOut", "int iColsOut "); + + if ((tm = Swig_typemap_lookup("out", n, name, 0))) { + Replaceall(tm, "$source", name); + Replaceall(tm, "$target", "obj"); + Replaceall(tm, "$result", "obj"); + emit_action_code(n, getf->code, tm); + Delete(tm); + } else { + Swig_warning(WARN_TYPEMAP_VAROUT_UNDEF, input_file, line_number, "Unable to read variable of type %s\n", SwigType_str(type, 0)); + } + + Append(getf->code, "}\n"); + Wrapper_print(getf, f_wrappers); + Printf(f_header, "const %s %s=%s;\n",SwigType_str(type,0),iname,value); + Printf(f_builder, "\"%s\",\"%s\";",iname,getname); + + return SWIG_OK; + } + }; extern "C" Language *swig_scilab(void) {