2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2008 - INRIA - Vincent COUVERT
4 * Copyright (C) 2010 - DIGITEO - Yann COLLETTE
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.1-en.txt
15 #include "strdup_Windows.h"
19 #include "GetMatlabVariable.h"
20 #include "api_scilab.h"
24 NO_MORE_VARIABLES = -1,
25 UNKNOWN_VARIABLE_TYPE = 0
28 int sci_matfile_varwrite(char *fname, unsigned long fname_len)
30 int nbRow = 0, nbCol = 0;
31 mat_t *matfile = NULL;
32 matvar_t *matvar = NULL;
36 int compressionFlag = 0;
38 int * fd_addr = NULL, * name_addr = NULL, * cp_flag_addr = NULL;
45 /* Input argument is the index of the file to write */
47 sciErr = getVarAddressFromPosition(pvApiCtx, 1, &fd_addr);
50 printError(&sciErr, 0);
53 sciErr = getVarType(pvApiCtx, fd_addr, &var_type);
56 printError(&sciErr, 0);
60 if (var_type == sci_matrix)
62 getScalarDouble(pvApiCtx, fd_addr, &tmp_dbl);
63 if (!isScalar(pvApiCtx, fd_addr))
65 Scierror(999, _("%s: Wrong size for first input argument: Single double expected.\n"), fname);
68 fileIndex = (int)tmp_dbl;
72 Scierror(999, _("%s: Wrong type for first input argument: Double expected.\n"), fname);
76 /* Gets the corresponding matfile */
77 matfile_manager(MATFILEMANAGER_GETFILE, &fileIndex, &matfile);
79 /* Second argument is the variable name */
81 sciErr = getVarAddressFromPosition(pvApiCtx, 2, &name_addr);
84 printError(&sciErr, 0);
87 sciErr = getVarType(pvApiCtx, name_addr, &var_type);
90 printError(&sciErr, 0);
94 if (var_type == sci_strings)
96 getAllocatedSingleString(pvApiCtx, name_addr, &varname);
97 sciErr = getVarDimension(pvApiCtx, name_addr, &nbRow, &nbCol);
100 printError(&sciErr, 0);
105 Scierror(999, _("%s: Wrong size for second input argument: Single string expected.\n"), fname);
107 freeAllocatedSingleString(varname);
114 Scierror(999, _("%s: Wrong type for second input argument: Single string expected.\n"), fname);
116 freeAllocatedSingleString(varname);
121 /* Third argument is the variable data */
122 /* TODO update this NULL */
123 matvar = GetMatlabVariable(pvApiCtx, 3, varname, Mat_GetVersion(matfile), NULL, -1);
125 /* Fourth argument is the compression flag */
127 sciErr = getVarAddressFromPosition(pvApiCtx, 4, &cp_flag_addr);
130 printError(&sciErr, 0);
133 sciErr = getVarType(pvApiCtx, cp_flag_addr, &var_type);
136 printError(&sciErr, 0);
140 if (var_type == sci_boolean)
142 getScalarBoolean(pvApiCtx, cp_flag_addr, &compressionFlag);
143 if (!isScalar(pvApiCtx, cp_flag_addr))
145 Scierror(999, _("%s: Wrong size for fourth input argument: Single boolean expected.\n"), fname);
147 freeAllocatedSingleString(varname);
154 Scierror(999, _("%s: Wrong type for fourth input argument: Single boolean expected.\n"), fname);
156 freeAllocatedSingleString(varname);
161 flag = Mat_VarWrite(matfile, matvar, compressionFlag);
163 /* Return execution flag */
164 var_type = (flag == 0);
165 createScalarBoolean(pvApiCtx, Rhs + 1, var_type);
167 freeAllocatedSingleString(varname);