bug 11022: -append can be use in save and export_to_hdf5 functions at any position...
[scilab.git] / scilab / modules / io / sci_gateway / c / sci_save.c
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2006 - INRIA - Allan CORNET
4  *
5  * This file must be used under the terms of the CeCILL.
6  * This source file is licensed as described in the file COPYING, which
7  * you should have received as part of this distribution.  The terms
8  * are also available at
9  * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
10  *
11  */
12
13 #include "gw_io.h"
14 #include "stack-c.h"
15 #include "api_scilab.h"
16 #include "localization.h"
17 #include "sciprint.h"
18 #include "warningmode.h"
19 /*--------------------------------------------------------------------------*/
20 extern int C2F(intsave)(); /* fortran subroutine */
21 /*--------------------------------------------------------------------------*/
22 int sci_save(char *fname, unsigned long fname_len)
23 {
24     SciErr sciErr;
25
26     int iOldSave    = FALSE;
27
28     int* piAddr1    = NULL;
29     int iType1      = 0;
30
31     CheckRhs(1, 100000);
32     CheckLhs(0, 1);
33
34     //filename or file descriptor
35     sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddr1);
36     if (sciErr.iErr)
37     {
38         printError(&sciErr, 0);
39         return 1;
40     }
41
42     sciErr = getVarType(pvApiCtx, piAddr1, &iType1);
43     if (sciErr.iErr)
44     {
45         printError(&sciErr, 0);
46         return 1;
47     }
48
49     if (iType1 == sci_strings)
50     {
51         int* piAddrI    = NULL;
52         int* piAddrI2   = NULL;
53         int iTypeI      = 0;
54         int iRowsI      = 0;
55         int iColsI      = 0;
56         char* pstVarI   = NULL;
57
58         int i = 0;
59         for (i = 2 ; i <= Rhs ; i++)
60         {
61             sciErr = getVarAddressFromPosition(pvApiCtx, i, &piAddrI);
62             if (sciErr.iErr)
63             {
64                 printError(&sciErr, 0);
65                 return 1;
66             }
67
68             sciErr = getVarType(pvApiCtx, piAddrI, &iTypeI);
69             if (sciErr.iErr)
70             {
71                 printError(&sciErr, 0);
72                 return 1;
73             }
74
75             if (iTypeI != sci_strings)
76             {
77                 iOldSave = TRUE;
78                 break;
79             }
80
81             sciErr = getVarDimension(pvApiCtx, piAddrI, &iRowsI, &iColsI);
82             if (sciErr.iErr)
83             {
84                 printError(&sciErr, 0);
85                 return 1;
86             }
87
88             if (iRowsI != 1 || iColsI != 1)
89             {
90                 iOldSave = TRUE;
91                 break;
92             }
93
94             if (getAllocatedSingleString(pvApiCtx, piAddrI, &pstVarI))
95             {
96                 return 1;
97             }
98
99             if(strcmp(pstVarI, "-append") != 0)
100             {
101                 //try to get variable by name
102                 sciErr = getVarAddressFromName(pvApiCtx, pstVarI, &piAddrI2);
103                 if (sciErr.iErr)
104                 {
105                     // Try old save because here the input variable can be of type "string" but not a variable name
106                     // Ex: a=""; save(filename, a);
107                     iOldSave = TRUE;
108                     break;
109                 }
110
111                 if (piAddrI2 == 0)
112                 {
113                     iOldSave = TRUE;
114                     break;
115                 }
116             }
117
118             freeAllocatedSingleString(pstVarI);
119         }
120
121         {
122             int lw = 0;
123             //call "overload" to prepare data to export_to_hdf5 function.
124             C2F(overload) (&lw, "save", (unsigned long)strlen("save"));
125         }
126     }
127     else
128     {
129         iOldSave = TRUE;
130     }
131
132
133     //new -> %_export_to_hdf5
134     //old
135     if (iOldSave)
136     {
137         if (getWarningMode())
138         {
139             sciprint(_("WARNING: Scilab 6 will not support the file format used.\n"));
140             sciprint(_("WARNING: Please quote the variable declaration. Example, save('myData.sod',a) becomes save('myData.sod','a').\n"));
141             sciprint(_("WARNING: See help('save') for the rational.\n"));
142         }
143         C2F(intsave)();
144     }
145
146     return 0;
147 }
148 /*--------------------------------------------------------------------------*/