8edcb13549ab65564e29fcf12673efa870da3adb
[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
44 /*--------------------------------------------------------------------------*/
45 void UpdateBrowseVar(BOOL update)
46 {
47     SciErr err;
48     int iGlobalVariablesUsed = 0;
49     int iGlobalVariablesTotal = 0;
50     int iLocalVariablesUsed = 0;
51     int iLocalVariablesTotal = 0;
52     int i = 0;
53
54     if (update && !BrowseVar::isVariableBrowserOpened(getScilabJavaVM()))
55     {
56         return;
57     }
58
59     // First get how many global / local variable we have.
60     C2F(getvariablesinfo) (&iLocalVariablesTotal, &iLocalVariablesUsed);
61     C2F(getgvariablesinfo) (&iGlobalVariablesTotal, &iGlobalVariablesUsed);
62
63     char **pstAllVariableNames = (char **)MALLOC((iLocalVariablesUsed + iGlobalVariablesUsed) * sizeof(char *));
64     char **pstAllVariableVisibility = (char **)MALLOC((iLocalVariablesUsed + iGlobalVariablesUsed) * sizeof(char *));
65     int *piAllVariableBytes = (int *)MALLOC((iLocalVariablesUsed + iGlobalVariablesUsed) * sizeof(int));
66     char **pstAllVariableSizes = (char **)MALLOC((iLocalVariablesUsed + iGlobalVariablesUsed) * sizeof(char *));
67     int *piAllVariableTypes = (int *)MALLOC((iLocalVariablesUsed + iGlobalVariablesUsed) * sizeof(int));
68     int *piAllVariableIntegerTypes = (int *)MALLOC((iLocalVariablesUsed + iGlobalVariablesUsed) * sizeof(int));
69     bool *piAllVariableFromUser = (bool *) MALLOC((iLocalVariablesUsed + iGlobalVariablesUsed) * sizeof(bool));
70     int nbRows, nbCols;
71     char *sizeStr = NULL;
72
73     std::set < string > scilabDefaultVariablesSet = createScilabDefaultVariablesSet();
74
75     // for each local variable get information
76     for (; i < iLocalVariablesUsed; ++i)
77     {
78         // name
79         pstAllVariableNames[i] = getLocalNamefromId(i + 1);
80         // type
81         err = getNamedVarType(pvApiCtx, pstAllVariableNames[i], &piAllVariableTypes[i]);
82         if (!err.iErr)
83         {
84             piAllVariableBytes[i] = getLocalSizefromId(i);
85             err = getNamedVarDimension(pvApiCtx, pstAllVariableNames[i], &nbRows, &nbCols);
86         }
87
88         if (err.iErr || nbRows * nbCols == 0)
89         {
90 #define N_A "N/A"
91             pstAllVariableSizes[i] = (char *)MALLOC((sizeof(N_A) + 1) * sizeof(char));
92             strcpy(pstAllVariableSizes[i], N_A);
93         }
94         else
95         {
96             // 11 =strlen("2147483647")+1 (1 for security)
97             sizeStr = (char *)MALLOC((11 + 11 + 1 + 1) * sizeof(char));
98             sprintf(sizeStr, "%dx%d", nbRows, nbCols);
99             pstAllVariableSizes[i] = strdup(sizeStr);
100             FREE(sizeStr);
101         }
102
103
104         if (piAllVariableTypes[i] == sci_ints)
105         {
106             // Integer case
107             int iPrec       = 0;
108             err = getNamedMatrixOfIntegerPrecision(pvApiCtx, pstAllVariableNames[i], &iPrec);
109             switch (iPrec)
110             {
111                 case SCI_INT8:
112                     piAllVariableIntegerTypes[i] = 8;
113                     break;
114                 case SCI_INT16:
115                     piAllVariableIntegerTypes[i] = 16;
116                     break;
117                 case SCI_INT32:
118                     piAllVariableIntegerTypes[i] = 32;
119                     break;
120 #ifdef __SCILAB_INT64__
121                 case SCI_INT64:
122                     piAllVariableIntegerTypes[i] = 64;
123                     break;
124 #endif
125                 default:
126                     piAllVariableIntegerTypes[i] = 0; // Should never occurs
127                     break;
128             }
129         }
130         else
131         {
132             piAllVariableIntegerTypes[i] = -1;
133         }
134
135         // global / local ??
136         pstAllVariableVisibility[i] = strdup("local");
137
138         if (scilabDefaultVariablesSet.find(string(pstAllVariableNames[i])) == scilabDefaultVariablesSet.end() && piAllVariableTypes[i] != sci_lib)
139         {
140             piAllVariableFromUser[i] = TRUE;
141         }
142         else
143         {
144             piAllVariableFromUser[i] = FALSE;
145         }
146     }
147
148     // for each global variable get information
149     for (int j = 0; j < iGlobalVariablesUsed; ++j, ++i)
150     {
151         // name
152         pstAllVariableNames[i] = getGlobalNamefromId(j);
153         // Bytes used
154         piAllVariableBytes[i] = getGlobalSizefromId(j);
155         // type
156         // Calling "API Scilab": not yet implemented for global variable
157         //getNamedVarType(pvApiCtx, pstAllVariableNames[i], &piAllVariableTypes[i]);
158         // Using old stack operations...
159         int pos = C2F(vstk).isiz + 2 + j;
160
161         piAllVariableTypes[i] = C2F(gettype) (&pos);
162
163         // Sizes of the variable
164         getNamedVarDimension(pvApiCtx, pstAllVariableNames[i], &nbRows, &nbCols);
165         // 11 =strlen("2147483647")+1 (1 for security)
166         sizeStr = (char *)MALLOC((11 + 11 + 1 + 1) * sizeof(char));
167         sprintf(sizeStr, "%dx%d", nbRows, nbCols);
168         pstAllVariableSizes[i] = strdup(sizeStr);
169         FREE(sizeStr);
170
171         // global / local ??
172         pstAllVariableVisibility[i] = strdup("global");
173
174         if (scilabDefaultVariablesSet.find(string(pstAllVariableNames[i])) == scilabDefaultVariablesSet.end()
175                 && piAllVariableTypes[i] != sci_c_function && piAllVariableTypes[i] != sci_lib)
176         {
177             piAllVariableFromUser[i] = TRUE;
178         }
179         else
180         {
181             piAllVariableFromUser[i] = FALSE;
182         }
183     }
184
185     // Launch Java Variable Browser through JNI
186     BrowseVar::openVariableBrowser(getScilabJavaVM(),
187                                    BOOLtobool(update),
188                                    pstAllVariableNames, iLocalVariablesUsed + iGlobalVariablesUsed,
189                                    piAllVariableBytes, iLocalVariablesUsed + iGlobalVariablesUsed,
190                                    piAllVariableTypes, iLocalVariablesUsed + iGlobalVariablesUsed,
191                                    piAllVariableIntegerTypes, iLocalVariablesUsed + iGlobalVariablesUsed,
192                                    pstAllVariableSizes, iLocalVariablesUsed + iGlobalVariablesUsed,
193                                    pstAllVariableVisibility, iLocalVariablesUsed + iGlobalVariablesUsed,
194                                    piAllVariableFromUser, iLocalVariablesUsed + iGlobalVariablesUsed);
195
196     freeArrayOfString(pstAllVariableNames, iLocalVariablesUsed + iGlobalVariablesUsed);
197     freeArrayOfString(pstAllVariableVisibility, iLocalVariablesUsed + iGlobalVariablesUsed);
198
199     if (piAllVariableFromUser)
200     {
201         FREE(piAllVariableFromUser);
202         piAllVariableFromUser = NULL;
203     }
204
205     if (piAllVariableBytes)
206     {
207         FREE(piAllVariableBytes);
208         piAllVariableBytes = NULL;
209     }
210
211     if (piAllVariableTypes)
212     {
213         FREE(piAllVariableTypes);
214         piAllVariableTypes = NULL;
215     }
216
217     if (piAllVariableIntegerTypes)
218     {
219         FREE(piAllVariableIntegerTypes);
220         piAllVariableIntegerTypes = NULL;
221     }
222
223     if (pstAllVariableSizes)
224     {
225         FREE(pstAllVariableSizes);
226         pstAllVariableSizes = NULL;
227     }
228 }
229
230 /*--------------------------------------------------------------------------*/
231 static std::set < string > createScilabDefaultVariablesSet()
232 {
233     string arr[] = { "home",
234                      "PWD",
235                      "%tk",
236                      "%pvm",
237                      "MSDOS",
238                      "%F",
239                      "%T",
240                      "%f",
241                      "%t",
242                      "%e",
243                      "%pi",
244                      "%modalWarning",
245                      "%nan",
246                      "%inf",
247                      "SCI",
248                      "WSCI",
249                      "SCIHOME",
250                      "TMPDIR",
251                      "%gui",
252                      "%fftw",
253                      "%helps",
254                      "%eps",
255                      "%io",
256                      "%i",
257                      "demolist",
258                      "%z",
259                      "%s",
260                      "$",
261                      "%toolboxes",
262                      "%toolboxes_dir",
263                      "TICTOC",
264                      "%helps_modules",
265                      "%_atoms_cache",
266                      "evoid" // Constant for external object
267                    };
268     int i = 0;
269
270 #define NBELEMENT 33
271     std::set < string > ScilabDefaultVariables;
272
273     for (i = 0; i <= NBELEMENT; i++)
274     {
275         ScilabDefaultVariables.insert(arr[i]);
276     }
277
278     return ScilabDefaultVariables;
279 }