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-en.txt
14 #include "api_scilab.h"
15 #include "matfile_manager.h"
17 #include "localization.h"
20 #include "freeArrayOfString.h"
21 #include "os_strdup.h"
25 #define MATIO_ERROR if(_SciErr.iErr) \
27 printError(&_SciErr, 0); \
31 int sci_matfile_listvar(char* fname, void* pvApiCtx)
33 int nbRow = 0, nbCol = 0;
34 mat_t *matfile = NULL;
35 matvar_t *matvar = NULL;
37 char **varnames = NULL;
38 double *varclasses = NULL;
39 double *vartypes = NULL;
40 int nbvar = 0, var_type;
48 /* First Rhs is the index of the file to read */
50 _SciErr = getVarAddressFromPosition(pvApiCtx, 1, &fd_addr); MATIO_ERROR;
51 _SciErr = getVarType(pvApiCtx, fd_addr, &var_type); MATIO_ERROR;
53 if (var_type == sci_matrix)
55 getScalarDouble(pvApiCtx, fd_addr, &tmp_dbl);
56 if (!isScalar(pvApiCtx, fd_addr))
58 Scierror(999, _("%s: Wrong size for first input argument: Single double expected.\n"), fname);
61 fileIndex = (int)tmp_dbl;
65 Scierror(999, _("%s: Wrong type for first input argument: Double expected.\n"), fname);
69 /* Gets the corresponding matfile */
70 matfile_manager(MATFILEMANAGER_GETFILE, &fileIndex, &matfile);
73 Scierror(999, _("%s: Invalid file identifier.\n"), fname);
77 /* Back to the beginning of the file */
78 if (Mat_Rewind(matfile) != 0)
80 Scierror(999, _("%s: Could not rewind the file %s.\n"), "matfile_listvar", matfile->filename);
84 matvar = Mat_VarReadNext(matfile);
85 while (matvar != NULL && matvar->name != NULL)
88 varnames = (char**) REALLOC(varnames, nbvar*sizeof(char*));
91 Scierror(999, _("%s: No more memory.\n"), "matfile_listvar");
94 varnames[nbvar-1] = os_strdup(matvar->name);
95 varclasses = (double*) REALLOC(varclasses, nbvar*sizeof(double));
98 Scierror(999, _("%s: No more memory.\n"), "matfile_listvar");
101 varclasses[nbvar-1] = (double) matvar->class_type;
102 vartypes = (double*) REALLOC(vartypes, nbvar*sizeof(double));
103 if (varnames == NULL)
105 Scierror(999, _("%s: No more memory.\n"), "matfile_listvar");
108 vartypes[nbvar-1] = (double) matvar->data_type;
111 matvar = Mat_VarReadNext(matfile);
116 /* Return the variable names list */
117 nbRow = nbvar; nbCol = 1;
118 _SciErr = createMatrixOfString(pvApiCtx, Rhs+1, nbRow, nbCol, varnames); MATIO_ERROR;
121 /* Return the variable classes */
124 _SciErr = createMatrixOfDouble(pvApiCtx, Rhs+2, nbRow, nbCol, varclasses); MATIO_ERROR;
128 /* Return the variable types */
131 _SciErr = createMatrixOfDouble(pvApiCtx, Rhs+3, nbRow, nbCol, vartypes); MATIO_ERROR;
135 freeArrayOfString(varnames, nbvar);