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