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);
69 sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddressVarTwo);
72 printError(&sciErr, 0);
76 sciErr = getVarAddressFromPosition(pvApiCtx, 3, &piAddressVarThree);
79 printError(&sciErr, 0);
83 sciErr = getVarType(pvApiCtx, piAddressVarOne, &iType1);
86 printError(&sciErr, 0);
90 sciErr = getVarType(pvApiCtx, piAddressVarTwo, &iType2);
93 printError(&sciErr, 0);
97 sciErr = getVarType(pvApiCtx, piAddressVarThree, &iType3);
100 printError(&sciErr, 0);
104 if ( (iType1 == sci_strings) && (iType2 == sci_strings) && (iType3 == sci_strings) )
106 char *pStVarOne = NULL;
109 wchar_t *pStVarTwo = NULL;
112 char *pStVarThree = NULL;
113 int lenStVarThree = 0;
119 sciErr = getVarDimension(pvApiCtx, piAddressVarOne, &m1, &n1);
122 printError(&sciErr, 0);
126 if ( (m1 != n1) && (n1 != 1) )
128 Scierror(999,_("%s: Wrong size for input argument #%d: A string expected.\n"), fname, 1);
132 sciErr = getVarDimension(pvApiCtx, piAddressVarTwo, &m2, &n2);
135 printError(&sciErr, 0);
139 if ( (m2 != n2) && (n2 != 1) )
141 Scierror(999,_("%s: Wrong size for input argument #%d: A string expected.\n"), fname, 2);
145 sciErr = getVarDimension(pvApiCtx, piAddressVarThree, &m3, &n3);
148 printError(&sciErr, 0);
152 if ( (m3 != n3) && (n3 != 1) )
154 Scierror(999,_("%s: Wrong size for input argument #%d: A string expected.\n"), fname, 3);
158 // get lenStVarOne value
159 sciErr = getMatrixOfString(pvApiCtx, piAddressVarOne, &m1, &n1, &lenStVarOne, NULL);
162 printError(&sciErr, 0);
166 // get value of first argument
167 pStVarOne = (char*)MALLOC(sizeof(char)*(lenStVarOne + 1));
168 if (pStVarOne == NULL)
170 Scierror(999,_("%s: Memory allocation error.\n"),fname);
174 sciErr = getMatrixOfString(pvApiCtx, piAddressVarOne, &m1, &n1, &lenStVarOne, &pStVarOne);
177 FREE(pStVarOne); pStVarOne = NULL;
178 printError(&sciErr, 0);
182 if (strcmp(pStVarOne, FILE_OPEN_STR) == 0)
184 FREE(pStVarOne); pStVarOne = NULL;
186 // get lenStVarThree value
187 sciErr = getMatrixOfString(pvApiCtx, piAddressVarThree, &m3, &n3, &lenStVarThree, NULL);
190 printError(&sciErr, 0);
194 // get value of third argument
195 pStVarThree = (char*)MALLOC(sizeof(char)*(lenStVarThree + 1));
196 if (pStVarThree == NULL)
198 Scierror(999,_("%s: Memory allocation error.\n"),fname);
202 sciErr = getMatrixOfString(pvApiCtx, piAddressVarThree, &m3, &n3, &lenStVarThree, &pStVarThree);
205 FREE(pStVarThree); pStVarThree = NULL;
206 printError(&sciErr, 0);
210 if (strcmp(pStVarThree, FILE_OLD_STR) == 0)
212 FREE(pStVarThree); pStVarThree = NULL;
214 // get lenStVarTwo value
215 sciErr = getMatrixOfWideString(pvApiCtx, piAddressVarTwo, &m2, &n2, &lenStVarTwo, NULL);
218 printError(&sciErr, 0);
222 // get value of second argument
223 pStVarTwo = (wchar_t*)MALLOC(sizeof(wchar_t)*(lenStVarTwo + 1));
224 if (pStVarTwo == NULL)
226 Scierror(999,_("%s: Memory allocation error.\n"),fname);
230 sciErr = getMatrixOfWideString(pvApiCtx, piAddressVarTwo, &m2, &n2, &lenStVarTwo, &pStVarTwo);
233 FREE(pStVarTwo); pStVarTwo = NULL;
234 printError(&sciErr, 0);
238 if (!FileExistW(pStVarTwo))
242 double dOutErrCode = 240.;
243 int m_out = 1, n_out = 1;
245 sciErr = createMatrixOfDouble(pvApiCtx, Rhs + 1, 0, 0, NULL);
248 printError(&sciErr, 0);
252 sciErr = createMatrixOfDouble(pvApiCtx, Rhs + 2, m_out, n_out, &dOutErrCode);
255 printError(&sciErr, 0);
265 char *filename = wide_string_to_UTF8(pStVarTwo);
268 Scierror(240, _("%s: The file \"%s\" does not exist.\n"),fname, filename);
274 Scierror(240, _("%s: The file does not exist.\n"),fname);
280 FREE(pStVarTwo); pStVarTwo = NULL;
283 FREE(pStVarThree); pStVarThree = NULL;
286 FREE(pStVarOne); pStVarOne = NULL;
293 /*--------------------------------------------------------------------------*/
294 static int sci_file_no_rhs(char *fname)
297 int m_out = 0, n_out = 0;
300 double *IdUsed = NULL;
305 IdUsed = GetFilesIdUsed(&sizeArray);
311 sciErr = createMatrixOfDouble(pvApiCtx, Rhs + 1, m_out, n_out, IdUsed);
317 printError(&sciErr, 0);
324 if (Lhs > 1) /* Types */
326 char **TypeIdsAsString = GetTypesUsedAsString(&sizeArray);
331 sciErr = createMatrixOfString(pvApiCtx, Rhs + 2, m_out, n_out, TypeIdsAsString);
332 freeArrayOfString(TypeIdsAsString, sizeArray);
336 printError(&sciErr, 0);
344 if (Lhs > 2) /* names */
346 char **Filenames = GetFilenamesUsed(&sizeArray);
351 sciErr = createMatrixOfString(pvApiCtx, Rhs + 3, m_out, n_out, Filenames);
352 freeArrayOfString(Filenames, sizeArray);
356 printError(&sciErr, 0);
364 if (Lhs > 3) /* mod */
366 double *Modes = GetModesUsed(&sizeArray);
371 sciErr = createMatrixOfDouble(pvApiCtx, Rhs + 4, m_out, n_out, Modes);
376 printError(&sciErr, 0);
384 if (Lhs > 4) /* swap */
386 double *SwapId = GetSwapsUsed(&sizeArray);
391 sciErr = createMatrixOfDouble(pvApiCtx, Rhs + 5, m_out, n_out, SwapId);
397 printError(&sciErr, 0);
408 /*--------------------------------------------------------------------------*/
409 static int sci_file_one_rhs(char *fname)
416 int *piAddressVarOne = NULL;
417 double *pdVarOne = NULL;
422 /* get Address of inputs */
423 sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddressVarOne);
426 printError(&sciErr, 0);
430 sciErr = getVarType(pvApiCtx, piAddressVarOne, &iType);
433 printError(&sciErr, 0);
437 /* check input type */
438 if ( iType != sci_matrix )
440 Scierror(201,_("%s: Wrong type for input argument #%d: A scalar expected.\n"),fname,1);
444 sciErr = getMatrixOfDouble(pvApiCtx, piAddressVarOne,&m1,&n1,&pdVarOne);
447 printError(&sciErr, 0);
451 if( n1 != 1 || m1 != 1)
453 Scierror(999,_("%s: Wrong size for input argument #%d: A scalar expected.\n"),fname,1);
457 iID = (int) *pdVarOne;
459 if (*pdVarOne != (double)iID)
461 Scierror(999,_("%s: Wrong value for input argument #%d: An integer expected.\n"),fname,1);
466 if (GetFileTypeOpenedInScilab(iID) != 0)
470 sciErr = createMatrixOfDouble(pvApiCtx, Rhs + 1, m_out, n_out, pdVarOne);
477 sciErr = createMatrixOfDouble(pvApiCtx, Rhs + 1, m_out, n_out, NULL);
482 printError(&sciErr, 0);
488 if (Lhs > 1) /* Type */
490 if (GetFileTypeOpenedInScilab(iID) != 0)
492 char *TypeIdAsString = GetFileTypeOpenedInScilabAsString(iID);
495 sciErr = createMatrixOfString(pvApiCtx, Rhs + 2, m_out, n_out, &TypeIdAsString);
496 FREE(TypeIdAsString);
497 TypeIdAsString = NULL;
504 sciErr = createMatrixOfDouble(pvApiCtx, Rhs + 2, m_out, n_out, NULL);
509 printError(&sciErr, 0);
516 if (Lhs > 2) /* name */
518 if (GetFileTypeOpenedInScilab(iID) != 0)
520 char *filename = NULL;
523 if (GetFileNameOpenedInScilab(iID) == NULL)
525 filename = strdup("");
529 filename = strdup(GetFileNameOpenedInScilab(iID));
532 sciErr = createMatrixOfString(pvApiCtx, Rhs + 3, m_out, n_out, &filename);
541 sciErr = createMatrixOfDouble(pvApiCtx, Rhs + 3, m_out, n_out, NULL);
546 printError(&sciErr, 0);
553 if (Lhs > 3) /* mod */
555 if (GetFileTypeOpenedInScilab(iID) != 0)
557 double ModeId = (double)GetFileModeOpenedInScilab(iID);
560 sciErr = createMatrixOfDouble(pvApiCtx, Rhs + 4, m_out, n_out, &ModeId);
567 sciErr = createMatrixOfDouble(pvApiCtx, Rhs + 4, m_out, n_out, NULL);
572 printError(&sciErr, 0);
579 if (Lhs > 4) /* swap */
581 if (GetFileTypeOpenedInScilab(iID) != 0)
583 double SwapId = (double)GetSwapStatus(iID);
586 sciErr = createMatrixOfDouble(pvApiCtx, Rhs + 5, m_out, n_out, &SwapId);
593 sciErr = createMatrixOfDouble(pvApiCtx, Rhs + 5, m_out, n_out, NULL);
598 printError(&sciErr, 0);
607 /*--------------------------------------------------------------------------*/