Fix bug 4812 : xs2... function doesn't work if the first argument is a handle
[scilab.git] / scilab / modules / graphic_export / src / c / xs2file.c
1 /*
2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2006 - INRIA - Fabrice Leray
4 * Copyright (C) 2006 - INRIA - Jean-Baptiste Silvy
5 * Copyright (C) 2009 - DIGITEO - Allan CORNET
6 * desc : interface for xs2file routine
7
8 * This file must be used under the terms of the CeCILL.
9 * This source file is licensed as described in the file COPYING, which
10 * you should have received as part of this distribution.  The terms
11 * are also available at    
12 * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
13 *
14 */
15
16 #include <string.h>
17 #include <stdio.h>
18
19 #include "xs2file.h"
20 #include "stack-c.h"
21 #include "GetProperty.h"
22 #include "WindowList.h"
23 #include "IsAScalar.h"
24 #include "CurrentObjectsManagement.h"
25 #include "localization.h"
26 #include "SetJavaProperty.h"
27 #include "Scierror.h"
28 #include "expandPathVariable.h"
29 #include "PATH_MAX.h"
30 #include "MALLOC.h"
31 #include "freeArrayOfString.h"
32 /*--------------------------------------------------------------------------*/
33 static BOOL isVectorialExport(ExportFileType fileType);
34 /*--------------------------------------------------------------------------*/
35 int xs2file(char * fname, ExportFileType fileType )
36 {
37         /* Check input and output sizes */
38         CheckLhs(0,1);
39         if (isVectorialExport(fileType))
40         {
41                 CheckRhs(2,3);
42         }
43         else
44         {
45                 CheckRhs(2,2);
46         }
47
48         if (GetType(1) != sci_matrix && GetType(1) != sci_handles)
49         {
50                 Scierror(999,_("%s: Wrong type for input argument #%d: An integer or a handle expected.\n"),fname, 1);
51           LhsVar(1) = 0;
52           C2F(putlhsvar)();
53                 return 0;
54         }
55
56         if ( (GetType(2) == sci_strings) )
57         {       
58                 char **fileName = NULL;
59                 char *real_filename = NULL;
60                 ExportOrientation orientation = EXPORT_PORTRAIT; /* default orientation */
61                 long int lout = 0;
62                 int out_n = 0;
63                 int m1 = 0, n1 = 0, l1 = 0;
64                 int figurenum = -1;
65     sciPointObj* figurePtr = NULL;
66                 int status = 0;
67
68                 /* get handle by figure number */
69     if(GetType(1) == sci_matrix)
70     {
71                 GetRhsVar(1,MATRIX_OF_INTEGER_DATATYPE,&m1,&n1,&l1);
72       if(m1*n1 != 1)
73       {
74         Scierror(999,_("%s: Wrong size for input argument #%d: A scalar expected.\n"),fname, 1);        
75                           LhsVar(1) = 0;
76                           C2F(putlhsvar)();
77                           return 0;
78       }
79
80                 figurenum = *istk(l1);
81                   if (!sciIsExistingFigure(figurenum))
82                   {
83                           Scierror(999, "%s: Input argument #%d must be a valid figure_id.\n",fname, 1);
84                           LhsVar(1) = 0;
85                           C2F(putlhsvar)();
86                           return 0;
87                   }
88       figurePtr = getFigureFromIndex(figurenum);
89     }
90     /* check given handle */
91     if(GetType(1) == sci_handles)
92     {
93       GetRhsVar(1,GRAPHICAL_HANDLE_DATATYPE,&m1,&n1,&l1);
94       if(m1*n1 != 1)
95       {
96         Scierror(999,_("%s: Wrong size for input argument #%d: A graphic handle expected.\n"),fname, 1);        
97                           LhsVar(1) = 0;
98                           C2F(putlhsvar)();
99                           return 0;
100       }
101       figurePtr = sciGetPointerFromHandle(getHandleFromStack(l1));
102
103       if(figurePtr == NULL)
104       {
105                           Scierror(999, "%s: Input argument #%d must be a valid handle.\n",fname, 1);
106                           LhsVar(1) = 0;
107                           C2F(putlhsvar)();
108                           return 0;        
109       }
110       startFigureDataReading(figurePtr);
111       if(sciGetEntityType(figurePtr)!=SCI_FIGURE)
112       {
113                           Scierror(999, "%s: Input argument #%d must be a handle on a figure.\n", fname, 1);
114                           LhsVar(1) = 0;
115                           C2F(putlhsvar)();
116                           return 0;        
117       }
118       endFigureDataReading(figurePtr);
119     }
120
121                 /* get file name */
122                 GetRhsVar(2,MATRIX_OF_STRING_DATATYPE,&m1,&n1,&fileName);
123                 if (m1*n1 == 1)
124                 {
125                         if (Rhs == 3)
126                         {
127                                 int nbCol = 0;
128                                 int nbRow = 0;
129                                 char **sciOrientation = NULL;
130
131                                 if (GetType(3) != sci_strings)
132                                 {
133                                         freeArrayOfString(fileName,m1*n1);
134                                         Scierror(999,_("%s: Wrong type for input argument #%d: Single character string expected.\n"),fname, 3);
135                                         return 0;
136                                 }
137
138                                 GetRhsVar(3,MATRIX_OF_STRING_DATATYPE,&nbRow,&nbCol,&sciOrientation);
139                                 if (nbRow*nbCol == 1)
140                                 {
141                                         /* Value should be 'landscape' or 'portrait' but check only the first character */
142                                         /* for compatibility with Scilab 4*/
143                                         if (strcmp(sciOrientation[0], "landscape") == 0 || strcmp(sciOrientation[0], "l") == 0)
144                                         {
145                                                 freeArrayOfString(sciOrientation,nbRow*nbCol);
146                                                 orientation = EXPORT_LANDSCAPE;
147                                         }
148                                         else if(strcmp(sciOrientation[0], "portrait") == 0 || strcmp(sciOrientation[0], "p") == 0)
149                                         {
150                                                 freeArrayOfString(sciOrientation,nbRow*nbCol);
151                                                 orientation = EXPORT_PORTRAIT;
152                                         }
153                                         else
154                                         {
155                                                 freeArrayOfString(fileName,m1*n1);
156                                                 freeArrayOfString(sciOrientation,nbRow*nbCol);
157                                                 Scierror(999,_("%s: Wrong value for input argument #%d: '%s' or '%s' expected.\n"),fname, 3, "portrait", "landscape");
158                                                 return 0;
159                                         }
160                                 }
161                                 else
162                                 {
163                                         freeArrayOfString(fileName,m1*n1);
164                                         freeArrayOfString(sciOrientation,nbRow*nbCol);
165                                         Scierror(999,_("%s: Wrong size for input argument #%d: Single character string expected.\n"),fname, 3);
166                                         return 0;
167                                 }
168                         }
169
170                         /* Replaces SCI, ~, HOME, TMPDIR by the real path */
171                         real_filename = expandPathVariable(fileName[0]);
172
173                         /* Call the function for exporting file */
174                         status = exportToFile(figurePtr, real_filename, fileType, orientation);
175
176                         /* free pointers no more used */
177                         if (real_filename){FREE(real_filename);real_filename = NULL;}
178                         freeArrayOfString(fileName,m1*n1);
179
180                         /* treat errors */
181                         switch(status)
182                         {
183                         case EXPORT_UNKNOWN_GLEXCEPTION_ERROR :
184                                 Scierror(999,_("%s: OpenGL error during export.\n"),fname);
185                                 return 0;
186                         case EXPORT_IOEXCEPTION_ERROR :
187                                 Scierror(999,_("%s: Unable to create export file, permission denied.\n"),fname);
188                                 return 0;
189                         case EXPORT_INVALID_FILE :
190                                 Scierror(999,_("%s: Unable to create export file, permission denied.\n"),fname);
191                                 return 0;
192                         case EXPORT_GL2PS_ERROR :
193                                 Scierror(999,_("%s: GL2PS error during export.\n"),fname);
194                                 return 0;
195                         case EXPORT_GL2PS_OVERFLOW :
196                                 Scierror(999,_("%s: Unable to create export file, figure is too complex.\n"),fname);
197                                 return 0;
198                         case EXPORT_GL2PS_UNINITIALIZED :
199                                 Scierror(999,_("%s: GL2PS error during export.\n"),fname);
200                                 return 0;
201                         default :
202                                 // NO ERROR
203                                 break;
204                         }
205                 }
206                 else
207                 {
208                         freeArrayOfString(fileName,m1*n1);
209                         Scierror(999,_("%s: Wrong size for input argument #%d: Single character string expected.\n"),fname, 2);
210                         return 0;
211                 }
212         }
213         else
214         {
215                 Scierror(999,_("%s: Wrong type for input argument #%d: Single character string expected.\n"),fname, 2);
216                 return 0;
217         }
218
219         LhsVar(1) = 0;
220         C2F(putlhsvar)();
221         return 0;
222 }
223 /*--------------------------------------------------------------------------*/
224 static BOOL isVectorialExport(ExportFileType fileType)
225 {
226         return   fileType == EPS_EXPORT
227                 || fileType == PS_EXPORT
228                 || fileType == PDF_EXPORT
229                 || fileType == SVG_EXPORT;
230 }
231 /*--------------------------------------------------------------------------*/
232