2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2006 - INRIA - Allan CORNET
4 * Copyright (C) 2010 - 2011 - DIGITEO - Allan CORNET
6 * Copyright (C) 2012 - 2016 - Scilab Enterprises
8 * This file is hereby licensed under the terms of the GNU GPL v2.0,
9 * pursuant to article 5.3.4 of the CeCILL v.2.1.
10 * This file was originally licensed under the terms of the CeCILL v2.1,
11 * and continues to be available under such terms.
12 * For more information, see the COPYING file which you should have received
13 * along with this program.
16 /*--------------------------------------------------------------------------*/
18 #include "api_scilab.h"
19 #include "sci_malloc.h"
20 #include "gw_fileio.h"
22 #include "localization.h"
23 #include "freeArrayOfString.h"
24 #include "expandPathVariable.h"
25 #include "os_string.h"
26 #include "fscanfMat.h"
28 /*--------------------------------------------------------------------------*/
29 static void freeVar(char** filename, char** expandedFilename, char** Format, char** separator);
30 /*--------------------------------------------------------------------------*/
31 int sci_fscanfMat(char *fname, void* pvApiCtx)
34 int *piAddressVarOne = NULL;
38 char *filename = NULL;
39 char *expandedFilename = NULL;
41 char *separator = NULL;
42 BOOL bIsDefaultSeparator = TRUE;
44 fscanfMatResult *results = NULL;
51 int *piAddressVarThree = NULL;
55 sciErr = getVarAddressFromPosition(pvApiCtx, 3, &piAddressVarThree);
58 printError(&sciErr, 0);
59 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 3);
63 if (isStringType(pvApiCtx, piAddressVarThree) == 0 || isScalar(pvApiCtx, piAddressVarThree) == 0)
65 Scierror(999, _("%s: Wrong type for input argument #%d: string expected.\n"), fname, 3);
69 if (getAllocatedSingleString(pvApiCtx, piAddressVarThree, &separator))
71 freeVar(&filename, &expandedFilename, &Format, &separator);
72 Scierror(999, _("%s: Memory allocation error.\n"), fname);
76 bIsDefaultSeparator = FALSE;
81 int *piAddressVarTwo = NULL;
85 sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddressVarTwo);
88 freeVar(&filename, &expandedFilename, &Format, &separator);
89 printError(&sciErr, 0);
90 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 2);
94 sciErr = getVarType(pvApiCtx, piAddressVarTwo, &iType2);
97 freeVar(&filename, &expandedFilename, &Format, &separator);
98 printError(&sciErr, 0);
99 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 2);
103 if (isStringType(pvApiCtx, piAddressVarTwo) == 0 || isScalar(pvApiCtx, piAddressVarTwo) == 0)
105 freeVar(&filename, &expandedFilename, &Format, &separator);
106 Scierror(999, _("%s: Wrong type for input argument #%d: string expected.\n"), fname, 2);
110 if (getAllocatedSingleString(pvApiCtx, piAddressVarTwo, &Format))
112 freeVar(&filename, &expandedFilename, &Format, &separator);
113 Scierror(999, _("%s: Memory allocation error.\n"), fname);
119 Format = os_strdup(DEFAULT_FSCANFMAT_FORMAT);
122 sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddressVarOne);
125 freeVar(&filename, &expandedFilename, &Format, &separator);
126 printError(&sciErr, 0);
127 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
131 if (isStringType(pvApiCtx, piAddressVarOne) == 0 || isScalar(pvApiCtx, piAddressVarOne) == 0)
133 freeVar(&filename, &expandedFilename, &Format, &separator);
134 Scierror(999, _("%s: Wrong type for input argument #%d: string expected.\n"), fname, 1);
138 if (getAllocatedSingleString(pvApiCtx, piAddressVarOne, &filename))
140 freeVar(&filename, &expandedFilename, &Format, &separator);
141 Scierror(999, _("%s: Memory allocation error.\n"), fname);
145 expandedFilename = expandPathVariable(filename);
146 if (bIsDefaultSeparator)
148 #define NB_DEFAULT_SUPPORTED_SEPARATORS 2
151 /* default separator can be a space or a tabulation */
152 char *supportedSeparators[NB_DEFAULT_SUPPORTED_SEPARATORS] = {DEFAULT_FSCANFMAT_SEPARATOR, "\t"};
155 for (i = 0; i < NB_DEFAULT_SUPPORTED_SEPARATORS; i++)
157 results = fscanfMat(expandedFilename, Format, supportedSeparators[i]);
158 if (results && results->err == FSCANFMAT_NO_ERROR)
163 freeFscanfMatResult(results);
168 results = fscanfMat(expandedFilename, Format, separator);
169 if (results && results->err != FSCANFMAT_NO_ERROR)
171 freeFscanfMatResult(results);
177 freeVar(&filename, &expandedFilename, &Format, &separator);
178 Scierror(999, _("%s: Memory allocation error.\n"), fname);
182 freeVar(NULL, &expandedFilename, &Format, &separator);
183 switch (results->err)
185 case FSCANFMAT_NO_ERROR:
187 if ( (results->values) && (results->m > 0) && (results->n > 0))
189 sciErr = createMatrixOfDouble(pvApiCtx, Rhs + 1, results->m, results->n, results->values);
193 freeFscanfMatResult(results);
194 printError(&sciErr, 0);
195 Scierror(999, _("%s: Memory allocation error.\n"), fname);
201 if (createEmptyMatrix(pvApiCtx, Rhs + 1))
204 freeFscanfMatResult(results);
205 Scierror(999, _("%s: Memory allocation error.\n"), fname);
216 sciErr = createMatrixOfString(pvApiCtx, Rhs + 2, results->sizeText, 1, (char const * const*) results->text);
220 freeFscanfMatResult(results);
221 printError(&sciErr, 0);
222 Scierror(999, _("%s: Memory allocation error.\n"), fname);
228 if (createSingleString(pvApiCtx, Rhs + 2, ""))
231 freeFscanfMatResult(results);
232 Scierror(999, _("%s: Memory allocation error.\n"), fname);
240 freeFscanfMatResult(results);
245 case FSCANFMAT_MOPEN_ERROR:
247 Scierror(999, _("%s: can not open file %s.\n"), fname, filename);
251 case FSCANFMAT_READLINES_ERROR:
253 Scierror(999, _("%s: can not read file %s.\n"), fname, filename);
257 case FSCANFMAT_FORMAT_ERROR:
259 Scierror(999, _("%s: Invalid format.\n"), fname);
263 case FSCANFMAT_MEMORY_ALLOCATION:
265 Scierror(999, _("%s: Memory allocation error.\n"), fname);
270 case FSCANFMAT_ERROR:
272 Scierror(999, _("%s: error.\n"), fname);
278 /*--------------------------------------------------------------------------*/
279 static void freeVar(char** filename, char** expandedFilename, char** Format, char** separator)
281 if (filename && *filename)
287 if (expandedFilename && *expandedFilename)
289 FREE(*expandedFilename);
290 *expandedFilename = NULL;
293 if (Format && *Format)
299 if (separator && *separator)