The variable browser also shows the user type of a mlist
[scilab.git] / scilab / modules / ui_data / src / cpp / UpdateBrowseVar.cpp
1 /*
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
5  *
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
11  *
12  */
13
14 #include <iostream>
15 #include "BrowseVar.hxx"
16
17 #include <string>
18 #include <iterator>
19 using std::string;
20
21 #include <set>
22
23 extern "C"
24 {
25 #include <string.h>
26 #include "UpdateBrowseVar.h"
27 #include "localization.h"
28 #include "MALLOC.h"
29 #include "BOOL.h"
30 #include "stackinfo.h"
31 #include "api_scilab.h"
32 #include "getScilabJavaVM.h"
33 #include "Scierror.h"
34 #include "freeArrayOfString.h"
35 #include "sci_types.h"
36 #ifdef _MSC_VER
37 #include "strdup_windows.h"
38 #endif
39 }
40 using namespace org_scilab_modules_ui_data;
41
42 static std::set < string > createScilabDefaultVariablesSet();
43 static char * getListName(char * variableName);
44
45 /*--------------------------------------------------------------------------*/
46 void UpdateBrowseVar(BOOL update)
47 {
48     SciErr err;
49     int iGlobalVariablesUsed = 0;
50     int iGlobalVariablesTotal = 0;
51     int iLocalVariablesUsed = 0;
52     int iLocalVariablesTotal = 0;
53     int i = 0;
54
55     if (update && !BrowseVar::isVariableBrowserOpened(getScilabJavaVM()))
56     {
57         return;
58     }
59
60     // First get how many global / local variable we have.
61     C2F(getvariablesinfo) (&iLocalVariablesTotal, &iLocalVariablesUsed);
62     C2F(getgvariablesinfo) (&iGlobalVariablesTotal, &iGlobalVariablesUsed);
63
64     char **pstAllVariableNames = (char **)MALLOC((iLocalVariablesUsed + iGlobalVariablesUsed) * sizeof(char *));
65     char **pstAllVariableVisibility = (char **)MALLOC((iLocalVariablesUsed + iGlobalVariablesUsed) * sizeof(char *));
66     char **pstAllVariableListTypes = (char **)MALLOC((iLocalVariablesUsed + iGlobalVariablesUsed) * sizeof(char *));
67     int *piAllVariableBytes = (int *)MALLOC((iLocalVariablesUsed + iGlobalVariablesUsed) * sizeof(int));
68     char **pstAllVariableSizes = (char **)MALLOC((iLocalVariablesUsed + iGlobalVariablesUsed) * sizeof(char *));
69     int *piAllVariableTypes = (int *)MALLOC((iLocalVariablesUsed + iGlobalVariablesUsed) * sizeof(int));
70     int *piAllVariableIntegerTypes = (int *)MALLOC((iLocalVariablesUsed + iGlobalVariablesUsed) * sizeof(int));
71     bool *piAllVariableFromUser = (bool *) MALLOC((iLocalVariablesUsed + iGlobalVariablesUsed) * sizeof(bool));
72     int nbRows, nbCols;
73     char *sizeStr = NULL;
74
75     std::set < string > scilabDefaultVariablesSet = createScilabDefaultVariablesSet();
76
77     // for each local variable get information
78     for (; i < iLocalVariablesUsed; ++i)
79     {
80         // name
81         pstAllVariableNames[i] = getLocalNamefromId(i + 1);
82         // type
83         err = getNamedVarType(pvApiCtx, pstAllVariableNames[i], &piAllVariableTypes[i]);
84         if (!err.iErr)
85         {
86             piAllVariableBytes[i] = getLocalSizefromId(i);
87             err = getNamedVarDimension(pvApiCtx, pstAllVariableNames[i], &nbRows, &nbCols);
88         }
89
90         if (err.iErr || nbRows * nbCols == 0)
91         {
92 #define N_A "N/A"
93             pstAllVariableSizes[i] = (char *)MALLOC((sizeof(N_A) + 1) * sizeof(char));
94             strcpy(pstAllVariableSizes[i], N_A);
95         }
96         else
97         {
98             // 11 =strlen("2147483647")+1 (1 for security)
99             sizeStr = (char *)MALLOC((11 + 11 + 1 + 1) * sizeof(char));
100             sprintf(sizeStr, "%dx%d", nbRows, nbCols);
101             pstAllVariableSizes[i] = strdup(sizeStr);
102             FREE(sizeStr);
103         }
104
105
106         if (piAllVariableTypes[i] == sci_ints)
107         {
108             // Integer case
109             int iPrec       = 0;
110             err = getNamedMatrixOfIntegerPrecision(pvApiCtx, pstAllVariableNames[i], &iPrec);
111             switch (iPrec)
112             {
113                 case SCI_INT8:
114                     piAllVariableIntegerTypes[i] = 8;
115                     break;
116                 case SCI_INT16:
117                     piAllVariableIntegerTypes[i] = 16;
118                     break;
119                 case SCI_INT32:
120                     piAllVariableIntegerTypes[i] = 32;
121                     break;
122 #ifdef __SCILAB_INT64__
123                 case SCI_INT64:
124                     piAllVariableIntegerTypes[i] = 64;
125                     break;
126 #endif
127                 default:
128                     piAllVariableIntegerTypes[i] = 0; // Should never occurs
129                     break;
130             }
131         }
132         else
133         {
134             piAllVariableIntegerTypes[i] = -1;
135         }
136
137         if (piAllVariableTypes[i] == sci_tlist || piAllVariableTypes[i] == sci_mlist)
138         {
139             pstAllVariableListTypes[i] = getListName(pstAllVariableNames[i]);
140         }
141         else
142         {
143             pstAllVariableListTypes[i] = strdup("");
144         }
145
146
147         // global / local ??
148         pstAllVariableVisibility[i] = strdup("local");
149
150         if (scilabDefaultVariablesSet.find(string(pstAllVariableNames[i])) == scilabDefaultVariablesSet.end() && piAllVariableTypes[i] != sci_lib)
151         {
152             piAllVariableFromUser[i] = TRUE;
153         }
154         else
155         {
156             piAllVariableFromUser[i] = FALSE;
157         }
158     }
159
160     // for each global variable get information
161     for (int j = 0; j < iGlobalVariablesUsed; ++j, ++i)
162     {
163         // name
164         pstAllVariableNames[i] = getGlobalNamefromId(j);
165         // Bytes used - 8 is the number of bytes in a word
166         piAllVariableBytes[i] = getGlobalSizefromId(j) * 8;
167         // type
168         // Calling "API Scilab": not yet implemented for global variable
169         //getNamedVarType(pvApiCtx, pstAllVariableNames[i], &piAllVariableTypes[i]);
170         // Using old stack operations...
171         int pos = C2F(vstk).isiz + 2 + j;
172
173         piAllVariableTypes[i] = C2F(gettype) (&pos);
174
175         // Sizes of the variable
176         getNamedVarDimension(pvApiCtx, pstAllVariableNames[i], &nbRows, &nbCols);
177         // 11 =strlen("2147483647")+1 (1 for security)
178         sizeStr = (char *)MALLOC((11 + 11 + 1 + 1) * sizeof(char));
179         sprintf(sizeStr, "%dx%d", nbRows, nbCols);
180         pstAllVariableSizes[i] = strdup(sizeStr);
181         FREE(sizeStr);
182
183         // global / local ??
184         pstAllVariableVisibility[i] = strdup("global");
185
186
187         if (piAllVariableTypes[i] == sci_tlist || piAllVariableTypes[i] == sci_mlist)
188         {
189             pstAllVariableListTypes[i] = getListName(pstAllVariableNames[i]);
190         }
191         else
192         {
193             pstAllVariableListTypes[i] = strdup("");
194         }
195
196
197         if (scilabDefaultVariablesSet.find(string(pstAllVariableNames[i])) == scilabDefaultVariablesSet.end()
198                 && piAllVariableTypes[i] != sci_c_function && piAllVariableTypes[i] != sci_lib)
199         {
200             piAllVariableFromUser[i] = TRUE;
201         }
202         else
203         {
204             piAllVariableFromUser[i] = FALSE;
205         }
206     }
207
208     // Launch Java Variable Browser through JNI
209     BrowseVar::openVariableBrowser(getScilabJavaVM(),
210                                    BOOLtobool(update),
211                                    pstAllVariableNames, iLocalVariablesUsed + iGlobalVariablesUsed,
212                                    piAllVariableBytes, iLocalVariablesUsed + iGlobalVariablesUsed,
213                                    piAllVariableTypes, iLocalVariablesUsed + iGlobalVariablesUsed,
214                                    piAllVariableIntegerTypes, iLocalVariablesUsed + iGlobalVariablesUsed,
215                                    pstAllVariableListTypes, iLocalVariablesUsed + iGlobalVariablesUsed,
216                                    pstAllVariableSizes, iLocalVariablesUsed + iGlobalVariablesUsed,
217                                    pstAllVariableVisibility, iLocalVariablesUsed + iGlobalVariablesUsed,
218                                    piAllVariableFromUser, iLocalVariablesUsed + iGlobalVariablesUsed);
219
220     freeArrayOfString(pstAllVariableNames, iLocalVariablesUsed + iGlobalVariablesUsed);
221     freeArrayOfString(pstAllVariableVisibility, iLocalVariablesUsed + iGlobalVariablesUsed);
222     freeArrayOfString(pstAllVariableSizes, iLocalVariablesUsed + iGlobalVariablesUsed);
223     freeArrayOfString(pstAllVariableListTypes, iLocalVariablesUsed + iGlobalVariablesUsed);
224
225     if (piAllVariableFromUser)
226     {
227         FREE(piAllVariableFromUser);
228         piAllVariableFromUser = NULL;
229     }
230
231     if (piAllVariableBytes)
232     {
233         FREE(piAllVariableBytes);
234         piAllVariableBytes = NULL;
235     }
236
237     if (piAllVariableTypes)
238     {
239         FREE(piAllVariableTypes);
240         piAllVariableTypes = NULL;
241     }
242
243     if (piAllVariableIntegerTypes)
244     {
245         FREE(piAllVariableIntegerTypes);
246         piAllVariableIntegerTypes = NULL;
247     }
248
249 }
250
251 /*--------------------------------------------------------------------------*/
252 static std::set < string > createScilabDefaultVariablesSet()
253 {
254     string arr[] = { "home",
255                      "PWD",
256                      "%tk",
257                      "%pvm",
258                      "MSDOS",
259                      "%F",
260                      "%T",
261                      "%f",
262                      "%t",
263                      "%e",
264                      "%pi",
265                      "%modalWarning",
266                      "%nan",
267                      "%inf",
268                      "SCI",
269                      "WSCI",
270                      "SCIHOME",
271                      "TMPDIR",
272                      "%gui",
273                      "%fftw",
274                      "%helps",
275                      "%eps",
276                      "%io",
277                      "%i",
278                      "demolist",
279                      "%z",
280                      "%s",
281                      "$",
282                      "%toolboxes",
283                      "%toolboxes_dir",
284                      "TICTOC",
285                      "%helps_modules",
286                      "%_atoms_cache",
287                      "evoid" // Constant for external object
288                    };
289     int i = 0;
290
291 #define NBELEMENT 33
292     std::set < string > ScilabDefaultVariables;
293
294     for (i = 0; i <= NBELEMENT; i++)
295     {
296         ScilabDefaultVariables.insert(arr[i]);
297     }
298
299     return ScilabDefaultVariables;
300 }
301
302 static char * getListName(char * variableName)
303 {
304     SciErr sciErr;
305     int *piAddr = NULL;
306     int* piAddr1 = NULL;
307     int iRows = 0;
308     int iCols = 0;
309     char **pstType;
310     char *tmpChar;
311     sciErr = getVarAddressFromName(pvApiCtx, variableName, &piAddr);
312     if (sciErr.iErr)
313     {
314         return strdup("");
315     }
316
317     sciErr = getListItemAddress(pvApiCtx, piAddr, 1, &piAddr1);
318     if (sciErr.iErr)
319     {
320         return strdup("");
321     }
322
323     if (getAllocatedMatrixOfString(pvApiCtx, piAddr1, &iRows, &iCols, &pstType))
324     {
325
326         return strdup("");
327     }
328     tmpChar = strdup(pstType[0]);
329     freeAllocatedMatrixOfString(iRows, iCols, pstType);
330     return tmpChar;
331 }