9dd796ec66d5e37d6d02fb5e9cd32c9f16c9784f
[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         if(Rhs > 1)
59         {
60             int i = 0;
61             for (i = 2 ; i <= Rhs ; i++)
62             {
63                 sciErr = getVarAddressFromPosition(pvApiCtx, i, &piAddrI);
64                 if (sciErr.iErr)
65                 {
66                     printError(&sciErr, 0);
67                     return 1;
68                 }
69
70                 sciErr = getVarType(pvApiCtx, piAddrI, &iTypeI);
71                 if (sciErr.iErr)
72                 {
73                     printError(&sciErr, 0);
74                     return 1;
75                 }
76
77                 if (iTypeI != sci_strings)
78                 {
79                     iOldSave = TRUE;
80                     break;
81                 }
82
83                 sciErr = getVarDimension(pvApiCtx, piAddrI, &iRowsI, &iColsI);
84                 if (sciErr.iErr)
85                 {
86                     printError(&sciErr, 0);
87                     return 1;
88                 }
89
90                 if (iRowsI != 1 || iColsI != 1)
91                 {
92                     iOldSave = TRUE;
93                     break;
94                 }
95
96                 if (getAllocatedSingleString(pvApiCtx, piAddrI, &pstVarI))
97                 {
98                     return 1;
99                 }
100
101                 if(strcmp(pstVarI, "-append") != 0)
102                 {
103                     //try to get variable by name
104                     sciErr = getVarAddressFromName(pvApiCtx, pstVarI, &piAddrI2);
105                     if (sciErr.iErr)
106                     {
107                         // Try old save because here the input variable can be of type "string" but not a variable name
108                         // Ex: a=""; save(filename, a);
109                         iOldSave = TRUE;
110                         break;
111                     }
112
113                     if (piAddrI2 == 0)
114                     {
115                         iOldSave = TRUE;
116                         break;
117                     }
118                 }
119
120                 freeAllocatedSingleString(pstVarI);
121             }
122
123             if(iOldSave == FALSE)
124             {
125                 int lw = 0;
126                 //call "overload" to prepare data to export_to_hdf5 function.
127                 C2F(overload) (&lw, "save", (unsigned long)strlen("save"));
128             }
129         }
130         else
131         {
132             iOldSave = TRUE;
133         }
134     }
135     else
136     {
137         iOldSave = TRUE;
138     }
139
140
141     //old save
142
143     if (iOldSave)
144     {//show warning only for variable save, not for environment.
145         if (getWarningMode() && Rhs > 1)
146         {
147             sciprint(_("%s: Scilab 6 will not support the file format used.\n"), _("Warning"));
148             sciprint(_("%s: Please quote the variable declaration. Example, save('myData.sod',a) becomes save('myData.sod','a').\n"), _("Warning"));
149             sciprint(_("%s: See help('save') for the rational.\n"), _("Warning"));
150         }
151         C2F(intsave)();
152     }
153
154     return 0;
155 }
156 /*--------------------------------------------------------------------------*/