summaryrefslogtreecommitdiff
path: root/modules/ui_data/src/cpp/BrowseVarManager.cpp
blob: 9879bfd3f962f3fe8e4cf5bfe8bc07d72ed07691 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/*
 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
 * Copyright (C) 2010 - DIGITEO - Allan CORNET
 * Copyright (C) 2010 - DIGITEO - Bruno JOFRET
 *
 * This file must be used under the terms of the CeCILL.
 * This source file is licensed as described in the file COPYING, which
 * you should have received as part of this distribution.  The terms
 * are also available at
 * http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
 *
 */

#include <iostream>
#include "BrowseVar.hxx"

#include <sstream>
#include <string>
#include <iterator>
using std::string;

#include <set>

extern "C"
{
#include <string.h>
#include "BrowseVarManager.h"
#include "localization.h"
#include "MALLOC.h"
#include "BOOL.h"
#include "stackinfo.h"
#include "api_scilab.h"
#include "getScilabJavaVM.h"
#include "Scierror.h"
#include "freeArrayOfString.h"
#include "sci_types.h"
#ifdef _MSC_VER
#include "strdup_windows.h"
#endif
}
using namespace org_scilab_modules_ui_data;

static std::set < string > createScilabDefaultVariablesSet();
static char * getListName(char * variableName);
static std::string formatMatrix(int nbRows, int nbCols, BOOL isComplex, double *pdblReal, double *pdblImg);
static char * valueToDisplay(char * variableName, int variableType, int nbRows, int nbCols);
void OpenBrowseVar()
{
    BrowseVar::openVariableBrowser(getScilabJavaVM());
    SetBrowseVarData();
}

void UpdateBrowseVar()
{
    if (BrowseVar::isVariableBrowserOpened(getScilabJavaVM()))
    {
        SetBrowseVarData();
    }
}

