1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
/*
* Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
* Copyright (C) 2012 - Scilab Enterprises - Calixte DENIZET
*
* This file must be used under the terms of the CeCILL.
* This source file is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at
* http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
*
*/
#include "ScilabGateway.hxx"
extern "C" {
#include "scicurdir.h"
}
namespace org_modules_external_objects
{
int ScilabGateway::deff(char * fname, const int envId, void * pvApiCtx)
{
static int ONE = 1;
static int TWO = 2;
static int THREE = 3;
SciErr err;
char ** names[] = {0, 0, 0};
int ret = 0;
std::ostringstream os;
char * str;
int * addr[] = {0, 0, 0};
int rows[] = {0, 0, 0};
int cols[] = {0, 0, 0};
int error = 0;
char * cwd = 0;
CheckInputArgument(pvApiCtx, 3, 3);
ScilabAbstractEnvironment & env = ScilabEnvironments::getEnvironment(envId);
ScilabGatewayOptions & options = env.getGatewayOptions();
OptionsHelper & helper = env.getOptionsHelper();
OptionsHelper::setCopyOccurred(false);
ScilabObjects::initialization(env, pvApiCtx);
options.setIsNew(false);
for (int i = 0; i < 3; i++)
{
err = getVarAddressFromPosition(pvApiCtx, i + 1, &(addr[i]));
if (err.iErr)
{
throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data"));
}
if (!isStringType(pvApiCtx, addr[i]))
{
throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Wrong type for input argument #%d: A String expected."), 1);
}
err = getVarDimension(pvApiCtx, addr[i], &(rows[i]), &(cols[i]));
if (err.iErr)
{
throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data"));
}
}
if (rows[0] != 1 || cols[0] != 1)
{
throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid dimensions for input argument #%d: A single string expected."), 1);
}
if (rows[1] != rows[2] || cols[1] != cols[2])
{
throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid dimensions: arguments #2 and #3 must have the same."));
}
for (int i = 0; i < 3; i++)
{
if (getAllocatedMatrixOfString(pvApiCtx, addr[i], &(rows[i]), &(cols[i]), &(names[i])))
{
for (int j = 0; j < i; j++)
{
freeAllocatedMatrixOfString(rows[j], cols[j], names[j]);
}
throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data"));
}
}
cwd = scigetcwd(&error);
if (error)
{
FREE(cwd);
cwd = 0;
}
try
{
ret = env.loadclass(names[0][0], cwd, false, helper.getAllowReload());
}
catch (std::exception & e)
{
FREE(cwd);
for (int j = 0; j < 3; j++)
{
freeAllocatedMatrixOfString(rows[j], cols[j], names[j]);
}
throw;
}
FREE(cwd);
for (int i = 0; i < rows[1] * cols[1]; i++)
{
err = createMatrixOfString(pvApiCtx, ONE, 1, 1, (const char * const *) & (names[2][i]));
if (err.iErr)
{
for (int j = 0; j < 3; j++)
{
freeAllocatedMatrixOfString(rows[j], cols[j], names[j]);
}
env.removeobject(ret);
throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot create the data"));
}
os.str("");
os << "y=" << names[2][i] << "(varargin)" << std::flush;
str = strdup(os.str().c_str());
err = createMatrixOfString(pvApiCtx, TWO, 1, 1, (const char * const *)&str);
free(str);
if (err.iErr)
{
for (int j = 0; j < 3; j++)
{
freeAllocatedMatrixOfString(rows[j], cols[j], names[j]);
}
env.removeobject(ret);
throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot create the data"));
}
os.str("");
os << "y=invoke_lu(int32(" << ret << "),int32(" << envId << "),\"" << names[1][i] << "\",varargin)" << std::flush;
str = strdup(os.str().c_str());
err = createMatrixOfString(pvApiCtx, THREE, 1, 1, (const char * const *)&str);
free(str);
if (err.iErr)
{
for (int j = 0; j < 3; j++)
{
freeAllocatedMatrixOfString(rows[j], cols[j], names[j]);
}
env.removeobject(ret);
throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot create the data"));
}
SciString(&ONE, const_cast<char *>("!_deff_wrapper"), &ONE, &THREE);
}
for (int i = 0; i < 3; i++)
{
freeAllocatedMatrixOfString(rows[0], cols[0], names[i]);
}
LhsVar(1) = 0;
PutLhsVar();
return 0;
}
}
|