2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2010 - DIGITEO - Allan CORNET
4 * Copyright (C) 2010 - DIGITEO - Allan SIMON
5 * Copyright (C) 2010 - DIGITEO - Bruno JOFRET
6 * Copyright (C) 2011 - DIGITEO - Calixte DENIZET
8 * Copyright (C) 2012 - 2016 - Scilab Enterprises
10 * This file is hereby licensed under the terms of the GNU GPL v2.0,
11 * pursuant to article 5.3.4 of the CeCILL v.2.1.
12 * This file was originally licensed under the terms of the CeCILL v2.1,
13 * and continues to be available under such terms.
14 * For more information, see the COPYING file which you should have received
15 * along with this program.
20 #include "EditVar.hxx"
21 #include "GiwsException.hxx"
22 //#include "ScilabToJava.hxx"
26 #include "api_scilab.h"
27 #include "localization.h"
29 #include "sci_malloc.h"
30 #include "freeArrayOfString.h"
32 #include "getScilabJavaVM.h"
33 #include "localization.h"
37 using namespace org_scilab_modules_ui_data;
38 /*--------------------------------------------------------------------------*/
39 template <typename T, typename U>
40 T** wrap(U * x, int r, int c)
43 for (int i = 0; i < r; i++)
46 for (int j = 0; j < c; j++)
48 xx[i][j] = static_cast<T>(x[j * r + i]);
54 /*--------------------------------------------------------------------------*/
55 /* For example if the matrix is [1 2 0 0 0;0 1 2 0 0;0 2 1 3 0 0;0 0 0 0 0;0 0 0 0 1], then
56 r=5, c=5, nbItem=8, nbItemRow=[2 2 3 0 1], colPos=[1 2 2 3 2 3 4 5] and x=[1 2 1 2 2 1 3 1] */
57 double ** wrapSparse(int nbItem, int * nbItemRow, int * colPos, int r, int c, double * x)
59 double ** xx = new double*[r];
61 for (int i = 0; i < r; i++)
63 xx[i] = new double[c];
64 memset(xx[i], 0, c * sizeof(double));
65 for (int j = 0; j < nbItemRow[i]; j++)
67 xx[i][colPos[prev + j] - 1] = x[prev + j];
74 /*--------------------------------------------------------------------------*/
76 int ** wrapSparse(int nbItem, int * nbItemRow, int * colPos, int r, int c)
78 int ** xx = new int*[r];
80 for (int i = 0; i < r; i++)
83 memset(xx[i], 0, c * sizeof(int));
84 for (int j = 0; j < nbItemRow[i]; j++)
86 xx[i][colPos[prev + j] - 1] = 1;
93 /*--------------------------------------------------------------------------*/
95 void clearWrap(T ** x, int r)
97 for (int i = 0; i < r; i++)
104 /*--------------------------------------------------------------------------*/
105 int sci_editvar(char * fname, void* pvApiCtx)
107 CheckRhs(1, 4); /* TODO change this in the future */
120 int ** ppiBool = NULL;
122 char * piInt8 = NULL;
123 char ** ppiInt8 = NULL;
125 unsigned char * piUInt8 = NULL;
126 short ** ppiUInt8 = NULL;
128 short * piInt16 = NULL;
129 short ** ppiInt16 = NULL;
131 unsigned short * piUInt16 = NULL;
132 int ** ppiUInt16 = NULL;
134 int * piInt32 = NULL;
135 int ** ppiInt32 = NULL;
137 unsigned int * piUInt32 = NULL;
138 long long int ** ppiUInt32 = NULL;
141 double * pdblReal = NULL;
142 double * pdblImg = NULL;
143 double ** ppdblRealMatrix = NULL;
144 double ** ppdblImgMatrix = NULL;
146 char ** pstData = NULL;
147 char *** ppstData = NULL;
149 int * piAddressVarOne = NULL;
150 char * pStVarOne = NULL;
154 double * rowsIndex = NULL;
156 double * colsIndex = NULL;
160 int * piNbItemRow = NULL;
161 int * piColPos = NULL;
165 Scierror(999, _("%s: Wrong number of input argument(s): %d, %d or %d expected.\n"), fname, 1, 2, 4);
170 sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddressVarOne);
173 printError(&sciErr, 0);
174 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
178 sciErr = getVarType(pvApiCtx, piAddressVarOne, &iType);
181 printError(&sciErr, 0);
182 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
186 if (iType != sci_strings)
188 Scierror(999, _("%s: Wrong type for input argument #%d: A String expected.\n"), fname, 1);
192 /* get variable name */
193 if (getAllocatedSingleString(pvApiCtx, piAddressVarOne, &pStVarOne))
195 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
199 if (strcmp(pStVarOne, "ans") == 0)
201 Scierror(999, _("%s: ans cannot be edited.\n"), fname);
206 /* get address of the variable*/
207 sciErr = getVarAddressFromName(pvApiCtx, pStVarOne, &piAddr);
210 Scierror(4, _("%s: Undefined variable: %s.\n"), fname, pStVarOne);
215 /* Workaround to check for permanent variable.*/
217 if (strcmp(pStVarOne, "$") == 0 ||
218 strcmp(pStVarOne, "%e") == 0 ||
219 strcmp(pStVarOne, "%eps") == 0 ||
220 strcmp(pStVarOne, "%fftw") == 0 ||
221 strcmp(pStVarOne, "%f") == 0 ||
222 strcmp(pStVarOne, "%F") == 0 ||
223 strcmp(pStVarOne, "%gui") == 0 ||
224 strcmp(pStVarOne, "%i") == 0 ||
225 strcmp(pStVarOne, "%io") == 0 ||
226 strcmp(pStVarOne, "%inf") == 0 ||
227 strcmp(pStVarOne, "%nan") == 0 ||
228 strcmp(pStVarOne, "%pi") == 0 ||
229 strcmp(pStVarOne, "%s") == 0 ||
230 strcmp(pStVarOne, "%tk") == 0 ||
231 strcmp(pStVarOne, "%t") == 0 ||
232 strcmp(pStVarOne, "%T") == 0 ||
233 strcmp(pStVarOne, "%z") == 0 ||
234 strcmp(pStVarOne, "evoid") == 0 ||
235 strcmp(pStVarOne, "home") == 0 ||
236 strcmp(pStVarOne, "PWD") == 0 ||
237 strcmp(pStVarOne, "SCI") == 0 ||
238 strcmp(pStVarOne, "SCIHOME") == 0 ||
239 strcmp(pStVarOne, "TMPDIR") == 0 )
241 Scierror(13, _("Redefining permanent variable.\n"), fname);
248 /* get address of the variable*/
249 sciErr = getVarAddressFromName(pvApiCtx, pStVarOne, &piAddr);
252 Scierror(4, _("%s: Undefined variable: %s.\n"), fname, pStVarOne);
259 sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddr);
263 printError(&sciErr, 0);
264 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 2);
271 sciErr = getVarAddressFromPosition(pvApiCtx, 3, &addr);
274 printError(&sciErr, 0);
275 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 3);
280 if (!isDoubleType(pvApiCtx, addr))
282 Scierror(999, _("%s: Wrong type for input argument #%d: Double expected.\n"), fname, 3);
287 sciErr = getMatrixOfDouble(pvApiCtx, addr, &iRows, &iCols, &rowsIndex);
291 printError(&sciErr, 0);
292 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 3);
296 nbRowsIndex = iRows * iCols;
298 sciErr = getVarAddressFromPosition(pvApiCtx, 4, &addr);
301 printError(&sciErr, 0);
302 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 4);
307 if (!isDoubleType(pvApiCtx, addr))
309 Scierror(999, _("%s: Wrong type for input argument #%d: Double expected.\n"), fname, 4);
314 sciErr = getMatrixOfDouble(pvApiCtx, addr, &iRows, &iCols, &colsIndex);
318 printError(&sciErr, 0);
319 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 4);
323 nbColsIndex = iRows * iCols;
326 //org_modules_commons::ScilabToJava::sendVariable(std::string(pStVarOne), true, pvApiCtx);
328 /* get type of the named variable */
329 sciErr = getVarType(pvApiCtx, piAddr, &iType);
333 printError(&sciErr, 0);
334 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
343 iComplex = isVarComplex(pvApiCtx, piAddr);
345 /* check complexity */
348 /* get size and data from Scilab memory */
349 sciErr = getComplexMatrixOfDouble(pvApiCtx, piAddr, &iRows, &iCols, &pdblReal, &pdblImg);
353 printError(&sciErr, 0);
354 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
358 ppdblRealMatrix = wrap<double, double>(pdblReal, iRows, iCols);
359 ppdblImgMatrix = wrap<double, double>(pdblImg, iRows, iCols);
363 if (nbRowsIndex == 0 || nbColsIndex == 0)
365 /* Launch Java Variable Editor through JNI */
366 EditVar::openVariableEditorComplex(getScilabJavaVM(),
377 /* Launch Java Variable Editor through JNI */
378 EditVar::refreshVariableEditorComplex(getScilabJavaVM(),
392 catch (const GiwsException::JniException & e)
394 Scierror(999, _("%s: Java exception arisen:\n%s\n"), fname, e.what());
397 clearWrap<double>(ppdblRealMatrix, iRows);
398 clearWrap<double>(ppdblImgMatrix, iRows);
402 /* get size and data from Scilab memory */
403 sciErr = getMatrixOfDouble(pvApiCtx, piAddr, &iRows, &iCols, &pdblReal);
407 printError(&sciErr, 0);
408 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
412 * we need this to make the links between the API (which return a double*)
413 * and the JNI which needs a double**
415 ppdblRealMatrix = wrap<double, double>(pdblReal, iRows, iCols);
417 /* Launch Java Variable Editor through JNI */
420 if (nbRowsIndex == 0 || nbColsIndex == 0)
422 EditVar::openVariableEditorDouble(getScilabJavaVM(), ppdblRealMatrix, iRows, iCols, pStVarOne);
426 EditVar::refreshVariableEditorDouble(getScilabJavaVM(), ppdblRealMatrix, iRows, iCols, rowsIndex, nbRowsIndex, colsIndex, nbColsIndex, pStVarOne);
429 catch (const GiwsException::JniException & e)
431 Scierror(999, _("%s: Java exception arisen:\n%s\n"), fname, e.what());
434 clearWrap<double>(ppdblRealMatrix, iRows);
441 if (getAllocatedMatrixOfString(pvApiCtx, piAddr, &iRows, &iCols, &pstData))
444 printError(&sciErr, 0);
445 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
450 * we need this to make the links between the API (which return a char**)
451 * and the JNI which needs a 2-dims char*
453 ppstData = wrap<char *, char *>(pstData, iRows, iCols);
455 /* Launch Java Variable Editor through JNI */
458 if (nbRowsIndex == 0 || nbColsIndex == 0)
460 EditVar::openVariableEditorString(getScilabJavaVM(), ppstData, iRows, iCols, pStVarOne);
464 EditVar::refreshVariableEditorString(getScilabJavaVM(), ppstData, iRows, iCols, rowsIndex, nbRowsIndex, colsIndex, nbColsIndex, pStVarOne);
467 catch (const GiwsException::JniException & e)
469 Scierror(999, _("%s: Java exception arisen:\n%s\n"), fname, e.what());
472 freeAllocatedMatrixOfString(iRows, iCols, pstData);
474 clearWrap<char *>(ppstData, iRows);
479 //get size and data from Scilab memory
480 sciErr = getMatrixOfBoolean(pvApiCtx, piAddr, &iRows, &iCols, &piBool);
484 printError(&sciErr, 0);
485 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
490 * we need this to make the links between the API (which return a int*)
491 * and the JNI which needs a int**
493 ppiBool = wrap<int, int>(piBool, iRows, iCols);
495 /* Launch Java Variable Editor through JNI */
498 if (nbRowsIndex == 0 || nbColsIndex == 0)
500 EditVar::openVariableEditorBoolean(getScilabJavaVM(), ppiBool, iRows, iCols, pStVarOne);
504 EditVar::refreshVariableEditorBoolean(getScilabJavaVM(), ppiBool, iRows, iCols, rowsIndex, nbRowsIndex, colsIndex, nbColsIndex, pStVarOne);
507 catch (const GiwsException::JniException & e)
509 Scierror(999, _("%s: Java exception arisen:\n%s\n"), fname, e.what());
512 clearWrap<int>(ppiBool, iRows);
517 //get size and data from Scilab memory
519 sciErr = getMatrixOfIntegerPrecision(pvApiCtx, piAddr, &prec);
523 printError(&sciErr, 0);
524 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
531 sciErr = getMatrixOfInteger8(pvApiCtx, piAddr, &iRows, &iCols, &piInt8);
534 printError(&sciErr, 0);
535 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
539 ppiInt8 = wrap<char, char>(piInt8, iRows, iCols);
541 /* Launch Java Variable Editor through JNI */
544 if (nbRowsIndex == 0 || nbColsIndex == 0)
546 EditVar::openVariableEditorInteger8(getScilabJavaVM(), reinterpret_cast<byte**>(ppiInt8), iRows, iCols, pStVarOne);
550 EditVar::refreshVariableEditorInteger8(getScilabJavaVM(), reinterpret_cast<byte**>(ppiInt8), iRows, iCols, rowsIndex, nbRowsIndex, colsIndex, nbColsIndex, pStVarOne);
553 catch (const GiwsException::JniException & e)
555 Scierror(999, _("%s: Java exception arisen:\n%s\n"), fname, e.what());
558 clearWrap<char>(ppiInt8, iRows);
563 sciErr = getMatrixOfUnsignedInteger8(pvApiCtx, piAddr, &iRows, &iCols, &piUInt8);
566 printError(&sciErr, 0);
567 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
571 ppiUInt8 = wrap<short, unsigned char>(piUInt8, iRows, iCols);
573 /* Launch Java Variable Editor through JNI */
576 if (nbRowsIndex == 0 || nbColsIndex == 0)
578 EditVar::openVariableEditorUInteger8(getScilabJavaVM(), ppiUInt8, iRows, iCols, pStVarOne);
582 EditVar::refreshVariableEditorUInteger8(getScilabJavaVM(), ppiUInt8, iRows, iCols, rowsIndex, nbRowsIndex, colsIndex, nbColsIndex, pStVarOne);
585 catch (const GiwsException::JniException & e)
587 Scierror(999, _("%s: Java exception arisen:\n%s\n"), fname, e.what());
590 clearWrap<short>(ppiUInt8, iRows);
595 sciErr = getMatrixOfInteger16(pvApiCtx, piAddr, &iRows, &iCols, &piInt16);
598 printError(&sciErr, 0);
599 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
603 ppiInt16 = wrap<short, short>(piInt16, iRows, iCols);
605 /* Launch Java Variable Editor through JNI */
608 if (nbRowsIndex == 0 || nbColsIndex == 0)
610 EditVar::openVariableEditorInteger16(getScilabJavaVM(), ppiInt16, iRows, iCols, pStVarOne);
614 EditVar::refreshVariableEditorInteger16(getScilabJavaVM(), ppiInt16, iRows, iCols, rowsIndex, nbRowsIndex, colsIndex, nbColsIndex, pStVarOne);
617 catch (const GiwsException::JniException & e)
619 Scierror(999, _("%s: Java exception arisen:\n%s\n"), fname, e.what());
622 clearWrap<short>(ppiInt16, iRows);
627 sciErr = getMatrixOfUnsignedInteger16(pvApiCtx, piAddr, &iRows, &iCols, &piUInt16);
630 printError(&sciErr, 0);
631 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
635 ppiUInt16 = wrap<int, unsigned short>(piUInt16, iRows, iCols);
637 /* Launch Java Variable Editor through JNI */
640 if (nbRowsIndex == 0 || nbColsIndex == 0)
642 EditVar::openVariableEditorUInteger16(getScilabJavaVM(), ppiUInt16, iRows, iCols, pStVarOne);
646 EditVar::refreshVariableEditorUInteger16(getScilabJavaVM(), ppiUInt16, iRows, iCols, rowsIndex, nbRowsIndex, colsIndex, nbColsIndex, pStVarOne);
649 catch (const GiwsException::JniException & e)
651 Scierror(999, _("%s: Java exception arisen:\n%s\n"), fname, e.what());
654 clearWrap<int>(ppiUInt16, iRows);
659 sciErr = getMatrixOfInteger32(pvApiCtx, piAddr, &iRows, &iCols, &piInt32);
662 printError(&sciErr, 0);
663 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
667 ppiInt32 = wrap<int>(piInt32, iRows, iCols);
669 /* Launch Java Variable Editor through JNI */
672 if (nbRowsIndex == 0 || nbColsIndex == 0)
674 EditVar::openVariableEditorInteger32(getScilabJavaVM(), ppiInt32, iRows, iCols, pStVarOne);
678 EditVar::refreshVariableEditorInteger32(getScilabJavaVM(), ppiInt32, iRows, iCols, rowsIndex, nbRowsIndex, colsIndex, nbColsIndex, pStVarOne);
681 catch (const GiwsException::JniException & e)
683 Scierror(999, _("%s: Java exception arisen:\n%s\n"), fname, e.what());
686 clearWrap<int>(ppiInt32, iRows);
691 sciErr = getMatrixOfUnsignedInteger32(pvApiCtx, piAddr, &iRows, &iCols, &piUInt32);
694 printError(&sciErr, 0);
695 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
699 ppiUInt32 = wrap<long long int, unsigned int>(piUInt32, iRows, iCols);
701 /* Launch Java Variable Editor through JNI */
704 if (nbRowsIndex == 0 || nbColsIndex == 0)
706 EditVar::openVariableEditorUInteger32(getScilabJavaVM(), ppiUInt32, iRows, iCols, pStVarOne);
710 EditVar::refreshVariableEditorUInteger32(getScilabJavaVM(), ppiUInt32, iRows, iCols, rowsIndex, nbRowsIndex, colsIndex, nbColsIndex, pStVarOne);
713 catch (const GiwsException::JniException & e)
715 Scierror(999, _("%s: Java exception arisen:\n%s\n"), fname, e.what());
718 clearWrap<long long int>(ppiUInt32, iRows);
724 Scierror(42, _("%s: Type not handle yet"), fname);
730 case sci_boolean_sparse :
731 sciErr = getBooleanSparseMatrix(pvApiCtx, piAddr, &iRows, &iCols, &piNbItem, &piNbItemRow, &piColPos);
735 printError(&sciErr, 0);
736 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
740 ppiBool = wrapSparse(piNbItem, piNbItemRow, piColPos, iRows, iCols);
742 /* Launch Java Variable Editor through JNI */
745 if (nbRowsIndex == 0 || nbColsIndex == 0)
747 EditVar::openVariableEditorBooleanSparse(getScilabJavaVM(), ppiBool, iRows, iCols, pStVarOne);
751 EditVar::refreshVariableEditorBooleanSparse(getScilabJavaVM(), ppiBool, iRows, iCols, rowsIndex, nbRowsIndex, colsIndex, nbColsIndex, pStVarOne);
754 catch (const GiwsException::JniException & e)
756 Scierror(999, _("%s: Java exception arisen:\n%s\n"), fname, e.what());
759 clearWrap<int>(ppiBool, iRows);
762 if (isVarComplex(pvApiCtx, piAddr))
764 sciErr = getComplexSparseMatrix(pvApiCtx, piAddr, &iRows, &iCols, &piNbItem, &piNbItemRow, &piColPos, &pdblReal, &pdblImg);
768 printError(&sciErr, 0);
769 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
773 ppdblRealMatrix = wrapSparse(piNbItem, piNbItemRow, piColPos, iRows, iCols, pdblReal);
774 ppdblImgMatrix = wrapSparse(piNbItem, piNbItemRow, piColPos, iRows, iCols, pdblImg);
776 /* Launch Java Variable Editor through JNI */
779 if (nbRowsIndex == 0 || nbColsIndex == 0)
781 EditVar::openVariableEditorComplexSparse(getScilabJavaVM(), ppdblRealMatrix, iRows, iCols, ppdblImgMatrix, iRows, iCols, pStVarOne);
785 EditVar::refreshVariableEditorComplexSparse(getScilabJavaVM(), ppdblRealMatrix, iRows, iCols, ppdblImgMatrix, iRows, iCols, rowsIndex, nbRowsIndex, colsIndex, nbColsIndex, pStVarOne);
788 catch (const GiwsException::JniException & e)
790 Scierror(999, _("%s: Java exception arisen:\n%s\n"), fname, e.what());
793 clearWrap<double>(ppdblRealMatrix, iRows);
794 clearWrap<double>(ppdblImgMatrix, iRows);
798 sciErr = getSparseMatrix(pvApiCtx, piAddr, &iRows, &iCols, &piNbItem, &piNbItemRow, &piColPos, &pdblReal);
802 printError(&sciErr, 0);
803 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
807 ppdblRealMatrix = wrapSparse(piNbItem, piNbItemRow, piColPos, iRows, iCols, pdblReal);
809 /* Launch Java Variable Editor through JNI */
812 if (nbRowsIndex == 0 || nbColsIndex == 0)
814 EditVar::openVariableEditorSparse(getScilabJavaVM(), ppdblRealMatrix, iRows, iCols, pStVarOne);
818 EditVar::refreshVariableEditorSparse(getScilabJavaVM(), ppdblRealMatrix, iRows, iCols, rowsIndex, nbRowsIndex, colsIndex, nbColsIndex, pStVarOne);
821 catch (const GiwsException::JniException & e)
823 Scierror(999, _("%s: Java exception arisen:\n%s\n"), fname, e.what());
826 clearWrap<double>(ppdblRealMatrix, iRows);
831 Scierror(42, _("%s: Type not handle yet"), fname);
841 /*--------------------------------------------------------------------------*/