void SetBrowseVarData()
{
    SciErr err;
    int iGlobalVariablesUsed = 0;
    int iGlobalVariablesTotal = 0;
    int iLocalVariablesUsed = 0;
    int iLocalVariablesTotal = 0;
    int i = 0;

    // First get how many global / local variable we have.
    C2F(getvariablesinfo) (&iLocalVariablesTotal, &iLocalVariablesUsed);
    C2F(getgvariablesinfo) (&iGlobalVariablesTotal, &iGlobalVariablesUsed);

    char **pstAllVariableNames = (char **)MALLOC((iLocalVariablesUsed + iGlobalVariablesUsed) * sizeof(char *));
    char **pstAllVariableVisibility = (char **)MALLOC((iLocalVariablesUsed + iGlobalVariablesUsed) * sizeof(char *));
    char **pstAllVariableListTypes = (char **)MALLOC((iLocalVariablesUsed + iGlobalVariablesUsed) * sizeof(char *));
    int *piAllVariableBytes = (int *)MALLOC((iLocalVariablesUsed + iGlobalVariablesUsed) * sizeof(int));
    char **pstAllVariableSizes = (char **)MALLOC((iLocalVariablesUsed + iGlobalVariablesUsed) * sizeof(char *));
    int *piAllVariableTypes = (int *)MALLOC((iLocalVariablesUsed + iGlobalVariablesUsed) * sizeof(int));
    int *piAllVariableIntegerTypes = (int *)MALLOC((iLocalVariablesUsed + iGlobalVariablesUsed) * sizeof(int));
    bool *piAllVariableFromUser = (bool *) MALLOC((iLocalVariablesUsed + iGlobalVariablesUsed) * sizeof(bool));
    /* Necessary for the plots in the var browser */
    int *piAllVariableNbRows = (int *)MALLOC((iLocalVariablesUsed + iGlobalVariablesUsed) * sizeof(int));
    int *piAllVariableNbCols = (int *)MALLOC((iLocalVariablesUsed + iGlobalVariablesUsed) * sizeof(int));

    int nbRows, nbCols;
    char *sizeStr = NULL;

    std::set < string > scilabDefaultVariablesSet = createScilabDefaultVariablesSet();

    // for each local variable get information
    for (; i < iLocalVariablesUsed; ++i)
    {
        // name
        pstAllVariableNames[i] = getLocalNamefromId(i + 1);
        // type
        err = getNamedVarType(pvApiCtx, pstAllVariableNames[i], &piAllVariableTypes[i]);
        if (!err.iErr)
        {
            piAllVariableBytes[i] = getLocalSizefromId(i);
            err = getNamedVarDimension(pvApiCtx, pstAllVariableNames[i], &nbRows, &nbCols);
        }

        if (err.iErr || nbRows * nbCols == 0)
        {
#define N_A "N/A"
            pstAllVariableSizes[i] = (char *)MALLOC((sizeof(N_A) + 1) * sizeof(char));
            strcpy(pstAllVariableSizes[i], N_A);
        }
        else
        {
            pstAllVariableSizes[i] = valueToDisplay(pstAllVariableNames[i], piAllVariableTypes[i], nbRows, nbCols);
            piAllVariableNbRows[i] = nbRows;
            piAllVariableNbCols[i] = nbCols;
        }


        if (piAllVariableTypes[i] == sci_ints)
        {
            // Integer case
            int iPrec       = 0;
            err = getNamedMatrixOfIntegerPrecision(pvApiCtx, pstAllVariableNames[i], &iPrec);
            switch (iPrec)
            {
                case SCI_INT8:
                    piAllVariableIntegerTypes[i] = 8;
                    break;
                case SCI_INT16:
                    piAllVariableIntegerTypes[i] = 16;
                    break;
                case SCI_INT32:
                    piAllVariableIntegerTypes[i] = 32;
                    break;
#ifdef __SCILAB_INT64__
                case SCI_INT64:
                    piAllVariableIntegerTypes[i] = 64;
                    break;
#endif
                default:
                    piAllVariableIntegerTypes[i] = 0; // Should never occurs
                    break;
            }
        }
        else
        {
            piAllVariableIntegerTypes[i] = -1;
        }

        if (piAllVariableTypes[i] == sci_tlist || piAllVariableTypes[i] == sci_mlist)
        {
            pstAllVariableListTypes[i] = getListName(pstAllVariableNames[i]);
        }
        else
        {
            pstAllVariableListTypes[i] = strdup("");
        }


        // global / local ??
        pstAllVariableVisibility[i] = strdup("local");

        if (scilabDefaultVariablesSet.find(string(pstAllVariableNames[i])) == scilabDefaultVariablesSet.end() && piAllVariableTypes[i] != sci_lib)
        {
            piAllVariableFromUser[i] = TRUE;
        }
        else
        {
            piAllVariableFromUser[i] = FALSE;
        }
    }

    // for each global variable get information
    for (int j = 0; j < iGlobalVariablesUsed; ++j, ++i)
    {
        // name
        pstAllVariableNames[i] = getGlobalNamefromId(j);
        // Bytes used - 8 is the number of bytes in a word
        piAllVariableBytes[i] = getGlobalSizefromId(j) * 8;
        // type
        // Calling "API Scilab": not yet implemented for global variable
        //getNamedVarType(pvApiCtx, pstAllVariableNames[i], &piAllVariableTypes[i]);
        // Using old stack operations...
        int pos = C2F(vstk).isiz + 2 + j;

        piAllVariableTypes[i] = C2F(gettype) (&pos);

        // Sizes of the variable
        getNamedVarDimension(pvApiCtx, pstAllVariableNames[i], &nbRows, &nbCols);
        pstAllVariableSizes[i] = valueToDisplay(pstAllVariableNames[i], piAllVariableTypes[i], nbRows, nbCols);
        piAllVariableNbRows[i] = nbRows;
        piAllVariableNbCols[i] = nbCols;


        // global / local ??
        pstAllVariableVisibility[i] = strdup("global");


        if (piAllVariableTypes[i] == sci_tlist || piAllVariableTypes[i] == sci_mlist)
        {
            pstAllVariableListTypes[i] = getListName(pstAllVariableNames[i]);
        }
        else
        {
            pstAllVariableListTypes[i] = strdup("");
        }


        if (scilabDefaultVariablesSet.find(string(pstAllVariableNames[i])) == scilabDefaultVariablesSet.end()
                && piAllVariableTypes[i] != sci_c_function && piAllVariableTypes[i] != sci_lib)
        {
            piAllVariableFromUser[i] = TRUE;
        }
        else
        {
            piAllVariableFromUser[i] = FALSE;
        }
    }

    // Launch Java Variable Browser through JNI
    BrowseVar::setVariableBrowserData(getScilabJavaVM(),
                                      pstAllVariableNames, iLocalVariablesUsed + iGlobalVariablesUsed,
                                      piAllVariableBytes, iLocalVariablesUsed + iGlobalVariablesUsed,
                                      piAllVariableTypes, iLocalVariablesUsed + iGlobalVariablesUsed,
                                      piAllVariableIntegerTypes, iLocalVariablesUsed + iGlobalVariablesUsed,
                                      pstAllVariableListTypes, iLocalVariablesUsed + iGlobalVariablesUsed,
                                      pstAllVariableSizes, iLocalVariablesUsed + iGlobalVariablesUsed,
                                      piAllVariableNbRows, iLocalVariablesUsed + iGlobalVariablesUsed,
                                      piAllVariableNbCols, iLocalVariablesUsed + iGlobalVariablesUsed,
                                      pstAllVariableVisibility, iLocalVariablesUsed + iGlobalVariablesUsed,
                                      piAllVariableFromUser, iLocalVariablesUsed + iGlobalVariablesUsed);

    freeArrayOfString(pstAllVariableNames, iLocalVariablesUsed + iGlobalVariablesUsed);
    freeArrayOfString(pstAllVariableVisibility, iLocalVariablesUsed + iGlobalVariablesUsed);
    freeArrayOfString(pstAllVariableSizes, iLocalVariablesUsed + iGlobalVariablesUsed);
    freeArrayOfString(pstAllVariableListTypes, iLocalVariablesUsed + iGlobalVariablesUsed);

    if (piAllVariableFromUser)
    {
        FREE(piAllVariableFromUser);
        piAllVariableFromUser = NULL;
    }

    if (piAllVariableBytes)
    {
        FREE(piAllVariableBytes);
        piAllVariableBytes = NULL;
    }

    if (piAllVariableTypes)
    {
        FREE(piAllVariableTypes);
        piAllVariableTypes = NULL;
    }

    if (piAllVariableIntegerTypes)
    {
        FREE(piAllVariableIntegerTypes);
        piAllVariableIntegerTypes = NULL;
    }

    if (piAllVariableNbRows)
    {
        FREE(piAllVariableNbRows);
        piAllVariableNbRows = NULL;
    }

    if (piAllVariableNbCols)
    {
        FREE(piAllVariableNbCols);
        piAllVariableNbCols = NULL;
    }
}

