a better way to deal with constants and enums and some change about the doc

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/branches/gsoc2009-sploving@11515 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Baozeng Ding 2009-08-08 13:25:29 +00:00
commit b1a384dc3c
10 changed files with 223 additions and 255 deletions

View file

@ -27,6 +27,7 @@
<li><a href="#Scilab_nn12">Enums</a> <li><a href="#Scilab_nn12">Enums</a>
<li><a href="#Scilab_nn13">Pointers</a> <li><a href="#Scilab_nn13">Pointers</a>
<li><a href="#Scilab_nn14">Structs</a> <li><a href="#Scilab_nn14">Structs</a>
<li><a href="#Scilab_nn15">Arrays</a>
</ul> </ul>
</ul> </ul>
</div> </div>
@ -194,7 +195,7 @@ int fact(int n); </pre></div>
<div class="targetlang"><pre>Scilab:1&gt;fact(4) <div class="targetlang"><pre>Scilab:1&gt;fact(4)
ant=24 </pre></div> ant=24 </pre></div>
<H3><a name="scilab_nn10"></a>27.3.3 Global variables</H3> <H3><a name="scilab_nn10"></a>36.3.3 Global variables</H3>
<p> <p>
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. 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.
@ -210,27 +211,12 @@ c = 3
scilab:5&gt; Foo_get() scilab:5&gt; Foo_get()
ans = 4 ans = 4
scilab:6&gt; Foo_set([1,2,3;4,5,6]);
scilab:7&gt; Foo_get()
ans =
1. 2. 3.
4. 5. 6.
scilab:8&gt; Foo_set([1+2*%i,2+3*%i;3+4*%i,7+8*%i]);
scilab:9&gt; Foo_get()
ans =
1. + 2.i 2. + 3.i
3. + 4.i 7. + 8.i
</pre></div> </pre></div>
<H3><a name="Scilab_nn11"></a>27.3.4 Constants</H3> <H3><a name="Scilab_nn11"></a>36.3.4 Constants</H3>
<p> <p>
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: C constants are not really constant in Scilab. When dealing with the constants, the get function will be generated. For example given some constants:
</p> </p>
<div class="code"><pre>%module example <div class="code"><pre>%module example
@ -242,47 +228,32 @@ scilab:9&gt; Foo_get()
#define SCONST2 "\"Hello World\"" #define SCONST2 "\"Hello World\""
</pre></div> </pre></div>
<p> <p>It is easy to use them in Scilab:</p>
A file called example.sce will be created, which could be interpreted by the scilab. The code in the file is as following:
</p>
<div class="code"><pre>....
example.ICONST = 42
example.FCONST = 2.1828
example.CCONST = ascii(120)
example.CCONST2 = ascii(10)
example.SCONST = "Hello World"
example.SCONST2 = """Hello World"""
example.EXPR = 42+3*(2.1828)
example.iconst = 37
example.fconst = 3.14
.... </pre></div>
<p>It is easy to use the C constants after run the command "exec example.sce":</p>
<div class="targetlang"><pre> <div class="targetlang"><pre>
scilab:1&gt; exec example.sce; scilab:1&gt; exec loader.sce;
scilab:2&gt; example.ICONST scilab:2&gt; ICONST_get();
ans= 42 ans= 42
scilab:3&gt; example.FCONST scilab:3&gt; FCONST_get();
ans= 2.1828 ans= 2.1828
scilab:4&gt; example.CCONST scilab:4&gt; CCONST_get();
ans=x ans=x
scilab:5&gt; example.CCONST2 scilab:5&gt; CCONST2_get();
ans= ans=
scilab:6&gt; example.SCONST scilab:6&gt; SCONST_get();
ans= Hello World ans= Hello World
scilab:7&gt; example.SCONST2 scilab:7&gt; SCONST2_get();
ans= "Hello World" ans= "Hello World"
scilab:8&gt; example.EXPR scilab:8&gt; EXPR_get();
ans= 48.5484 ans= 48.5484
scilab:9&gt; example.iconst scilab:9&gt; iconst_get();
ans= 37 ans= 37
scilab:10&gt; example.fconst scilab:10&gt; fconst_get();
ans= 3.14 ans= 3.14
</pre></div> </pre></div>
<H3><a name="Scilab_nn12"></a>27.3.5 Enums</H3> <H3><a name="Scilab_nn12"></a>36.3.5 Enums</H3>
<p> The way that deals with the enums is similar to the constants. For example: <p> The way that deals with the enums is similar to the constants. For example:
</p> </p>
@ -292,30 +263,24 @@ typedef enum { RED, BLUE, GREEN } color;
</pre></div> </pre></div>
<p> <p>
A file called example.sce will be created, which could be interpreted by the scilab. The code in the file is as following: Some code like RED_get(), BLUE_get(),GREEN_get() will be generated. So it could be used as the following:
</p> </p>
<div class="code"><pre>....
color.RED=0;
color.BLUE=color.RED + 1;
color.GREEN=color.BLUE + 1;
.... </pre></div>
<p>It is easy to use the enums after run the command "exec example.sce":</p>
<div class="targetlang"><pre> <div class="targetlang"><pre>
scilab:1&gt; exec example.sce; scilab:1&gt; exec loader.sce;
scilab:2&gt; printf(" RED = %i\n", color.RED); scilab:2&gt; printf(" RED = %i\n", RED_get());
RED = 0 RED = 0
scilab:3&gt; printf(" BLUE = %i\n", color.BLUE); scilab:3&gt; printf(" BLUE = %i\n", BLUE_get());
BLUE = 1 BLUE = 1
scilab:4&gt; printf(" GREEN = %i\n", color.GREEN); scilab:4&gt; printf(" GREEN = %i\n", GREEN_get());
GREEN = 2 GREEN = 2
</pre></div> </pre></div>
<H3><a name="Scilab_nn13"></a>27.3.5 Pointers</H3> <H3><a name="Scilab_nn13"></a>36.3.6 Pointers</H3>
<p> <p>
Pointers are fully supported by SWIG. One way to deal with the pointers is using the INPUT and OUTPUT typemaps. For example, in order to call C functions as the following: Pointers are fully supported by SWIG. One way to deal with the pointers is using the INPUT and OUTPUT typemaps. For example, in order to call C functions as the following:
</p> </p>
@ -356,7 +321,7 @@ scilab:4&gt; printf(" 42/37 = %d remainder %d\n",q,r);
we only need a real value instead. we only need a real value instead.
</p> </p>
<H3><a name="Scilab_nn14"></a>27.3.6 Structs</H3> <H3><a name="Scilab_nn14"></a>36.3.7 Structs</H3>
<p> <p>
SWIG creates a set of accessor functions when encountering a structure or union. For example: SWIG creates a set of accessor functions when encountering a structure or union. For example:
</p> </p>
@ -371,14 +336,53 @@ typedef struct {
<p> When wrappered, it would generate two main function: Foo_x_set(), which set the data value of the structrure and Foo_x_get() which could obtain the value of the structrure. Run it in Scilab: <p> When wrappered, it would generate two main function: Foo_x_set(), which set the data value of the structrure and Foo_x_get() which could obtain the value of the structrure. Run it in Scilab:
</p> </p>
<div class="targetlang"><pre> <div class="targetlang"><pre>
a=new_Foo(); scilab:1&gt; a=new_Foo();
Foo_x_set(a,100); scilab:2&gt; Foo_x_set(a,100);
Foo_x_get(a) scilab:3&gt; Foo_x_get(a)
ans = ans =
100 100
</pre></div> </pre></div>
<H3><a name="Scilab_nn15"></a>36.3.8 Arrays</H3>
<p>
Arrays are fully supported by SWIG and Scilab. In SWIG, they are handled as pointers. And Scilab also supports the pointer well. So it is easy to deal with the arrays. For example:
</p>
<div class="code"><pre>%module example
%inline %{
int x[10];
double y[7];
void initArray()
{
int i, n;
n = sizeof(x)/sizeof(x[0]);
for(i = 0; i < n; i++)
x[i] = i;
n = sizeof(y)/sizeof(y[0]);
for(i = 0; i < n; i++)
y[i] = ((double) i)/ ((double) n);
return;
%}
</pre></div>
<p> When wrappered, it would generate the following funtion: x_set(), x_get(), y_set(), y_get(), and _wrap_initArray. So it could be used like this:
</p>
<div class="targetlang"><pre>
scilab:1&gt; exec loader.sce
scilab:2&gt; initArray();
scilab:3&gt; x_get()
ans =
0 1 2 3 4 5 6 7 8 9
scilab:4&gt;y_get()
ans =
0. 0.1428571 0.2857143 0.4285714 0.5714286 0.7142857 0.8571429
</pre></div>

View file

@ -24,10 +24,5 @@
%constant int iconst = 37; %constant int iconst = 37;
%constant double fconst = 3.14; %constant double fconst = 3.14;
%inline %{
void constant_test(const int x) {
printf("%i", x);
}
%}

View file

@ -1,24 +1,23 @@
// loader the *.so // loader the *.so
exec loader.sce; exec loader.sce;
exec example.sce;
printf("ICONST = %i (should be 42)\n", ICONST); printf("ICONST = %i (should be 42)\n", ICONST_get());
printf("FCONST = %f (should be 2.1828)\n", FCONST); printf("FCONST = %f (should be 2.1828)\n", FCONST_get());
printf("CCONST = %c (should be ''x'')\n", CCONST); printf("CCONST = %c (should be ''x'')\n", CCONST_get());
printf("CCONST2 = %s (this should be on a new line)\n", CCONST2); printf("CCONST2 = %s (this should be on a new line)\n", CCONST2_get());
printf("SCONST = %s (should be ''Hello World'')\n", SCONST); printf("SCONST = %s (should be ''Hello World'')\n", SCONST_get());
printf("SCONST2 = %s (should be "'""Hello World"""')\n", SCONST2); printf("SCONST2 = %s (should be "'""Hello World"""')\n", SCONST2_get());
printf("EXPR = %f (should be 48.5484)\n", EXPR); printf("EXPR = %f (should be 48.5484)\n", EXPR_get());
printf("iconst = %i (should be 37)\n", iconst); printf("iconst = %i (should be 37)\n", iconst_get());
printf("fconst = %f (should be 3.14)\n", fconst); printf("fconst = %f (should be 3.14)\n", fconst_get());
try try
printf("EXTERN = %s (Arg! This should not printf(anything)\n", EXTERN); printf("EXTERN = %s (Arg! This should not printf(anything)\n", EXTERN_get());
catch catch
printf("EXTERN is not defined (good)\n"); printf("EXTERN is not defined (good)\n");
end end
try try
printf("FOO = %i (Arg! This should not printf(anything)\n", FOO); printf("FOO = %i (Arg! This should not printf(anything)\n", FOO_get());
catch catch
printf("FOO is not defined (good)\n"); printf("FOO is not defined (good)\n");
end end

View file

@ -5,12 +5,12 @@
void enum_test(color c) { void enum_test(color c) {
if (c == RED) { if (c == RED) {
sciprint("color = RED, "); printf("color = RED\n");
} else if (c == BLUE) { } else if (c == BLUE) {
sciprint("color = BLUE, "); printf("color = BLUE\n ");
} else if (c == GREEN) { } else if (c == GREEN) {
sciprint("color = GREEN, "); printf("color = GREEN\n");
} else { } else {
sciprint("color = Unknown color!, "); printf("color = Unknown color!\n");
} }
} }

View file

@ -1,21 +1,19 @@
// loader the *.so // loader the *.so
exec loader.sce; exec loader.sce;
exec example.sce;
// Print out the value of some enums // Print out the value of some enums
printf("*** color ***\n"); printf("*** color ***\n");
printf(" RED = %i\n", color.RED); printf(" RED = %i\n", RED_get());
printf(" BLUE = %i\n", color.BLUE); printf(" BLUE = %i\n", BLUE_get());
printf(" GREEN = %i\n", color.GREEN); printf(" GREEN = %i\n", GREEN_get());
printf("\nTesting use of enums with functions\n"); printf("\nTesting use of enums with functions\n");
enum_test(color.RED); enum_test(RED_get());
enum_test(color.BLUE); enum_test(BLUE_get());
enum_test(color.GREEN); enum_test(GREEN_get());
enum_test(1234); enum_test(int32(1234));
exit exit

View file

@ -1,8 +1,7 @@
exec loader.sce exec loader.sce
exec enums.sce
bar1(foo1.CSP_ITERATION_BWD) bar1(CSP_ITERATION_BWD_get())
bar2(foo3.ABCDE) bar2(ABCDE_get())
bar3(foo3.FGHJI) bar3(FGHJI_get())
exit exit

View file

@ -1,7 +1,6 @@
exec loader.sce; exec loader.sce;
exec li_math.sce;
x = fmod(M_PI, M_1_PI) x = fmod(M_PI_get(), M_1_PI_get())
exit exit

View file

@ -1,7 +1,7 @@
exec loader.sce exec loader.sce
initArray(); initArray();
x_get() if x_get() <> int32([0,1,2,3,4,5,6,7,8,9]) then pause, end
y_get() if y_get() <> [0/7,1/7,2/7,3/7,4/7,5/7,6/7] then pase, end
exit exit

View file

@ -272,6 +272,19 @@
} }
} }
%typemap(in) enum SWIGTYPE (int iRows, int iCols) {
int *piAddrVar;
int *_piData;
getVarAddressFromPosition($argnum, &piAddrVar);
getVarDimension(piAddrVar, &iRows, &iCols);
if (getVarType(piAddrVar) != sci_ints || iRows != 1 || iCols != 1 || isVarComplex(piAddrVar)) {
Scierror(999, _("%s: Wrong type for input argument #%d: Real scalar expected.\n"), fname, $argnum);
}
getMatrixOfInteger32(piAddrVar, &iRows, &iCols, &_piData);
$1 = ($1_ltype)*_piData;
}
%typemap(in) SWIGTYPE * { %typemap(in) SWIGTYPE * {
int *piAddrVar; int *piAddrVar;
void *_piData = NULL; void *_piData = NULL;
@ -371,6 +384,7 @@
%typemap(out,noblock=1) void { %typemap(out,noblock=1) void {
} }
/* Pointers */ /* Pointers */
%typemap(out) signed char *, %typemap(out) signed char *,
short *, short *,
@ -397,6 +411,15 @@
if ($1) free($1); if ($1) free($1);
} }
%typemap(out) enum SWIGTYPE (int iRowsOut, int iColsOut) {
int temp;
temp = (int)($result);
iRowsOut = 1;
iColsOut = 1;
createMatrixOfInteger32(iVarOut, iRowsOut, iColsOut, &temp);
LhsVar(iOutNum) = iVarOut;
}
%typemap(out) SWIGTYPE * { %typemap(out) SWIGTYPE * {
createPointer(iVarOut, (void *)$result); createPointer(iVarOut, (void *)$result);
LhsVar(iOutNum) = iVarOut; LhsVar(iOutNum) = iVarOut;
@ -420,7 +443,9 @@
long, long,
unsigned long, unsigned long,
float, float,
double { double,
long long,
unsigned long long {
double *_piData; double *_piData;
getVarAddressFromPosition($argnum, &piAddrVar); getVarAddressFromPosition($argnum, &piAddrVar);
getVarDimension(piAddrVar, &iRows, &iCols); getVarDimension(piAddrVar, &iRows, &iCols);
@ -629,6 +654,17 @@
$1 = _pstStrings; $1 = _pstStrings;
} }
%typemap(varin,noblock=1) enum SWIGTYPE {
int *_piData;
getVarAddressFromPosition($argnum, &piAddrVar);
getVarDimension(piAddrVar, &iRows, &iCols);
if (getVarType(piAddrVar) != sci_ints || iRows != 1 || iCols != 1 || isVarComplex(piAddrVar)) {
Scierror(999, _("%s: Wrong type for input argument #%d: Real scalar expected.\n"), fname, $argnum);
}
getMatrixOfInteger32(piAddrVar, &iRows, &iCols, &_piData);
$1 = ($1_ltype)*_piData;
}
%typemap(varin,noblock=1) SWIGTYPE * { %typemap(varin,noblock=1) SWIGTYPE * {
void *_piData = NULL; void *_piData = NULL;
getVarAddressFromPosition($argnum, &piAddrVar); getVarAddressFromPosition($argnum, &piAddrVar);
@ -670,58 +706,79 @@
* ----------------------------------------------------------------------------- */ * ----------------------------------------------------------------------------- */
/* Basic C types */ /* Basic C types */
%typemap(varout,noblock=1) signed char { %typemap(varout,noblock=1) signed char {
createMatrixOfInteger8(iVarOut, iRowsOut, iColsOut, &$result); signed char temp = $result;
createMatrixOfInteger8(iVarOut, iRowsOut, iColsOut, &temp);
LhsVar(iOutNum) = iVarOut; LhsVar(iOutNum) = iVarOut;
} }
%typemap(varout,noblock=1) unsigned char { %typemap(varout,noblock=1) unsigned char {
createMatrixOfUnsignedInteger8(iVarOut, iRowsOut, iColsOut, &$result); unsigned char temp = $result;
createMatrixOfUnsignedInteger8(iVarOut, iRowsOut, iColsOut, &temp);
LhsVar(iOutNum) = iVarOut; LhsVar(iOutNum) = iVarOut;
} }
%typemap(varout,noblock=1) short { %typemap(varout,noblock=1) short {
createMatrixOfInteger16(iVarOut, iRowsOut, iColsOut, &$result); short temp = $result;
createMatrixOfInteger16(iVarOut, iRowsOut, iColsOut, &temp);
LhsVar(iOutNum) = iVarOut; LhsVar(iOutNum) = iVarOut;
} }
%typemap(varout,noblock=1) unsigned short { %typemap(varout,noblock=1) unsigned short {
createMatrixOfUnsignedInteger16(iVarOut, iRowsOut, iColsOut, &$result); unsigned short temp = $result;
createMatrixOfUnsignedInteger16(iVarOut, iRowsOut, iColsOut, &temp);
LhsVar(iOutNum) = iVarOut; LhsVar(iOutNum) = iVarOut;
} }
%typemap(varout,noblock=1) int, %typemap(varout,noblock=1) int,
long { long {
createMatrixOfInteger32(iVarOut, iRowsOut, iColsOut, &$result); int temp = $result;
createMatrixOfInteger32(iVarOut, iRowsOut, iColsOut, &temp);
LhsVar(iOutNum) = iVarOut; LhsVar(iOutNum) = iVarOut;
} }
%typemap(varout,noblock=1) unsigned int, %typemap(varout,noblock=1) unsigned int,
unsigned long { unsigned long {
createMatrixOfUnsignedInteger32(iVarOut, iRowsOut, iColsOut, &$result); unsigned int temp = $result;
createMatrixOfUnsignedInteger32(iVarOut, iRowsOut, iColsOut, &temp);
LhsVar(iOutNum) = iVarOut; LhsVar(iOutNum) = iVarOut;
} }
%typemap(varout,noblock=1) double { %typemap(varout,noblock=1) double {
createMatrixOfDouble(iVarOut, iRowsOut, iColsOut, &$result); double temp = $result;
LhsVar(iOutNum) = iVarOut;
}
%typemap(varout,noblock=1) float {
double temp;
temp = (double)$result;
createMatrixOfDouble(iVarOut, iRowsOut, iColsOut, &temp); createMatrixOfDouble(iVarOut, iRowsOut, iColsOut, &temp);
LhsVar(iOutNum) = iVarOut; LhsVar(iOutNum) = iVarOut;
} }
%typemap(varout,noblock=1) char { %typemap(varout,noblock=1) float {
char *temp; double temp = $result;
temp = (char*)&($result); createMatrixOfDouble(iVarOut, iRowsOut, iColsOut, &temp);
createMatrixOfString(iVarOut, iRowsOut, iColsOut, &temp);
LhsVar(iOutNum) = iVarOut; LhsVar(iOutNum) = iVarOut;
} }
%typemap(varout,noblock=1) long long {
long long temp = $result;
createMatrixOfInteger64(iVarOut, iRowsOut, iColsOut, &temp);
LhsVar(iOutNum) = iVarOut;
}
%typemap(varout,noblock=1) unsigned long long {
unsigned long long temp = $result;
createMatrixOfUnsignedInteger64(iVarOut, iRowsOut, iColsOut, &temp);
LhsVar(iOutNum) = iVarOut;
}
%typemap(varout,noblock=1) char {
char *temp = (char *)malloc(sizeof($result) + 1);
*temp = $result;
*(temp+1) = '\0';
createMatrixOfString(iVarOut, iRowsOut, iColsOut, &temp);
LhsVar(iOutNum) = iVarOut;
free(temp);
}
%typemap(varout,noblock=1) char * { %typemap(varout,noblock=1) char * {
createMatrixOfString(iVarOut, iRowsOut, iColsOut, &($result)); char *temp = $result;
createMatrixOfString(iVarOut, iRowsOut, iColsOut, &temp);
LhsVar(iOutNum) = iVarOut; LhsVar(iOutNum) = iVarOut;
} }
@ -786,9 +843,7 @@
} }
%typemap(varout,noblock=1) float [ANY] { %typemap(varout,noblock=1) float [ANY] {
double temp; createMatrixOfDouble(iVarOut, 1, $1_dim0, (double *)$result);
temp = (double)$result;
createMatrixOfDouble(iVarOut, 1, $1_dim0, (double *)&temp);
LhsVar(iOutNum) = iVarOut; LhsVar(iOutNum) = iVarOut;
} }
@ -800,6 +855,12 @@
LhsVar(iOutNum) = iVarOut; LhsVar(iOutNum) = iVarOut;
} }
%typemap(varout,noblock=1) enum SWIGTYPE {
int temp = $result;
createMatrixOfInteger32(iVarOut, iRowsOut, iColsOut, &temp);
LhsVar(iOutNum) = iVarOut;
}
%typemap(varout,noblock=1) SWIGTYPE * { %typemap(varout,noblock=1) SWIGTYPE * {
createPointer(iVarOut, (void *)$result); createPointer(iVarOut, (void *)$result);
LhsVar(iOutNum) = iVarOut; LhsVar(iOutNum) = iVarOut;
@ -811,8 +872,7 @@
} }
/* ------------------------------------------------------------ /* ------------------------------------------------------------
* Enums mapped as integer values * size_t mapped as int
* ------------------------------------------------------------ */ * ------------------------------------------------------------ */
%apply int { enum SWIGTYPE };
%apply int { size_t }; %apply int { size_t };

