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++)
159 freeFscanfMatResult(results);
161 results = fscanfMat(expandedFilename, Format, supportedSeparators[i]);
162 if (results && results->err == FSCANFMAT_NO_ERROR)
170 results = fscanfMat(expandedFilename, Format, separator);
171 if (results && results->err != FSCANFMAT_NO_ERROR)
173 freeFscanfMatResult(results);
180 freeVar(&filename, &expandedFilename, &Format, &separator);
181 Scierror(999, _("%s: Memory allocation error.\n"), fname);
185 freeVar(NULL, &expandedFilename, &Format, &separator);
186 switch (results->err)
188 case FSCANFMAT_NO_ERROR:
190 if ( (results->values) && (results->m > 0) && (results->n > 0))
192 sciErr = createMatrixOfDouble(pvApiCtx, Rhs + 1, results->m, results->n, results->values);
196 freeFscanfMatResult(results);
197 printError(&sciErr, 0);
198 Scierror(999, _("%s: Memory allocation error.\n"), fname);
204 if (createEmptyMatrix(pvApiCtx, Rhs + 1))
207 freeFscanfMatResult(results);
208 Scierror(999, _("%s: Memory allocation error.\n"), fname);
219 sciErr = createMatrixOfString(pvApiCtx, Rhs + 2, results->sizeText, 1, (char const * const*) results->text);
223 freeFscanfMatResult(results);
224 printError(&sciErr, 0);
225 Scierror(999, _("%s: Memory allocation error.\n"), fname);
231 if (createSingleString(pvApiCtx, Rhs + 2, ""))
234 freeFscanfMatResult(results);
235 Scierror(999, _("%s: Memory allocation error.\n"), fname);
243 freeFscanfMatResult(results);
248 case FSCANFMAT_MOPEN_ERROR:
250 Scierror(999, _("%s: can not open file %s.\n"), fname, filename);
254 case FSCANFMAT_READLINES_ERROR:
256 Scierror(999, _("%s: can not read file %s.\n"), fname, filename);
260 case FSCANFMAT_FORMAT_ERROR:
262 Scierror(999, _("%s: Invalid format.\n"), fname);
266 case FSCANFMAT_MEMORY_ALLOCATION:
268 Scierror(999, _("%s: Memory allocation error.\n"), fname);
273 case FSCANFMAT_ERROR:
275 Scierror(999, _("%s: error.\n"), fname);
281 /*--------------------------------------------------------------------------*/
282 static void freeVar(char** filename, char** expandedFilename, char** Format, char** separator)
284 if (filename && *filename)
290 if (expandedFilename && *expandedFilename)
292 FREE(*expandedFilename);
293 *expandedFilename = NULL;
296 if (Format && *Format)
302 if (separator && *separator)