c21e797d33cc382e1f2636b5eda6da18d00bd7c7
[scilab.git] / scilab / modules / io / sci_gateway / c / sci_file.c
1 /*
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
5  * 
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
11  *
12  */
13
14 /*--------------------------------------------------------------------------*/
15 #include <string.h>
16 #include "gw_io.h"
17 #include "stack-c.h"
18 #include "api_scilab.h"
19 #include "FileExist.h"
20 #include "MALLOC.h"
21 #include "Scierror.h"
22 #include "localization.h"
23 #include "charEncoding.h"
24 #include "filesmanagement.h"
25 #include "freeArrayOfString.h"
26 #ifdef _MSC_VER
27 #include "strdup_windows.h"
28 #endif
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)
39 {
40         if (Rhs == 0)
41         {
42                 return sci_file_no_rhs(fname);
43         }
44
45         if (Rhs == 1)
46         {
47                 return sci_file_one_rhs(fname);
48         }
49
50         if (Rhs >= 3)
51         {
52                 SciErr sciErr;
53
54                 int *piAddressVarOne = NULL;
55                 int *piAddressVarTwo = NULL;
56                 int *piAddressVarThree = NULL;
57
58                 int iType1 = 0;
59                 int iType2 = 0;
60                 int iType3 = 0;
61
62                 sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddressVarOne);
63                 if(sciErr.iErr)
64                 {
65                         printError(&sciErr, 0);
66                         return 0;
67                 }
68
69                 sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddressVarTwo);
70                 if(sciErr.iErr)
71                 {
72                         printError(&sciErr, 0);
73                         return 0;
74                 }
75
76                 sciErr = getVarAddressFromPosition(pvApiCtx, 3, &piAddressVarThree);
77                 if(sciErr.iErr)
78                 {
79                         printError(&sciErr, 0);
80                         return 0;
81                 }
82
83                 sciErr = getVarType(pvApiCtx, piAddressVarOne, &iType1);
84                 if(sciErr.iErr)
85                 {
86                         printError(&sciErr, 0);
87                         return 0;
88                 }
89
90                 sciErr = getVarType(pvApiCtx, piAddressVarTwo, &iType2);
91                 if(sciErr.iErr)
92                 {
93                         printError(&sciErr, 0);
94                         return 0;
95                 }
96
97                 sciErr = getVarType(pvApiCtx, piAddressVarThree, &iType3);
98                 if(sciErr.iErr)
99                 {
100                         printError(&sciErr, 0);
101                         return 0;
102                 }
103
104                 if ( (iType1 == sci_strings) && (iType2 == sci_strings) && (iType3 == sci_strings) )
105                 {
106                         char *pStVarOne = NULL;
107                         int lenStVarOne = 0;
108
109                         wchar_t *pStVarTwo = NULL;
110                         int lenStVarTwo = 0;
111
112                         char *pStVarThree = NULL;
113                         int lenStVarThree = 0;
114
115                         int m1 = 0, n1 = 0;
116                         int m2 = 0, n2 = 0;
117                         int m3 = 0, n3 = 0;
118
119                         sciErr = getVarDimension(pvApiCtx, piAddressVarOne, &m1, &n1);
120                         if(sciErr.iErr)
121                         {
122                                 printError(&sciErr, 0);
123                                 return 0;
124                         }
125
126                         if ( (m1 != n1) && (n1 != 1) ) 
127                         {
128                                 Scierror(999,_("%s: Wrong size for input argument #%d: A string expected.\n"), fname, 1);
129                                 return 0;
130                         }
131
132                         sciErr = getVarDimension(pvApiCtx, piAddressVarTwo, &m2, &n2);
133                         if(sciErr.iErr)
134                         {
135                                 printError(&sciErr, 0);
136                                 return 0;
137                         }
138
139                         if ( (m2 != n2) && (n2 != 1) ) 
140                         {
141                                 Scierror(999,_("%s: Wrong size for input argument #%d: A string expected.\n"), fname, 2);
142                                 return 0;
143                         }
144
145                         sciErr = getVarDimension(pvApiCtx, piAddressVarThree, &m3, &n3);
146                         if(sciErr.iErr)
147                         {
148                                 printError(&sciErr, 0);
149                                 return 0;
150                         }
151
152                         if ( (m3 != n3) && (n3 != 1) ) 
153                         {
154                                 Scierror(999,_("%s: Wrong size for input argument #%d: A string expected.\n"), fname, 3);
155                                 return 0;
156                         }
157
158                         // get lenStVarOne value
159                         sciErr = getMatrixOfString(pvApiCtx, piAddressVarOne, &m1, &n1, &lenStVarOne, NULL);
160                         if(sciErr.iErr)
161                         {
162                                 printError(&sciErr, 0);
163                                 return 0;
164                         }
165
166                         // get value of first argument
167                         pStVarOne = (char*)MALLOC(sizeof(char)*(lenStVarOne + 1));
168                         if (pStVarOne == NULL)
169                         {
170                                 Scierror(999,_("%s: Memory allocation error.\n"),fname);
171                                 return 0;
172                         }
173
174                         sciErr = getMatrixOfString(pvApiCtx, piAddressVarOne, &m1, &n1, &lenStVarOne, &pStVarOne);
175                         if(sciErr.iErr)
176                         {
177                                 FREE(pStVarOne); pStVarOne = NULL;
178                                 printError(&sciErr, 0);
179                                 return 0;
180                         }
181
182                         if (strcmp(pStVarOne, FILE_OPEN_STR) == 0)
183                         {
184                                 FREE(pStVarOne); pStVarOne = NULL;
185
186                                 // get lenStVarThree value
187                                 sciErr = getMatrixOfString(pvApiCtx, piAddressVarThree, &m3, &n3, &lenStVarThree, NULL);
188                                 if(sciErr.iErr)
189                                 {
190                                         printError(&sciErr, 0);
191                                         return 0;
192                                 }
193
194                                 // get value of third argument
195                                 pStVarThree = (char*)MALLOC(sizeof(char)*(lenStVarThree + 1));
196                                 if (pStVarThree == NULL)
197                                 {
198                                         Scierror(999,_("%s: Memory allocation error.\n"),fname);
199                                         return 0;
200                                 }
201
202                                 sciErr = getMatrixOfString(pvApiCtx, piAddressVarThree, &m3, &n3, &lenStVarThree, &pStVarThree);
203                                 if(sciErr.iErr)
204                                 {
205                                         FREE(pStVarThree); pStVarThree = NULL; 
206                                         printError(&sciErr, 0);
207                                         return 0;
208                                 }
209
210                                 if (strcmp(pStVarThree, FILE_OLD_STR) == 0)
211                                 {
212                                         FREE(pStVarThree); pStVarThree = NULL; 
213
214                                         // get lenStVarTwo value
215                                         sciErr = getMatrixOfWideString(pvApiCtx, piAddressVarTwo, &m2, &n2, &lenStVarTwo, NULL);
216                                         if(sciErr.iErr)
217                                         {
218                                                 printError(&sciErr, 0);
219                                                 return 0;
220                                         }
221
222                                         // get value of second argument
223                                         pStVarTwo = (wchar_t*)MALLOC(sizeof(wchar_t)*(lenStVarTwo + 1));
224                                         if (pStVarTwo == NULL)
225                                         {
226                                                 Scierror(999,_("%s: Memory allocation error.\n"),fname);
227                                                 return 0;
228                                         }
229
230                                         sciErr = getMatrixOfWideString(pvApiCtx, piAddressVarTwo, &m2, &n2, &lenStVarTwo, &pStVarTwo);
231                                         if(sciErr.iErr)
232                                         {
233                                                 FREE(pStVarTwo); pStVarTwo = NULL;
234                                                 printError(&sciErr, 0);
235                                                 return 0;
236                                         }
237
238                                         if (!FileExistW(pStVarTwo))
239                                         {
240                                                 if (Lhs == 2)
241                                                 {
242                                                         double dOutErrCode = 240.;
243                                                         int m_out = 1,  n_out = 1;
244
245                                                         sciErr = createMatrixOfDouble(pvApiCtx, Rhs + 1, 0, 0, NULL);
246                                                         if(sciErr.iErr)
247                                                         {
248                                                                 printError(&sciErr, 0);
249                                                                 return 0;
250                                                         }
251
252                                                         sciErr = createMatrixOfDouble(pvApiCtx, Rhs + 2, m_out, n_out, &dOutErrCode);
253                                                         if(sciErr.iErr)
254                                                         {
255                                                                 printError(&sciErr, 0);
256                                                                 return 0;
257                                                         }
258
259                                                         LhsVar(2) = Rhs + 2;
260                                                         LhsVar(1) = Rhs + 1;
261                                                         PutLhsVar();
262                                                 }
263                                                 else
264                                                 {
265                                                         char *filename = wide_string_to_UTF8(pStVarTwo);
266                                                         if (filename)
267                                                         {
268                                                                 Scierror(240, _("%s: The file \"%s\" does not exist.\n"),fname, filename);
269                                                                 FREE(filename);
270                                                                 filename = NULL;
271                                                         }
272                                                         else
273                                                         {
274                                                                 Scierror(240, _("%s: The file does not exist.\n"),fname);
275                                                         }
276                                                 }
277                                                 return 0;
278                                         }
279
280                                         FREE(pStVarTwo); pStVarTwo = NULL;
281                                 }
282
283                                 FREE(pStVarThree); pStVarThree = NULL; 
284                         }
285
286                         FREE(pStVarOne); pStVarOne = NULL;
287                 }
288         }
289
290         C2F(intfile)();
291         return 0;
292 }
293 /*--------------------------------------------------------------------------*/
294 static int sci_file_no_rhs(char *fname)
295 {
296         SciErr sciErr;
297         int m_out = 0, n_out = 0;
298
299         int sizeArray = 0;
300         double *IdUsed = NULL;
301
302         CheckLhs(0,5);
303
304         /* Lhs == 0 IDs*/
305         IdUsed = GetFilesIdUsed(&sizeArray);
306
307         if (IdUsed)
308         {
309                 m_out = 1;
310                 n_out = sizeArray;
311                 sciErr = createMatrixOfDouble(pvApiCtx, Rhs + 1, m_out, n_out, IdUsed);
312                 FREE(IdUsed);
313                 IdUsed = NULL;
314
315                 if(sciErr.iErr)
316                 {
317                         printError(&sciErr, 0);
318                         return 0;
319                 }
320
321                 LhsVar(1) = Rhs + 1; 
322         }
323
324         if (Lhs > 1) /* Types */
325         {
326                 char **TypeIdsAsString = GetTypesUsedAsString(&sizeArray);
327                 if (TypeIdsAsString)
328                 {
329                         m_out = 1;
330                         n_out = sizeArray;
331                         sciErr = createMatrixOfString(pvApiCtx, Rhs + 2, m_out, n_out, TypeIdsAsString);
332                         freeArrayOfString(TypeIdsAsString, sizeArray);
333
334                         if(sciErr.iErr)
335                         {
336                                 printError(&sciErr, 0);
337                                 return 0;
338                         }
339
340                         LhsVar(2) = Rhs + 2; 
341                 }
342         }
343
344         if (Lhs > 2) /* names */
345         {
346                 char **Filenames = GetFilenamesUsed(&sizeArray);
347                 if (Filenames)
348                 {
349                         m_out = 1;
350                         n_out = sizeArray;
351                         sciErr = createMatrixOfString(pvApiCtx, Rhs + 3, m_out, n_out, Filenames);
352                         freeArrayOfString(Filenames, sizeArray);
353
354                         if(sciErr.iErr)
355                         {
356                                 printError(&sciErr, 0);
357                                 return 0;
358                         }
359
360                         LhsVar(3) = Rhs + 3; 
361                 }
362         }
363
364         if (Lhs > 3) /* mod */
365         {
366                 double *Modes = GetModesUsed(&sizeArray);
367                 if (Modes)
368                 {
369                         m_out = 1;
370                         n_out = sizeArray;
371                         sciErr = createMatrixOfDouble(pvApiCtx, Rhs + 4, m_out, n_out, Modes);
372                         FREE(Modes);
373                         Modes = NULL;
374                         if(sciErr.iErr)
375                         {
376                                 printError(&sciErr, 0);
377                                 return 0;
378                         }
379
380                         LhsVar(4) = Rhs + 4; 
381                 }
382         }
383
384         if (Lhs > 4) /* swap */
385         {
386                 double *SwapId = GetSwapsUsed(&sizeArray);
387                 if (SwapId)
388                 {
389                         m_out = 1;
390                         n_out = sizeArray;
391                         sciErr = createMatrixOfDouble(pvApiCtx, Rhs + 5, m_out, n_out, SwapId);
392                         FREE(SwapId);
393                         SwapId = NULL;
394
395                         if(sciErr.iErr)
396                         {
397                                 printError(&sciErr, 0);
398                                 return 0;
399                         }
400
401                         LhsVar(5) = Rhs + 5; 
402                 }
403         }
404
405         PutLhsVar();
406         return 0;
407 }
408 /*--------------------------------------------------------------------------*/
409 static int sci_file_one_rhs(char *fname)
410 {
411         SciErr sciErr;
412
413         int iID = 0;
414         int m1 = 0, n1 = 0;
415         int iType = 0;
416         int *piAddressVarOne = NULL;
417         double *pdVarOne = NULL;
418
419         int m_out = 0;
420         int n_out = 0;
421         
422         /* get Address of inputs */
423         sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddressVarOne);
424         if(sciErr.iErr)
425         {
426                 printError(&sciErr, 0);
427                 return 0;
428         }
429
430         sciErr = getVarType(pvApiCtx, piAddressVarOne, &iType);
431         if(sciErr.iErr)
432         {
433                 printError(&sciErr, 0);
434                 return 0;
435         }
436
437         /* check input type */
438         if ( iType != sci_matrix )
439         {
440                 Scierror(201,_("%s: Wrong type for input argument #%d: A scalar expected.\n"),fname,1);
441                 return 0;
442         }
443
444         sciErr = getMatrixOfDouble(pvApiCtx, piAddressVarOne,&m1,&n1,&pdVarOne);
445         if(sciErr.iErr)
446         {
447                 printError(&sciErr, 0);
448                 return 0;
449         }
450
451         if( n1 != 1 || m1 != 1)
452         {
453                 Scierror(999,_("%s: Wrong size for input argument #%d: A scalar expected.\n"),fname,1);
454                 return 0;
455         }
456
457         iID = (int) *pdVarOne;
458
459         if (*pdVarOne != (double)iID)
460         {
461                 Scierror(999,_("%s: Wrong value for input argument #%d: An integer expected.\n"),fname,1);
462                 return 0;
463         }
464
465         /* Lhs = 0 ID */
466         if (GetFileTypeOpenedInScilab(iID) != 0)
467         {
468                 m_out = 1;
469                 n_out = 1;
470                 sciErr = createMatrixOfDouble(pvApiCtx, Rhs + 1, m_out, n_out, pdVarOne);
471         }
472         else
473         {
474                 /* returns [] */
475                 m_out = 0;
476                 n_out = 0;
477                 sciErr = createMatrixOfDouble(pvApiCtx, Rhs + 1, m_out, n_out, NULL);
478         }
479
480         if(sciErr.iErr)
481         {
482                 printError(&sciErr, 0);
483                 return 0;
484         }
485
486         LhsVar(1) = Rhs + 1; 
487
488         if (Lhs > 1) /* Type */
489         {
490                 if (GetFileTypeOpenedInScilab(iID) != 0)
491                 {
492                         char *TypeIdAsString = GetFileTypeOpenedInScilabAsString(iID);
493                         m_out = 1;
494                         n_out = 1;
495                         sciErr = createMatrixOfString(pvApiCtx, Rhs + 2, m_out, n_out, &TypeIdAsString);
496                         FREE(TypeIdAsString);
497                         TypeIdAsString = NULL;
498                 }
499                 else
500                 {
501                         /* returns [] */
502                         m_out = 0;
503                         n_out = 0;
504                         sciErr = createMatrixOfDouble(pvApiCtx, Rhs + 2, m_out, n_out, NULL);
505                 }
506
507                 if(sciErr.iErr)
508                 {
509                         printError(&sciErr, 0);
510                         return 0;
511                 }
512
513                 LhsVar(2) = Rhs + 2; 
514         }
515
516         if (Lhs > 2) /* name */
517         {
518                 if (GetFileTypeOpenedInScilab(iID) != 0)
519                 {
520                         char *filename = NULL;
521                         m_out = 1;
522                         n_out = 1;
523                         if (GetFileNameOpenedInScilab(iID) == NULL)
524                         {
525                                 filename = strdup("");
526                         }
527                         else
528                         {
529                                 filename = strdup(GetFileNameOpenedInScilab(iID));
530                         }
531
532                         sciErr = createMatrixOfString(pvApiCtx, Rhs + 3, m_out, n_out, &filename);
533                         FREE(filename);
534                         filename = NULL;
535                 }
536                 else
537                 {
538                         /* returns [] */
539                         m_out = 0;
540                         n_out = 0;
541                         sciErr = createMatrixOfDouble(pvApiCtx, Rhs + 3, m_out, n_out, NULL);
542                 }
543
544                 if(sciErr.iErr)
545                 {
546                         printError(&sciErr, 0);
547                         return 0;
548                 }
549
550                 LhsVar(3) = Rhs + 3; 
551         }
552
553         if (Lhs > 3)  /* mod */
554         {
555                 if (GetFileTypeOpenedInScilab(iID) != 0)
556                 {
557                         double ModeId = (double)GetFileModeOpenedInScilab(iID);
558                         m_out = 1;
559                         n_out = 1;
560                         sciErr = createMatrixOfDouble(pvApiCtx, Rhs + 4, m_out, n_out, &ModeId);
561                 }
562                 else
563                 {
564                         /* returns [] */
565                         m_out = 0;
566                         n_out = 0;
567                         sciErr = createMatrixOfDouble(pvApiCtx, Rhs + 4, m_out, n_out, NULL);
568                 }
569
570                 if(sciErr.iErr)
571                 {
572                         printError(&sciErr, 0);
573                         return 0;
574                 }
575
576                 LhsVar(4) = Rhs + 4; 
577         }
578
579         if (Lhs > 4) /* swap */
580         {
581                 if (GetFileTypeOpenedInScilab(iID) != 0)
582                 {
583                         double SwapId = (double)GetSwapStatus(iID);
584                         m_out = 1;
585                         n_out = 1;
586                         sciErr = createMatrixOfDouble(pvApiCtx, Rhs + 5, m_out, n_out, &SwapId);
587                 }
588                 else
589                 {
590                         /* returns [] */
591                         m_out = 0;
592                         n_out = 0;
593                         sciErr = createMatrixOfDouble(pvApiCtx, Rhs + 5, m_out, n_out, NULL);
594                 }
595
596                 if(sciErr.iErr)
597                 {
598                         printError(&sciErr, 0);
599                         return 0;
600                 }
601                 LhsVar(5) = Rhs + 5; 
602         }
603
604         PutLhsVar();
605         return 0;
606 }
607 /*--------------------------------------------------------------------------*/