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