View file

@ -26,14 +26,10 @@ private:
File *f_init; File *f_init;
String *f_builder_code; String *f_builder_code;
String *f_example_code;
bool hasfunction_flag;
bool hasconstant_flag;
public: public:
SCILAB(): SCILAB():
f_builder_code(NewString("")), f_example_code(NewString("")), hasfunction_flag(false), hasconstant_flag(false) { f_builder_code(NewString("")) {
} }
@ -100,7 +96,7 @@ public:
Printf(f_runtime, "#include \"stack-c.h\"\n"); Printf(f_runtime, "#include \"stack-c.h\"\n");
Printf(f_runtime, "#include \"sciprint.h\"\n"); Printf(f_runtime, "#include \"sciprint.h\"\n");
Printf(f_runtime, "#include \"Scierror.h\"\n"); Printf(f_runtime, "#include \"Scierror.h\"\n");
Printf(f_runtime, "#include \"api_variable.h\"\n"); Printf(f_runtime, "#include \"api_scilab.h\"\n");
Printf(f_runtime, "#include \"localization.h\"\n"); Printf(f_runtime, "#include \"localization.h\"\n");
/* Initialize the builder.sce file code */ /* Initialize the builder.sce file code */
@ -113,31 +109,14 @@ public:
Language::top(n); Language::top(n);
/* create the file to generate the module: "builder.sce" */ /* create the file to generate the module: "builder.sce" */
if(hasfunction_flag) { Printf(f_builder_code, "];\n");
Printf(f_builder_code, "];\n"); Printf(f_builder_code, "ilib_build(ilib_name,table,files,libs);\n");
Printf(f_builder_code, "ilib_build(ilib_name,table,files,libs);\n"); Printf(f_builder_code, "exit");
Printf(f_builder_code, "exit"); File *f_builder=NewFile(NewStringf("%sbuilder.sce", SWIG_output_directory()), "w", SWIG_output_files());
File *f_builder=NewFile(NewStringf("%sbuilder.sce", SWIG_output_directory()), "w", SWIG_output_files()); Printv(f_builder, f_builder_code, NIL);
Printv(f_builder, f_builder_code, NIL); Close(f_builder);
Close(f_builder); Delete(f_builder);
Delete(f_builder); Delete(f_builder_code);
Delete(f_builder_code);
}
else {
Delete(f_builder_code);
}
/* create the file for constants: "module.sce" */
if(hasconstant_flag) {
File *f_example = NewFile(NewStringf("%s%s.sce", SWIG_output_directory(), module), "w", SWIG_output_files());
Printv(f_example, f_example_code, NIL);
Close(f_example);
Delete(f_example);
Delete(f_example_code);
}
else {
Delete(f_example_code);
}
/* Dump out all the files */ /* Dump out all the files */
Dump(f_runtime, f_begin); Dump(f_runtime, f_begin);
@ -162,8 +141,6 @@ public:
virtual int functionWrapper(Node *n) { virtual int functionWrapper(Node *n) {
hasfunction_flag = true;
/* A new wrapper function object */ /* A new wrapper function object */
Wrapper *f = NewWrapper(); Wrapper *f = NewWrapper();
Parm *p; Parm *p;
@ -341,8 +318,6 @@ public:
virtual int variableWrapper(Node *n) { virtual int variableWrapper(Node *n) {
hasfunction_flag = true;
/* Get the useful information from the node */ /* Get the useful information from the node */
String *name = Getattr(n, "name"); String *name = Getattr(n, "name");
String *iname = Getattr(n, "sym:name"); String *iname = Getattr(n, "sym:name");
@ -456,44 +431,46 @@ public:
virtual int constantWrapper(Node *n) { virtual int constantWrapper(Node *n) {
/* set the flag so to generate the example.sce */
hasconstant_flag = true;
/* Get the useful information from the node */ /* Get the useful information from the node */
String *name = Getattr(n, "name");
String *iname = Getattr(n, "sym:name"); String *iname = Getattr(n, "sym:name");
SwigType *type = Getattr(n, "type"); SwigType *type = Getattr(n, "type");
String *rawval = Getattr(n, "rawval"); String *rawval = Getattr(n, "rawval");
String *value = rawval ? rawval : Getattr(n, "value"); String *value = rawval ? rawval : Getattr(n, "value");
String *tempvalue = NewString(""); String *tm;
/* set the value format to be the scilab format */ if (!addSymbol(iname, n))
if (!Strcmp(type, "char")) { return SWIG_ERROR;
value = Getattr(n, "rawvalue");
char *temp = (Char(value)); /*use the get function to get the constant value */
tempvalue = NewString("ascii"); Wrapper *getf = NewWrapper();
Printf(tempvalue, "(%d)", (unsigned int)*temp); String *getname = Swig_name_get(iname);
value = Copy(tempvalue); Setattr(n, "wrap:name", getname);
Delete(tempvalue); int addfail = 0;
} Printv(getf->def, "int ", getname, " (char *fname,unsigned long fname_len){\n", NIL);
else {
if (!Strcmp(type, "p.char")) { /* Check the number of input and output */
char *temp = (Char(value)); Printf(getf->def, "CheckRhs(0, 0);\n");
int len = strlen(temp); Printf(getf->def, "CheckLhs(1, 1);\n");
for (int i = 0; i < len; ++i) {
if (temp[i] == '\\') { /* Insert the order of output parameters*/
temp[i] = '"'; Printf(getf->def, "\nint iOutNum=1;\nint iVarOut=Rhs+1;");
++i;
} if ((tm = Swig_typemap_lookup("varout", n, name, 0))) {
} Replaceall(tm, "$result", value);
Printf(tempvalue, "%s",temp); Replaceall(tm, "iRowsOut", "1");
value = Copy(tempvalue); Replaceall(tm, "iColsOut", "1");
} addfail = emit_action_code(n, getf->code, tm);
Delete(tempvalue); 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));
} }
/* write into the code string */ /*Dump the wrapper function */
Printf(f_example_code, "%s = %s\n", iname, value); Append(getf->code, "}\n");
Wrapper_print(getf, f_wrappers);
Printf(f_builder_code, "\"%s\",\"%s\";", getname, getname);
return SWIG_OK; return SWIG_OK;
} }
@ -502,9 +479,6 @@ public:
* --------------------------------------------------------------------- */ * --------------------------------------------------------------------- */
virtual int enumDeclaration(Node *n) { virtual int enumDeclaration(Node *n) {
/* set the flag so to generate the example.sce */
hasconstant_flag = true;
return Language::enumDeclaration(n); return Language::enumDeclaration(n);
} }
@ -513,68 +487,8 @@ public:
* --------------------------------------------------------------------- */ * --------------------------------------------------------------------- */
virtual int enumvalueDeclaration(Node *n) { virtual int enumvalueDeclaration(Node *n) {
return Language::enumvalueDeclaration(n);
/* get the name of the enumvalue */ }
String *iname = Getattr(n, "sym:name");
/* get the name of the enum name */
String *parentName = Getattr(parentNode(n), "sym:name");
/* set the name to be the enum.enumvalue format */
if (parentName) {
/*if the enum has a name*/
if(!Getattr(parentNode(n), "unnamedinstance")) {
String *temp = Copy(parentName);
Printf(temp, ".%s", iname);
Setattr(n, "sym:name", temp);
Delete(temp);
iname = Getattr(n, "sym:name");
}
}
/* set the value attribute to be the integer */
String *value;
String *enumvalue = Getattr(n, "enumvalue");
if (enumvalue) {
if (Len(enumvalue) == 1) {
char *temp = (Char(enumvalue));
/*set the value of char into the format of integer*/
if (((*temp <= 'z') && (*temp >= 'a')) || ((*temp <= 'Z') && (*temp >= 'A'))) {
String *tempInteger = NewString("");
Printf(tempInteger, "%i", int(*temp));
Setattr(n, "value", tempInteger);
Delete(tempInteger);
}
else {
Setattr(n, "value", enumvalue);
}
}
else {
Setattr(n, "value", enumvalue);
}
}
else {
if (n != firstChild(parentNode(n))) {
enumvalue = Getattr(n, "enumvalueex");
if (parentName) {
if (!Getattr(parentNode(n), "unnamedinstance")) {
String *temp = Copy(parentName);
Printf(temp, ".%s", enumvalue);
enumvalue = Copy(temp);
}
}
Setattr(n, "value", enumvalue);
}
else {
Setattr(n, "value", Getattr(n, "enumvalueex"));
}
}
value = Getattr(n, "value");
/* write into the code string */
Printf(f_example_code, "%s = %s;\n", iname, value);
return SWIG_OK;
}
}; };
extern "C" Language *swig_scilab(void) { extern "C" Language *swig_scilab(void) {