/*--------------------------------------------------------------------------*/
static std::set < string > createScilabDefaultVariablesSet()
{
    string arr[] = { "home",
                     "PWD",
                     "%tk",
                     "%pvm",
                     "MSDOS",
                     "%F",
                     "%T",
                     "%f",
                     "%t",
                     "%e",
                     "%pi",
                     "%modalWarning",
                     "%nan",
                     "%inf",
                     "SCI",
                     "WSCI",
                     "SCIHOME",
                     "TMPDIR",
                     "%gui",
                     "%fftw",
                     "%helps",
                     "%eps",
                     "%io",
                     "%i",
                     "demolist",
                     "%z",
                     "%s",
                     "$",
                     "%toolboxes",
                     "%toolboxes_dir",
                     "TICTOC",
                     "%helps_modules",
                     "%_atoms_cache",
                     "evoid", // Constant for external object
                     "jvoid", // Constant for external object Java (jims)
                     "jnull", // Constant for external object Java (jims)
                     "enull"  // Constant for external object
                   };
    int i = 0;

#define NBELEMENT 37
    std::set < string > ScilabDefaultVariables;

    for (i = 0; i < NBELEMENT; i++)
    {
        ScilabDefaultVariables.insert(arr[i]);
    }

    return ScilabDefaultVariables;
}

