Remove useless variables.
[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     bool *piAllVariableFromUser = (bool *) MALLOC((iLocalVariablesUsed + iGlobalVariablesUsed) * sizeof(BOOL));
69     int nbRows, nbCols;
70     char *sizeStr = NULL;
71
72     std::set < string > scilabDefaultVariablesSet = createScilabDefaultVariablesSet();
73
74     // for each local variable get informations
75     for (; i < iLocalVariablesUsed; ++i)
76     {
77         // name
78         pstAllVariableNames[i] = getLocalNamefromId(i + 1);
79         // type
80         err = getNamedVarType(pvApiCtx, pstAllVariableNames[i], &piAllVariableTypes[i]);
81         if (!err.iErr)
82         {
83             piAllVariableBytes[i] = getLocalSizefromId(i);
84             err = getNamedVarDimension(pvApiCtx, pstAllVariableNames[i], &nbRows, &nbCols);
85         }
86
87         if (err.iErr || nbRows * nbCols == 0)
88         {
89 #define N_A "N/A"
90             pstAllVariableSizes[i] = (char *)MALLOC((sizeof(N_A) + 1) * sizeof(char));
91             strcpy(pstAllVariableSizes[i], N_A);
92         }
93         else
94         {
95             // 11 =strlen("2147483647")+1 (1 for security)
96             sizeStr = (char *)MALLOC((11 + 11 + 1 + 1) * sizeof(char));
97             sprintf(sizeStr, "%dx%d", nbRows, nbCols);
98             pstAllVariableSizes[i] = strdup(sizeStr);
99             FREE(sizeStr);
100         }
101
102         // global / local ??
103         pstAllVariableVisibility[i] = strdup("local");
104
105         if (scilabDefaultVariablesSet.find(string(pstAllVariableNames[i])) == scilabDefaultVariablesSet.end() && piAllVariableTypes[i] != sci_c_function    /*TODO: voir si je fais sauter ou pas */
106                 && piAllVariableTypes[i] != sci_lib)
107         {
108             piAllVariableFromUser[i] = TRUE;
109         }
110         else
111         {
112             piAllVariableFromUser[i] = FALSE;
113         }
114     }
115
116     // for each global variable get informations
117     for (int j = 0; j < iGlobalVariablesUsed; ++j, ++i)
118     {
119         // name
120         pstAllVariableNames[i] = getGlobalNamefromId(j);
121         // Bytes used
122         piAllVariableBytes[i] = getGlobalSizefromId(j);
123         // type
124         // Calling "API Scilab": not yet implemented for global variable
125         //getNamedVarType(pvApiCtx, pstAllVariableNames[i], &piAllVariableTypes[i]);
126         // Using old stack operations...
127         int pos = C2F(vstk).isiz + 2 + j;
128
129         piAllVariableTypes[i] = C2F(gettype) (&pos);
130
131         // Sizes of the variable
132         getNamedVarDimension(pvApiCtx, pstAllVariableNames[i], &nbRows, &nbCols);
133         // 11 =strlen("2147483647")+1 (1 for security)
134         sizeStr = (char *)MALLOC((11 + 11 + 1 + 1) * sizeof(char));
135         sprintf(sizeStr, "%dx%d", nbRows, nbCols);
136         pstAllVariableSizes[i] = strdup(sizeStr);
137         FREE(sizeStr);
138
139         // global / local ??
140         pstAllVariableVisibility[i] = strdup("global");
141
142         if (scilabDefaultVariablesSet.find(string(pstAllVariableNames[i])) == scilabDefaultVariablesSet.end()
143                 && piAllVariableTypes[i] != sci_c_function && piAllVariableTypes[i] != sci_lib)
144         {
145             piAllVariableFromUser[i] = TRUE;
146         }
147         else
148         {
149             piAllVariableFromUser[i] = FALSE;
150         }
151     }
152
153     // Launch Java Variable Browser through JNI
154     BrowseVar::openVariableBrowser(getScilabJavaVM(),
155                                    BOOLtobool(update),
156                                    pstAllVariableNames, iLocalVariablesUsed + iGlobalVariablesUsed,
157                                    piAllVariableBytes, iLocalVariablesUsed + iGlobalVariablesUsed,
158                                    piAllVariableTypes, iLocalVariablesUsed + iGlobalVariablesUsed,
159                                    pstAllVariableSizes, iLocalVariablesUsed + iGlobalVariablesUsed,
160                                    pstAllVariableVisibility, iLocalVariablesUsed + iGlobalVariablesUsed,
161                                    piAllVariableFromUser, iLocalVariablesUsed + iGlobalVariablesUsed);
162
163     freeArrayOfString(pstAllVariableNames, iLocalVariablesUsed + iGlobalVariablesUsed);
164     freeArrayOfString(pstAllVariableVisibility, iLocalVariablesUsed + iGlobalVariablesUsed);
165     if (piAllVariableBytes)
166     {
167         FREE(piAllVariableBytes);
168         piAllVariableBytes = NULL;
169     }
170
171     if (piAllVariableTypes)
172     {
173         FREE(piAllVariableTypes);
174         piAllVariableTypes = NULL;
175     }
176
177     if (pstAllVariableSizes)
178     {
179         FREE(pstAllVariableSizes);
180         pstAllVariableSizes = NULL;
181     }
182 }
183
184 /*--------------------------------------------------------------------------*/
185 static std::set < string > createScilabDefaultVariablesSet()
186 {
187     string arr[] = { "home",
188                      "PWD",
189                      "%tk",
190                      "%pvm",
191                      "MSDOS",
192                      "%F",
193                      "%T",
194                      "%f",
195                      "%t",
196                      "%e",
197                      "%pi",
198                      "%modalWarning",
199                      "%nan",
200                      "%inf",
201                      "SCI",
202                      "WSCI",
203                      "SCIHOME",
204                      "TMPDIR",
205                      "%gui",
206                      "%fftw",
207                      "%helps",
208                      "%eps",
209                      "%io",
210                      "%i",
211                      "demolist",
212                      "%z",
213                      "%s",
214                      "$",
215                      "%toolboxes",
216                      "%toolboxes_dir",
217                      "TICTOC",
218                      "%helps_modules"
219                    };
220     int i = 0;
221
222 #define NBELEMENT 31
223     std::set < string > ScilabDefaultVariables;
224
225     for (i = 0; i <= NBELEMENT; i++)
226     {
227         ScilabDefaultVariables.insert(arr[i]);
228     }
229
230     return ScilabDefaultVariables;
231 }