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 "UpdateBrowseVar.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);
47 /*--------------------------------------------------------------------------*/
48 void UpdateBrowseVar(BOOL update)
51 int iGlobalVariablesUsed = 0;
52 int iGlobalVariablesTotal = 0;
53 int iLocalVariablesUsed = 0;
54 int iLocalVariablesTotal = 0;
57 if (update && !BrowseVar::isVariableBrowserOpened(getScilabJavaVM()))
62 // First get how many global / local variable we have.
63 C2F(getvariablesinfo) (&iLocalVariablesTotal, &iLocalVariablesUsed);
64 C2F(getgvariablesinfo) (&iGlobalVariablesTotal, &iGlobalVariablesUsed);
66 char **pstAllVariableNames = (char **)MALLOC((iLocalVariablesUsed + iGlobalVariablesUsed) * sizeof(char *));
67 char **pstAllVariableVisibility = (char **)MALLOC((iLocalVariablesUsed + iGlobalVariablesUsed) * sizeof(char *));
68 char **pstAllVariableListTypes = (char **)MALLOC((iLocalVariablesUsed + iGlobalVariablesUsed) * sizeof(char *));
69 int *piAllVariableBytes = (int *)MALLOC((iLocalVariablesUsed + iGlobalVariablesUsed) * sizeof(int));
70 char **pstAllVariableSizes = (char **)MALLOC((iLocalVariablesUsed + iGlobalVariablesUsed) * sizeof(char *));
71 int *piAllVariableTypes = (int *)MALLOC((iLocalVariablesUsed + iGlobalVariablesUsed) * sizeof(int));
72 int *piAllVariableIntegerTypes = (int *)MALLOC((iLocalVariablesUsed + iGlobalVariablesUsed) * sizeof(int));
73 bool *piAllVariableFromUser = (bool *) MALLOC((iLocalVariablesUsed + iGlobalVariablesUsed) * sizeof(bool));
74 /* Necessary for the plots in the var browser */
75 int *piAllVariableNbRows = (int *)MALLOC((iLocalVariablesUsed + iGlobalVariablesUsed) * sizeof(int));
76 int *piAllVariableNbCols = (int *)MALLOC((iLocalVariablesUsed + iGlobalVariablesUsed) * sizeof(int));
81 std::set < string > scilabDefaultVariablesSet = createScilabDefaultVariablesSet();
83 // for each local variable get information
84 for (; i < iLocalVariablesUsed; ++i)
87 pstAllVariableNames[i] = getLocalNamefromId(i + 1);
89 err = getNamedVarType(pvApiCtx, pstAllVariableNames[i], &piAllVariableTypes[i]);
92 piAllVariableBytes[i] = getLocalSizefromId(i);
93 err = getNamedVarDimension(pvApiCtx, pstAllVariableNames[i], &nbRows, &nbCols);
96 if (err.iErr || nbRows * nbCols == 0)
99 pstAllVariableSizes[i] = (char *)MALLOC((sizeof(N_A) + 1) * sizeof(char));
100 strcpy(pstAllVariableSizes[i], N_A);
104 pstAllVariableSizes[i] = valueToDisplay(pstAllVariableNames[i], piAllVariableTypes[i], nbRows, nbCols);
105 piAllVariableNbRows[i] = nbRows;
106 piAllVariableNbCols[i] = nbCols;
110 if (piAllVariableTypes[i] == sci_ints)
114 err = getNamedMatrixOfIntegerPrecision(pvApiCtx, pstAllVariableNames[i], &iPrec);
118 piAllVariableIntegerTypes[i] = 8;
121 piAllVariableIntegerTypes[i] = 16;
124 piAllVariableIntegerTypes[i] = 32;
126 #ifdef __SCILAB_INT64__
128 piAllVariableIntegerTypes[i] = 64;
132 piAllVariableIntegerTypes[i] = 0; // Should never occurs
138 piAllVariableIntegerTypes[i] = -1;
141 if (piAllVariableTypes[i] == sci_tlist || piAllVariableTypes[i] == sci_mlist)
143 pstAllVariableListTypes[i] = getListName(pstAllVariableNames[i]);
147 pstAllVariableListTypes[i] = strdup("");
152 pstAllVariableVisibility[i] = strdup("local");
154 if (scilabDefaultVariablesSet.find(string(pstAllVariableNames[i])) == scilabDefaultVariablesSet.end() && piAllVariableTypes[i] != sci_lib)
156 piAllVariableFromUser[i] = TRUE;
160 piAllVariableFromUser[i] = FALSE;
164 // for each global variable get information
165 for (int j = 0; j < iGlobalVariablesUsed; ++j, ++i)
168 pstAllVariableNames[i] = getGlobalNamefromId(j);
169 // Bytes used - 8 is the number of bytes in a word
170 piAllVariableBytes[i] = getGlobalSizefromId(j) * 8;
172 // Calling "API Scilab": not yet implemented for global variable
173 //getNamedVarType(pvApiCtx, pstAllVariableNames[i], &piAllVariableTypes[i]);
174 // Using old stack operations...
175 int pos = C2F(vstk).isiz + 2 + j;
177 piAllVariableTypes[i] = C2F(gettype) (&pos);
179 // Sizes of the variable
180 getNamedVarDimension(pvApiCtx, pstAllVariableNames[i], &nbRows, &nbCols);
181 pstAllVariableSizes[i] = valueToDisplay(pstAllVariableNames[i], piAllVariableTypes[i], nbRows, nbCols);
182 piAllVariableNbRows[i] = nbRows;
183 piAllVariableNbCols[i] = nbCols;
187 pstAllVariableVisibility[i] = strdup("global");
190 if (piAllVariableTypes[i] == sci_tlist || piAllVariableTypes[i] == sci_mlist)
192 pstAllVariableListTypes[i] = getListName(pstAllVariableNames[i]);
196 pstAllVariableListTypes[i] = strdup("");
200 if (scilabDefaultVariablesSet.find(string(pstAllVariableNames[i])) == scilabDefaultVariablesSet.end()
201 && piAllVariableTypes[i] != sci_c_function && piAllVariableTypes[i] != sci_lib)
203 piAllVariableFromUser[i] = TRUE;
207 piAllVariableFromUser[i] = FALSE;
211 // Launch Java Variable Browser through JNI
212 BrowseVar::openVariableBrowser(getScilabJavaVM(),
214 pstAllVariableNames, iLocalVariablesUsed + iGlobalVariablesUsed,
215 piAllVariableBytes, iLocalVariablesUsed + iGlobalVariablesUsed,
216 piAllVariableTypes, iLocalVariablesUsed + iGlobalVariablesUsed,
217 piAllVariableIntegerTypes, iLocalVariablesUsed + iGlobalVariablesUsed,
218 pstAllVariableListTypes, iLocalVariablesUsed + iGlobalVariablesUsed,
219 pstAllVariableSizes, iLocalVariablesUsed + iGlobalVariablesUsed,
220 piAllVariableNbRows, iLocalVariablesUsed + iGlobalVariablesUsed,
221 piAllVariableNbCols, iLocalVariablesUsed + iGlobalVariablesUsed,
222 pstAllVariableVisibility, iLocalVariablesUsed + iGlobalVariablesUsed,
223 piAllVariableFromUser, iLocalVariablesUsed + iGlobalVariablesUsed);
225 freeArrayOfString(pstAllVariableNames, iLocalVariablesUsed + iGlobalVariablesUsed);
226 freeArrayOfString(pstAllVariableVisibility, iLocalVariablesUsed + iGlobalVariablesUsed);
227 freeArrayOfString(pstAllVariableSizes, iLocalVariablesUsed + iGlobalVariablesUsed);
228 freeArrayOfString(pstAllVariableListTypes, iLocalVariablesUsed + iGlobalVariablesUsed);
230 if (piAllVariableFromUser)
232 FREE(piAllVariableFromUser);
233 piAllVariableFromUser = NULL;
236 if (piAllVariableBytes)
238 FREE(piAllVariableBytes);
239 piAllVariableBytes = NULL;
242 if (piAllVariableTypes)
244 FREE(piAllVariableTypes);
245 piAllVariableTypes = NULL;
248 if (piAllVariableIntegerTypes)
250 FREE(piAllVariableIntegerTypes);
251 piAllVariableIntegerTypes = NULL;
254 if (piAllVariableNbRows)
256 FREE(piAllVariableNbRows);
257 piAllVariableNbRows = NULL;
260 if (piAllVariableNbCols)
262 FREE(piAllVariableNbCols);
263 piAllVariableNbCols = NULL;
267 /*--------------------------------------------------------------------------*/
268 static std::set < string > createScilabDefaultVariablesSet()
270 string arr[] = { "home",
303 "evoid" // Constant for external object
308 std::set < string > ScilabDefaultVariables;
310 for (i = 0; i <= NBELEMENT; i++)
312 ScilabDefaultVariables.insert(arr[i]);
315 return ScilabDefaultVariables;
318 static char * getListName(char * variableName)
327 sciErr = getVarAddressFromName(pvApiCtx, variableName, &piAddr);
333 sciErr = getListItemAddress(pvApiCtx, piAddr, 1, &piAddr1);
339 if (getAllocatedMatrixOfString(pvApiCtx, piAddr1, &iRows, &iCols, &pstType))
344 tmpChar = strdup(pstType[0]);
345 freeAllocatedMatrixOfString(iRows, iCols, pstType);
349 static char * valueToDisplay(char * variableName, int variableType, int nbRows, int nbCols)
354 // 4 is the dimension max to which display the content
355 if (nbRows * nbCols <= 4 && variableType == sci_matrix)
357 // Small double value, display it
358 double* pdblReal = (double *)malloc(((nbRows) * (nbCols)) * sizeof(double));
359 double* pdblImg = (double *)malloc(((nbRows) * (nbCols)) * sizeof(double));
360 BOOL isComplex = FALSE;
362 if (isNamedVarComplex(pvApiCtx, variableName))
364 err = readNamedComplexMatrixOfDouble(pvApiCtx, variableName, &nbRows, &nbCols, pdblReal, pdblImg);
369 err = readNamedMatrixOfDouble(pvApiCtx, variableName, &nbRows, &nbCols, pdblReal);
373 return strdup(formatMatrix(nbRows, nbCols, isComplex, pdblReal, pdblImg).c_str());
377 char *sizeStr = NULL;
378 // 11 =strlen("2147483647")+1 (1 for security)
379 sizeStr = (char *)MALLOC((11 + 11 + 1 + 1) * sizeof(char));
380 sprintf(sizeStr, "%dx%d", nbRows, nbCols);
385 std::string formatMatrix(int nbRows, int nbCols, BOOL isComplex, double *pdblReal, double *pdblImg)
388 #define PRECISION_DISPLAY 3
389 if (nbRows * nbCols == 1)
391 std::ostringstream os;
392 os.precision(PRECISION_DISPLAY);
393 os << pdblReal[0]; // Convert the double to string
396 os << " + " << pdblImg[0] << "i";
401 std::string formated = "[";
402 for (j = 0 ; j < nbRows ; j++)
404 for (i = 0 ; i < nbCols ; i++)
406 /* Display the formated matrix ... the way the user
408 std::ostringstream os;
409 os.precision(PRECISION_DISPLAY);
410 os << pdblReal[i * nbRows + j]; // Convert the double to string
411 formated += os.str();
414 std::ostringstream osComplex;
415 osComplex.precision(PRECISION_DISPLAY);
416 osComplex << pdblImg[i * nbRows + j];
417 formated += " + " + osComplex.str() + "i";
421 if (i + 1 != nbCols) // Not the last element of the matrix
426 if (j + 1 != nbRows) // Not the last line of the matrix
431 return formated + "]";