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 * Copyright (C) 2012 - 2016 - Scilab Enterprises
8 * This file is hereby licensed under the terms of the GNU GPL v2.0,
9 * pursuant to article 5.3.4 of the CeCILL v.2.1.
10 * This file was originally licensed under the terms of the CeCILL v2.1,
11 * and continues to be available under such terms.
12 * For more information, see the COPYING file which you should have received
13 * along with this program.
23 #include "alltypes.hxx"
24 #include "types_tools.hxx"
26 #include "BrowseVar.hxx"
35 #include "context.hxx"
39 #include "BrowseVarManager.h"
40 #include "localization.h"
41 #include "sci_malloc.h"
43 #include "getScilabJavaVM.h"
45 #include "freeArrayOfString.h"
46 #include "os_string.h"
47 #include "api_scilab.h"
51 #define EMPTY_MATRIX "[ ]"
52 #define LOCAL_STR "local"
53 #define GLOBAL_STR "global"
54 #define INHERITED_STR "inherited"
55 #define STRUCT_STR "st"
56 #define NEG_INF_STR "-Inf"
59 #define PLUS_STR " + "
60 #define MINUS_STR " - "
62 #define LBRACKET_STR "["
63 #define RBRACKET_STR "]"
64 #define COMMA_STR ", "
65 #define SEMICOLON_STR "; "
67 using namespace org_scilab_modules_ui_data;
70 static std::set<string> createScilabDefaultVariablesSet();
71 static char * getListName(char * variableName);
72 static std::string formatMatrix(int nbRows, int nbCols, double *pdblReal, double *pdblImg);
73 static char * valueToDisplay(types::InternalType* pIT);
76 BrowseVar::openVariableBrowser(getScilabJavaVM());
80 void UpdateBrowseVar()
82 if (BrowseVar::isVariableBrowserOpened(getScilabJavaVM()))
88 void SetBrowseVarData()
91 int iLocalVariablesUsed = 0;
94 symbol::Context* ctx = symbol::Context::getInstance();
96 std::list<symbol::Variable*> lstVars;
97 std::list<symbol::Library*> lstLibs;
99 iLocalVariablesUsed = ctx->getVarsToVariableBrowser(lstVars);
100 iLocalVariablesUsed += ctx->getLibsToVariableBrowser(lstLibs);
102 char **pstAllVariableNames = new char*[iLocalVariablesUsed]();
103 char **pstAllVariableVisibility = new char*[iLocalVariablesUsed]();
104 char **pstAllVariableListTypes = new char*[iLocalVariablesUsed]();
105 int *piAllVariableBytes = new int[iLocalVariablesUsed]();
106 char **pstAllVariableSizes = new char*[iLocalVariablesUsed]();
107 int *piAllVariableTypes = new int[iLocalVariablesUsed]();
108 int *piAllVariableIntegerTypes = new int[iLocalVariablesUsed]();
109 bool *piAllVariableFromUser = new bool[iLocalVariablesUsed]();
110 /* Necessary for the plots in the var browser */
111 int *piAllVariableNbRows = new int[iLocalVariablesUsed]();
112 int *piAllVariableNbCols = new int[iLocalVariablesUsed]();
115 char *sizeStr = NULL;
117 std::set<string> scilabDefaultVariablesSet = createScilabDefaultVariablesSet();
118 int iLevel = ctx->getScopeLevel();
120 // for each local variable get information
121 for (auto var : lstVars)
123 //get top level value
124 symbol::ScopedVariable* sv = var->top();
126 types::InternalType* pIT = sv->m_pIT;
129 pstAllVariableNames[i] = wide_string_to_UTF8(var->getSymbol().getName().data());
132 if (sv->m_globalVisible == true)
134 pstAllVariableVisibility[i] = os_strdup(GLOBAL_STR);
135 pIT = var->getGlobalValue();
137 else if (sv->m_iLevel != iLevel)
139 pstAllVariableVisibility[i] = os_strdup(INHERITED_STR);
143 pstAllVariableVisibility[i] = os_strdup(LOCAL_STR);
147 err = getVarType(NULL, (int*)pIT, &piAllVariableTypes[i]);
150 piAllVariableBytes[i] = 0;
151 err = getVarDimension(NULL, (int*)pIT, &nbRows, &nbCols);
156 pstAllVariableSizes[i] = os_strdup(N_A);
158 else if (nbRows * nbCols == 0)
160 pstAllVariableSizes[i] = os_strdup(EMPTY_MATRIX);
164 pstAllVariableSizes[i] = valueToDisplay(pIT);
165 piAllVariableNbRows[i] = nbRows;
166 piAllVariableNbCols[i] = nbCols;
169 if (piAllVariableTypes[i] == sci_ints)
173 err = getMatrixOfIntegerPrecision(NULL, (int*)pIT, &iPrec);
177 piAllVariableIntegerTypes[i] = 8;
180 piAllVariableIntegerTypes[i] = 16;
183 piAllVariableIntegerTypes[i] = 32;
185 #ifdef __SCILAB_INT64__
187 piAllVariableIntegerTypes[i] = 64;
191 piAllVariableIntegerTypes[i] = 0; // Should never occurs
197 piAllVariableIntegerTypes[i] = -1;
200 if (pIT->isTList() || pIT->isMList())
202 pstAllVariableListTypes[i] = getListName(pstAllVariableNames[i]);
204 else if (pIT->isStruct())
206 pstAllVariableListTypes[i] = os_strdup(STRUCT_STR);
210 pstAllVariableListTypes[i] = os_strdup("");
213 if (scilabDefaultVariablesSet.find(string(pstAllVariableNames[i])) == scilabDefaultVariablesSet.end() && piAllVariableTypes[i] != sci_lib)
215 piAllVariableFromUser[i] = TRUE;
219 piAllVariableFromUser[i] = FALSE;
225 for (auto lib : lstLibs)
227 //get top level value
228 symbol::ScopedLibrary* sl = lib->top();
231 pstAllVariableNames[i] = wide_string_to_UTF8(lib->getSymbol().getName().data());
234 if (sl->m_iLevel != iLevel)
236 pstAllVariableVisibility[i] = os_strdup(INHERITED_STR);
240 pstAllVariableVisibility[i] = os_strdup(LOCAL_STR);
243 piAllVariableTypes[i] = sci_lib;
245 piAllVariableBytes[i] = 0;
246 pstAllVariableSizes[i] = os_strdup(N_A);
247 piAllVariableIntegerTypes[i] = -1;
248 pstAllVariableListTypes[i] = os_strdup("");
249 piAllVariableFromUser[i] = FALSE;
250 piAllVariableNbRows[i] = 1;
251 piAllVariableNbCols[i] = 1;
255 // Launch Java Variable Browser through JNI
256 BrowseVar::setVariableBrowserData(getScilabJavaVM(),
257 pstAllVariableNames, iLocalVariablesUsed,
258 piAllVariableBytes, iLocalVariablesUsed,
259 piAllVariableTypes, iLocalVariablesUsed,
260 piAllVariableIntegerTypes, iLocalVariablesUsed,
261 pstAllVariableListTypes, iLocalVariablesUsed,
262 pstAllVariableSizes, iLocalVariablesUsed,
263 piAllVariableNbRows, iLocalVariablesUsed,
264 piAllVariableNbCols, iLocalVariablesUsed,
265 pstAllVariableVisibility, iLocalVariablesUsed,
266 piAllVariableFromUser, iLocalVariablesUsed);
268 for (int i = 0; i < iLocalVariablesUsed; ++i)
270 FREE(pstAllVariableNames[i]);
271 FREE(pstAllVariableVisibility[i]);
272 FREE(pstAllVariableSizes[i]);
273 FREE(pstAllVariableListTypes[i]);
276 delete[] pstAllVariableNames;
277 delete[] pstAllVariableVisibility;
278 delete[] pstAllVariableSizes;
279 delete[] pstAllVariableListTypes;
281 delete[] piAllVariableFromUser;
282 delete[] piAllVariableBytes;
283 delete[] piAllVariableTypes;
284 delete[] piAllVariableIntegerTypes;
285 delete[] piAllVariableNbRows;
286 delete[] piAllVariableNbCols;
289 /*--------------------------------------------------------------------------*/
290 static std::set<string> createScilabDefaultVariablesSet()
292 string arr[] = { "home",
325 "evoid", // Constant for external object
326 "jvoid", // Constant for external object Java (jims)
327 "jnull", // Constant for external object Java (jims)
328 "enull" // Constant for external object
333 std::set<string> ScilabDefaultVariables;
335 for (i = 0; i < NBELEMENT; i++)
337 ScilabDefaultVariables.insert(arr[i]);
340 return ScilabDefaultVariables;
343 static char * getListName(char * variableName)
352 sciErr = getVarAddressFromName(NULL, variableName, &piAddr);
355 return os_strdup("");
358 sciErr = getListItemAddress(NULL, piAddr, 1, &piAddr1);
361 return os_strdup("");
364 if (getAllocatedMatrixOfString(NULL, piAddr1, &iRows, &iCols, &pstType))
367 return os_strdup("");
369 tmpChar = os_strdup(pstType[0]);
370 freeAllocatedMatrixOfString(iRows, iCols, pstType);
374 static char *valueToDisplay(types::InternalType* pIT)
376 types::GenericType *pGT = pIT->getAs<types::GenericType>();
377 int *piDims = pGT->getDimsArray();
379 if (pIT->isDouble() && pGT->getDims() < 3 && pGT->getSize() <= 4) {
380 // 4 is the dimension max to which display the content
381 types::Double* pD = pIT->getAs<types::Double>();
382 // Small double value, display it
383 double* pdblReal = nullptr;
384 double* pdblImg = nullptr;
385 pdblReal = pD->get();
389 pdblImg = pD->getImg();
392 return os_strdup(formatMatrix(piDims[0], piDims[1], pdblReal, pdblImg).c_str());
396 std::string sizeStr = std::to_string(piDims[0]);
397 for (int i = 1; i < pGT->getDims(); i++)
400 sizeStr.append(std::to_string(piDims[i]));
403 return os_strdup(sizeStr.data());
407 std::string addDouble(double dbl)
409 #define PRECISION_DISPLAY 3
410 std::ostringstream os;
415 else if (finite(dbl))
417 os.precision(PRECISION_DISPLAY);
418 os << dbl; // Convert the double to string
434 std::string formatMatrix(int nbRows, int nbCols, double *pdblReal, double *pdblImg)
437 if (nbRows * nbCols == 1)
439 std::ostringstream os;
440 std::string formatted = addDouble(pdblReal[0]);
443 double img = pdblImg[0];
446 formatted += PLUS_STR;
450 formatted += MINUS_STR;
452 formatted += addDouble(std::fabs(img));
458 std::string formatted = LBRACKET_STR;
459 for (j = 0 ; j < nbRows ; j++)
461 for (i = 0 ; i < nbCols ; i++)
463 /* Display the formatted matrix ... the way the user
465 formatted += addDouble(pdblReal[i * nbRows + j]);
468 double img = pdblImg[i * nbRows + j];
471 formatted += PLUS_STR;
475 formatted += MINUS_STR;
478 formatted += addDouble(std::fabs(img));
483 if (i + 1 != nbCols) // Not the last element of the matrix
485 formatted += COMMA_STR;
488 if (j + 1 != nbRows) // Not the last line of the matrix
490 formatted += SEMICOLON_STR;
493 return formatted + "]";