2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2010 - DIGITEO - Allan CORNET
4 * Copyright (C) 2010 - DIGITEO - Bruno JOFRET
6 * This file must be used under the terms of the CeCILL.
7 * This source file is licensed as described in the file COPYING, which
8 * you should have received as part of this distribution. The terms
9 * are also available at
10 * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
15 #include "BrowseVar.hxx"
27 #include "BrowseVarManager.h"
28 #include "localization.h"
31 #include "stackinfo.h"
32 #include "api_scilab.h"
33 #include "getScilabJavaVM.h"
35 #include "freeArrayOfString.h"
36 #include "sci_types.h"
38 #include "strdup_windows.h"
41 using namespace org_scilab_modules_ui_data;
43 static std::set < string > createScilabDefaultVariablesSet();
44 static char * getListName(char * variableName);
45 static std::string formatMatrix(int nbRows, int nbCols, BOOL isComplex, double *pdblReal, double *pdblImg);
46 static char * valueToDisplay(char * variableName, int variableType, int nbRows, int nbCols);
49 BrowseVar::openVariableBrowser(getScilabJavaVM());
53 void UpdateBrowseVar()
55 if (BrowseVar::isVariableBrowserOpened(getScilabJavaVM()))
61 void SetBrowseVarData()
64 int iGlobalVariablesUsed = 0;
65 int iGlobalVariablesTotal = 0;
66 int iLocalVariablesUsed = 0;
67 int iLocalVariablesTotal = 0;
70 // First get how many global / local variable we have.
71 C2F(getvariablesinfo) (&iLocalVariablesTotal, &iLocalVariablesUsed);
72 C2F(getgvariablesinfo) (&iGlobalVariablesTotal, &iGlobalVariablesUsed);
74 char **pstAllVariableNames = (char **)MALLOC((iLocalVariablesUsed + iGlobalVariablesUsed) * sizeof(char *));
75 char **pstAllVariableVisibility = (char **)MALLOC((iLocalVariablesUsed + iGlobalVariablesUsed) * sizeof(char *));
76 char **pstAllVariableListTypes = (char **)MALLOC((iLocalVariablesUsed + iGlobalVariablesUsed) * sizeof(char *));
77 int *piAllVariableBytes = (int *)MALLOC((iLocalVariablesUsed + iGlobalVariablesUsed) * sizeof(int));
78 char **pstAllVariableSizes = (char **)MALLOC((iLocalVariablesUsed + iGlobalVariablesUsed) * sizeof(char *));
79 int *piAllVariableTypes = (int *)MALLOC((iLocalVariablesUsed + iGlobalVariablesUsed) * sizeof(int));
80 int *piAllVariableIntegerTypes = (int *)MALLOC((iLocalVariablesUsed + iGlobalVariablesUsed) * sizeof(int));
81 bool *piAllVariableFromUser = (bool *) MALLOC((iLocalVariablesUsed + iGlobalVariablesUsed) * sizeof(bool));
82 /* Necessary for the plots in the var browser */
83 int *piAllVariableNbRows = (int *)MALLOC((iLocalVariablesUsed + iGlobalVariablesUsed) * sizeof(int));
84 int *piAllVariableNbCols = (int *)MALLOC((iLocalVariablesUsed + iGlobalVariablesUsed) * sizeof(int));
89 std::set < string > scilabDefaultVariablesSet = createScilabDefaultVariablesSet();
91 // for each local variable get information
92 for (; i < iLocalVariablesUsed; ++i)
95 pstAllVariableNames[i] = getLocalNamefromId(i + 1);
97 err = getNamedVarType(pvApiCtx, pstAllVariableNames[i], &piAllVariableTypes[i]);
100 piAllVariableBytes[i] = getLocalSizefromId(i);
101 err = getNamedVarDimension(pvApiCtx, pstAllVariableNames[i], &nbRows, &nbCols);
104 if (err.iErr || nbRows * nbCols == 0)
107 pstAllVariableSizes[i] = (char *)MALLOC((sizeof(N_A) + 1) * sizeof(char));
108 strcpy(pstAllVariableSizes[i], N_A);
112 pstAllVariableSizes[i] = valueToDisplay(pstAllVariableNames[i], piAllVariableTypes[i], nbRows, nbCols);
113 piAllVariableNbRows[i] = nbRows;
114 piAllVariableNbCols[i] = nbCols;
118 if (piAllVariableTypes[i] == sci_ints)
122 err = getNamedMatrixOfIntegerPrecision(pvApiCtx, pstAllVariableNames[i], &iPrec);
126 piAllVariableIntegerTypes[i] = 8;
129 piAllVariableIntegerTypes[i] = 16;
132 piAllVariableIntegerTypes[i] = 32;
134 #ifdef __SCILAB_INT64__
136 piAllVariableIntegerTypes[i] = 64;
140 piAllVariableIntegerTypes[i] = 0; // Should never occurs
146 piAllVariableIntegerTypes[i] = -1;
149 if (piAllVariableTypes[i] == sci_tlist || piAllVariableTypes[i] == sci_mlist)
151 pstAllVariableListTypes[i] = getListName(pstAllVariableNames[i]);
155 pstAllVariableListTypes[i] = strdup("");
160 pstAllVariableVisibility[i] = strdup("local");
162 if (scilabDefaultVariablesSet.find(string(pstAllVariableNames[i])) == scilabDefaultVariablesSet.end() && piAllVariableTypes[i] != sci_lib)
164 piAllVariableFromUser[i] = TRUE;
168 piAllVariableFromUser[i] = FALSE;
172 // for each global variable get information
173 for (int j = 0; j < iGlobalVariablesUsed; ++j, ++i)
176 pstAllVariableNames[i] = getGlobalNamefromId(j);
177 // Bytes used - 8 is the number of bytes in a word
178 piAllVariableBytes[i] = getGlobalSizefromId(j) * 8;
180 // Calling "API Scilab": not yet implemented for global variable
181 //getNamedVarType(pvApiCtx, pstAllVariableNames[i], &piAllVariableTypes[i]);
182 // Using old stack operations...
183 int pos = C2F(vstk).isiz + 2 + j;
185 piAllVariableTypes[i] = C2F(gettype) (&pos);
187 // Sizes of the variable
188 getNamedVarDimension(pvApiCtx, pstAllVariableNames[i], &nbRows, &nbCols);
189 pstAllVariableSizes[i] = valueToDisplay(pstAllVariableNames[i], piAllVariableTypes[i], nbRows, nbCols);
190 piAllVariableNbRows[i] = nbRows;
191 piAllVariableNbCols[i] = nbCols;
195 pstAllVariableVisibility[i] = strdup("global");
198 if (piAllVariableTypes[i] == sci_tlist || piAllVariableTypes[i] == sci_mlist)
200 pstAllVariableListTypes[i] = getListName(pstAllVariableNames[i]);
204 pstAllVariableListTypes[i] = strdup("");
208 if (scilabDefaultVariablesSet.find(string(pstAllVariableNames[i])) == scilabDefaultVariablesSet.end()
209 && piAllVariableTypes[i] != sci_c_function && piAllVariableTypes[i] != sci_lib)
211 piAllVariableFromUser[i] = TRUE;
215 piAllVariableFromUser[i] = FALSE;
219 // Launch Java Variable Browser through JNI
220 BrowseVar::setVariableBrowserData(getScilabJavaVM(),
221 pstAllVariableNames, iLocalVariablesUsed + iGlobalVariablesUsed,
222 piAllVariableBytes, iLocalVariablesUsed + iGlobalVariablesUsed,
223 piAllVariableTypes, iLocalVariablesUsed + iGlobalVariablesUsed,
224 piAllVariableIntegerTypes, iLocalVariablesUsed + iGlobalVariablesUsed,
225 pstAllVariableListTypes, iLocalVariablesUsed + iGlobalVariablesUsed,
226 pstAllVariableSizes, iLocalVariablesUsed + iGlobalVariablesUsed,
227 piAllVariableNbRows, iLocalVariablesUsed + iGlobalVariablesUsed,
228 piAllVariableNbCols, iLocalVariablesUsed + iGlobalVariablesUsed,
229 pstAllVariableVisibility, iLocalVariablesUsed + iGlobalVariablesUsed,
230 piAllVariableFromUser, iLocalVariablesUsed + iGlobalVariablesUsed);
232 freeArrayOfString(pstAllVariableNames, iLocalVariablesUsed + iGlobalVariablesUsed);
233 freeArrayOfString(pstAllVariableVisibility, iLocalVariablesUsed + iGlobalVariablesUsed);
234 freeArrayOfString(pstAllVariableSizes, iLocalVariablesUsed + iGlobalVariablesUsed);
235 freeArrayOfString(pstAllVariableListTypes, iLocalVariablesUsed + iGlobalVariablesUsed);
237 if (piAllVariableFromUser)
239 FREE(piAllVariableFromUser);
240 piAllVariableFromUser = NULL;
243 if (piAllVariableBytes)
245 FREE(piAllVariableBytes);
246 piAllVariableBytes = NULL;
249 if (piAllVariableTypes)
251 FREE(piAllVariableTypes);
252 piAllVariableTypes = NULL;
255 if (piAllVariableIntegerTypes)
257 FREE(piAllVariableIntegerTypes);
258 piAllVariableIntegerTypes = NULL;
261 if (piAllVariableNbRows)
263 FREE(piAllVariableNbRows);
264 piAllVariableNbRows = NULL;
267 if (piAllVariableNbCols)
269 FREE(piAllVariableNbCols);
270 piAllVariableNbCols = NULL;
274 /*--------------------------------------------------------------------------*/
275 static std::set < string > createScilabDefaultVariablesSet()
277 string arr[] = { "home",
310 "evoid", // Constant for external object
311 "jvoid" // Constant for external object Java (jims)
316 std::set < string > ScilabDefaultVariables;
318 for (i = 0; i < NBELEMENT; i++)
320 ScilabDefaultVariables.insert(arr[i]);
323 return ScilabDefaultVariables;
326 static char * getListName(char * variableName)
335 sciErr = getVarAddressFromName(pvApiCtx, variableName, &piAddr);
341 sciErr = getListItemAddress(pvApiCtx, piAddr, 1, &piAddr1);
347 if (getAllocatedMatrixOfString(pvApiCtx, piAddr1, &iRows, &iCols, &pstType))
352 tmpChar = strdup(pstType[0]);
353 freeAllocatedMatrixOfString(iRows, iCols, pstType);
357 static char * valueToDisplay(char * variableName, int variableType, int nbRows, int nbCols)
362 // 4 is the dimension max to which display the content
363 if (nbRows * nbCols <= 4 && variableType == sci_matrix)
365 // Small double value, display it
366 double* pdblReal = (double *)malloc(((nbRows) * (nbCols)) * sizeof(double));
367 double* pdblImg = (double *)malloc(((nbRows) * (nbCols)) * sizeof(double));
368 BOOL isComplex = FALSE;
370 if (isNamedVarComplex(pvApiCtx, variableName))
372 err = readNamedComplexMatrixOfDouble(pvApiCtx, variableName, &nbRows, &nbCols, pdblReal, pdblImg);
377 err = readNamedMatrixOfDouble(pvApiCtx, variableName, &nbRows, &nbCols, pdblReal);
381 return strdup(formatMatrix(nbRows, nbCols, isComplex, pdblReal, pdblImg).c_str());
385 char *sizeStr = NULL;
386 // 11 =strlen("2147483647")+1 (1 for security)
387 sizeStr = (char *)MALLOC((11 + 11 + 1 + 1) * sizeof(char));
388 sprintf(sizeStr, "%dx%d", nbRows, nbCols);
393 std::string formatMatrix(int nbRows, int nbCols, BOOL isComplex, double *pdblReal, double *pdblImg)
396 #define PRECISION_DISPLAY 3
397 if (nbRows * nbCols == 1)
399 std::ostringstream os;
400 os.precision(PRECISION_DISPLAY);
401 os << pdblReal[0]; // Convert the double to string
404 os << " + " << pdblImg[0] << "i";
409 std::string formated = "[";
410 for (j = 0 ; j < nbRows ; j++)
412 for (i = 0 ; i < nbCols ; i++)
414 /* Display the formated matrix ... the way the user
416 std::ostringstream os;
417 os.precision(PRECISION_DISPLAY);
418 os << pdblReal[i * nbRows + j]; // Convert the double to string
419 formated += os.str();
422 std::ostringstream osComplex;
423 osComplex.precision(PRECISION_DISPLAY);
424 osComplex << pdblImg[i * nbRows + j];
425 formated += " + " + osComplex.str() + "i";
429 if (i + 1 != nbCols) // Not the last element of the matrix
434 if (j + 1 != nbRows) // Not the last line of the matrix
439 return formated + "]";