static char * getListName(char * variableName)
{
    SciErr sciErr;
    int *piAddr = NULL;
    int* piAddr1 = NULL;
    int iRows = 0;
    int iCols = 0;
    char **pstType;
    char *tmpChar;
    sciErr = getVarAddressFromName(pvApiCtx, variableName, &piAddr);
    if (sciErr.iErr)
    {
        return strdup("");
    }

    sciErr = getListItemAddress(pvApiCtx, piAddr, 1, &piAddr1);
    if (sciErr.iErr)
    {
        return strdup("");
    }

    if (getAllocatedMatrixOfString(pvApiCtx, piAddr1, &iRows, &iCols, &pstType))
    {

        return strdup("");
    }
    tmpChar = strdup(pstType[0]);
    freeAllocatedMatrixOfString(iRows, iCols, pstType);
    return tmpChar;
}

static char * valueToDisplay(char * variableName, int variableType, int nbRows, int nbCols)
{
    SciErr err;


    // 4 is the dimension max to which display the content
    if (nbRows * nbCols <= 4 && variableType == sci_matrix)
    {
        // Small double value, display it
        double* pdblReal = (double *)malloc(((nbRows) * (nbCols)) * sizeof(double));
        double* pdblImg = (double *)malloc(((nbRows) * (nbCols)) * sizeof(double));
        BOOL isComplex = FALSE;

        if (isNamedVarComplex(pvApiCtx, variableName))
        {
            err = readNamedComplexMatrixOfDouble(pvApiCtx, variableName, &nbRows, &nbCols, pdblReal, pdblImg);
            isComplex = TRUE;
        }
        else
        {
            err = readNamedMatrixOfDouble(pvApiCtx, variableName, &nbRows, &nbCols, pdblReal);
        }


        return strdup(formatMatrix(nbRows, nbCols, isComplex, pdblReal, pdblImg).c_str());
    }
    else
    {
        char *sizeStr = NULL;
        // 11 =strlen("2147483647")+1 (1 for security)
        sizeStr = (char *)MALLOC((11 + 11 + 1 + 1) * sizeof(char));
        sprintf(sizeStr, "%dx%d", nbRows, nbCols);
        return sizeStr;
    }
}

std::string formatMatrix(int nbRows, int nbCols, BOOL isComplex, double *pdblReal, double *pdblImg)
{
    int i, j ;
#define PRECISION_DISPLAY 3
    if (nbRows * nbCols == 1)
    {
        std::ostringstream os;
        os.precision(PRECISION_DISPLAY);
        os << pdblReal[0]; // Convert the double to string
        if (isComplex)
        {
            os << " + " << pdblImg[0] << "i";
        }
        return os.str();
    }

    std::string formated = "[";
    for (j = 0 ; j < nbRows ; j++)
    {
        for (i = 0 ; i < nbCols ; i++)
        {
            /* Display the formated matrix ... the way the user
             * expect */
            std::ostringstream os;
            os.precision(PRECISION_DISPLAY);
            os << pdblReal[i * nbRows + j]; // Convert the double to string
            formated += os.str();
            if (isComplex)
            {
                std::ostringstream osComplex;
                osComplex.precision(PRECISION_DISPLAY);
                osComplex << pdblImg[i * nbRows + j];
                formated += " + " + osComplex.str() + "i";
            }


            if (i + 1 != nbCols) // Not the last element of the matrix
            {
                formated += ", ";
            }
        }
        if (j + 1 != nbRows) // Not the last line of the matrix
        {
            formated += "; ";
        }
    }
    return formated + "]";
}