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 * This file must be used under the terms of the CeCILL.
9 * This source file is licensed as described in the file COPYING, which
10 * you should have received as part of this distribution. The terms
11 * are also available at
12 * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
17 #include "EditVar.hxx"
18 #include "GiwsException.hxx"
19 //#include "ScilabToJava.hxx"
23 #include "gw_ui_data.h"
25 #include "api_scilab.h"
26 #include "localization.h"
29 #include "freeArrayOfString.h"
31 #include "getScilabJavaVM.h"
32 #include "localization.h"
36 using namespace org_scilab_modules_ui_data;
37 /*--------------------------------------------------------------------------*/
38 template <typename T, typename U>
39 T** wrap(U * x, int r, int c)
42 for (int i = 0; i < r; i++)
45 for (int j = 0; j < c; j++)
47 xx[i][j] = static_cast<T>(x[j * r + i]);
53 /*--------------------------------------------------------------------------*/
54 /* 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
55 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] */
56 double ** wrapSparse(int nbItem, int * nbItemRow, int * colPos, int r, int c, double * x)
58 double ** xx = new double*[r];
60 for (int i = 0; i < r; i++)
62 xx[i] = new double[c];
63 memset(xx[i], 0, c * sizeof(double));
64 for (int j = 0; j < nbItemRow[i]; j++) {
65 xx[i][colPos[prev + j] - 1] = x[prev + j];
72 /*--------------------------------------------------------------------------*/
74 int ** wrapSparse(int nbItem, int * nbItemRow, int * colPos, int r, int c)
76 int ** xx = new int*[r];
78 for (int i = 0; i < r; i++)
81 memset(xx[i], 0, c * sizeof(int));
82 for (int j = 0; j < nbItemRow[i]; j++) {
83 xx[i][colPos[prev + j] - 1] = 1;
90 /*--------------------------------------------------------------------------*/
92 void clearWrap(T ** x, int r)
94 for (int i = 0; i < r; i++)
101 /*--------------------------------------------------------------------------*/
102 int sci_editvar(char * fname, unsigned long fname_len)
104 CheckRhs(1, 4); /* TODO change this in the future */
117 int ** ppiBool = NULL;
119 char * piInt8 = NULL;
120 char ** ppiInt8 = NULL;
122 unsigned char * piUInt8 = NULL;
123 short ** ppiUInt8 = NULL;
125 short * piInt16 = NULL;
126 short ** ppiInt16 = NULL;
128 unsigned short * piUInt16 = NULL;
129 int ** ppiUInt16 = NULL;
131 int * piInt32 = NULL;
132 int ** ppiInt32 = NULL;
134 unsigned int * piUInt32 = NULL;
135 long long int ** ppiUInt32 = NULL;
138 double * pdblReal = NULL;
139 double * pdblImg = NULL;
140 double ** ppdblRealMatrix = NULL;
141 double ** ppdblImgMatrix = NULL;
143 char ** pstData = NULL;
145 char *** ppstData = NULL;
147 int * piAddressVarOne = NULL;
148 char * pStVarOne = NULL;
152 double * rowsIndex = NULL;
154 double * colsIndex = NULL;
158 int * piNbItemRow = NULL;
159 int * piColPos = NULL;
163 Scierror(999,_("%s: Wrong number of input argument(s): 1, 2 or 4 expected.\n"), fname);
168 sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddressVarOne);
171 printError(&sciErr, 0);
172 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
176 sciErr = getVarType(pvApiCtx, piAddressVarOne, &iType);
179 printError(&sciErr, 0);
180 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
184 if (iType != sci_strings)
186 Scierror(999,_("%s: Wrong type for input argument #%d: A String expected.\n"), fname, 1);
191 sciErr = getMatrixOfString(pvApiCtx, piAddressVarOne, &m1, &n1, NULL, NULL);
194 printError(&sciErr, 0);
195 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
199 /* TODO maybe allow vectors in case someone wants to edit several variables in the same time? */
200 if (m1 != 1 || n1 != 1)
202 Scierror(999,_("%s: Wrong size for input argument #%d: A string expected.\n"), fname, 1);
207 sciErr = getMatrixOfString(pvApiCtx, piAddressVarOne, &m1, &n1, &lenStVarOne, NULL);
210 printError(&sciErr, 0);
211 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
215 pStVarOne = (char*)MALLOC(sizeof(char*) * (lenStVarOne + 1));
217 /* get variable name to edit */
218 sciErr = getMatrixOfString(pvApiCtx, piAddressVarOne, &m1, &n1, &lenStVarOne, &pStVarOne);
222 printError(&sciErr, 0);
223 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
227 if (strcmp(pStVarOne, "ans") == 0)
229 Scierror(999, _("%s: ans cannot be edited.\n"), fname);
234 /* get address of the variable*/
235 sciErr = getVarAddressFromName(pvApiCtx, pStVarOne, &piAddr);
238 Scierror(4, _("%s: Undefined variable %s.\n"), fname, pStVarOne);
245 /* get address of the variable*/
246 sciErr = getVarAddressFromName(pvApiCtx, pStVarOne, &piAddr);
249 Scierror(4, _("%s: Undefined variable %s.\n"), fname, pStVarOne);
256 sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddr);
260 printError(&sciErr, 0);
261 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 2);
268 sciErr = getVarAddressFromPosition(pvApiCtx, 3, &addr);
271 printError(&sciErr, 0);
272 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 3);
277 if (!isDoubleType(pvApiCtx, addr))
279 Scierror(999,_("%s: Wrong type for input argument #%d: Double expected.\n"), fname, 3);
284 sciErr = getMatrixOfDouble(pvApiCtx, addr, &iRows, &iCols, &rowsIndex);
288 printError(&sciErr, 0);
289 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 3);
293 nbRowsIndex = iRows * iCols;
295 sciErr = getVarAddressFromPosition(pvApiCtx, 4, &addr);
298 printError(&sciErr, 0);
299 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 4);
304 if (!isDoubleType(pvApiCtx, addr))
306 Scierror(999,_("%s: Wrong type for input argument #%d: Double expected.\n"), fname, 4);
311 sciErr = getMatrixOfDouble(pvApiCtx, addr, &iRows, &iCols, &colsIndex);
315 printError(&sciErr, 0);
316 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 4);
320 nbColsIndex = iRows * iCols;
323 //org_modules_commons::ScilabToJava::sendVariable(std::string(pStVarOne), true, pvApiCtx);
325 /* get type of the named variable */
326 sciErr = getVarType(pvApiCtx, piAddr, &iType);
330 printError(&sciErr, 0);
331 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
340 iComplex = isVarComplex(pvApiCtx, piAddr);
342 /* check complexity */
345 /* get size and data from Scilab memory */
346 sciErr = getComplexMatrixOfDouble(pvApiCtx, piAddr, &iRows, &iCols, &pdblReal, &pdblImg);
350 printError(&sciErr, 0);
351 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
355 ppdblRealMatrix = wrap<double, double>(pdblReal, iRows, iCols);
356 ppdblImgMatrix = wrap<double, double>(pdblImg, iRows, iCols);
360 if (nbRowsIndex == 0 || nbColsIndex == 0)
362 /* Launch Java Variable Editor through JNI */
363 EditVar::openVariableEditorComplex(getScilabJavaVM(),
374 /* Launch Java Variable Editor through JNI */
375 EditVar::refreshVariableEditorComplex(getScilabJavaVM(),
389 catch (const GiwsException::JniException & e)
391 Scierror(999, _("%s: Java exception arisen:\n%s\n"), fname, e.what());
394 clearWrap<double>(ppdblRealMatrix, iRows);
395 clearWrap<double>(ppdblImgMatrix, iRows);
399 /* get size and data from Scilab memory */
400 sciErr = getMatrixOfDouble(pvApiCtx, piAddr, &iRows, &iCols, &pdblReal);
404 printError(&sciErr, 0);
405 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
409 * we need this to make the links between the API (which return a double*)
410 * and the JNI which needs a double**
412 ppdblRealMatrix = wrap<double, double>(pdblReal, iRows, iCols);
414 /* Launch Java Variable Editor through JNI */
417 if (nbRowsIndex == 0 || nbColsIndex == 0)
419 EditVar::openVariableEditorDouble(getScilabJavaVM(), ppdblRealMatrix, iRows, iCols, pStVarOne);
423 EditVar::refreshVariableEditorDouble(getScilabJavaVM(), ppdblRealMatrix, iRows, iCols, rowsIndex, nbRowsIndex, colsIndex, nbColsIndex, pStVarOne);
426 catch (const GiwsException::JniException & e)
428 Scierror(999, _("%s: Java exception arisen:\n%s\n"), fname, e.what());
431 clearWrap<double>(ppdblRealMatrix, iRows);
437 //fisrt call to retrieve dimensions
438 sciErr = getMatrixOfString(pvApiCtx, piAddr, &iRows, &iCols, NULL, NULL);
442 printError(&sciErr, 0);
443 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
447 piLen = (int*)malloc(sizeof(int) * iRows * iCols);
449 //second call to retrieve length of each string
450 sciErr = getMatrixOfString(pvApiCtx, piAddr, &iRows, &iCols, piLen, NULL);
455 printError(&sciErr, 0);
456 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
460 pstData = (char**)calloc(iRows * iCols, sizeof(char*));
461 for(int i = 0 ; i < iRows * iCols ; i++)
463 pstData[i] = (char*)malloc(sizeof(char) * (piLen[i] + 1));//+ 1 for null termination
465 //third call to retrieve data
466 sciErr = getMatrixOfString(pvApiCtx, piAddr, &iRows, &iCols, piLen, pstData);
471 freeArrayOfString(pstData, iRows * iCols);
472 printError(&sciErr, 0);
473 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
478 * we need this to make the links between the API (which return a char**)
479 * and the JNI which needs a char***
481 ppstData = wrap<char *, char *>(pstData, iRows, iCols);
483 /* Launch Java Variable Editor through JNI */
486 if (nbRowsIndex == 0 || nbColsIndex == 0)
488 EditVar::openVariableEditorString(getScilabJavaVM(), ppstData, iRows, iCols, pStVarOne);
492 EditVar::refreshVariableEditorString(getScilabJavaVM(), ppstData, iRows, iCols, rowsIndex, nbRowsIndex, colsIndex, nbColsIndex, pStVarOne);
495 catch (const GiwsException::JniException & e)
497 Scierror(999, _("%s: Java exception arisen:\n%s\n"), fname, e.what());
501 freeArrayOfString(pstData, iRows * iCols);
503 clearWrap<char *>(ppstData, iRows);
508 //get size and data from Scilab memory
509 sciErr = getMatrixOfBoolean(pvApiCtx, piAddr, &iRows, &iCols, &piBool);
513 printError(&sciErr, 0);
514 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
519 * we need this to make the links between the API (which return a int*)
520 * and the JNI which needs a int**
522 ppiBool = wrap<int, int>(piBool, iRows, iCols);
524 /* Launch Java Variable Editor through JNI */
527 if (nbRowsIndex == 0 || nbColsIndex == 0)
529 EditVar::openVariableEditorBoolean(getScilabJavaVM(), ppiBool, iRows, iCols, pStVarOne);
533 EditVar::refreshVariableEditorBoolean(getScilabJavaVM(), ppiBool, iRows, iCols, rowsIndex, nbRowsIndex, colsIndex, nbColsIndex, pStVarOne);
536 catch (const GiwsException::JniException & e)
538 Scierror(999, _("%s: Java exception arisen:\n%s\n"), fname, e.what());
541 clearWrap<int>(ppiBool, iRows);
546 //get size and data from Scilab memory
548 sciErr = getMatrixOfIntegerPrecision(pvApiCtx, piAddr, &prec);
552 printError(&sciErr, 0);
553 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
560 sciErr = getMatrixOfInteger8(pvApiCtx, piAddr, &iRows, &iCols, &piInt8);
563 printError(&sciErr, 0);
564 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
568 ppiInt8 = wrap<char, char>(piInt8, iRows, iCols);
570 /* Launch Java Variable Editor through JNI */
573 if (nbRowsIndex == 0 || nbColsIndex == 0)
575 EditVar::openVariableEditorInteger8(getScilabJavaVM(), reinterpret_cast<byte**>(ppiInt8), iRows, iCols, pStVarOne);
579 EditVar::refreshVariableEditorInteger8(getScilabJavaVM(), reinterpret_cast<byte**>(ppiInt8), iRows, iCols, rowsIndex, nbRowsIndex, colsIndex, nbColsIndex, pStVarOne);
582 catch (const GiwsException::JniException & e)
584 Scierror(999, _("%s: Java exception arisen:\n%s\n"), fname, e.what());
587 clearWrap<char>(ppiInt8, iRows);
592 sciErr = getMatrixOfUnsignedInteger8(pvApiCtx, piAddr, &iRows, &iCols, &piUInt8);
595 printError(&sciErr, 0);
596 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
600 ppiUInt8 = wrap<short, unsigned char>(piUInt8, iRows, iCols);
602 /* Launch Java Variable Editor through JNI */
605 if (nbRowsIndex == 0 || nbColsIndex == 0)
607 EditVar::openVariableEditorUInteger8(getScilabJavaVM(), ppiUInt8, iRows, iCols, pStVarOne);
611 EditVar::refreshVariableEditorUInteger8(getScilabJavaVM(), ppiUInt8, iRows, iCols, rowsIndex, nbRowsIndex, colsIndex, nbColsIndex, pStVarOne);
614 catch (const GiwsException::JniException & e)
616 Scierror(999, _("%s: Java exception arisen:\n%s\n"), fname, e.what());
619 clearWrap<short>(ppiUInt8, iRows);
624 sciErr = getMatrixOfInteger16(pvApiCtx, piAddr, &iRows, &iCols, &piInt16);
627 printError(&sciErr, 0);
628 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
632 ppiInt16 = wrap<short, short>(piInt16, iRows, iCols);
634 /* Launch Java Variable Editor through JNI */
637 if (nbRowsIndex == 0 || nbColsIndex == 0)
639 EditVar::openVariableEditorInteger16(getScilabJavaVM(), ppiInt16, iRows, iCols, pStVarOne);
643 EditVar::refreshVariableEditorInteger16(getScilabJavaVM(), ppiInt16, iRows, iCols, rowsIndex, nbRowsIndex, colsIndex, nbColsIndex, pStVarOne);
646 catch (const GiwsException::JniException & e)
648 Scierror(999, _("%s: Java exception arisen:\n%s\n"), fname, e.what());
651 clearWrap<short>(ppiInt16, iRows);
656 sciErr = getMatrixOfUnsignedInteger16(pvApiCtx, piAddr, &iRows, &iCols, &piUInt16);
659 printError(&sciErr, 0);
660 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
664 ppiUInt16 = wrap<int, unsigned short>(piUInt16, iRows, iCols);
666 /* Launch Java Variable Editor through JNI */
669 if (nbRowsIndex == 0 || nbColsIndex == 0)
671 EditVar::openVariableEditorUInteger16(getScilabJavaVM(), ppiUInt16, iRows, iCols, pStVarOne);
675 EditVar::refreshVariableEditorUInteger16(getScilabJavaVM(), ppiUInt16, iRows, iCols, rowsIndex, nbRowsIndex, colsIndex, nbColsIndex, pStVarOne);
678 catch (const GiwsException::JniException & e)
680 Scierror(999, _("%s: Java exception arisen:\n%s\n"), fname, e.what());
683 clearWrap<int>(ppiUInt16, iRows);
688 sciErr = getMatrixOfInteger32(pvApiCtx, piAddr, &iRows, &iCols, &piInt32);
691 printError(&sciErr, 0);
692 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
696 ppiInt32 = wrap<int>(piInt32, iRows, iCols);
698 /* Launch Java Variable Editor through JNI */
701 if (nbRowsIndex == 0 || nbColsIndex == 0)
703 EditVar::openVariableEditorInteger32(getScilabJavaVM(), ppiInt32, iRows, iCols, pStVarOne);
707 EditVar::refreshVariableEditorInteger32(getScilabJavaVM(), ppiInt32, iRows, iCols, rowsIndex, nbRowsIndex, colsIndex, nbColsIndex, pStVarOne);
710 catch (const GiwsException::JniException & e)
712 Scierror(999, _("%s: Java exception arisen:\n%s\n"), fname, e.what());
715 clearWrap<int>(ppiInt32, iRows);
720 sciErr = getMatrixOfUnsignedInteger32(pvApiCtx, piAddr, &iRows, &iCols, &piUInt32);
723 printError(&sciErr, 0);
724 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
728 ppiUInt32 = wrap<long long int, unsigned int>(piUInt32, iRows, iCols);
730 /* Launch Java Variable Editor through JNI */
733 if (nbRowsIndex == 0 || nbColsIndex == 0)
735 EditVar::openVariableEditorUInteger32(getScilabJavaVM(), ppiUInt32, iRows, iCols, pStVarOne);
739 EditVar::refreshVariableEditorUInteger32(getScilabJavaVM(), ppiUInt32, iRows, iCols, rowsIndex, nbRowsIndex, colsIndex, nbColsIndex, pStVarOne);
742 catch (const GiwsException::JniException & e)
744 Scierror(999, _("%s: Java exception arisen:\n%s\n"), fname, e.what());
747 clearWrap<long long int>(ppiUInt32, iRows);
753 Scierror(42, _("%s: Type not handle yet"), fname);
759 case sci_boolean_sparse :
760 sciErr = getBooleanSparseMatrix(pvApiCtx, piAddr, &iRows, &iCols, &piNbItem, &piNbItemRow, &piColPos);
764 printError(&sciErr, 0);
765 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
769 ppiBool = wrapSparse(piNbItem, piNbItemRow, piColPos, iRows, iCols);
771 /* Launch Java Variable Editor through JNI */
774 if (nbRowsIndex == 0 || nbColsIndex == 0)
776 EditVar::openVariableEditorBooleanSparse(getScilabJavaVM(), ppiBool, iRows, iCols, pStVarOne);
780 EditVar::refreshVariableEditorBooleanSparse(getScilabJavaVM(), ppiBool, iRows, iCols, rowsIndex, nbRowsIndex, colsIndex, nbColsIndex, pStVarOne);
783 catch (const GiwsException::JniException & e)
785 Scierror(999, _("%s: Java exception arisen:\n%s\n"), fname, e.what());
788 clearWrap<int>(ppiBool, iRows);
791 if (isVarComplex(pvApiCtx, piAddr))
793 sciErr = getComplexSparseMatrix(pvApiCtx, piAddr, &iRows, &iCols, &piNbItem, &piNbItemRow, &piColPos, &pdblReal, &pdblImg);
797 printError(&sciErr, 0);
798 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
802 ppdblRealMatrix = wrapSparse(piNbItem, piNbItemRow, piColPos, iRows, iCols, pdblReal);
803 ppdblImgMatrix = wrapSparse(piNbItem, piNbItemRow, piColPos, iRows, iCols, pdblImg);
805 /* Launch Java Variable Editor through JNI */
808 if (nbRowsIndex == 0 || nbColsIndex == 0)
810 EditVar::openVariableEditorComplexSparse(getScilabJavaVM(), ppdblRealMatrix, iRows, iCols, ppdblImgMatrix, iRows, iCols, pStVarOne);
814 EditVar::refreshVariableEditorComplexSparse(getScilabJavaVM(), ppdblRealMatrix, iRows, iCols, ppdblImgMatrix, iRows, iCols, rowsIndex, nbRowsIndex, colsIndex, nbColsIndex, pStVarOne);
817 catch (const GiwsException::JniException & e)
819 Scierror(999, _("%s: Java exception arisen:\n%s\n"), fname, e.what());
822 clearWrap<double>(ppdblRealMatrix, iRows);
823 clearWrap<double>(ppdblImgMatrix, iRows);
827 sciErr = getSparseMatrix(pvApiCtx, piAddr, &iRows, &iCols, &piNbItem, &piNbItemRow, &piColPos, &pdblReal);
831 printError(&sciErr, 0);
832 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
836 ppdblRealMatrix = wrapSparse(piNbItem, piNbItemRow, piColPos, iRows, iCols, pdblReal);
838 /* Launch Java Variable Editor through JNI */
841 if (nbRowsIndex == 0 || nbColsIndex == 0)
843 EditVar::openVariableEditorSparse(getScilabJavaVM(), ppdblRealMatrix, iRows, iCols, pStVarOne);
847 EditVar::refreshVariableEditorSparse(getScilabJavaVM(), ppdblRealMatrix, iRows, iCols, rowsIndex, nbRowsIndex, colsIndex, nbColsIndex, pStVarOne);
850 catch (const GiwsException::JniException & e)
852 Scierror(999, _("%s: Java exception arisen:\n%s\n"), fname, e.what());
855 clearWrap<double>(ppdblRealMatrix, iRows);
860 Scierror(42, _("%s: Type not handle yet"), fname);
870 /*--------------------------------------------------------------------------*/