Fix direct-casting BOOL / int or BOOL / bool
[scilab.git] / scilab / modules / gui / sci_gateway / cpp / sci_toprint.cpp
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) INRIA - Allan CORNET
4  * Copyright (C) 2008 - INRIA - Vincent COUVERT (Java version)
5  * Copyright (C) 2011 - DIGITEO - Allan CORNET
6  *
7  * This file must be used under the terms of the CeCILL.
8  * This source file is licensed as described in the file COPYING, which
9  * you should have received as part of this distribution.  The terms
10  * are also available at
11  * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
12  *
13 */
14
15 /*--------------------------------------------------------------------------*/
16 #include "CallScilabBridge.hxx"
17 extern "C"
18 {
19 #include "api_scilab.h"
20 #include "stack-c.h"
21 #include "MALLOC.h"
22 #include "sciprint.h"
23 #include "Scierror.h"
24 #include "FileExist.h"
25 #include "freeArrayOfString.h"
26 #include "localization.h"
27 #include "gw_gui.h"
28 #include "getScilabJavaVM.h"
29 #include "getFullFilename.h"
30 #include "loadOnUseClassPath.h"
31 #include "WindowList.h"
32 #include "warningmode.h"
33 }
34 /*--------------------------------------------------------------------------*/
35 static BOOL loadedDep = FALSE;
36
37 /*--------------------------------------------------------------------------*/
38 using namespace org_scilab_modules_gui_bridge;
39
40 /*--------------------------------------------------------------------------*/
41 static int sci_toprint_one_rhs(const char *fname);
42 static int sci_toprint_two_rhs(const char *fname);
43
44 /*--------------------------------------------------------------------------*/
45 int sci_toprint(char *fname, unsigned long l)
46 {
47     CheckRhs(1, 2);
48     CheckLhs(0, 1);
49
50     if (!loadedDep)
51     {
52         loadOnUseClassPath("pdf_ps_eps_graphic_export");
53         loadedDep = TRUE;
54     }
55
56     if (Rhs == 1)
57     {
58         return sci_toprint_one_rhs(fname);
59     }
60     else
61     {
62         return sci_toprint_two_rhs(fname);
63     }
64     return 0;
65 }
66
67 /*--------------------------------------------------------------------------*/
68 static int sci_toprint_one_rhs(const char *fname)
69 {
70     SciErr sciErr;
71     int *piAddressVarOne = NULL;
72
73     sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddressVarOne);
74     if (sciErr.iErr)
75     {
76         printError(&sciErr, 0);
77         return 0;
78     }
79
80     if (isStringType(pvApiCtx, piAddressVarOne))
81     {
82         char *fileName = NULL;
83         BOOL iRet = FALSE;
84
85         if (getAllocatedSingleString(pvApiCtx, piAddressVarOne, &fileName) == 0)
86         {
87             char *fullName = getFullFilename(fileName);
88
89             freeAllocatedSingleString(fileName);
90             if (fullName)
91             {
92                 if (FileExist(fullName))
93                 {
94                     try
95                     {
96                         iRet = booltoBOOL(CallScilabBridge::printFile(getScilabJavaVM(), fullName));
97                     }
98                     catch(const GiwsException::JniException & e)
99                     {
100                         FREE(fullName);
101                         fullName = NULL;
102                         Scierror(999, _("%s: An exception occurred: %s\n%s\n"), fname, e.getJavaDescription().c_str(),
103                                  e.getJavaExceptionName().c_str());
104                         return 0;
105                     }
106                 }
107                 else
108                 {
109                     if (getWarningMode())
110                     {
111                         sciprint(_("%s: The file %s does not exist.\n"), fname, fullName);
112                     }
113                     iRet = FALSE;
114                 }
115                 FREE(fullName);
116                 fullName = NULL;
117             }
118
119             createScalarBoolean(pvApiCtx, Rhs + 1, iRet);
120             LhsVar(1) = Rhs + 1;
121             PutLhsVar();
122         }
123         else
124         {
125             Scierror(999, _("%s: Memory allocation error.\n"), fname);
126         }
127     }
128     else if (isDoubleType(pvApiCtx, piAddressVarOne))
129     {
130         if (isScalar(pvApiCtx, piAddressVarOne))
131         {
132             double dValue = 0.;
133
134             if (!getScalarDouble(pvApiCtx, piAddressVarOne, &dValue))
135             {
136                 int num_win = (int)dValue;
137                 BOOL iRet = FALSE;
138
139                 if (num_win < 0)
140                 {
141                     Scierror(999, _("%s: Wrong value for input argument #%d: Non-negative integers expected.\n"), fname);
142                     return 0;
143                 }
144
145                 if (!sciIsExistingFigure((int)num_win))
146                 {
147                     Scierror(999, "%s: Figure with figure_id %d does not exist.\n", fname, (int)num_win);
148                     return 0;
149                 }
150
151                 try
152                 {
153                     iRet = booltoBOOL(CallScilabBridge::printFigure(getScilabJavaVM(), num_win, FALSE, FALSE));
154                 }
155                 catch(const GiwsException::JniException & e)
156                 {
157                     Scierror(999, _("%s: An exception occurred: %s\n%s\n"), fname, e.getJavaDescription().c_str(), e.getJavaExceptionName().c_str());
158                     return 0;
159                 }
160
161                 createScalarBoolean(pvApiCtx, Rhs + 1, iRet);
162                 LhsVar(1) = Rhs + 1;
163                 PutLhsVar();
164             }
165             else
166             {
167                 Scierror(999, _("%s: Memory allocation error.\n"), fname);
168             }
169         }
170         else
171         {
172             Scierror(999, _("%s: Wrong size for input argument #%d: Non-negative integer expected.\n"), fname, 1);
173         }
174     }
175     else
176     {
177         Scierror(999, _("%s: Wrong type for input argument #%d.\n"), fname, 1);
178     }
179     return 0;
180 }
181
182 /*--------------------------------------------------------------------------*/
183 static int sci_toprint_two_rhs(const char *fname)
184 {
185     SciErr sciErr;
186     int *piAddressVarOne = NULL;
187     int *piAddressVarTwo = NULL;
188
189     sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddressVarOne);
190     if (sciErr.iErr)
191     {
192         printError(&sciErr, 0);
193         return 0;
194     }
195
196     sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddressVarTwo);
197     if (sciErr.iErr)
198     {
199         printError(&sciErr, 0);
200         return 0;
201     }
202
203     if (isStringType(pvApiCtx, piAddressVarOne))
204     {
205         if (isScalar(pvApiCtx, piAddressVarTwo))
206         {
207             if (isStringType(pvApiCtx, piAddressVarTwo))
208             {
209                 char *pageHeader = NULL;
210                 char **pStVarOne = NULL;
211                 int *lenStVarOne = NULL;
212                 int mOne = 0, nOne = 0;
213                 int mnOne = 0;
214                 int lenLineToPrint = 0;
215                 int i = 0;
216                 char *lines = NULL;
217
218                 sciErr = getMatrixOfString(pvApiCtx, piAddressVarOne, &mOne, &nOne, NULL, NULL);
219                 if (sciErr.iErr)
220                 {
221                     printError(&sciErr, 0);
222                     return 0;
223                 }
224
225                 if (!((mOne == 1) || (nOne == 1)))
226                 {
227                     Scierror(999, _("%s: Wrong size for input argument #%d: A 1-by-n or m-by-1 array expected.\n"), fname, 1);
228                     return 0;
229                 }
230
231                 mnOne = mOne * nOne;
232
233                 lenStVarOne = (int *)MALLOC(sizeof(int) * mnOne);
234                 if (lenStVarOne == NULL)
235                 {
236                     Scierror(999, _("%s: No more memory.\n"), fname);
237                     return 0;
238                 }
239
240                 sciErr = getMatrixOfString(pvApiCtx, piAddressVarOne, &mOne, &nOne, lenStVarOne, NULL);
241                 if (sciErr.iErr)
242                 {
243                     printError(&sciErr, 0);
244                     return 0;
245                 }
246
247                 for (i = 0; i < mnOne; i++)
248                 {
249                     lenLineToPrint = lenLineToPrint + lenStVarOne[i] + (int)strlen("\n") + 1;
250                 }
251
252                 pStVarOne = (char **)MALLOC(sizeof(char *) * mnOne);
253                 if (pStVarOne == NULL)
254                 {
255                     FREE(lenStVarOne);
256                     lenStVarOne = NULL;
257                     Scierror(999, _("%s: No more memory.\n"), fname);
258                     return 0;
259                 }
260
261                 for (i = 0; i < mnOne; i++)
262                 {
263                     pStVarOne[i] = (char *)MALLOC(sizeof(char) * (lenStVarOne[i] + 1));
264                     if (pStVarOne[i] == NULL)
265                     {
266                         freeArrayOfString(pStVarOne, i);
267                         if (lenStVarOne)
268                         {
269                             FREE(lenStVarOne);
270                             lenStVarOne = NULL;
271                         }
272                         Scierror(999, _("%s: No more memory.\n"), fname);
273                         return 0;
274                     }
275                 }
276
277                 sciErr = getMatrixOfString(pvApiCtx, piAddressVarOne, &mOne, &nOne, lenStVarOne, pStVarOne);
278                 if (lenStVarOne)
279                 {
280                     FREE(lenStVarOne);
281                     lenStVarOne = NULL;
282                 }
283                 if (sciErr.iErr)
284                 {
285                     freeArrayOfString(pStVarOne, mnOne);
286                     printError(&sciErr, 0);
287                     return 0;
288                 }
289
290                 lines = (char *)MALLOC((lenLineToPrint + 1) * sizeof(char));
291                 if (lines == NULL)
292                 {
293                     freeArrayOfString(pStVarOne, mnOne);
294                     Scierror(999, _("%s: No more memory.\n"), fname);
295                     return 0;
296                 }
297
298                 for (i = 0; i < mnOne; i++)
299                 {
300                     if (i == 0)
301                     {
302                         sprintf(lines, "%s\n", pStVarOne[i]);
303                     }
304                     else
305                     {
306                         sprintf(lines, "%s%s\n", lines, pStVarOne[i]);
307                     }
308                 }
309                 freeArrayOfString(pStVarOne, mnOne);
310
311                 if (getAllocatedSingleString(pvApiCtx, piAddressVarTwo, &pageHeader) == 0)
312                 {
313                     BOOL iRet = FALSE;
314
315                     try
316                     {
317                         iRet = booltoBOOL(CallScilabBridge::printString(getScilabJavaVM(), lines, pageHeader));
318                     }
319
320                     catch(const GiwsException::JniException & e)
321                     {
322                         freeAllocatedSingleString(pageHeader);
323                         if (lines)
324                         {
325                             FREE(lines);
326                             lines = NULL;
327                         }
328                         Scierror(999, _("%s: An exception occurred: %s\n%s\n"), fname, e.getJavaDescription().c_str(),
329                                  e.getJavaExceptionName().c_str());
330                         return 0;
331                     }
332
333                     freeAllocatedSingleString(pageHeader);
334                     if (lines)
335                     {
336                         FREE(lines);
337                         lines = NULL;
338                     }
339
340                     createScalarBoolean(pvApiCtx, Rhs + 1, iRet);
341                     LhsVar(1) = Rhs + 1;
342                     PutLhsVar();
343                 }
344                 else
345                 {
346                     if (lines)
347                     {
348                         FREE(lines);
349                         lines = NULL;
350                     }
351                     Scierror(999, _("%s: Memory allocation error.\n"), fname);
352                     return 0;
353                 }
354             }
355             else
356             {
357                 Scierror(999, _("%s: Wrong type for input argument #%d: String expected.\n"), fname, 2);
358                 return 0;
359             }
360         }
361         else
362         {
363             Scierror(999, _("%s: Wrong size for input argument #%d: String expected.\n"), fname, 2);
364             return 0;
365         }
366     }
367     else if (isDoubleType(pvApiCtx, piAddressVarOne))
368     {
369         if (isScalar(pvApiCtx, piAddressVarOne))
370         {
371             int num_win = 0;
372             double dValue = 0.;
373
374             if (!getScalarDouble(pvApiCtx, piAddressVarOne, &dValue))
375             {
376                 num_win = (int)dValue;
377
378                 if (num_win < 0)
379                 {
380                     Scierror(999, _("%s: Wrong value for input argument #%d: Non-negative integers expected.\n"), fname);
381                     return 0;
382                 }
383             }
384             else
385             {
386                 Scierror(999, _("%s: Memory allocation error.\n"), fname);
387                 return 0;
388             }
389
390             if (!sciIsExistingFigure((int)num_win))
391             {
392                 Scierror(999, "%s: Figure with figure_id %d does not exist.\n", fname, (int)num_win);
393                 return 0;
394             }
395
396             if (isStringType(pvApiCtx, piAddressVarTwo))
397             {
398                 BOOL iRet = FALSE;
399
400                 if (isScalar(pvApiCtx, piAddressVarTwo))
401                 {
402                     char *outputType = NULL;
403
404                     if (getAllocatedSingleString(pvApiCtx, piAddressVarTwo, &outputType) == 0)
405                     {
406                         if ((strcmp(outputType, "pos") == 0) || (strcmp(outputType, "gdi") == 0))
407                         {
408                             try
409                             {
410                                 if (strcmp(outputType, "pos") == 0)
411                                 {
412                                     iRet = booltoBOOL(CallScilabBridge::printFigure(getScilabJavaVM(), num_win, TRUE, FALSE));
413                                 }
414                                 else
415                                 {
416                                     iRet = booltoBOOL((int)CallScilabBridge::printFigure(getScilabJavaVM(), num_win, FALSE, FALSE));
417                                 }
418                             }
419                             catch(const GiwsException::JniException & e)
420                             {
421                                 Scierror(999, _("%s: An exception occurred: %s\n%s\n"), fname, e.getJavaDescription().c_str(),
422                                          e.getJavaExceptionName().c_str());
423                                 freeAllocatedSingleString(outputType);
424                                 return 0;
425                             }
426
427                             createScalarBoolean(pvApiCtx, Rhs + 1, iRet);
428                             LhsVar(1) = Rhs + 1;
429                             PutLhsVar();
430                         }
431                         else
432                         {
433                             Scierror(999, _("%s: Wrong input argument #%d: '%s' or '%s' expected.\n"), fname, 2, "pos", "gdi");
434                         }
435                         freeAllocatedSingleString(outputType);
436                     }
437                     else
438                     {
439                         Scierror(999, _("%s: Memory allocation error.\n"), fname);
440                     }
441                 }
442                 else
443                 {
444                     Scierror(999, _("%s: Wrong size for input argument #%d: 'pos' or 'gdi' value expected.\n"), fname, 2);
445                 }
446             }
447             else
448             {
449                 Scierror(999, _("%s: Wrong type for input argument #%d.\n"), fname, 2);
450             }
451         }
452         else
453         {
454             Scierror(999, _("%s: Wrong size for input argument #%d: Non-negative integer expected.\n"), fname, 1);
455         }
456     }
457     else
458     {
459         Scierror(999, _("%s: Wrong type for input argument #%d.\n"), fname, 1);
460     }
461     return 0;
462 }
463
464 /*--------------------------------------------------------------------------*/