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);
175 sciErr = getVarType(pvApiCtx, piAddressVarOne, &iType);
178 printError(&sciErr, 0);
182 if (iType != sci_strings)
184 Scierror(999,_("%s: Wrong type for input argument #%d: A String expected.\n"), fname, 1);
189 sciErr = getMatrixOfString(pvApiCtx, piAddressVarOne, &m1, &n1, NULL, NULL);
192 printError(&sciErr, 0);
196 /* TODO maybe allow vectors in case someone wants to edit several variables in the same time? */
197 if (m1 != 1 || n1 != 1)
199 Scierror(999,_("%s: Wrong size for input argument #%d: A string expected.\n"), fname, 1);
204 sciErr = getMatrixOfString(pvApiCtx, piAddressVarOne, &m1, &n1, &lenStVarOne, NULL);
207 printError(&sciErr, 0);
211 pStVarOne = (char*)MALLOC(sizeof(char*) * (lenStVarOne + 1));
213 /* get variable name to edit */
214 sciErr = getMatrixOfString(pvApiCtx, piAddressVarOne, &m1, &n1, &lenStVarOne, &pStVarOne);
218 printError(&sciErr, 0);
222 if (strcmp(pStVarOne, "ans") == 0)
224 Scierror(999, _("%s: ans cannot be edited.\n"), fname);
229 /* get address of the variable*/
230 sciErr = getVarAddressFromName(pvApiCtx, pStVarOne, &piAddr);
233 Scierror(4, _("%s: Undefined variable %s.\n"), fname, pStVarOne);
240 /* get address of the variable*/
241 sciErr = getVarAddressFromName(pvApiCtx, pStVarOne, &piAddr);
244 Scierror(4, _("%s: Undefined variable %s.\n"), fname, pStVarOne);
251 sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddr);
255 printError(&sciErr, 0);
262 sciErr = getVarAddressFromPosition(pvApiCtx, 3, &addr);
265 printError(&sciErr, 0);
270 if (!isDoubleType(pvApiCtx, addr))
272 Scierror(999,_("%s: Wrong type for input argument #%d: Double expected.\n"), fname, 3);
277 sciErr = getMatrixOfDouble(pvApiCtx, addr, &iRows, &iCols, &rowsIndex);
281 printError(&sciErr, 0);
285 nbRowsIndex = iRows * iCols;
287 sciErr = getVarAddressFromPosition(pvApiCtx, 4, &addr);
290 printError(&sciErr, 0);
295 if (!isDoubleType(pvApiCtx, addr))
297 Scierror(999,_("%s: Wrong type for input argument #%d: Double expected.\n"), fname, 4);
302 sciErr = getMatrixOfDouble(pvApiCtx, addr, &iRows, &iCols, &colsIndex);
306 printError(&sciErr, 0);
310 nbColsIndex = iRows * iCols;
313 //org_modules_commons::ScilabToJava::sendVariable(std::string(pStVarOne), true, pvApiCtx);
315 /* get type of the named variable */
316 sciErr = getVarType(pvApiCtx, piAddr, &iType);
320 printError(&sciErr, 0);
329 iComplex = isVarComplex(pvApiCtx, piAddr);
331 /* check complexity */
334 /* get size and data from Scilab memory */
335 sciErr = getComplexMatrixOfDouble(pvApiCtx, piAddr, &iRows, &iCols, &pdblReal, &pdblImg);
339 printError(&sciErr, 0);
343 ppdblRealMatrix = wrap<double, double>(pdblReal, iRows, iCols);
344 ppdblImgMatrix = wrap<double, double>(pdblImg, iRows, iCols);
348 if (nbRowsIndex == 0 || nbColsIndex == 0)
350 /* Launch Java Variable Editor through JNI */
351 EditVar::openVariableEditorComplex(getScilabJavaVM(),
362 /* Launch Java Variable Editor through JNI */
363 EditVar::refreshVariableEditorComplex(getScilabJavaVM(),
377 catch (const GiwsException::JniException & e)
379 Scierror(999, _("%s: Java exception arisen:\n%s\n"), fname, e.what());
382 clearWrap<double>(ppdblRealMatrix, iRows);
383 clearWrap<double>(ppdblImgMatrix, iRows);
387 /* get size and data from Scilab memory */
388 sciErr = getMatrixOfDouble(pvApiCtx, piAddr, &iRows, &iCols, &pdblReal);
392 printError(&sciErr, 0);
396 * we need this to make the links between the API (which return a double*)
397 * and the JNI which needs a double**
399 ppdblRealMatrix = wrap<double, double>(pdblReal, iRows, iCols);
401 /* Launch Java Variable Editor through JNI */
404 if (nbRowsIndex == 0 || nbColsIndex == 0)
406 EditVar::openVariableEditorDouble(getScilabJavaVM(), ppdblRealMatrix, iRows, iCols, pStVarOne);
410 EditVar::refreshVariableEditorDouble(getScilabJavaVM(), ppdblRealMatrix, iRows, iCols, rowsIndex, nbRowsIndex, colsIndex, nbColsIndex, pStVarOne);
413 catch (const GiwsException::JniException & e)
415 Scierror(999, _("%s: Java exception arisen:\n%s\n"), fname, e.what());
418 clearWrap<double>(ppdblRealMatrix, iRows);
424 //fisrt call to retrieve dimensions
425 sciErr = getMatrixOfString(pvApiCtx, piAddr, &iRows, &iCols, NULL, NULL);
429 printError(&sciErr, 0);
433 piLen = (int*)malloc(sizeof(int) * iRows * iCols);
435 //second call to retrieve length of each string
436 sciErr = getMatrixOfString(pvApiCtx, piAddr, &iRows, &iCols, piLen, NULL);
441 printError(&sciErr, 0);
445 pstData = (char**)calloc(iRows * iCols, sizeof(char*));
446 for(int i = 0 ; i < iRows * iCols ; i++)
448 pstData[i] = (char*)malloc(sizeof(char) * (piLen[i] + 1));//+ 1 for null termination
450 //third call to retrieve data
451 sciErr = getMatrixOfString(pvApiCtx, piAddr, &iRows, &iCols, piLen, pstData);
456 freeArrayOfString(pstData, iRows * iCols);
457 printError(&sciErr, 0);
462 * we need this to make the links between the API (which return a char**)
463 * and the JNI which needs a char***
465 ppstData = wrap<char *, char *>(pstData, iRows, iCols);
467 /* Launch Java Variable Editor through JNI */
470 if (nbRowsIndex == 0 || nbColsIndex == 0)
472 EditVar::openVariableEditorString(getScilabJavaVM(), ppstData, iRows, iCols, pStVarOne);
476 EditVar::refreshVariableEditorString(getScilabJavaVM(), ppstData, iRows, iCols, rowsIndex, nbRowsIndex, colsIndex, nbColsIndex, pStVarOne);
479 catch (const GiwsException::JniException & e)
481 Scierror(999, _("%s: Java exception arisen:\n%s\n"), fname, e.what());
485 freeArrayOfString(pstData, iRows * iCols);
487 clearWrap<char *>(ppstData, iRows);
492 //get size and data from Scilab memory
493 sciErr = getMatrixOfBoolean(pvApiCtx, piAddr, &iRows, &iCols, &piBool);
497 printError(&sciErr, 0);
502 * we need this to make the links between the API (which return a int*)
503 * and the JNI which needs a int**
505 ppiBool = wrap<int, int>(piBool, iRows, iCols);
507 /* Launch Java Variable Editor through JNI */
510 if (nbRowsIndex == 0 || nbColsIndex == 0)
512 EditVar::openVariableEditorBoolean(getScilabJavaVM(), ppiBool, iRows, iCols, pStVarOne);
516 EditVar::refreshVariableEditorBoolean(getScilabJavaVM(), ppiBool, iRows, iCols, rowsIndex, nbRowsIndex, colsIndex, nbColsIndex, pStVarOne);
519 catch (const GiwsException::JniException & e)
521 Scierror(999, _("%s: Java exception arisen:\n%s\n"), fname, e.what());
524 clearWrap<int>(ppiBool, iRows);
529 //get size and data from Scilab memory
531 sciErr = getMatrixOfIntegerPrecision(pvApiCtx, piAddr, &prec);
535 printError(&sciErr, 0);
542 sciErr = getMatrixOfInteger8(pvApiCtx, piAddr, &iRows, &iCols, &piInt8);
545 printError(&sciErr, 0);
549 ppiInt8 = wrap<char, char>(piInt8, iRows, iCols);
551 /* Launch Java Variable Editor through JNI */
554 if (nbRowsIndex == 0 || nbColsIndex == 0)
556 EditVar::openVariableEditorInteger8(getScilabJavaVM(), reinterpret_cast<byte**>(ppiInt8), iRows, iCols, pStVarOne);
560 EditVar::refreshVariableEditorInteger8(getScilabJavaVM(), reinterpret_cast<byte**>(ppiInt8), iRows, iCols, rowsIndex, nbRowsIndex, colsIndex, nbColsIndex, pStVarOne);
563 catch (const GiwsException::JniException & e)
565 Scierror(999, _("%s: Java exception arisen:\n%s\n"), fname, e.what());
568 clearWrap<char>(ppiInt8, iRows);
573 sciErr = getMatrixOfUnsignedInteger8(pvApiCtx, piAddr, &iRows, &iCols, &piUInt8);
576 printError(&sciErr, 0);
580 ppiUInt8 = wrap<short, unsigned char>(piUInt8, iRows, iCols);
582 /* Launch Java Variable Editor through JNI */
585 if (nbRowsIndex == 0 || nbColsIndex == 0)
587 EditVar::openVariableEditorUInteger8(getScilabJavaVM(), ppiUInt8, iRows, iCols, pStVarOne);
591 EditVar::refreshVariableEditorUInteger8(getScilabJavaVM(), ppiUInt8, iRows, iCols, rowsIndex, nbRowsIndex, colsIndex, nbColsIndex, pStVarOne);
594 catch (const GiwsException::JniException & e)
596 Scierror(999, _("%s: Java exception arisen:\n%s\n"), fname, e.what());
599 clearWrap<short>(ppiUInt8, iRows);
604 sciErr = getMatrixOfInteger16(pvApiCtx, piAddr, &iRows, &iCols, &piInt16);
607 printError(&sciErr, 0);
611 ppiInt16 = wrap<short, short>(piInt16, iRows, iCols);
613 /* Launch Java Variable Editor through JNI */
616 if (nbRowsIndex == 0 || nbColsIndex == 0)
618 EditVar::openVariableEditorInteger16(getScilabJavaVM(), ppiInt16, iRows, iCols, pStVarOne);
622 EditVar::refreshVariableEditorInteger16(getScilabJavaVM(), ppiInt16, iRows, iCols, rowsIndex, nbRowsIndex, colsIndex, nbColsIndex, pStVarOne);
625 catch (const GiwsException::JniException & e)
627 Scierror(999, _("%s: Java exception arisen:\n%s\n"), fname, e.what());
630 clearWrap<short>(ppiInt16, iRows);
635 sciErr = getMatrixOfUnsignedInteger16(pvApiCtx, piAddr, &iRows, &iCols, &piUInt16);
638 printError(&sciErr, 0);
642 ppiUInt16 = wrap<int, unsigned short>(piUInt16, iRows, iCols);
644 /* Launch Java Variable Editor through JNI */
647 if (nbRowsIndex == 0 || nbColsIndex == 0)
649 EditVar::openVariableEditorUInteger16(getScilabJavaVM(), ppiUInt16, iRows, iCols, pStVarOne);
653 EditVar::refreshVariableEditorUInteger16(getScilabJavaVM(), ppiUInt16, iRows, iCols, rowsIndex, nbRowsIndex, colsIndex, nbColsIndex, pStVarOne);
656 catch (const GiwsException::JniException & e)
658 Scierror(999, _("%s: Java exception arisen:\n%s\n"), fname, e.what());
661 clearWrap<int>(ppiUInt16, iRows);
666 sciErr = getMatrixOfInteger32(pvApiCtx, piAddr, &iRows, &iCols, &piInt32);
669 printError(&sciErr, 0);
673 ppiInt32 = wrap<int>(piInt32, iRows, iCols);
675 /* Launch Java Variable Editor through JNI */
678 if (nbRowsIndex == 0 || nbColsIndex == 0)
680 EditVar::openVariableEditorInteger32(getScilabJavaVM(), ppiInt32, iRows, iCols, pStVarOne);
684 EditVar::refreshVariableEditorInteger32(getScilabJavaVM(), ppiInt32, iRows, iCols, rowsIndex, nbRowsIndex, colsIndex, nbColsIndex, pStVarOne);
687 catch (const GiwsException::JniException & e)
689 Scierror(999, _("%s: Java exception arisen:\n%s\n"), fname, e.what());
692 clearWrap<int>(ppiInt32, iRows);
697 sciErr = getMatrixOfUnsignedInteger32(pvApiCtx, piAddr, &iRows, &iCols, &piUInt32);
700 printError(&sciErr, 0);
704 ppiUInt32 = wrap<long long int, unsigned int>(piUInt32, iRows, iCols);
706 /* Launch Java Variable Editor through JNI */
709 if (nbRowsIndex == 0 || nbColsIndex == 0)
711 EditVar::openVariableEditorUInteger32(getScilabJavaVM(), ppiUInt32, iRows, iCols, pStVarOne);
715 EditVar::refreshVariableEditorUInteger32(getScilabJavaVM(), ppiUInt32, iRows, iCols, rowsIndex, nbRowsIndex, colsIndex, nbColsIndex, pStVarOne);
718 catch (const GiwsException::JniException & e)
720 Scierror(999, _("%s: Java exception arisen:\n%s\n"), fname, e.what());
723 clearWrap<long long int>(ppiUInt32, iRows);
729 Scierror(42, _("%s: Type not handle yet"), fname);
735 case sci_boolean_sparse :
736 sciErr = getBooleanSparseMatrix(pvApiCtx, piAddr, &iRows, &iCols, &piNbItem, &piNbItemRow, &piColPos);
740 printError(&sciErr, 0);
744 ppiBool = wrapSparse(piNbItem, piNbItemRow, piColPos, iRows, iCols);
746 /* Launch Java Variable Editor through JNI */
749 if (nbRowsIndex == 0 || nbColsIndex == 0)
751 EditVar::openVariableEditorBooleanSparse(getScilabJavaVM(), ppiBool, iRows, iCols, pStVarOne);
755 EditVar::refreshVariableEditorBooleanSparse(getScilabJavaVM(), ppiBool, iRows, iCols, rowsIndex, nbRowsIndex, colsIndex, nbColsIndex, pStVarOne);
758 catch (const GiwsException::JniException & e)
760 Scierror(999, _("%s: Java exception arisen:\n%s\n"), fname, e.what());
763 clearWrap<int>(ppiBool, iRows);
766 if (isVarComplex(pvApiCtx, piAddr))
768 sciErr = getComplexSparseMatrix(pvApiCtx, piAddr, &iRows, &iCols, &piNbItem, &piNbItemRow, &piColPos, &pdblReal, &pdblImg);
772 printError(&sciErr, 0);
776 ppdblRealMatrix = wrapSparse(piNbItem, piNbItemRow, piColPos, iRows, iCols, pdblReal);
777 ppdblImgMatrix = wrapSparse(piNbItem, piNbItemRow, piColPos, iRows, iCols, pdblImg);
779 /* Launch Java Variable Editor through JNI */
782 if (nbRowsIndex == 0 || nbColsIndex == 0)
784 EditVar::openVariableEditorComplexSparse(getScilabJavaVM(), ppdblRealMatrix, iRows, iCols, ppdblImgMatrix, iRows, iCols, pStVarOne);
788 EditVar::refreshVariableEditorComplexSparse(getScilabJavaVM(), ppdblRealMatrix, iRows, iCols, ppdblImgMatrix, iRows, iCols, rowsIndex, nbRowsIndex, colsIndex, nbColsIndex, pStVarOne);
791 catch (const GiwsException::JniException & e)
793 Scierror(999, _("%s: Java exception arisen:\n%s\n"), fname, e.what());
796 clearWrap<double>(ppdblRealMatrix, iRows);
797 clearWrap<double>(ppdblImgMatrix, iRows);
801 sciErr = getSparseMatrix(pvApiCtx, piAddr, &iRows, &iCols, &piNbItem, &piNbItemRow, &piColPos, &pdblReal);
805 printError(&sciErr, 0);
809 ppdblRealMatrix = wrapSparse(piNbItem, piNbItemRow, piColPos, iRows, iCols, pdblReal);
811 /* Launch Java Variable Editor through JNI */
814 if (nbRowsIndex == 0 || nbColsIndex == 0)
816 EditVar::openVariableEditorSparse(getScilabJavaVM(), ppdblRealMatrix, iRows, iCols, pStVarOne);
820 EditVar::refreshVariableEditorSparse(getScilabJavaVM(), ppdblRealMatrix, iRows, iCols, rowsIndex, nbRowsIndex, colsIndex, nbColsIndex, pStVarOne);
823 catch (const GiwsException::JniException & e)
825 Scierror(999, _("%s: Java exception arisen:\n%s\n"), fname, e.what());
828 clearWrap<double>(ppdblRealMatrix, iRows);
833 Scierror(42, _("%s: Type not handle yet"), fname);
843 /*--------------------------------------------------------------------------*/