ec3c5b414271e86a773702c0d74830fbc42704c2
[scilab.git] / scilab / modules / scicos_blocks / src / cpp / affich2.cpp
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2009 - DIGITEO - Antoine ELIAS
4  * Copyright (C) 2010 - DIGITEO - ClĂ©ment DAVID
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.1-en.txt
11  */
12
13 #include <math.h>
14 #include <stdio.h>
15 #include <string.h>
16
17 #include "Helpers.hxx"
18
19 extern "C"
20 {
21 #include "machine.h"
22 #include "dynlib_scicos_blocks.h"
23 #include "MALLOC.h"
24 #include "scicos_block4.h"
25 #include "scicos.h"
26 #include "core_math.h"
27 #ifdef _MSC_VER
28 #include "strdup_windows.h"
29 #endif
30
31     double C2F(sciround) (double *x);
32     SCICOS_BLOCKS_IMPEXP void affich2(scicos_block * block, int flag);
33 }
34 /*--------------------------------------------------------------------------*/
35 using namespace org_scilab_modules_xcos_block;
36
37 /*--------------------------------------------------------------------------*/
38 SCICOS_BLOCKS_IMPEXP void affich2(scicos_block * block, int flag)
39 {
40     int i;
41     int j;
42     int iRowsIn = 0;
43     int iColsIn = 0;
44     double *pdblReal = NULL;
45     char ***pstValue = NULL;
46     char pstConv[128];
47
48     iRowsIn = GetInPortRows(block, 1);
49     iColsIn = GetInPortCols(block, 1);
50
51     pdblReal = (double *)GetInPortPtrs(block, 1);
52
53     //functions
54     switch (flag)
55     {
56         case StateUpdate:          //state evolution
57         case ReInitialization:
58             // Getting the allocated area
59             pstValue = (char ***)block->work[0];
60
61             for (i = 0; i < iRowsIn; i++)
62             {
63                 for (j = 0; j < iColsIn; j++)
64                 {
65                     int iDigit = GetIparPtrs(block)[3];
66                     int iPrec = GetIparPtrs(block)[4];
67
68                     double dblScale = pow((double)10, iPrec);
69                     double dblTemp = pdblReal[i + (j * iRowsIn)] * dblScale;
70                     double dblValue = C2F(sciround) (&dblTemp) / dblScale;
71                     char pstFormat[10];
72
73 #if _MSC_VER
74                     //"%0.2f"
75                     sprintf_s(pstFormat, 10, "%%%d.%df", iDigit, iPrec);
76                     sprintf_s(pstConv, 128, pstFormat, dblValue);
77 #else
78                     sprintf(pstFormat, "%%%d.%df", iDigit, iPrec);
79                     sprintf(pstConv, pstFormat, dblValue);
80 #endif
81                     pstValue[i][j] = strdup(pstConv);
82                 }
83             }
84
85             AfficheBlock_setValue(block->label, pstValue, iRowsIn, iColsIn);
86
87             break;
88
89         case Initialization:       //init
90             pstValue = (char ***)MALLOC(sizeof(char **) * iRowsIn);
91
92             for (i = 0; i < iRowsIn; i++)
93             {
94                 pstValue[i] = (char **)MALLOC(sizeof(char *) * iColsIn);
95
96                 for (j = 0; j < iColsIn; j++)
97                 {
98 #if _MSC_VER
99                     sprintf_s(pstConv, 128, "%0.2f", 0.0);
100 #else
101                     sprintf(pstConv, "%0.2f", 0.0);
102 #endif
103                     pstValue[i][j] = strdup(pstConv);
104                 }
105             }
106
107             if (AfficheBlock_setValue(block->label, pstValue, iRowsIn, iColsIn) == 0)
108             {
109                 // storing the allocated area on the block work field.
110                 block->work[0] = pstValue;
111             }
112             else
113             {
114                 for (i = 0; i < iRowsIn; i++)
115                 {
116                     FREE(pstValue[i]);
117                 }
118                 FREE(pstValue);
119             }
120             break;
121
122         case Ending:
123             // Getting the allocated area
124             pstValue = (char ***)block->work[0];
125
126             for (i = 0; i < iRowsIn; i++)
127             {
128                 FREE(pstValue[i]);
129             }
130             FREE(pstValue);
131             break;
132
133         default:
134             break;
135     }
136 }
137
138 //