Scicos: fix some issues detected by clang-analyzer
[scilab.git] / scilab / modules / scicos / sci_gateway / cpp / sci_scicosim.cpp
1 /*
2  *  Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  *  Copyright (C) 2014 - Scilab Enterprises - Paul Bignier
4  *
5  * Copyright (C) 2012 - 2016 - Scilab Enterprises
6  *
7  * This file is hereby licensed under the terms of the GNU GPL v2.0,
8  * pursuant to article 5.3.4 of the CeCILL v.2.1.
9  * This file was originally licensed under the terms of the CeCILL v2.1,
10  * and continues to be available under such terms.
11  * For more information, see the COPYING file which you should have received
12  * along with this program.
13  *
14  */
15
16 #include <string>
17 #include <cstdio>
18 #include <cwchar>
19 #include <cstring>
20
21 #include "gw_scicos.hxx"
22
23 #include "internal.hxx"
24 #include "types.hxx"
25 #include "context.hxx"
26 #include "double.hxx"
27 #include "function.hxx"
28 #include "macro.hxx"
29 #include "list.hxx"
30 #include "tlist.hxx"
31 #include "string.hxx"
32 #include "int.hxx"
33
34 #include "il_state.hxx"
35 #include "il_sim.hxx"
36
37 extern "C"
38 {
39 #include "machine.h" /* C2F */
40 #include "import.h"
41 #include "sci_malloc.h"
42 #include "scicos.h" /* funnum2() */
43 #include "scicos-def.h"
44 #include "charEncoding.h"
45
46 #include "localization.h"
47 #include "Scierror.h"
48
49     COSIM_struct C2F(cosim);
50     /*--------------------------------------------------------------------------*/
51     // Variables defined in scicos.c
52     extern COSERR_struct coserr;
53     extern CURBLK_struct C2F(curblk);
54     /*--------------------------------------------------------------------------*/
55 }
56
57 #ifdef _MSC_VER
58 #define snprintf _snprintf
59 #endif
60
61 /*--------------------------------------------------------------------------*/
62 /* intsicosimc scicosim interface routine.
63 *
64 * [state,t] = scicosim(state,tcur,tf,sim,str,tol)
65 *
66 * rhs 1 state : Tlist
67 *        - 1  : state(1)     : !xcs  x  z  oz iz  tevts  evtspt  pointi  outtb  !
68 *        - 2  : state.x      : column vector of real
69 *        - 3  : state.z      : column vector of real
70 *        - 4  : state.oz     : list of scilab object
71 *        - 5  : state.iz     : column vector of real (empty object with flag "finish")
72 *        - 6  : state.tevts  : column vector of real
73 *        - 7  : state.evtspt : column vector of real
74 *        - 8  : state.pointi : real scalar
75 *        - 9  : state.outtb  : list of scilab object
76 * rhs 2 tcur  : real scalar
77 * rhs 3 tf    : real scalar
78 * rhs 4 sim   : Tlist
79 *        - 1  : sim(1) : !scs    funs    xptr    zptr    ozptr   zcptr   inpptr
80 *                         outptr inplnk  outlnk  rpar    rpptr   ipar    ipptr
81 *                         opar   opptr   clkptr  ordptr  execlk  ordclk  cord
82 *                         oord   zord    critev  nb      ztyp    nblk    ndcblk
83 *                         subscr funtyp  iord    labels  modptr  uids !
84 *        - 2  : sim.funs   : list of strings and/or scilab function
85 *        - 3  : sim.xptr   : column vector of real
86 *        - 4  : sim.zptr   : column vector of real
87 *        - 5  : sim.ozptr  : column vector of real
88 *        - 6  : sim.zcptr  : column vector of real
89 *        - 7  : sim.inpptr : column vector of real
90 *        - 8  : sim.outptr : column vector of real
91 *        - 9  : sim.inplnk : column vector of real
92 *        - 10 : sim.outlnk : column vector of real
93 *        - 11 : sim.rpar   : column vector of real
94 *        - 12 : sim.rpptr  : column vector of real
95 *        - 13 : sim.ipar   : column vector of real
96 *        - 14 : sim.ipptr  : column vector of real
97 *        - 15 : sim.opar   : list of scilab object
98 *        - 16 : sim.opptr  : column vector of real
99 *        - 17 : sim.clkptr : column vector of real
100 *        - 18 : sim.ordptr : column vector of real
101 *        - 19 : sim.execlk : matrix of real
102 *        - 20 : sim.ordclk : matrix of real
103 *        - 21 : sim.cord   : matrix of real
104 *        - 22 : sim.oord   : matrix of real
105 *        - 23 : sim.zord   : column vector ? of real
106 *        - 24 : sim.critev : column vector of real
107 *        - 25 : sim.nb     : real scalar
108 *        - 26 : sim.ztyp   : column vector of real
109 *        - 27 : sim.nblk   : real scalar
110 *        - 28 : sim.ndcblk : real scalar
111 *        - 29 : sim.subscr : column vector of real
112 *        - 30 : sim.funtyp : column vector of real
113 *        - 31 : sim.iord   : column vector of real
114 *        - 32 : sim.labels : column vector of strings
115 *        - 33 : sim.modptr : column vector of real
116 *        - 34 : sim.uids : column vector of strings
117 *
118 * rhs 5 str   : string flag : 'start','run','finish','linear'
119 * rhs 6 tol   : real vector of size (7,1) minimum (4,1)
120 *               [atol rtol ttol [deltat realtimescale solver hmax]]'
121 *
122 * 16/03/06, A.Layec : Rewritten from original fortran
123 *                     source code intsscicos in intcos.f.
124 *
125 * 29/03/06, Alan    : Improvement in accordance to c_pass2
126 *                     (int32 parameters).
127 *
128 * 31/05/06, Alan    : Add global variable int *il_state_save
129 *                     and int *il_sim_save in intcscicos.h to store
130 *                     stack address of list %cpr.state and %cpr.sim
131 *                     (to use with get/setscicosvars).
132 *
133 * 14/06/06, Alan    : Save common intersci before calling scicos
134 *                     (to disable scilab crash with scifunc.f).
135 *
136 * 13/11/06, Alan    : Get back to double parameters for sim and state
137 *                     (for better compatibility with scilab-4.x families).
138 *                     Remove il_sim_save global variable.
139 *
140 * 15/12/06, Alan    : Warnings compilation removed.
141 *                     This can crash scilab/scicos.
142 *                     Please report.
143 *
144 * xx/02/07, Alan    : Add opar/odstate : scilab lists of arbitrary object
145 *                     parameters/states.
146 *
147 * 08/12/14, Paul    : Rewrite to C++.
148 *
149 */
150 /*--------------------------------------------------------------------------*/
151
152 static const std::string funname = "scicosim";
153
154 types::Function::ReturnValue sci_scicosim(types::typed_list &in, int _iRetCount, types::typed_list &out)
155 {
156     /************************************
157     * Variables and constants definition
158     ************************************/
159     double simpar[7];
160
161     BOOL allocatedError = FALSE;
162     const int MAX_ERROR_LEN = 512;
163
164     if (in.size() != 6)
165     {
166         Scierror(77, _("%s: Wrong number of input argument(s): %d expected.\n"), funname.data(), 6);
167         return types::Function::Error;
168     }
169
170     if (_iRetCount > 2)
171     {
172         Scierror(78, _("%s: Wrong number of output argument(s): %d to %d expected.\n"), funname.data(), 1, 2);
173         return types::Function::Error;
174     }
175
176     /****************
177     * state (rhs 1)
178     ****************/
179     if (in[0]->isTList() == false)
180     {
181         Scierror(999, _("%s: Wrong type for input argument #%d : A tlist expected.\n"), funname.data(), 1);
182         return types::Function::Error;
183     }
184     types::TList* il_state_input = in[0]->getAs<types::TList>();
185     types::TList* il_state = new types::TList();
186
187     if (il_state_input->getSize() < 9)
188     {
189         Scierror(999, _("%s: Wrong size for input argument #%d : %d elements expected.\n"), funname.data(), 1, 9);
190         il_state->DecreaseRef();
191         il_state->killMe();
192         return types::Function::Error;
193     }
194
195     // Make a copy of 'il_state' in a global variabe
196     set_il_state(il_state_input);
197
198     types::String* header_state = il_state_input->get(0)->getAs<types::String>();
199     il_state->append(header_state->clone());
200
201     /*2 : state.x      */
202     if (il_state_input->get(1)->isDouble() == false)
203     {
204         Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 2, 1);
205         il_state->DecreaseRef();
206         il_state->killMe();
207         return types::Function::Error;
208     }
209     types::Double* il_state_x_input = il_state_input->get(1)->getAs<types::Double>();
210     il_state->append(il_state_x_input->clone());
211     types::Double* il_state_x = il_state->get(1)->getAs<types::Double>();
212     double* l_state_x = il_state_x->get();
213
214     /*3 : state.z      */
215     if (il_state_input->get(2)->isDouble() == false)
216     {
217         Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 3, 1);
218         il_state->DecreaseRef();
219         il_state->killMe();
220         return types::Function::Error;
221     }
222     types::Double* il_state_z_input = il_state_input->get(2)->getAs<types::Double>();
223     il_state->append(il_state_z_input->clone());
224     types::Double* il_state_z = il_state->get(2)->getAs<types::Double>();
225     double* l_state_z = il_state_z->get();
226
227     /*4 : state.oz     */
228     types::List* il_state_oz_input = il_state_input->get(3)->getAs<types::List>();
229     types::List* il_state_oz = new types::List();
230     for (int i = 0; i < il_state_oz_input->getSize(); ++i)
231     {
232         types::InternalType* l_state_oz_input = il_state_oz_input->get(i);
233         il_state_oz->append(l_state_oz_input->clone());
234     }
235     il_state->append(il_state_oz);
236     int noz = il_state_oz->getSize(); // 'nlnk' is the dimension of the list 'state.oz'
237
238     /*5 : state.iz     */
239     if (il_state_input->get(4)->isDouble() == false)
240     {
241         Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 5, 1);
242         return types::Function::Error;
243     }
244     types::Double* il_state_iz_input = il_state_input->get(4)->getAs<types::Double>();
245     il_state->append(il_state_iz_input->clone());
246     types::Double* il_state_iz = il_state->get(4)->getAs<types::Double>();
247     void** l_state_iz = (void**) il_state_iz->get();
248     int m1e5 = il_state_iz->getRows();
249
250     /*6 : state.tevts  */
251     if (il_state_input->get(5)->isDouble() == false)
252     {
253         Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 6, 1);
254         il_state->DecreaseRef();
255         il_state->killMe();
256         return types::Function::Error;
257     }
258     types::Double* il_state_tevts_input = il_state_input->get(5)->getAs<types::Double>();
259     il_state->append(il_state_tevts_input->clone());
260     types::Double* il_state_tevts = il_state->get(5)->getAs<types::Double>();
261     double* l_state_tevts = il_state_tevts->get();
262     int m1e6 = il_state_tevts->getRows();
263
264     /*7 : state.evtspt */
265     if (il_state_input->get(6)->isDouble() == false)
266     {
267         Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 7, 1);
268         il_state->DecreaseRef();
269         il_state->killMe();
270         return types::Function::Error;
271     }
272     types::Double* il_state_evtspt_input = il_state_input->get(6)->getAs<types::Double>();
273     il_state->append(il_state_evtspt_input->clone());
274     types::Double* il_state_evtspt = il_state->get(6)->getAs<types::Double>();
275     il_state_evtspt->convertToInteger();
276     int* l_state_evtspt = (int*) il_state_evtspt->get();
277     int m1e7 = il_state_evtspt->getRows();
278
279     /*8 : state.pointi */
280     if (il_state_input->get(7)->isDouble() == false)
281     {
282         Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 8, 1);
283         il_state->DecreaseRef();
284         il_state->killMe();
285         return types::Function::Error;
286     }
287     types::Double* il_pointi_input = il_state_input->get(7)->getAs<types::Double>();
288     il_state->append(il_pointi_input->clone());
289     types::Double* il_pointi = il_state->get(7)->getAs<types::Double>();
290     il_pointi->convertToInteger();
291     int* l_pointi = (int*) il_pointi->get();
292
293     /*9 : state.outtb  */
294     if (il_state_input->get(8)->isList() == false)
295     {
296         Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A list expected.\n"), funname.data(), 9, 1);
297         il_state->DecreaseRef();
298         il_state->killMe();
299         return types::Function::Error;
300     }
301     types::List* il_state_outtb_input = il_state_input->get(8)->getAs<types::List>();
302     types::List* il_state_outtb = new types::List();
303     for (int i = 0; i < il_state_outtb_input->getSize(); ++i)
304     {
305         types::InternalType* l_state_outtb_input = il_state_outtb_input->get(i);
306         il_state_outtb->append(l_state_outtb_input->clone());
307     }
308     il_state->append(il_state_outtb);
309     int nlnk = il_state_outtb->getSize(); // 'nlnk' is the dimension of the list 'state.outtb'
310
311     /***************
312     * tcur (rhs 2)
313     ***************/
314     if (in[1]->isDouble() == false)
315     {
316         Scierror(999, _("%s: Wrong type for input argument #%d : A matrix expected.\n"), funname.data(), 2);
317         il_state->DecreaseRef();
318         il_state->killMe();
319         return types::Function::Error;
320     }
321     types::Double* il_tcur_input = in[1]->getAs<types::Double>();
322     types::Double* il_tcur = il_tcur_input->clone()->getAs<types::Double>();
323     if (il_tcur->isScalar() == false)
324     {
325         Scierror(999, _("%s: Wrong size for input argument #%d : A scalar expected.\n"), funname.data(), 2);
326         il_state->DecreaseRef();
327         il_state->killMe();
328         il_tcur->DecreaseRef();
329         il_tcur->killMe();
330         return types::Function::Error;
331     }
332     double* l_tcur = il_tcur->get();
333
334     /*************
335     * tf (rhs 3)
336     *************/
337     if (in[2]->isDouble() == false)
338     {
339         Scierror(999, _("%s: Wrong type for input argument #%d : A matrix expected.\n"), funname.data(), 3);
340         il_state->DecreaseRef();
341         il_state->killMe();
342         il_tcur->DecreaseRef();
343         il_tcur->killMe();
344         return types::Function::Error;
345     }
346     types::Double* il_tf = in[2]->getAs<types::Double>();
347     if (il_tf->isScalar() == false)
348     {
349         Scierror(999, _("%s: Wrong size for input argument #%d : A scalar expected.\n"), funname.data(), 3);
350         il_state->DecreaseRef();
351         il_state->killMe();
352         il_tcur->DecreaseRef();
353         il_tcur->killMe();
354         return types::Function::Error;
355     }
356     double* l_tf = il_tf->get();
357
358     /*************
359     * sim (rhs 4)
360     *************/
361     if (in[3]->isTList() == false)
362     {
363         Scierror(999, _("%s: Wrong type for input argument #%d : A tlist expected.\n"), funname.data(), 4);
364         il_state->DecreaseRef();
365         il_state->killMe();
366         il_tcur->DecreaseRef();
367         il_tcur->killMe();
368         return types::Function::Error;
369     }
370     types::TList* il_sim_input = in[3]->getAs<types::TList>();
371     types::TList* il_sim = new types::TList();
372
373     // Make a copy of 'il_sim' in a global variabe
374     set_il_sim(il_sim_input);
375
376     if (il_sim_input->getSize() < 34)
377     {
378         Scierror(999, _("%s: Wrong size for input argument #%d : %d elements expected.\n"), funname.data(), 4, 34);
379         il_state->DecreaseRef();
380         il_state->killMe();
381         il_tcur->DecreaseRef();
382         il_tcur->killMe();
383         il_sim->DecreaseRef();
384         il_sim->killMe();
385         return types::Function::Error;
386     }
387
388     types::String* header_sim = il_sim_input->get(0)->getAs<types::String>();
389     il_sim->append(header_sim->clone());
390
391     /*2  : sim.funs*/
392     if (il_sim_input->get(1)->isList() == false)
393     {
394         Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A list expected.\n"), funname.data(), 2, 4);
395         il_state->DecreaseRef();
396         il_state->killMe();
397         il_tcur->DecreaseRef();
398         il_tcur->killMe();
399         il_sim->DecreaseRef();
400         il_sim->killMe();
401         return types::Function::Error;
402     }
403     types::List* il_sim_fun_input = il_sim_input->get(1)->getAs<types::List>();
404     types::List* il_sim_fun = new types::List();
405     for (int i = 0; i < il_sim_fun_input->getSize(); ++i)
406     {
407         types::InternalType* l_sim_fun_input = il_sim_fun_input->get(i);
408         il_sim_fun->append(l_sim_fun_input->clone());
409     }
410     il_sim->append(il_sim_fun);
411     int nblk = il_sim_fun->getSize(); // 'nblk' is the dimension of the list 'sim.funs'
412
413     /*3  : sim.xptr   */
414     if (il_sim_input->get(2)->isDouble() == false)
415     {
416         Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 3, 4);
417         il_state->DecreaseRef();
418         il_state->killMe();
419         il_tcur->DecreaseRef();
420         il_tcur->killMe();
421         il_sim->DecreaseRef();
422         il_sim->killMe();
423         return types::Function::Error;
424     }
425     types::Double* il_sim_xptr_input = il_sim_input->get(2)->getAs<types::Double>();
426     il_sim->append(il_sim_xptr_input->clone());
427     types::Double* il_sim_xptr = il_sim->get(2)->getAs<types::Double>();
428     il_sim_xptr->convertToInteger();
429     int* l_sim_xptr = (int*) il_sim_xptr->get();
430     int m_xptr = il_sim_xptr->getRows();
431
432     /*4  : sim.zptr   */
433     if (il_sim_input->get(3)->isDouble() == false)
434     {
435         Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 4, 4);
436         il_state->DecreaseRef();
437         il_state->killMe();
438         il_tcur->DecreaseRef();
439         il_tcur->killMe();
440         il_sim->DecreaseRef();
441         il_sim->killMe();
442         return types::Function::Error;
443     }
444     types::Double* il_sim_zptr_input = il_sim_input->get(3)->getAs<types::Double>();
445     il_sim->append(il_sim_zptr_input->clone());
446     types::Double* il_sim_zptr = il_sim->get(3)->getAs<types::Double>();
447     il_sim_zptr->convertToInteger();
448     int* l_sim_zptr = (int*) il_sim_zptr->get();
449     int m_zptr = il_sim_zptr->getRows();
450
451     /*5  : sim.ozptr   */
452     if (il_sim_input->get(4)->isDouble() == false)
453     {
454         Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 5, 4);
455         il_state->DecreaseRef();
456         il_state->killMe();
457         il_tcur->DecreaseRef();
458         il_tcur->killMe();
459         il_sim->DecreaseRef();
460         il_sim->killMe();
461         return types::Function::Error;
462     }
463     types::Double* il_sim_ozptr_input = il_sim_input->get(4)->getAs<types::Double>();
464     il_sim->append(il_sim_ozptr_input->clone());
465     types::Double* il_sim_ozptr = il_sim->get(4)->getAs<types::Double>();
466     il_sim_ozptr->convertToInteger();
467     int* l_sim_ozptr = (int*) il_sim_ozptr->get();
468     int m_ozptr = il_sim_ozptr->getRows();
469
470     /*6  : sim.zcptr  */
471     if (il_sim_input->get(5)->isDouble() == false)
472     {
473         Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 6, 4);
474         il_state->DecreaseRef();
475         il_state->killMe();
476         il_tcur->DecreaseRef();
477         il_tcur->killMe();
478         il_sim->DecreaseRef();
479         il_sim->killMe();
480         return types::Function::Error;
481     }
482     types::Double* il_sim_zcptr_input = il_sim_input->get(5)->getAs<types::Double>();
483     il_sim->append(il_sim_zcptr_input->clone());
484     types::Double* il_sim_zcptr = il_sim->get(5)->getAs<types::Double>();
485     il_sim_zcptr->convertToInteger();
486     int* l_sim_zcptr = (int*) il_sim_zcptr->get();
487     int m_zcptr = il_sim_zcptr->getRows();
488
489     /*7  : sim.inpptr */
490     if (il_sim_input->get(6)->isDouble() == false)
491     {
492         Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 7, 4);
493         il_state->DecreaseRef();
494         il_state->killMe();
495         il_tcur->DecreaseRef();
496         il_tcur->killMe();
497         il_sim->DecreaseRef();
498         il_sim->killMe();
499         return types::Function::Error;
500     }
501     types::Double* il_sim_inpptr_input = il_sim_input->get(6)->getAs<types::Double>();
502     il_sim->append(il_sim_inpptr_input->clone());
503     types::Double* il_sim_inpptr = il_sim->get(6)->getAs<types::Double>();
504     il_sim_inpptr->convertToInteger();
505     int* l_sim_inpptr = (int*) il_sim_inpptr->get();
506     int m_inpptr = il_sim_inpptr->getRows();
507
508     /*8  : sim.outptr */
509     if (il_sim_input->get(7)->isDouble() == false)
510     {
511         Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 8, 4);
512         il_state->DecreaseRef();
513         il_state->killMe();
514         il_tcur->DecreaseRef();
515         il_tcur->killMe();
516         il_sim->DecreaseRef();
517         il_sim->killMe();
518         return types::Function::Error;
519     }
520     types::Double* il_sim_outpptr_input = il_sim_input->get(7)->getAs<types::Double>();
521     il_sim->append(il_sim_outpptr_input->clone());
522     types::Double* il_sim_outptr = il_sim->get(7)->getAs<types::Double>();
523     il_sim_outptr->convertToInteger();
524     int* l_sim_outptr = (int*) il_sim_outptr->get();
525     int m_outptr = il_sim_outptr->getRows();
526
527     /*9  : sim.inplnk */
528     if (il_sim_input->get(8)->isDouble() == false)
529     {
530         Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 9, 4);
531         il_state->DecreaseRef();
532         il_state->killMe();
533         il_tcur->DecreaseRef();
534         il_tcur->killMe();
535         il_sim->DecreaseRef();
536         il_sim->killMe();
537         return types::Function::Error;
538     }
539     types::Double* il_sim_inplnk_input = il_sim_input->get(8)->getAs<types::Double>();
540     il_sim->append(il_sim_inplnk_input->clone());
541     types::Double* il_sim_inplnk = il_sim->get(8)->getAs<types::Double>();
542     il_sim_inplnk->convertToInteger();
543     int* l_sim_inplnk = (int*) il_sim_inplnk->get();
544
545     /*10  : sim.outlnk */
546     if (il_sim_input->get(9)->isDouble() == false)
547     {
548         Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 10, 4);
549         il_state->DecreaseRef();
550         il_state->killMe();
551         il_tcur->DecreaseRef();
552         il_tcur->killMe();
553         il_sim->DecreaseRef();
554         il_sim->killMe();
555         return types::Function::Error;
556     }
557     types::Double* il_sim_outlnk_input = il_sim_input->get(9)->getAs<types::Double>();
558     il_sim->append(il_sim_outlnk_input->clone());
559     types::Double* il_sim_outlnk = il_sim->get(9)->getAs<types::Double>();
560     il_sim_outlnk->convertToInteger();
561     int* l_sim_outlnk = (int*) il_sim_outlnk->get();
562
563     /*11 : sim.rpar   */
564     if (il_sim_input->get(10)->isDouble() == false)
565     {
566         Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 11, 4);
567         il_state->DecreaseRef();
568         il_state->killMe();
569         il_tcur->DecreaseRef();
570         il_tcur->killMe();
571         il_sim->DecreaseRef();
572         il_sim->killMe();
573         return types::Function::Error;
574     }
575     types::Double* il_sim_rpar_input = il_sim_input->get(10)->getAs<types::Double>();
576     il_sim->append(il_sim_rpar_input->clone());
577     types::Double* il_sim_rpar = il_sim->get(10)->getAs<types::Double>();
578     double* l_sim_rpar = il_sim_rpar->get();
579
580     /*12 : sim.rpptr  */
581     if (il_sim_input->get(11)->isDouble() == false)
582     {
583         Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 12, 4);
584         il_state->DecreaseRef();
585         il_state->killMe();
586         il_tcur->DecreaseRef();
587         il_tcur->killMe();
588         il_sim->DecreaseRef();
589         il_sim->killMe();
590         return types::Function::Error;
591     }
592     types::Double* il_sim_rpptr_input = il_sim_input->get(11)->getAs<types::Double>();
593     il_sim->append(il_sim_rpptr_input->clone());
594     types::Double* il_sim_rpptr = il_sim->get(11)->getAs<types::Double>();
595     il_sim_rpptr->convertToInteger();
596     int* l_sim_rpptr = (int*) il_sim_rpptr->get();
597     int m_rpptr = il_sim_rpptr->getRows();
598
599     /*13 : sim.ipar   */
600     if (il_sim_input->get(12)->isDouble() == false)
601     {
602         Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 13, 4);
603         il_state->DecreaseRef();
604         il_state->killMe();
605         il_tcur->DecreaseRef();
606         il_tcur->killMe();
607         il_sim->DecreaseRef();
608         il_sim->killMe();
609         return types::Function::Error;
610     }
611     types::Double* il_sim_ipar_input = il_sim_input->get(12)->getAs<types::Double>();
612     il_sim->append(il_sim_ipar_input->clone());
613     types::Double* il_sim_ipar = il_sim->get(12)->getAs<types::Double>();
614     il_sim_ipar->convertToInteger();
615     int* l_sim_ipar = (int*) il_sim_ipar->get();
616
617     /*14 : sim.ipptr  */
618     if (il_sim_input->get(13)->isDouble() == false)
619     {
620         Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 14, 4);
621         il_state->DecreaseRef();
622         il_state->killMe();
623         il_tcur->DecreaseRef();
624         il_tcur->killMe();
625         il_sim->DecreaseRef();
626         il_sim->killMe();
627         return types::Function::Error;
628     }
629     types::Double* il_sim_ipptr_input = il_sim_input->get(13)->getAs<types::Double>();
630     il_sim->append(il_sim_ipptr_input->clone());
631     types::Double* il_sim_ipptr = il_sim->get(13)->getAs<types::Double>();
632     il_sim_ipptr->convertToInteger();
633     int* l_sim_ipptr = (int*) il_sim_ipptr->get();
634     int m_ipptr = il_sim_ipptr->getRows();
635
636     /*15 : sim.opar   */
637     if (il_sim_input->get(14)->isList() == false)
638     {
639         Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A list expected.\n"), funname.data(), 15, 4);
640         il_state->DecreaseRef();
641         il_state->killMe();
642         il_tcur->DecreaseRef();
643         il_tcur->killMe();
644         il_sim->DecreaseRef();
645         il_sim->killMe();
646         return types::Function::Error;
647     }
648     types::List* il_sim_opar_input = il_sim_input->get(14)->getAs<types::List>();
649     types::List* il_sim_opar = new types::List();
650     for (int i = 0; i < il_sim_opar_input->getSize(); ++i)
651     {
652         types::InternalType* l_sim_opar_input = il_sim_opar_input->get(i);
653         il_sim_opar->append(l_sim_opar_input->clone());
654     }
655     il_sim->append(il_sim_opar);
656     int nopar = il_sim_opar->getSize(); // 'nopar' is the dimension of the list 'sim.opar'
657
658     /*16 : sim.opptr  */
659     if (il_sim_input->get(15)->isDouble() == false)
660     {
661         Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 16, 4);
662         il_state->DecreaseRef();
663         il_state->killMe();
664         il_tcur->DecreaseRef();
665         il_tcur->killMe();
666         il_sim->DecreaseRef();
667         il_sim->killMe();
668         return types::Function::Error;
669     }
670     types::Double* il_sim_opptr_input = il_sim_input->get(15)->getAs<types::Double>();
671     il_sim->append(il_sim_opptr_input->clone());
672     types::Double* il_sim_opptr = il_sim->get(15)->getAs<types::Double>();
673     il_sim_opptr->convertToInteger();
674     int* l_sim_opptr = (int*) il_sim_opptr->get();
675     int m_opptr = il_sim_opptr->getRows();
676
677     /*17 : sim.clkptr */
678     if (il_sim_input->get(16)->isDouble() == false)
679     {
680         Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 17, 4);
681         il_state->DecreaseRef();
682         il_state->killMe();
683         il_tcur->DecreaseRef();
684         il_tcur->killMe();
685         il_sim->DecreaseRef();
686         il_sim->killMe();
687         return types::Function::Error;
688     }
689     types::Double* il_sim_clkptr_input = il_sim_input->get(16)->getAs<types::Double>();
690     il_sim->append(il_sim_clkptr_input->clone());
691     types::Double* il_sim_clkptr = il_sim->get(16)->getAs<types::Double>();
692     il_sim_clkptr->convertToInteger();
693     int* l_sim_clkptr = (int*) il_sim_clkptr->get();
694     int m_clkptr = il_sim_clkptr->getRows();
695
696     /*18 : sim.ordptr */
697     if (il_sim_input->get(17)->isDouble() == false)
698     {
699         Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 18, 4);
700         il_state->DecreaseRef();
701         il_state->killMe();
702         il_tcur->DecreaseRef();
703         il_tcur->killMe();
704         il_sim->DecreaseRef();
705         il_sim->killMe();
706         return types::Function::Error;
707     }
708     types::Double* il_sim_ordptr_input = il_sim_input->get(17)->getAs<types::Double>();
709     il_sim->append(il_sim_ordptr_input->clone());
710     types::Double* il_sim_ordptr = il_sim->get(17)->getAs<types::Double>();
711     il_sim_ordptr->convertToInteger();
712     int* l_sim_ordptr = (int*) il_sim_ordptr->get();
713     int m_ordptr = il_sim_ordptr->getRows();
714
715     /*19 : sim.execlk */
716     if (il_sim_input->get(18)->isDouble() == false)
717     {
718         Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 19, 4);
719         il_state->DecreaseRef();
720         il_state->killMe();
721         il_tcur->DecreaseRef();
722         il_tcur->killMe();
723         il_sim->DecreaseRef();
724         il_sim->killMe();
725         return types::Function::Error;
726     }
727     types::Double* il_sim_execlk_input = il_sim_input->get(18)->getAs<types::Double>();
728     il_sim->append(il_sim_execlk_input->clone());
729
730     /*20 : sim.ordclk */
731     if (il_sim_input->get(19)->isDouble() == false)
732     {
733         Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 20, 4);
734         il_state->DecreaseRef();
735         il_state->killMe();
736         il_tcur->DecreaseRef();
737         il_tcur->killMe();
738         il_sim->DecreaseRef();
739         il_sim->killMe();
740         return types::Function::Error;
741     }
742     types::Double* il_sim_ordclk_input = il_sim_input->get(19)->getAs<types::Double>();
743     il_sim->append(il_sim_ordclk_input->clone());
744     types::Double* il_sim_ordclk = il_sim->get(19)->getAs<types::Double>();
745     il_sim_ordclk->convertToInteger();
746     int* l_sim_ordclk = (int*) il_sim_ordclk->get();
747     int m_ordclk = il_sim_ordclk->getRows();
748     int n_ordclk = il_sim_ordclk->getCols();
749
750     /*21 : sim.cord   */
751     if (il_sim_input->get(20)->isDouble() == false)
752     {
753         Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 21, 4);
754         il_state->DecreaseRef();
755         il_state->killMe();
756         il_tcur->DecreaseRef();
757         il_tcur->killMe();
758         il_sim->DecreaseRef();
759         il_sim->killMe();
760         return types::Function::Error;
761     }
762     types::Double* il_sim_cord_input = il_sim_input->get(20)->getAs<types::Double>();
763     il_sim->append(il_sim_cord_input->clone());
764     types::Double* il_sim_cord = il_sim->get(20)->getAs<types::Double>();
765     il_sim_cord->convertToInteger();
766     int* l_sim_cord = (int*) il_sim_cord->get();
767     int m_cord = il_sim_cord->getRows();
768     int n_cord = il_sim_cord->getCols();
769
770     /*22 : sim.oord   */
771     if (il_sim_input->get(21)->isDouble() == false)
772     {
773         Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 22, 4);
774         il_state->DecreaseRef();
775         il_state->killMe();
776         il_tcur->DecreaseRef();
777         il_tcur->killMe();
778         il_sim->DecreaseRef();
779         il_sim->killMe();
780         return types::Function::Error;
781     }
782     types::Double* il_sim_oord_input = il_sim_input->get(21)->getAs<types::Double>();
783     il_sim->append(il_sim_oord_input->clone());
784     types::Double* il_sim_oord = il_sim->get(21)->getAs<types::Double>();
785     il_sim_oord->convertToInteger();
786     int* l_sim_oord = (int*) il_sim_oord->get();
787     int m_oord = il_sim_oord->getRows();
788     int n_oord = il_sim_oord->getCols();
789
790     /*23 : sim.zord   */
791     if (il_sim_input->get(22)->isDouble() == false)
792     {
793         Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 23, 4);
794         il_state->DecreaseRef();
795         il_state->killMe();
796         il_tcur->DecreaseRef();
797         il_tcur->killMe();
798         il_sim->DecreaseRef();
799         il_sim->killMe();
800         return types::Function::Error;
801     }
802     types::Double* il_sim_zord_input = il_sim_input->get(22)->getAs<types::Double>();
803     il_sim->append(il_sim_zord_input->clone());
804     types::Double* il_sim_zord = il_sim->get(22)->getAs<types::Double>();
805     il_sim_zord->convertToInteger();
806     int* l_sim_zord = (int*) il_sim_zord->get();
807     int m_zord = il_sim_zord->getRows();
808     int n_zord = il_sim_zord->getCols();
809
810     /*24 : sim.critev */
811     if (il_sim_input->get(23)->isDouble() == false)
812     {
813         Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 24, 4);
814         il_state->DecreaseRef();
815         il_state->killMe();
816         il_tcur->DecreaseRef();
817         il_tcur->killMe();
818         il_sim->DecreaseRef();
819         il_sim->killMe();
820         return types::Function::Error;
821     }
822     types::Double* il_sim_critev_input = il_sim_input->get(23)->getAs<types::Double>();
823     il_sim->append(il_sim_critev_input->clone());
824     types::Double* il_sim_critev = il_sim->get(23)->getAs<types::Double>();
825     il_sim_critev->convertToInteger();
826     int* l_sim_critev = (int*) il_sim_critev->get();
827
828     /*25 : sim.nb     */
829     if (il_sim_input->get(24)->isDouble() == false)
830     {
831         Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 25, 4);
832         il_state->DecreaseRef();
833         il_state->killMe();
834         il_tcur->DecreaseRef();
835         il_tcur->killMe();
836         il_sim->DecreaseRef();
837         il_sim->killMe();
838         return types::Function::Error;
839     }
840     types::Double* il_sim_nb_input = il_sim_input->get(24)->getAs<types::Double>();
841     il_sim->append(il_sim_nb_input->clone());
842     types::Double* il_sim_nb = il_sim->get(24)->getAs<types::Double>();
843     double* l_sim_nb = il_sim_nb->get();
844
845     if (static_cast<int>(l_sim_nb[0]) != nblk) // Value of 'nb' must be equal to 'nblk'
846     {
847         Scierror(42, _("%s : Incompatible sim.nb RHS parameter.\n"), funname.data());
848         il_state->DecreaseRef();
849         il_state->killMe();
850         il_tcur->DecreaseRef();
851         il_tcur->killMe();
852         il_sim->DecreaseRef();
853         il_sim->killMe();
854         return types::Function::Error;
855     }
856
857     /*26 : sim.ztyp   */
858     if (il_sim_input->get(25)->isDouble() == false)
859     {
860         Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 26, 4);
861         il_state->DecreaseRef();
862         il_state->killMe();
863         il_tcur->DecreaseRef();
864         il_tcur->killMe();
865         il_sim->DecreaseRef();
866         il_sim->killMe();
867         return types::Function::Error;
868     }
869     types::Double* il_sim_ztyp_input = il_sim_input->get(25)->getAs<types::Double>();
870     il_sim->append(il_sim_ztyp_input->clone());
871     types::Double* il_sim_ztyp = il_sim->get(25)->getAs<types::Double>();
872     il_sim_ztyp->convertToInteger();
873     int* l_sim_ztyp = (int*) il_sim_ztyp->get();
874
875     /*27 : sim.nblk   */
876     if (il_sim_input->get(26)->isDouble() == false)
877     {
878         Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 27, 4);
879         il_state->DecreaseRef();
880         il_state->killMe();
881         il_tcur->DecreaseRef();
882         il_tcur->killMe();
883         il_sim->DecreaseRef();
884         il_sim->killMe();
885         return types::Function::Error;
886     }
887     types::Double* il_sim_nblk_input = il_sim_input->get(26)->getAs<types::Double>();
888     il_sim->append(il_sim_nblk_input->clone());
889
890     /*28 : sim.ndcblk */
891     if (il_sim_input->get(27)->isDouble() == false)
892     {
893         Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 28, 4);
894         il_state->DecreaseRef();
895         il_state->killMe();
896         il_tcur->DecreaseRef();
897         il_tcur->killMe();
898         il_sim->DecreaseRef();
899         il_sim->killMe();
900         return types::Function::Error;
901     }
902     types::Double* il_sim_ndcblk_input = il_sim_input->get(27)->getAs<types::Double>();
903     il_sim->append(il_sim_ndcblk_input->clone());
904
905     /*29 : sim.subscr */
906     if (il_sim_input->get(28)->isDouble() == false)
907     {
908         Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 29, 4);
909         il_state->DecreaseRef();
910         il_state->killMe();
911         il_tcur->DecreaseRef();
912         il_tcur->killMe();
913         il_sim->DecreaseRef();
914         il_sim->killMe();
915         return types::Function::Error;
916     }
917     types::Double* il_sim_subscr_input = il_sim_input->get(28)->getAs<types::Double>();
918     il_sim->append(il_sim_subscr_input->clone());
919     types::Double* il_sim_subscr = il_sim->get(28)->getAs<types::Double>();
920     il_sim_subscr->convertToInteger();
921     int* l_sim_subscr = (int*) il_sim_subscr->get();
922     int m_subscr = il_sim_subscr->getRows();
923
924     /*30 : sim.funtyp */
925     if (il_sim_input->get(29)->isDouble() == false)
926     {
927         Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 30, 4);
928         il_state->DecreaseRef();
929         il_state->killMe();
930         il_tcur->DecreaseRef();
931         il_tcur->killMe();
932         il_sim->DecreaseRef();
933         il_sim->killMe();
934         return types::Function::Error;
935     }
936     types::Double* il_sim_funtyp_input = il_sim_input->get(29)->getAs<types::Double>();
937     il_sim->append(il_sim_funtyp_input->clone());
938     types::Double* il_sim_funtyp = il_sim->get(29)->getAs<types::Double>();
939     il_sim_funtyp->convertToInteger();
940     int* l_sim_funtyp = (int*) il_sim_funtyp->get();
941
942     /*31 : sim.iord   */
943     if (il_sim_input->get(30)->isDouble() == false)
944     {
945         Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 31, 4);
946         il_state->DecreaseRef();
947         il_state->killMe();
948         il_tcur->DecreaseRef();
949         il_tcur->killMe();
950         il_sim->DecreaseRef();
951         il_sim->killMe();
952         return types::Function::Error;
953     }
954     types::Double* il_sim_iord_input = il_sim_input->get(30)->getAs<types::Double>();
955     il_sim->append(il_sim_iord_input->clone());
956     types::Double* il_sim_iord = il_sim->get(30)->getAs<types::Double>();
957     il_sim_iord->convertToInteger();
958     int* l_sim_iord = (int*) il_sim_iord->get();
959     int m_iord = il_sim_iord->getRows();
960     int n_iord = il_sim_iord->getCols();
961
962     /*32 : sim.labels */
963     if (il_sim_input->get(31)->isString() == false)
964     {
965         Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A string matrix expected.\n"), funname.data(), 32, 4);
966         il_state->DecreaseRef();
967         il_state->killMe();
968         il_tcur->DecreaseRef();
969         il_tcur->killMe();
970         il_sim->DecreaseRef();
971         il_sim->killMe();
972         return types::Function::Error;
973     }
974     types::String* il_sim_lab_input = il_sim_input->get(31)->getAs<types::String>();
975     il_sim->append(il_sim_lab_input->clone());
976     types::String* il_sim_lab = il_sim->get(31)->getAs<types::String>();
977     int* il_sim_labptr;
978     try
979     {
980         il_sim_labptr = new int[il_sim_lab->getSize()];
981     }
982     catch (const std::bad_alloc& /*e*/)
983     {
984         Scierror(999, _("%s: Memory allocation error.\n"), funname.data());
985         il_state->DecreaseRef();
986         il_state->killMe();
987         il_tcur->DecreaseRef();
988         il_tcur->killMe();
989         il_sim->DecreaseRef();
990         il_sim->killMe();
991         return types::Function::Error;
992     }
993     char** l_sim_lab;
994     try
995     {
996         l_sim_lab = new char*[il_sim_lab->getSize()];
997     }
998     catch (const std::bad_alloc& /*e*/)
999     {
1000         Scierror(999, _("%s: Memory allocation error.\n"), funname.data());
1001         il_state->DecreaseRef();
1002         il_state->killMe();
1003         il_tcur->DecreaseRef();
1004         il_tcur->killMe();
1005         il_sim->DecreaseRef();
1006         il_sim->killMe();
1007         delete[] il_sim_labptr;
1008         return types::Function::Error;
1009     }
1010     for (int i = 0; i < il_sim_lab->getSize(); ++i)
1011     {
1012         l_sim_lab[i] = wide_string_to_UTF8(il_sim_lab->get(i));
1013         il_sim_labptr[i] = static_cast<int>(strlen(l_sim_lab[i]));
1014     }
1015
1016     /*33 : sim.modptr */
1017     if (il_sim_input->get(32)->isDouble() == false)
1018     {
1019         Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A matrix expected.\n"), funname.data(), 33, 4);
1020         il_state->DecreaseRef();
1021         il_state->killMe();
1022         il_tcur->DecreaseRef();
1023         il_tcur->killMe();
1024         il_sim->DecreaseRef();
1025         il_sim->killMe();
1026         delete[] il_sim_labptr;
1027         delete[] l_sim_lab;
1028         return types::Function::Error;
1029     }
1030     types::Double* il_sim_modptr_input = il_sim_input->get(32)->getAs<types::Double>();
1031     il_sim->append(il_sim_modptr_input->clone());
1032     types::Double* il_sim_modptr = il_sim->get(32)->getAs<types::Double>();
1033     il_sim_modptr->convertToInteger();
1034     int* l_sim_modptr = (int*) il_sim_modptr->get();
1035
1036     /*34 : sim.uids */
1037     if (il_sim_input->get(33)->isString() == false)
1038     {
1039         Scierror(999, _("%s: Wrong type for element #%d of argument #%d : A string matrix expected.\n"), funname.data(), 34, 4);
1040         il_state->DecreaseRef();
1041         il_state->killMe();
1042         il_tcur->DecreaseRef();
1043         il_tcur->killMe();
1044         il_sim->DecreaseRef();
1045         il_sim->killMe();
1046         delete[] il_sim_labptr;
1047         delete[] l_sim_lab;
1048         return types::Function::Error;
1049     }
1050     types::String* il_sim_uid_input = il_sim_input->get(33)->getAs<types::String>();
1051     il_sim->append(il_sim_uid_input->clone());
1052     types::String* il_sim_uid = il_sim->get(33)->getAs<types::String>();
1053     int* il_sim_uidptr;
1054     try
1055     {
1056         il_sim_uidptr = new int[il_sim_uid->getSize()];
1057     }
1058     catch (const std::bad_alloc& /*e*/)
1059     {
1060         Scierror(999, _("%s: Memory allocation error.\n"), funname.data());
1061         il_state->DecreaseRef();
1062         il_state->killMe();
1063         il_tcur->DecreaseRef();
1064         il_tcur->killMe();
1065         il_sim->DecreaseRef();
1066         il_sim->killMe();
1067         delete[] il_sim_labptr;
1068         delete[] l_sim_lab;
1069         return types::Function::Error;
1070     }
1071     char** l_sim_uid;
1072     try
1073     {
1074         l_sim_uid = new char*[il_sim_uid->getSize()];
1075     }
1076     catch (const std::bad_alloc& /*e*/)
1077     {
1078         Scierror(999, _("%s: Memory allocation error.\n"), funname.data());
1079         il_state->DecreaseRef();
1080         il_state->killMe();
1081         il_tcur->DecreaseRef();
1082         il_tcur->killMe();
1083         il_sim->DecreaseRef();
1084         il_sim->killMe();
1085         delete[] il_sim_labptr;
1086         delete[] l_sim_lab;
1087         delete[] il_sim_uidptr;
1088         return types::Function::Error;
1089     }
1090     for (int i = 0; i < il_sim_uid->getSize(); ++i)
1091     {
1092         l_sim_uid[i] = wide_string_to_UTF8(il_sim_uid->get(i));
1093         il_sim_uidptr[i] = static_cast<int>(strlen(l_sim_uid[i]));
1094     }
1095
1096     /*************
1097     * str (rhs 5)
1098     *************/
1099     if (in[4]->isString() == false)
1100     {
1101         Scierror(999, _("%s: Wrong type for input argument #%d : A matrix expected.\n"), funname.data(), 5);
1102         il_state->DecreaseRef();
1103         il_state->killMe();
1104         il_tcur->DecreaseRef();
1105         il_tcur->killMe();
1106         il_sim->DecreaseRef();
1107         il_sim->killMe();
1108         delete[] il_sim_labptr;
1109         delete[] l_sim_lab;
1110         delete[] il_sim_uidptr;
1111         delete[] l_sim_uid;
1112         return types::Function::Error;
1113     }
1114     types::String* il_str = in[4]->getAs<types::String>();
1115     if (il_str->isScalar() == false)
1116     {
1117         Scierror(999, _("%s: Wrong size for input argument #%d : A scalar expected.\n"), funname.data(), 5);
1118         il_state->DecreaseRef();
1119         il_state->killMe();
1120         il_tcur->DecreaseRef();
1121         il_tcur->killMe();
1122         il_sim->DecreaseRef();
1123         il_sim->killMe();
1124         delete[] il_sim_labptr;
1125         delete[] l_sim_lab;
1126         delete[] il_sim_uidptr;
1127         delete[] l_sim_uid;
1128         return types::Function::Error;
1129     }
1130
1131     const std::wstring start  (L"start");
1132     const std::wstring run    (L"run");
1133     const std::wstring finish (L"finish");
1134     const std::wstring linear (L"linear");
1135     const std::wstring Kinsol (L"Kinsol");
1136     int flag;
1137     if (il_str->get(0) == start)
1138     {
1139         flag = 1;
1140     }
1141     else if (il_str->get(0) == run)
1142     {
1143         flag = 2;
1144     }
1145     else if (il_str->get(0) == finish)
1146     {
1147         flag = 3;
1148     }
1149     else if (il_str->get(0) == linear)
1150     {
1151         flag = 4;
1152     }
1153     else if (il_str->get(0) == Kinsol)
1154     {
1155         flag = 5;
1156     }
1157     else
1158     {
1159         Scierror(44, _("%s: Wrong value for input argument #%d : ""%s"", ""%s"" ""%s"" ""%s"" or ""%s"" expected.\n"), funname.data(), 5,
1160                  "start", "run", "finish", "linear", "Kinsol");
1161         il_state->DecreaseRef();
1162         il_state->killMe();
1163         il_tcur->DecreaseRef();
1164         il_tcur->killMe();
1165         il_sim->DecreaseRef();
1166         il_sim->killMe();
1167         delete[] il_sim_labptr;
1168         delete[] l_sim_lab;
1169         delete[] il_sim_uidptr;
1170         delete[] l_sim_uid;
1171         return types::Function::Error;
1172     }
1173
1174     /*************
1175     * tol (rhs 6)
1176     *************/
1177     if (in[5]->isDouble() == false)
1178     {
1179         Scierror(999, _("%s: Wrong type for input argument #%d : A matrix expected.\n"), funname.data(), 6);
1180         il_state->DecreaseRef();
1181         il_state->killMe();
1182         il_tcur->DecreaseRef();
1183         il_tcur->killMe();
1184         il_sim->DecreaseRef();
1185         il_sim->killMe();
1186         delete[] il_sim_labptr;
1187         delete[] l_sim_lab;
1188         delete[] il_sim_uidptr;
1189         delete[] l_sim_uid;
1190         return types::Function::Error;
1191     }
1192     types::Double* il_tol = in[5]->getAs<types::Double>();
1193     int m6 = il_tol->getSize();
1194     if (m6 < 4 || m6 > 7) // Check if 'tol' has 4 to 7 elements
1195     {
1196         Scierror(999, _("%s: Wrong size for input argument #%d : %d to %d elements expected.\n"), funname.data(), 6, 4, 7);
1197         il_state->DecreaseRef();
1198         il_state->killMe();
1199         il_tcur->DecreaseRef();
1200         il_tcur->killMe();
1201         il_sim->DecreaseRef();
1202         il_sim->killMe();
1203         delete[] il_sim_labptr;
1204         delete[] l_sim_lab;
1205         delete[] il_sim_uidptr;
1206         delete[] l_sim_uid;
1207         return types::Function::Error;
1208     }
1209
1210     /******************
1211     * Set 'simpar' array
1212     ******************/
1213     for (int i = 0; i < m6; ++i)
1214     {
1215         simpar[i] = il_tol->get(i);
1216     }
1217     for (int i = m6; i < 7; ++i)
1218     {
1219         simpar[i] = 0;
1220     }
1221
1222     /******************************
1223     * Cross variable size checking
1224     ******************************/
1225     int err_check = 0;
1226     if (m1e6 != m1e7)
1227     {
1228         err_check = 1;    // tevts vs evtspt
1229     }
1230     else if (m_xptr != m_zptr)
1231     {
1232         err_check = 2;    // xptr vs zptr
1233     }
1234     else if (m_xptr != m_ozptr)
1235     {
1236         err_check = 3;    // xptr vs ozptr
1237     }
1238     else if (m_xptr != m_zcptr)
1239     {
1240         err_check = 4;    // xptr vs zcptr
1241     }
1242     else if (m_xptr != m_inpptr)
1243     {
1244         err_check = 5;    // xptr vs inpptr
1245     }
1246     else if (m_xptr != m_outptr)
1247     {
1248         err_check = 6;    // xptr vs outptr
1249     }
1250     else if (m_xptr != m_rpptr)
1251     {
1252         err_check = 7;    // xptr vs rpptr
1253     }
1254     else if (m_xptr != m_ipptr)
1255     {
1256         err_check = 8;    // xptr vs ipptr
1257     }
1258     else if (m_xptr != m_opptr)
1259     {
1260         err_check = 8;    // xptr vs opptr
1261     }
1262     else if (m_xptr != m_clkptr)
1263     {
1264         err_check = 10;    // xptr vs clkptr
1265     }
1266     else if ((n_ordclk != 2) & (m_ordclk != 0))
1267     {
1268         err_check = 11;    // sim.ordclk
1269     }
1270     else if ((n_cord != 2) & (m_cord != 0))
1271     {
1272         err_check = 12;    // sim.cord
1273     }
1274     else if ((n_oord != 2) & (m_oord != 0))
1275     {
1276         err_check = 13;    // sim.oord
1277     }
1278     else if ((n_zord != 2) & (m_zord != 0))
1279     {
1280         err_check = 14;    // sim.zord
1281     }
1282     else if ((n_iord != 2) & (m_iord != 0))
1283     {
1284         err_check = 15;    // sim.iord
1285     }
1286     if (err_check != 0)
1287     {
1288         // Please write an error table here
1289         Scierror(42, _("%s : error in cross variable size checking : %d\n"), funname.data(), err_check);
1290         il_state->DecreaseRef();
1291         il_state->killMe();
1292         il_tcur->DecreaseRef();
1293         il_tcur->killMe();
1294         il_sim->DecreaseRef();
1295         il_sim->killMe();
1296         delete[] il_sim_labptr;
1297         delete[] l_sim_lab;
1298         delete[] il_sim_uidptr;
1299         delete[] l_sim_uid;
1300         return types::Function::Error;
1301     }
1302
1303     /*******************************
1304     * Set function table for blocks
1305     *******************************/
1306     // Define new variable 'lfunpt'
1307     void** lfunpt;
1308     try
1309     {
1310         lfunpt = new void*[nblk];
1311     }
1312     catch (const std::bad_alloc& /*e*/)
1313     {
1314         Scierror(999, _("%s: Memory allocation error.\n"), funname.data());
1315         il_state->DecreaseRef();
1316         il_state->killMe();
1317         il_tcur->DecreaseRef();
1318         il_tcur->killMe();
1319         il_sim->DecreaseRef();
1320         il_sim->killMe();
1321         delete[] il_sim_labptr;
1322         delete[] l_sim_lab;
1323         delete[] il_sim_uidptr;
1324         delete[] l_sim_uid;
1325         return types::Function::Error;
1326     }
1327
1328     // For each block
1329     for (int i = 0; i < nblk; ++i) // For each block
1330     {
1331         types::InternalType* pIT = il_sim_fun->get(i);
1332         // Block is defined by a Scilab function
1333         if (pIT->isCallable())
1334         {
1335             lfunpt[i] = (void*)pIT;
1336             // Keep 'l_sim_funtyp' negative for Scilab macros
1337             if (l_sim_funtyp[i] > 0)
1338             {
1339                 l_sim_funtyp[i] *= -1;
1340             }
1341         }
1342         // Block is defined by a function described by a string
1343         else if (pIT->isString())
1344         {
1345             types::String* funStr = pIT->getAs<types::String>();
1346             if (funStr->isScalar() == false)
1347             {
1348                 Scierror(999, _("%s: Wrong size for element #%d of input argument #%d : A scalar expected.\n"), funname.data(), i + 1, 4);
1349                 il_state->DecreaseRef();
1350                 il_state->killMe();
1351                 il_tcur->DecreaseRef();
1352                 il_tcur->killMe();
1353                 il_sim->DecreaseRef();
1354                 il_sim->killMe();
1355                 delete[] il_sim_labptr;
1356                 delete[] l_sim_lab;
1357                 delete[] il_sim_uidptr;
1358                 delete[] l_sim_uid;
1359                 delete[] lfunpt;
1360                 return types::Function::Error;
1361             }
1362
1363             wchar_t* w_str = funStr->get(0);
1364             char* c_str = wide_string_to_UTF8(w_str);
1365             if (strcmp(c_str, "ifthel") == 0)
1366             {
1367                 l_sim_funtyp[i] = 11; // Magic value for "if-then-else" block
1368             }
1369             else if (strcmp(c_str, "eselect") == 0)
1370             {
1371                 l_sim_funtyp[i] = 12; // Magic value for "eselect" block
1372             }
1373             else
1374             {
1375                 void* f = funnum2(c_str); // Search associated function number of function name
1376                 // Block is defined by a C or Fortran function
1377                 if (f != nullptr)
1378                 {
1379                     // C interface from "tabsim" defined in blocks.h
1380                     lfunpt[i] = f;
1381                     if (l_sim_funtyp[i] < 0)
1382                     {
1383                         // Keep 'l_sim_funtyp' positive for Fortran functions
1384                         l_sim_funtyp[i] *= -1;
1385                     }
1386                 }
1387                 // Block is defined by a predefined scilab function
1388                 else
1389                 {
1390                     ConfigVariable::EntryPointStr* pEP = ConfigVariable::getEntryPoint(w_str);
1391                     if (pEP)
1392                     {
1393                         //linked functions
1394                         lfunpt[i] = (void*)pEP->functionPtr;
1395                     }
1396                     else
1397                     {
1398                         types::InternalType* pMacro = symbol::Context::getInstance()->get(symbol::Symbol(w_str));
1399                         if (pMacro && pMacro->isCallable())
1400                         {
1401                             //macros
1402                             lfunpt[i] = (void*)pMacro;
1403                             l_sim_funtyp[i] *= -1;
1404                         }
1405                         else
1406                         {
1407                             Scierror(888, _("%s : unknown block : %s\n"), funname.data(), c_str);
1408                             il_state->DecreaseRef();
1409                             il_state->killMe();
1410                             il_tcur->DecreaseRef();
1411                             il_tcur->killMe();
1412                             il_sim->DecreaseRef();
1413                             il_sim->killMe();
1414                             delete[] il_sim_labptr;
1415                             delete[] l_sim_lab;
1416                             delete[] il_sim_uidptr;
1417                             delete[] l_sim_uid;
1418                             delete[] lfunpt;
1419                             FREE(c_str);
1420                             return types::Function::Error;
1421                         }
1422                     }
1423                 }
1424             }
1425             FREE(c_str);
1426         }
1427         else
1428         {
1429             Scierror(999, _("%s: Wrong type for element #%d of input argument #%d : string or macro expected.\n"), funname.data(), i + 1, 4);
1430             il_state->DecreaseRef();
1431             il_state->killMe();
1432             il_tcur->DecreaseRef();
1433             il_tcur->killMe();
1434             il_sim->DecreaseRef();
1435             il_sim->killMe();
1436             delete[] il_sim_labptr;
1437             delete[] l_sim_lab;
1438             delete[] il_sim_uidptr;
1439             delete[] l_sim_uid;
1440             delete[] lfunpt;
1441             return types::Function::Error;
1442         }
1443     }
1444
1445     /**********************
1446     * Set oz, ozsz, oztyp
1447     **********************/
1448     void** oz = nullptr;
1449     int* ozsz = nullptr;
1450     int* oztyp = nullptr;
1451     if (noz > 0)
1452     {
1453         // Allocation of 'oz'
1454         try
1455         {
1456             oz = new void*[noz];
1457         }
1458         catch (const std::bad_alloc& /*e*/)
1459         {
1460             Scierror(999, _("%s: Memory allocation error.\n"), funname.data());
1461             il_state->DecreaseRef();
1462             il_state->killMe();
1463             il_tcur->DecreaseRef();
1464             il_tcur->killMe();
1465             il_sim->DecreaseRef();
1466             il_sim->killMe();
1467             delete[] il_sim_labptr;
1468             delete[] l_sim_lab;
1469             delete[] il_sim_uidptr;
1470             delete[] l_sim_uid;
1471             delete[] lfunpt;
1472             return types::Function::Error;
1473         }
1474         // Allocation of 'ozsz'
1475         try
1476         {
1477             ozsz = new int[2 * noz];
1478         }
1479         catch (const std::bad_alloc& /*e*/)
1480         {
1481             Scierror(999, _("%s: Memory allocation error.\n"), funname.data());
1482             il_state->DecreaseRef();
1483             il_state->killMe();
1484             il_tcur->DecreaseRef();
1485             il_tcur->killMe();
1486             il_sim->DecreaseRef();
1487             il_sim->killMe();
1488             delete[] il_sim_labptr;
1489             delete[] l_sim_lab;
1490             delete[] il_sim_uidptr;
1491             delete[] l_sim_uid;
1492             delete[] lfunpt;
1493             delete[] oz;
1494             return types::Function::Error;
1495         }
1496         // Allocation of 'oztyp'
1497         try
1498         {
1499             oztyp = new int[noz];
1500         }
1501         catch (const std::bad_alloc& /*e*/)
1502         {
1503             Scierror(999, _("%s: Memory allocation error.\n"), funname.data());
1504             il_state->DecreaseRef();
1505             il_state->killMe();
1506             il_tcur->DecreaseRef();
1507             il_tcur->killMe();
1508             il_sim->DecreaseRef();
1509             il_sim->killMe();
1510             delete[] il_sim_labptr;
1511             delete[] l_sim_lab;
1512             delete[] il_sim_uidptr;
1513             delete[] l_sim_uid;
1514             delete[] lfunpt;
1515             delete[] oz;
1516             delete[] ozsz;
1517             return types::Function::Error;
1518         }
1519
1520         // Set vectors of 'oz'
1521         for (int j = 0; j < noz; ++j)
1522         {
1523             int subtype = il_state_oz->get(j)->getType();
1524
1525             switch (subtype) // Store type and address
1526             {
1527                 case types::InternalType::ScilabDouble :
1528                 {
1529                     types::Double* ozDouble = il_state_oz->get(j)->getAs<types::Double>();
1530                     if (ozDouble->isComplex() == false)
1531                     {
1532                         oztyp[j] = SCSREAL_N; // Double real matrix
1533                         oz[j] = (SCSREAL_COP *) ozDouble->get();
1534                     }
1535                     else
1536                     {
1537                         oztyp[j] = SCSCOMPLEX_N; // Double complex matrix
1538                         oz[j] = (SCSCOMPLEX_COP *) ozDouble->get();
1539                     }
1540                     ozsz[j] = ozDouble->getRows();
1541                     ozsz[j + noz] = ozDouble->getCols();
1542                     break;
1543                 }
1544                 case types::InternalType::ScilabInt8 :
1545                 {
1546                     types::Int8* ozInt8 = il_state_oz->get(j)->getAs<types::Int8>();
1547                     oztyp[j] = SCSINT8_N; // int8
1548                     ozsz[j] = ozInt8->getRows();
1549                     ozsz[j + noz] = ozInt8->getCols();
1550                     oz[j] = (SCSINT8_COP *) ozInt8->get();
1551                     break;
1552                 }
1553                 case types::InternalType::ScilabInt16 :
1554                 {
1555                     types::Int16* ozInt16 = il_state_oz->get(j)->getAs<types::Int16>();
1556                     oztyp[j] = SCSINT16_N; // int16
1557                     ozsz[j] = ozInt16->getRows();
1558                     ozsz[j + noz] = ozInt16->getCols();
1559                     oz[j] = (SCSINT16_COP *) ozInt16->get();
1560                     break;
1561                 }
1562                 case types::InternalType::ScilabInt32 :
1563                 {
1564                     types::Int32* ozInt32 = il_state_oz->get(j)->getAs<types::Int32>();
1565                     oztyp[j] = SCSINT32_N; // int32
1566                     ozsz[j] = ozInt32->getRows();
1567                     ozsz[j + noz] = ozInt32->getCols();
1568                     oz[j] = (SCSINT32_COP *) ozInt32->get();
1569                     break;
1570                 }
1571                 case types::InternalType::ScilabUInt8 :
1572                 {
1573                     types::UInt8* ozUInt8 = il_state_oz->get(j)->getAs<types::UInt8>();
1574                     oztyp[j] = SCSUINT8_N; // uint8
1575                     ozsz[j] = ozUInt8->getRows();
1576                     ozsz[j + noz] = ozUInt8->getCols();
1577                     oz[j] = (SCSUINT8_COP *) ozUInt8->get();
1578                     break;
1579                 }
1580                 case types::InternalType::ScilabUInt16 :
1581                 {
1582                     types::UInt16* ozUInt16 = il_state_oz->get(j)->getAs<types::UInt16>();
1583                     oztyp[j] = SCSUINT16_N; // uint16
1584                     ozsz[j] = ozUInt16->getRows();
1585                     ozsz[j + noz] = ozUInt16->getCols();
1586                     oz[j] = (SCSUINT16_COP *) ozUInt16->get();
1587                     break;
1588                 }
1589                 case types::InternalType::ScilabUInt32 :
1590                 {
1591                     types::UInt32* ozUInt32 = il_state_oz->get(j)->getAs<types::UInt32>();
1592                     oztyp[j] = SCSUINT32_N; // uint32
1593                     ozsz[j] = ozUInt32->getRows();
1594                     ozsz[j + noz] = ozUInt32->getCols();
1595                     oz[j] = (SCSUINT32_COP *) ozUInt32->get();
1596                     break;
1597                 }
1598                 default :
1599                 {
1600                     Scierror(999, _("%s: Wrong type for element #%d of element #%d of argument #%d : A matrix expected.\n"), funname.data(), j + 1, 4, 1);
1601                     il_state->DecreaseRef();
1602                     il_state->killMe();
1603                     il_tcur->DecreaseRef();
1604                     il_tcur->killMe();
1605                     il_sim->DecreaseRef();
1606                     il_sim->killMe();
1607                     delete[] il_sim_labptr;
1608                     delete[] l_sim_lab;
1609                     delete[] il_sim_uidptr;
1610                     delete[] l_sim_uid;
1611                     delete[] lfunpt;
1612                     delete[] oz;
1613                     delete[] ozsz;
1614                     delete[] oztyp;
1615                     return types::Function::Error;
1616                 }
1617             }
1618         }
1619     }
1620
1621     /****************************
1622     * Set opar, oparsz, opartyp
1623     ****************************/
1624     void** opar = nullptr;
1625     int* oparsz = nullptr;
1626     int* opartyp = nullptr;
1627     if (nopar > 0)
1628     {
1629         // Allocation of 'opar'
1630         try
1631         {
1632             opar = new void*[nopar];
1633         }
1634         catch (const std::bad_alloc& /*e*/)
1635         {
1636             Scierror(999, _("%s: Memory allocation error.\n"), funname.data());
1637             il_state->DecreaseRef();
1638             il_state->killMe();
1639             il_tcur->DecreaseRef();
1640             il_tcur->killMe();
1641             il_sim->DecreaseRef();
1642             il_sim->killMe();
1643             delete[] il_sim_labptr;
1644             delete[] l_sim_lab;
1645             delete[] il_sim_uidptr;
1646             delete[] l_sim_uid;
1647             delete[] lfunpt;
1648             delete[] oz;
1649             delete[] ozsz;
1650             delete[] oztyp;
1651             return types::Function::Error;
1652         }
1653         // Allocation of 'oparsz'
1654         try
1655         {
1656             oparsz = new int[2 * nopar];
1657         }
1658         catch (const std::bad_alloc& /*e*/)
1659         {
1660             Scierror(999, _("%s: Memory allocation error.\n"), funname.data());
1661             il_state->DecreaseRef();
1662             il_state->killMe();
1663             il_tcur->DecreaseRef();
1664             il_tcur->killMe();
1665             il_sim->DecreaseRef();
1666             il_sim->killMe();
1667             delete[] il_sim_labptr;
1668             delete[] l_sim_lab;
1669             delete[] il_sim_uidptr;
1670             delete[] l_sim_uid;
1671             delete[] lfunpt;
1672             delete[] oz;
1673             delete[] ozsz;
1674             delete[] oztyp;
1675             delete[] opar;
1676             return types::Function::Error;
1677         }
1678         // Allocation of 'opartyp'
1679         try
1680         {
1681             opartyp = new int[nopar];
1682         }
1683         catch (const std::bad_alloc& /*e*/)
1684         {
1685             Scierror(999, _("%s: Memory allocation error.\n"), funname.data());
1686             il_state->DecreaseRef();
1687             il_state->killMe();
1688             il_tcur->DecreaseRef();
1689             il_tcur->killMe();
1690             il_sim->DecreaseRef();
1691             il_sim->killMe();
1692             delete[] il_sim_labptr;
1693             delete[] l_sim_lab;
1694             delete[] il_sim_uidptr;
1695             delete[] l_sim_uid;
1696             delete[] lfunpt;
1697             delete[] oz;
1698             delete[] ozsz;
1699             delete[] oztyp;
1700             delete[] opar;
1701             delete[] oparsz;
1702             return types::Function::Error;
1703         }
1704
1705         // Set vectors of 'opar'
1706         for (int j = 0; j < nopar; ++j)
1707         {
1708             int subtype = il_sim_opar->get(j)->getType();
1709
1710             switch (subtype) // Store type and address
1711             {
1712                 case types::InternalType::ScilabDouble :
1713                 {
1714                     types::Double* oparDouble = il_sim_opar->get(j)->getAs<types::Double>();
1715                     if (oparDouble->isComplex() == false)
1716                     {
1717                         opartyp[j] = SCSREAL_N; // Double real matrix
1718                         opar[j] = (SCSREAL_COP *) oparDouble->get();
1719                     }
1720                     else
1721                     {
1722                         opartyp[j] = SCSCOMPLEX_N; // Double complex matrix
1723                         // Allocate a long array in order to make the real and complex parts contiguous (oparDouble->get() and oparDouble->getImg())
1724                         try
1725                         {
1726                             opar[j] = new double[2 * oparDouble->getSize()];
1727                         }
1728                         catch (const std::bad_alloc& /*e*/)
1729                         {
1730                             Scierror(999, _("%s: Memory allocation error.\n"), funname.data());
1731                             il_state->DecreaseRef();
1732                             il_state->killMe();
1733                             il_tcur->DecreaseRef();
1734                             il_tcur->killMe();
1735                             il_sim->DecreaseRef();
1736                             il_sim->killMe();
1737                             delete[] il_sim_labptr;
1738                             delete[] l_sim_lab;
1739                             delete[] il_sim_uidptr;
1740                             delete[] l_sim_uid;
1741                             delete[] lfunpt;
1742                             delete[] oz;
1743                             delete[] ozsz;
1744                             delete[] oztyp;
1745                             delete[] opar;
1746                             delete[] oparsz;
1747                             delete[] opartyp;
1748                             return types::Function::Error;
1749                         }
1750                         memcpy(opar[j], oparDouble->get(), oparDouble->getSize() * sizeof(double)); // Real part
1751                         memcpy((double*)(opar[j]) + oparDouble->getSize(), oparDouble->getImg(), oparDouble->getSize() * sizeof(double)); // Complex part
1752                         // FIXME: delete oparDouble because we copied it instead of using it?
1753                     }
1754                     oparsz[j] = oparDouble->getRows();
1755                     oparsz[j + nopar] = oparDouble->getCols();
1756                     break;
1757                 }
1758                 case types::InternalType::ScilabInt8 :
1759                 {
1760                     types::Int8* oparInt8 = il_sim_opar->get(j)->getAs<types::Int8>();
1761                     opartyp[j] = SCSINT8_N; // int8
1762                     oparsz[j] = oparInt8->getRows();
1763                     oparsz[j + nopar] = oparInt8->getCols();
1764                     opar[j] = (SCSINT8_COP *) oparInt8->get();
1765                     break;
1766                 }
1767                 case types::InternalType::ScilabInt16 :
1768                 {
1769                     types::Int16* oparInt16 = il_sim_opar->get(j)->getAs<types::Int16>();
1770                     opartyp[j] = SCSINT16_N; // int16
1771                     oparsz[j] = oparInt16->getRows();
1772                     oparsz[j + nopar] = oparInt16->getCols();
1773                     opar[j] = (SCSINT16_COP *) oparInt16->get();
1774                     break;
1775                 }
1776                 case types::InternalType::ScilabInt32 :
1777                 {
1778                     types::Int32* oparInt32 = il_sim_opar->get(j)->getAs<types::Int32>();
1779                     opartyp[j] = SCSINT32_N; // int32
1780                     oparsz[j] = oparInt32->getRows();
1781                     oparsz[j + nopar] = oparInt32->getCols();
1782                     opar[j] = (SCSINT32_COP *) oparInt32->get();
1783                     break;
1784                 }
1785                 case types::InternalType::ScilabUInt8 :
1786                 {
1787                     types::UInt8* oparUInt8 = il_sim_opar->get(j)->getAs<types::UInt8>();
1788                     opartyp[j] = SCSUINT8_N; // uint8
1789                     oparsz[j] = oparUInt8->getRows();
1790                     oparsz[j + nopar] = oparUInt8->getCols();
1791                     opar[j] = (SCSUINT8_COP *) oparUInt8->get();
1792                     break;
1793                 }
1794                 case types::InternalType::ScilabUInt16 :
1795                 {
1796                     types::UInt16* oparUInt16 = il_sim_opar->get(j)->getAs<types::UInt16>();
1797                     opartyp[j] = SCSUINT16_N; // uint16
1798                     oparsz[j] = oparUInt16->getRows();
1799                     oparsz[j + nopar] = oparUInt16->getCols();
1800                     opar[j] = (SCSUINT16_COP *) oparUInt16->get();
1801                     break;
1802                 }
1803                 case types::InternalType::ScilabUInt32 :
1804                 {
1805                     types::UInt32* oparUInt32 = il_sim_opar->get(j)->getAs<types::UInt32>();
1806                     opartyp[j] = SCSUINT32_N; // uint32
1807                     oparsz[j] = oparUInt32->getRows();
1808                     oparsz[j + nopar] = oparUInt32->getCols();
1809                     opar[j] = (SCSUINT32_COP *) oparUInt32->get();
1810                     break;
1811                 }
1812                 default :
1813                 {
1814                     Scierror(999, _("%s: Wrong type for element #%d of element #%d of argument #%d : A matrix expected.\n"), funname.data(), j + 1, 15, 4);
1815                     il_state->DecreaseRef();
1816                     il_state->killMe();
1817                     il_tcur->DecreaseRef();
1818                     il_tcur->killMe();
1819                     il_sim->DecreaseRef();
1820                     il_sim->killMe();
1821                     delete[] il_sim_labptr;
1822                     delete[] l_sim_lab;
1823                     delete[] il_sim_uidptr;
1824                     delete[] l_sim_uid;
1825                     delete[] lfunpt;
1826                     delete[] oz;
1827                     delete[] ozsz;
1828                     delete[] oztyp;
1829                     delete[] opar;
1830                     delete[] oparsz;
1831                     delete[] opartyp;
1832                     return types::Function::Error;
1833                 }
1834             }
1835         }
1836     }
1837
1838     // Declaration of 'outtb_elem'
1839     outtb_el* outtb_elem = nullptr;
1840     int nelem = 0;
1841
1842     /*******************************
1843     * Set outtbptr, outtbsz, outtbtyp
1844     *******************************/
1845     void** outtbptr = nullptr;
1846     int* outtbsz = nullptr;
1847     int* outtbtyp = nullptr;
1848     if (nlnk > 0)
1849     {
1850         // Allocation of 'outtbptr'
1851         try
1852         {
1853             outtbptr = new void*[nlnk];
1854         }
1855         catch (const std::bad_alloc& /*e*/)
1856         {
1857             Scierror(999, _("%s: Memory allocation error.\n"), funname.data());
1858             il_state->DecreaseRef();
1859             il_state->killMe();
1860             il_tcur->DecreaseRef();
1861             il_tcur->killMe();
1862             il_sim->DecreaseRef();
1863             il_sim->killMe();
1864             delete[] il_sim_labptr;
1865             delete[] l_sim_lab;
1866             delete[] il_sim_uidptr;
1867             delete[] l_sim_uid;
1868             delete[] lfunpt;
1869             delete[] oz;
1870             delete[] ozsz;
1871             delete[] oztyp;
1872             delete[] opar;
1873             delete[] oparsz;
1874             delete[] opartyp;
1875             return types::Function::Error;
1876         }
1877         // Allocation of 'outtbptrsz'
1878         try
1879         {
1880             outtbsz = new int[2 * nlnk];
1881         }
1882         catch (const std::bad_alloc& /*e*/)
1883         {
1884             Scierror(999, _("%s: Memory allocation error.\n"), funname.data());
1885             il_state->DecreaseRef();
1886             il_state->killMe();
1887             il_tcur->DecreaseRef();
1888             il_tcur->killMe();
1889             il_sim->DecreaseRef();
1890             il_sim->killMe();
1891             delete[] il_sim_labptr;
1892             delete[] l_sim_lab;
1893             delete[] il_sim_uidptr;
1894             delete[] l_sim_uid;
1895             delete[] lfunpt;
1896             delete[] oz;
1897             delete[] ozsz;
1898             delete[] oztyp;
1899             delete[] opar;
1900             delete[] oparsz;
1901             delete[] opartyp;
1902             delete[] outtbptr;
1903             return types::Function::Error;
1904         }
1905         // Allocation of 'outtbtyp'
1906         try
1907         {
1908             outtbtyp = new int[nlnk];
1909         }
1910         catch (const std::bad_alloc& /*e*/)
1911         {
1912             Scierror(999, _("%s: Memory allocation error.\n"), funname.data());
1913             il_state->DecreaseRef();
1914             il_state->killMe();
1915             il_tcur->DecreaseRef();
1916             il_tcur->killMe();
1917             il_sim->DecreaseRef();
1918             il_sim->killMe();
1919             delete[] il_sim_labptr;
1920             delete[] l_sim_lab;
1921             delete[] il_sim_uidptr;
1922             delete[] l_sim_uid;
1923             delete[] lfunpt;
1924             delete[] oz;
1925             delete[] ozsz;
1926             delete[] oztyp;
1927             delete[] opar;
1928             delete[] oparsz;
1929             delete[] opartyp;
1930             delete[] outtbptr;
1931             delete[] outtbsz;
1932             return types::Function::Error;
1933         }
1934
1935         // Set vectors of 'outtbptr'
1936         for (int j = 0; j < nlnk; ++j)
1937         {
1938             int subtype = il_state_outtb->get(j)->getType();
1939
1940             switch (subtype) // Store type and address
1941             {
1942                 case types::InternalType::ScilabDouble :
1943                 {
1944                     types::Double* outtbDouble = il_state_outtb->get(j)->getAs<types::Double>();
1945                     if (outtbDouble->isComplex() == false)
1946                     {
1947                         outtbtyp[j] = SCSREAL_N; // Double real matrix
1948                         outtbptr[j] = (SCSREAL_COP *) outtbDouble->get();
1949                     }
1950                     else
1951                     {
1952                         outtbtyp[j] = SCSCOMPLEX_N; // Double complex matrix
1953                         // Allocate a long array in order to make the real and complex parts contiguous (outtbDouble->get() and outtbDouble->getImg())
1954                         try
1955                         {
1956                             outtbptr[j] = new double[2 * outtbDouble->getSize()];
1957                         }
1958                         catch (const std::bad_alloc& /*e*/)
1959                         {
1960                             Scierror(999, _("%s: Memory allocation error.\n"), funname.data());
1961                             il_state->DecreaseRef();
1962                             il_state->killMe();
1963                             il_tcur->DecreaseRef();
1964                             il_tcur->killMe();
1965                             il_sim->DecreaseRef();
1966                             il_sim->killMe();
1967                             delete[] il_sim_labptr;
1968                             delete[] l_sim_lab;
1969                             delete[] il_sim_uidptr;
1970                             delete[] l_sim_uid;
1971                             delete[] lfunpt;
1972                             delete[] oz;
1973                             delete[] ozsz;
1974                             delete[] oztyp;
1975                             delete[] opar;
1976                             delete[] oparsz;
1977                             delete[] opartyp;
1978                             delete[] outtbptr;
1979                             delete[] outtbsz;
1980                             return types::Function::Error;
1981                         }
1982                         memcpy(outtbptr[j], outtbDouble->get(), outtbDouble->getSize() * sizeof(double)); // Real part
1983                         memcpy((double*)(outtbptr[j]) + outtbDouble->getSize(), outtbDouble->getImg(), outtbDouble->getSize() * sizeof(double)); // Complex part
1984                         // FIXME: delete outtbDouble because we copied it instead of using it?
1985                     }
1986                     outtbsz[j] = outtbDouble->getRows();
1987                     outtbsz[j + nlnk] = outtbDouble->getCols();
1988                     break;
1989                 }
1990                 case types::InternalType::ScilabInt8 :
1991                 {
1992                     types::Int8* outtbInt8 = il_state_outtb->get(j)->getAs<types::Int8>();
1993                     outtbtyp[j] = SCSINT8_N; // int8
1994                     outtbsz[j] = outtbInt8->getRows();
1995                     outtbsz[j + nlnk] = outtbInt8->getCols();
1996                     outtbptr[j] = (SCSINT8_COP *) outtbInt8->get();
1997                     break;
1998                 }
1999                 case types::InternalType::ScilabInt16 :
2000                 {
2001                     types::Int16* outtbInt16 = il_state_outtb->get(j)->getAs<types::Int16>();
2002                     outtbtyp[j] = SCSINT16_N; // int16
2003                     outtbsz[j] = outtbInt16->getRows();
2004                     outtbsz[j + nlnk] = outtbInt16->getCols();
2005                     outtbptr[j] = (SCSINT16_COP *) outtbInt16->get();
2006                     break;
2007                 }
2008                 case types::InternalType::ScilabInt32 :
2009                 {
2010                     types::Int32* outtbInt32 = il_state_outtb->get(j)->getAs<types::Int32>();
2011                     outtbtyp[j] = SCSINT32_N; // int32
2012                     outtbsz[j] = outtbInt32->getRows();
2013                     outtbsz[j + nlnk] = outtbInt32->getCols();
2014                     outtbptr[j] = (SCSINT32_COP *) outtbInt32->get();
2015                     break;
2016                 }
2017                 case types::InternalType::ScilabUInt8 :
2018                 {
2019                     types::UInt8* outtbUInt8 = il_state_outtb->get(j)->getAs<types::UInt8>();
2020                     outtbtyp[j] = SCSUINT8_N; // uint8
2021                     outtbsz[j] = outtbUInt8->getRows();
2022                     outtbsz[j + nlnk] = outtbUInt8->getCols();
2023                     outtbptr[j] = (SCSUINT8_COP *) outtbUInt8->get();
2024                     break;
2025                 }
2026                 case types::InternalType::ScilabUInt16 :
2027                 {
2028                     types::UInt16* outtbUInt16 = il_state_outtb->get(j)->getAs<types::UInt16>();
2029                     outtbtyp[j] = SCSUINT16_N; // uint16
2030                     outtbsz[j] = outtbUInt16->getRows();
2031                     outtbsz[j + nlnk] = outtbUInt16->getCols();
2032                     outtbptr[j] = (SCSUINT16_COP *) outtbUInt16->get();
2033                     break;
2034                 }
2035                 case types::InternalType::ScilabUInt32 :
2036                 {
2037                     types::UInt32* outtbUInt32 = il_state_outtb->get(j)->getAs<types::UInt32>();
2038                     outtbtyp[j] = SCSUINT32_N; // uint32
2039                     outtbsz[j] = outtbUInt32->getRows();
2040                     outtbsz[j + nlnk] = outtbUInt32->getCols();
2041                     outtbptr[j] = (SCSUINT32_COP *) outtbUInt32->get();
2042                     break;
2043                 }
2044                 default :
2045                 {
2046                     Scierror(999, _("%s: Wrong type for element #%d of element #%d of argument #%d : A matrix expected.\n"), funname.data(), j + 1, 9, 1);
2047                     il_state->DecreaseRef();
2048                     il_state->killMe();
2049                     il_tcur->DecreaseRef();
2050                     il_tcur->killMe();
2051                     il_sim->DecreaseRef();
2052                     il_sim->killMe();
2053                     delete[] il_sim_labptr;
2054                     delete[] l_sim_lab;
2055                     delete[] il_sim_uidptr;
2056                     delete[] l_sim_uid;
2057                     delete[] lfunpt;
2058                     delete[] outtbptr;
2059                     delete[] outtbtyp;
2060                     delete[] outtbsz;
2061                     delete[] opar;
2062                     delete[] oparsz;
2063                     delete[] opartyp;
2064                     delete[] oz;
2065                     delete[] ozsz;
2066                     delete[] oztyp;
2067                     FREE(outtb_elem);
2068                     return types::Function::Error;
2069                 }
2070             }
2071
2072             // Store 'lnk' and 'pos' in 'outtb_elem'
2073             int k = nelem;
2074             nelem += outtbsz[j] * outtbsz[j + nlnk];
2075             if ((outtb_elem = (outtb_el *) REALLOC(outtb_elem, nelem * sizeof(outtb_el))) == nullptr)
2076             {
2077                 Scierror(999, _("%s : No more free memory.\n"), funname.data());
2078                 il_state->DecreaseRef();
2079                 il_state->killMe();
2080                 il_tcur->DecreaseRef();
2081                 il_tcur->killMe();
2082                 il_sim->DecreaseRef();
2083                 il_sim->killMe();
2084                 delete[] il_sim_labptr;
2085                 delete[] l_sim_lab;
2086                 delete[] il_sim_uidptr;
2087                 delete[] l_sim_uid;
2088                 delete[] lfunpt;
2089                 delete[] outtbptr;
2090                 delete[] outtbtyp;
2091                 delete[] outtbsz;
2092                 delete[] opar;
2093                 delete[] oparsz;
2094                 delete[] opartyp;
2095                 delete[] oz;
2096                 delete[] ozsz;
2097                 delete[] oztyp;
2098                 FREE(outtb_elem);
2099                 return types::Function::Error;
2100             }
2101             for (int i = 0; i < outtbsz[j]*outtbsz[j + nlnk]; ++i)
2102             {
2103                 outtb_elem[k + i].lnk = j;
2104                 outtb_elem[k + i].pos = i;
2105             }
2106         }
2107     }
2108
2109     /************************
2110     * Call scicos simulator
2111     ************************/
2112     /* Syntax :
2113     * int C2F(scicos)(double *x_in, int *xptr_in, double *z__,
2114     *                 void **work,int *zptr,int *modptr_in,
2115     *                 void **oz,int *ozsz,int *oztyp,int *ozptr,
2116     *                 char **iz,int *izptr,double *t0_in,
2117     *                 double *tf_in,double *tevts_in,int *evtspt_in,
2118     *                 int *nevts,int *pointi_in,void **outtbptr_in,
2119     *                 int *outtbsz_in,int *outtbtyp_in,
2120     *                 outtb_el *outtb_elem_in,int *nelem1,int *nlnk1,
2121     *                 int *funptr,int *funtyp_in,int *inpptr_in,
2122     *                 int *outptr_in, int *inplnk_in,int *outlnk_in,
2123     *                 double *rpar,int *rpptr,int *ipar,int *ipptr,
2124     *                 void **opar,int *oparsz,int *opartyp,int *opptr,
2125     *                 int *clkptr_in,int *ordptr_in,int *nordptr1,
2126     *                 int *ordclk_in,int *cord_in,int *ncord1,
2127     *                 int *iord_in,int *niord1,int *oord_in,
2128     *                 int *noord1,int *zord_in,int *nzord1,
2129     *                 int *critev_in,int *nblk1,int *ztyp,
2130     *                 int *zcptr_in,int *subscr,int *nsubs,
2131     *                 double *simpar,int *flag__,int *ierr_out)
2132     */
2133
2134     int ierr = 0;
2135     C2F(scicos)(l_state_x, l_sim_xptr, l_state_z,
2136                 l_state_iz, l_sim_zptr, l_sim_modptr,
2137                 oz, ozsz, oztyp, l_sim_ozptr,
2138                 l_sim_lab, il_sim_labptr, l_sim_uid, il_sim_uidptr, l_tcur,
2139                 l_tf, l_state_tevts, l_state_evtspt,
2140                 &m1e5, l_pointi, outtbptr,
2141                 outtbsz, outtbtyp,
2142                 outtb_elem, &nelem, &nlnk,
2143                 lfunpt, l_sim_funtyp, l_sim_inpptr,
2144                 l_sim_outptr, l_sim_inplnk, l_sim_outlnk,
2145                 l_sim_rpar, l_sim_rpptr, l_sim_ipar, l_sim_ipptr,
2146                 opar, oparsz, opartyp, l_sim_opptr,
2147                 l_sim_clkptr, l_sim_ordptr, &m_ordptr,
2148                 l_sim_ordclk, l_sim_cord, &m_cord,
2149                 l_sim_iord, &m_iord, l_sim_oord,
2150                 &m_oord, l_sim_zord, &m_zord,
2151                 l_sim_critev, &nblk, l_sim_ztyp,
2152                 l_sim_zcptr, l_sim_subscr, &m_subscr,
2153                 simpar, &flag, &ierr);
2154
2155     /**********************
2156     * Free allocated arrays
2157     **********************/
2158     delete[] outtbptr;
2159     delete[] outtbtyp;
2160     delete[] outtbsz;
2161     delete[] opar;
2162     delete[] oparsz;
2163     delete[] opartyp;
2164     delete[] oz;
2165     delete[] ozsz;
2166     delete[] oztyp;
2167     delete[] lfunpt;
2168     FREE(outtb_elem);
2169
2170     /*************************************
2171     * Switch to appropriate message error
2172     *************************************/
2173     if (ierr > 0)
2174     {
2175         char* error;
2176         switch (ierr)
2177         {
2178             case 1  :
2179                 error = _("scheduling problem");
2180                 C2F(curblk).kfun = 0;
2181                 break;
2182
2183             case 2  :
2184                 error = _("input to zero-crossing stuck on zero");
2185                 C2F(curblk).kfun = 0;
2186                 break;
2187
2188             case 3  :
2189                 error = _("event conflict");
2190                 C2F(curblk).kfun = 0;
2191                 break;
2192
2193             case 4  :
2194                 error = _("algebraic loop detected");
2195                 C2F(curblk).kfun = 0;
2196                 break;
2197
2198             case 5  :
2199                 error = _("cannot allocate memory");
2200                 C2F(curblk).kfun = 0;
2201                 break;
2202
2203             case 6  :
2204                 try
2205                 {
2206                     error = new char[MAX_ERROR_LEN];
2207                     allocatedError = TRUE;
2208                     snprintf(error, MAX_ERROR_LEN, _("the block %d has been called with input out of its domain"), C2F(curblk).kfun);
2209                 }
2210                 catch (const std::bad_alloc& /*e*/)
2211                 {
2212                     error = _("cannot allocate memory");
2213                 }
2214                 break;
2215
2216             case 7  :
2217                 error = _("singularity in a block");
2218                 break;
2219
2220             case 8  :
2221                 error = _("block produces an internal error");
2222                 break;
2223
2224             case 10  :
2225                 error = coserr.buf;
2226                 break;
2227
2228             case 20  :
2229                 error = _("initial conditions not converging");
2230                 break;
2231
2232             case 21  :
2233                 try
2234                 {
2235                     error = new char[MAX_ERROR_LEN];
2236                     allocatedError = TRUE;
2237                     snprintf(error, MAX_ERROR_LEN, _("cannot allocate memory in block=%d"), C2F(curblk).kfun);
2238                 }
2239                 catch (const std::bad_alloc& /*e*/)
2240                 {
2241                     error = _("cannot allocate memory");
2242                 }
2243                 break;
2244
2245             case 22  :
2246                 error = _("sliding mode condition, cannot integrate");
2247                 break;
2248
2249             case 23  :
2250                 error = _("Cannot find the initial mode, maybe there is a sliding mode condition");
2251                 break;
2252             case 24  :
2253                 error = _("You have changed a parameter in your model, but the model has been compiled to use an XML file containing initial values and parameters. So you should either recompile your Scicos diagram or [re]launch the initialization interface to regenerate the XML file  with new parameters.");
2254                 break;
2255
2256             case 25  :
2257                 error = _("Undefined data type.");
2258                 break;
2259             case 26  :
2260                 error = _("The number of parameter provided by Scicos blocks is different from what expected by the code generated by the Modelica compiler. You might have relaxed a parameter using FIXED property (i.e., fixed=false) in a Modelica model. This will be corrected in the next version");
2261                 break;
2262             // In this case, you need to turn off the parameter embedded code generation mode by setting 'Modelica_ParEmb=%f' in the Scilab command window, and recompile the Scicos diagram
2263
2264             default  :
2265                 if (ierr >= 1000)
2266                 {
2267                     error = _("unknown or erroneous block");
2268                 }
2269                 else if (ierr >= 201 && ierr <= 416) // Sundials error messages, stored in coserr.buf
2270                 {
2271                     error = coserr.buf;
2272                 }
2273                 else if (ierr >= 100)
2274                 {
2275                     int istate = -(ierr - 100);
2276                     try
2277                     {
2278                         error = new char[MAX_ERROR_LEN];
2279                         allocatedError = TRUE;
2280                         snprintf(error, MAX_ERROR_LEN, _("integration problem istate=%d"), istate);
2281                     }
2282                     catch (const std::bad_alloc& /*e*/)
2283                     {
2284                         error = _("cannot allocate memory");
2285                     }
2286                     C2F(curblk).kfun = 0;
2287                 }
2288                 else
2289                 {
2290                     error = _("scicos unexpected error, please report...");
2291                     C2F(curblk).kfun = 0;
2292                 }
2293                 break;
2294         }
2295
2296         Scierror(888, "%s\n", error);
2297         il_state->DecreaseRef();
2298         il_state->killMe();
2299         il_tcur->DecreaseRef();
2300         il_tcur->killMe();
2301         il_sim->DecreaseRef();
2302         il_sim->killMe();
2303         delete[] il_sim_labptr;
2304         delete[] l_sim_lab;
2305         delete[] il_sim_uidptr;
2306         delete[] l_sim_uid;
2307         if (allocatedError)
2308         {
2309             delete[] error;
2310         }
2311         return types::Function::Error;
2312     }
2313
2314     /*********************
2315     * Return Lhs variables
2316     *********************/
2317     // Convert integer parameters of 'il_state' into double parameters
2318     il_state_evtspt->convertFromInteger();
2319     il_pointi->convertFromInteger();
2320
2321     out.push_back(il_state);
2322     out.push_back(il_tcur);
2323
2324     // End
2325     il_sim->DecreaseRef();
2326     il_sim->killMe();
2327     delete[] il_sim_labptr;
2328     delete[] l_sim_lab;
2329     delete[] il_sim_uidptr;
2330     delete[] l_sim_uid;
2331     return types::Function::OK;
2332 }