2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2006 - INRIA - Allan CORNET
4 * Copyright (C) 2009-2010 - DIGITEO - Allan CORNET
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-en.txt
14 /*--------------------------------------------------------------------------*/
18 #include "api_scilab.h"
19 #include "FileExist.h"
22 #include "localization.h"
23 #include "charEncoding.h"
24 #include "filesmanagement.h"
25 #include "freeArrayOfString.h"
27 #include "strdup_windows.h"
29 /*--------------------------------------------------------------------------*/
30 #define FILE_OPEN_STR "open"
31 #define FILE_OLD_STR "old"
32 /*--------------------------------------------------------------------------*/
33 extern int C2F(intfile)(); /* fortran subroutine */
34 /*--------------------------------------------------------------------------*/
35 static int sci_file_no_rhs(char *fname);
36 static int sci_file_one_rhs(char *fname);
37 /*--------------------------------------------------------------------------*/
38 int sci_file(char *fname,unsigned long fname_len)
42 return sci_file_no_rhs(fname);
47 return sci_file_one_rhs(fname);
54 int *piAddressVarOne = NULL;
55 int *piAddressVarTwo = NULL;
56 int *piAddressVarThree = NULL;
62 sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddressVarOne);
65 printError(&sciErr, 0);
66 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
70 sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddressVarTwo);
73 printError(&sciErr, 0);
74 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 2);
78 sciErr = getVarAddressFromPosition(pvApiCtx, 3, &piAddressVarThree);
81 printError(&sciErr, 0);
82 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 3);
86 sciErr = getVarType(pvApiCtx, piAddressVarOne, &iType1);
89 printError(&sciErr, 0);
90 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
94 sciErr = getVarType(pvApiCtx, piAddressVarTwo, &iType2);
97 printError(&sciErr, 0);
98 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 2);
102 sciErr = getVarType(pvApiCtx, piAddressVarThree, &iType3);
105 printError(&sciErr, 0);
106 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 3);
110 if ( (iType1 == sci_strings) && (iType2 == sci_strings) && (iType3 == sci_strings) )
112 char *pStVarOne = NULL;
115 wchar_t *pStVarTwo = NULL;
118 char *pStVarThree = NULL;
119 int lenStVarThree = 0;
125 sciErr = getVarDimension(pvApiCtx, piAddressVarOne, &m1, &n1);
128 printError(&sciErr, 0);
129 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
133 if ( (m1 != n1) && (n1 != 1) )
135 Scierror(999,_("%s: Wrong size for input argument #%d: A string expected.\n"), fname, 1);
139 sciErr = getVarDimension(pvApiCtx, piAddressVarTwo, &m2, &n2);
142 printError(&sciErr, 0);
143 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 2);
147 if ( (m2 != n2) && (n2 != 1) )
149 Scierror(999,_("%s: Wrong size for input argument #%d: A string expected.\n"), fname, 2);
153 sciErr = getVarDimension(pvApiCtx, piAddressVarThree, &m3, &n3);
156 printError(&sciErr, 0);
157 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 3);
161 if ( (m3 != n3) && (n3 != 1) )
163 Scierror(999,_("%s: Wrong size for input argument #%d: A string expected.\n"), fname, 3);
167 // get lenStVarOne value
168 sciErr = getMatrixOfString(pvApiCtx, piAddressVarOne, &m1, &n1, &lenStVarOne, NULL);
171 printError(&sciErr, 0);
172 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
176 // get value of first argument
177 pStVarOne = (char*)MALLOC(sizeof(char)*(lenStVarOne + 1));
178 if (pStVarOne == NULL)
180 Scierror(999,_("%s: Memory allocation error.\n"),fname);
184 sciErr = getMatrixOfString(pvApiCtx, piAddressVarOne, &m1, &n1, &lenStVarOne, &pStVarOne);
187 FREE(pStVarOne); pStVarOne = NULL;
188 printError(&sciErr, 0);
189 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
193 if (strcmp(pStVarOne, FILE_OPEN_STR) == 0)
195 FREE(pStVarOne); pStVarOne = NULL;
197 // get lenStVarThree value
198 sciErr = getMatrixOfString(pvApiCtx, piAddressVarThree, &m3, &n3, &lenStVarThree, NULL);
201 printError(&sciErr, 0);
202 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 3);
206 // get value of third argument
207 pStVarThree = (char*)MALLOC(sizeof(char)*(lenStVarThree + 1));
208 if (pStVarThree == NULL)
210 Scierror(999,_("%s: Memory allocation error.\n"),fname);
214 sciErr = getMatrixOfString(pvApiCtx, piAddressVarThree, &m3, &n3, &lenStVarThree, &pStVarThree);
217 FREE(pStVarThree); pStVarThree = NULL;
218 printError(&sciErr, 0);
219 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 3);
223 if (strcmp(pStVarThree, FILE_OLD_STR) == 0)
225 FREE(pStVarThree); pStVarThree = NULL;
227 // get lenStVarTwo value
228 sciErr = getMatrixOfWideString(pvApiCtx, piAddressVarTwo, &m2, &n2, &lenStVarTwo, NULL);
231 printError(&sciErr, 0);
232 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 2);
236 // get value of second argument
237 pStVarTwo = (wchar_t*)MALLOC(sizeof(wchar_t)*(lenStVarTwo + 1));
238 if (pStVarTwo == NULL)
240 Scierror(999,_("%s: Memory allocation error.\n"),fname);
244 sciErr = getMatrixOfWideString(pvApiCtx, piAddressVarTwo, &m2, &n2, &lenStVarTwo, &pStVarTwo);
247 FREE(pStVarTwo); pStVarTwo = NULL;
248 printError(&sciErr, 0);
249 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 2);
253 if (!FileExistW(pStVarTwo))
257 double dOutErrCode = 240.;
258 int m_out = 1, n_out = 1;
260 sciErr = createMatrixOfDouble(pvApiCtx, Rhs + 1, 0, 0, NULL);
263 printError(&sciErr, 0);
264 Scierror(999,_("%s: Memory allocation error.\n"), fname);
268 sciErr = createMatrixOfDouble(pvApiCtx, Rhs + 2, m_out, n_out, &dOutErrCode);
271 printError(&sciErr, 0);
272 Scierror(999,_("%s: Memory allocation error.\n"), fname);
282 char *filename = wide_string_to_UTF8(pStVarTwo);
285 Scierror(240, _("%s: The file \"%s\" does not exist.\n"),fname, filename);
291 Scierror(240, _("%s: The file does not exist.\n"),fname);
297 FREE(pStVarTwo); pStVarTwo = NULL;
300 FREE(pStVarThree); pStVarThree = NULL;
303 FREE(pStVarOne); pStVarOne = NULL;
310 /*--------------------------------------------------------------------------*/
311 static int sci_file_no_rhs(char *fname)
314 int m_out = 0, n_out = 0;
317 double *IdUsed = NULL;
322 IdUsed = GetFilesIdUsed(&sizeArray);
328 sciErr = createMatrixOfDouble(pvApiCtx, Rhs + 1, m_out, n_out, IdUsed);
334 printError(&sciErr, 0);
335 Scierror(999,_("%s: Memory allocation error.\n"), fname);
342 if (Lhs > 1) /* Types */
344 char **TypeIdsAsString = GetTypesUsedAsString(&sizeArray);
349 sciErr = createMatrixOfString(pvApiCtx, Rhs + 2, m_out, n_out, TypeIdsAsString);
350 freeArrayOfString(TypeIdsAsString, sizeArray);
354 printError(&sciErr, 0);
355 Scierror(999,_("%s: Memory allocation error.\n"), fname);
363 if (Lhs > 2) /* names */
365 char **Filenames = GetFilenamesUsed(&sizeArray);
370 sciErr = createMatrixOfString(pvApiCtx, Rhs + 3, m_out, n_out, Filenames);
371 freeArrayOfString(Filenames, sizeArray);
375 printError(&sciErr, 0);
376 Scierror(999,_("%s: Memory allocation error.\n"), fname);
384 if (Lhs > 3) /* mod */
386 double *Modes = GetModesUsed(&sizeArray);
391 sciErr = createMatrixOfDouble(pvApiCtx, Rhs + 4, m_out, n_out, Modes);
396 printError(&sciErr, 0);
397 Scierror(999,_("%s: Memory allocation error.\n"), fname);
405 if (Lhs > 4) /* swap */
407 double *SwapId = GetSwapsUsed(&sizeArray);
412 sciErr = createMatrixOfDouble(pvApiCtx, Rhs + 5, m_out, n_out, SwapId);
418 printError(&sciErr, 0);
419 Scierror(999,_("%s: Memory allocation error.\n"), fname);
430 /*--------------------------------------------------------------------------*/
431 static int sci_file_one_rhs(char *fname)
438 int *piAddressVarOne = NULL;
439 double *pdVarOne = NULL;
444 /* get Address of inputs */
445 sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddressVarOne);
448 printError(&sciErr, 0);
449 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
453 sciErr = getVarType(pvApiCtx, piAddressVarOne, &iType);
456 printError(&sciErr, 0);
457 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
461 /* check input type */
462 if ( iType != sci_matrix )
464 Scierror(201,_("%s: Wrong type for input argument #%d: A scalar expected.\n"),fname,1);
468 sciErr = getMatrixOfDouble(pvApiCtx, piAddressVarOne,&m1,&n1,&pdVarOne);
471 printError(&sciErr, 0);
472 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
476 if( n1 != 1 || m1 != 1)
478 Scierror(999,_("%s: Wrong size for input argument #%d: A scalar expected.\n"),fname,1);
482 iID = (int) *pdVarOne;
484 if (*pdVarOne != (double)iID)
486 Scierror(999,_("%s: Wrong value for input argument #%d: An integer expected.\n"),fname,1);
491 if (GetFileTypeOpenedInScilab(iID) != 0)
495 sciErr = createMatrixOfDouble(pvApiCtx, Rhs + 1, m_out, n_out, pdVarOne);
502 sciErr = createMatrixOfDouble(pvApiCtx, Rhs + 1, m_out, n_out, NULL);
507 printError(&sciErr, 0);
508 Scierror(999,_("%s: Memory allocation error.\n"), fname);
514 if (Lhs > 1) /* Type */
516 if (GetFileTypeOpenedInScilab(iID) != 0)
518 char *TypeIdAsString = GetFileTypeOpenedInScilabAsString(iID);
521 sciErr = createMatrixOfString(pvApiCtx, Rhs + 2, m_out, n_out, &TypeIdAsString);
522 FREE(TypeIdAsString);
523 TypeIdAsString = NULL;
530 sciErr = createMatrixOfDouble(pvApiCtx, Rhs + 2, m_out, n_out, NULL);
535 printError(&sciErr, 0);
536 Scierror(999,_("%s: Memory allocation error.\n"), fname);
543 if (Lhs > 2) /* name */
545 if (GetFileTypeOpenedInScilab(iID) != 0)
547 char *filename = NULL;
550 if (GetFileNameOpenedInScilab(iID) == NULL)
552 filename = strdup("");
556 filename = strdup(GetFileNameOpenedInScilab(iID));
559 sciErr = createMatrixOfString(pvApiCtx, Rhs + 3, m_out, n_out, &filename);
568 sciErr = createMatrixOfDouble(pvApiCtx, Rhs + 3, m_out, n_out, NULL);
573 printError(&sciErr, 0);
574 Scierror(999,_("%s: Memory allocation error.\n"), fname);
581 if (Lhs > 3) /* mod */
583 if (GetFileTypeOpenedInScilab(iID) != 0)
585 double ModeId = (double)GetFileModeOpenedInScilab(iID);
588 sciErr = createMatrixOfDouble(pvApiCtx, Rhs + 4, m_out, n_out, &ModeId);
595 sciErr = createMatrixOfDouble(pvApiCtx, Rhs + 4, m_out, n_out, NULL);
600 printError(&sciErr, 0);
601 Scierror(999,_("%s: Memory allocation error.\n"), fname);
608 if (Lhs > 4) /* swap */
610 if (GetFileTypeOpenedInScilab(iID) != 0)
612 double SwapId = (double)GetSwapStatus(iID);
615 sciErr = createMatrixOfDouble(pvApiCtx, Rhs + 5, m_out, n_out, &SwapId);
622 sciErr = createMatrixOfDouble(pvApiCtx, Rhs + 5, m_out, n_out, NULL);
627 printError(&sciErr, 0);
628 Scierror(999,_("%s: Memory allocation error.\n"), fname);
637 /*--------------------------------------------------------------------------*/