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"
22 #include "strdup_Windows.h"
25 int sci_matfile_listvar(char *fname, unsigned long fname_len)
27 int nbRow = 0, nbCol = 0;
28 mat_t *matfile = NULL;
29 matvar_t *matvar = NULL;
31 char **varnames = NULL;
32 double *varclasses = NULL;
33 double *vartypes = NULL;
34 int nbvar = 0, var_type;
42 /* First Rhs is the index of the file to read */
44 sciErr = getVarAddressFromPosition(pvApiCtx, 1, &fd_addr);
47 printError(&sciErr, 0);
50 sciErr = getVarType(pvApiCtx, fd_addr, &var_type);
53 printError(&sciErr, 0);
57 if (var_type == sci_matrix)
59 getScalarDouble(pvApiCtx, fd_addr, &tmp_dbl);
60 if (!isScalar(pvApiCtx, fd_addr))
62 Scierror(999, _("%s: Wrong size for first input argument: Single double expected.\n"), fname);
65 fileIndex = (int)tmp_dbl;
69 Scierror(999, _("%s: Wrong type for first input argument: Double expected.\n"), fname);
73 /* Gets the corresponding matfile */
74 matfile_manager(MATFILEMANAGER_GETFILE, &fileIndex, &matfile);
78 Scierror(999, _("%s: Invalid file identifier.\n"), fname);
82 /* Back to the beginning of the file */
83 if (Mat_Rewind(matfile) != 0)
85 Scierror(999, _("%s: Could not rewind the file %s.\n"), "matfile_listvar", matfile->filename);
89 matvar = Mat_VarReadNext(matfile);
90 while (matvar != NULL && matvar->name != NULL)
93 varnames = (char**) REALLOC(varnames, nbvar * sizeof(char*));
96 Scierror(999, _("%s: No more memory.\n"), "matfile_listvar");
99 varnames[nbvar - 1] = strdup(matvar->name);
100 varclasses = (double*) REALLOC(varclasses, nbvar * sizeof(double));
101 if (varnames == NULL)
103 Scierror(999, _("%s: No more memory.\n"), "matfile_listvar");
106 varclasses[nbvar - 1] = (double) matvar->class_type;
107 vartypes = (double*) REALLOC(vartypes, nbvar * sizeof(double));
108 if (varnames == NULL)
110 Scierror(999, _("%s: No more memory.\n"), "matfile_listvar");
113 vartypes[nbvar - 1] = (double) matvar->data_type;
116 matvar = Mat_VarReadNext(matfile);
121 /* Return the variable names list */
124 sciErr = createMatrixOfString(pvApiCtx, Rhs + 1, nbRow, nbCol, varnames);
127 printError(&sciErr, 0);
132 /* Return the variable classes */
135 sciErr = createMatrixOfDouble(pvApiCtx, Rhs + 2, nbRow, nbCol, varclasses);
138 printError(&sciErr, 0);
144 /* Return the variable types */
147 sciErr = createMatrixOfDouble(pvApiCtx, Rhs + 3, nbRow, nbCol, vartypes);
150 printError(&sciErr, 0);
156 freeArrayOfString(varnames, nbvar);