2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) INRIA - Cong WU , Allan CORNET
4 * Copyright (C) DIGITEO - 2009 - Allan CORNET
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
13 /*------------------------------------------------------------------------*/
15 #include "gw_string.h"
18 #include "api_scilab.h"
19 #include "freeArrayOfString.h"
21 #include "localization.h"
23 #include "pcre_error.h"
24 #include "pcre_private.h"
26 /*-------------------------------------------------------------------------------------*/
29 /*-------------------------------------------------------------------------------------*/
30 int sci_strsubst(char *fname, unsigned long fname_len)
33 int *piAddressVarOne = NULL;
35 int mOne = 0, nOne = 0;
40 sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddressVarOne);
43 printError(&sciErr, 0);
44 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
47 sciErr = getVarType(pvApiCtx, piAddressVarOne, &iType);
50 printError(&sciErr, 0);
51 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
55 if (iType == sci_strings)
57 char **pStVarOne = NULL;
58 int *lenStVarOne = NULL;
60 int *piAddressVarTwo = NULL;
62 int mTwo = 0, nTwo = 0;
63 char *pStVarTwo = NULL;
66 int *piAddressVarThree = NULL;
67 int mThree = 0, nThree = 0;
68 char *pStVarThree = NULL;
69 int lenStVarThree = 0;
72 BOOL isRegExp = FALSE;
76 int *piAddressVarFour = NULL;
78 int mFour = 0, nFour = 0;
79 char *pStVarFour = NULL;
82 sciErr = getVarAddressFromPosition(pvApiCtx, 4, &piAddressVarFour);
85 printError(&sciErr, 0);
86 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 4);
90 sciErr = getVarType(pvApiCtx, piAddressVarFour, &iType4);
93 printError(&sciErr, 0);
94 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 4);
98 if (iType4 != sci_strings)
100 Scierror(999, _("%s: Wrong type for input argument #%d: A string expected.\n"), fname, 4);
104 sciErr = getMatrixOfString(pvApiCtx, piAddressVarFour, &mFour, &nFour, &lenStVarFour, NULL);
107 printError(&sciErr, 0);
108 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 4);
112 if ((mFour != nFour) && (nFour != 1))
114 Scierror(999, _("%s: Wrong size for input argument #%d: A string expected.\n"), fname, 4);
118 pStVarFour = (char *)MALLOC(sizeof(char) * (lenStVarFour + 1));
121 sciErr = getMatrixOfString(pvApiCtx, piAddressVarFour, &mFour, &nFour, &lenStVarFour, &pStVarFour);
124 printError(&sciErr, 0);
125 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 4);
129 if ((strcmp(pStVarFour, CHAR_R) == 0) || (strcmp(pStVarFour, CHAR_S) == 0))
131 if (strcmp(pStVarFour, CHAR_R) == 0)
146 Scierror(999, _("%s: Wrong value for input argument #%d: '%s' or '%s' expected.\n"), fname, 4, "s", "r");
152 Scierror(999, _("%s: No more memory.\n"), fname);
157 sciErr = getVarAddressFromPosition(pvApiCtx, 3, &piAddressVarThree);
160 printError(&sciErr, 0);
161 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 3);
165 // checks type 3th input argument
166 sciErr = getVarType(pvApiCtx, piAddressVarThree, &iType3);
169 printError(&sciErr, 0);
170 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 3);
174 if (iType3 != sci_strings)
176 Scierror(999, _("%s: Wrong type for input argument #%d: A string expected.\n"), fname, 3);
180 // checks dimension 3th input argument
181 sciErr = getMatrixOfString(pvApiCtx, piAddressVarThree, &mThree, &nThree, &lenStVarThree, NULL);
184 printError(&sciErr, 0);
185 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 3);
189 if ((mThree != nThree) && (nThree != 1))
191 Scierror(999, _("%s: Wrong size for input argument #%d: A string expected.\n"), fname, 3);
195 sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddressVarTwo);
198 printError(&sciErr, 0);
199 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 2);
203 // checks type 2nd input argument
204 sciErr = getVarType(pvApiCtx, piAddressVarTwo, &iType2);
207 printError(&sciErr, 0);
208 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 2);
212 if (iType2 != sci_strings)
214 Scierror(999, _("%s: Wrong type for input argument #%d: A string expected.\n"), fname, 2);
218 // checks dimension 2nd input argument
219 sciErr = getMatrixOfString(pvApiCtx, piAddressVarTwo, &mTwo, &nTwo, &lenStVarTwo, NULL);
222 printError(&sciErr, 0);
223 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 2);
227 if ((mTwo != nTwo) && (nTwo != 1))
229 Scierror(999, _("%s: Wrong size for input argument #%d: A string expected.\n"), fname, 2);
233 sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddressVarOne);
236 printError(&sciErr, 0);
237 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
241 // checks type 1st input argument
242 sciErr = getVarType(pvApiCtx, piAddressVarOne, &iType);
243 if (iType != sci_strings)
245 Scierror(999, _("%s: Wrong type for input argument #%d: A string expected.\n"), fname, 1);
249 // get length 3th input argument
250 sciErr = getMatrixOfString(pvApiCtx, piAddressVarThree, &mThree, &nThree, &lenStVarThree, NULL);
253 printError(&sciErr, 0);
254 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 3);
258 pStVarThree = (char *)MALLOC(sizeof(char) * (lenStVarThree + 1));
261 // get value 3th input argument
262 sciErr = getMatrixOfString(pvApiCtx, piAddressVarThree, &mThree, &nThree, &lenStVarThree, &pStVarThree);
265 printError(&sciErr, 0);
266 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 3);
270 // get length 2nd input argument
271 sciErr = getMatrixOfString(pvApiCtx, piAddressVarTwo, &mTwo, &nTwo, &lenStVarTwo, NULL);
274 printError(&sciErr, 0);
275 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 2);
279 pStVarTwo = (char *)MALLOC(sizeof(char) * (lenStVarTwo + 1));
282 // get value 2nd input argument
283 sciErr = getMatrixOfString(pvApiCtx, piAddressVarTwo, &mTwo, &nTwo, &lenStVarTwo, &pStVarTwo);
286 printError(&sciErr, 0);
287 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 2);
291 sciErr = getMatrixOfString(pvApiCtx, piAddressVarOne, &mOne, &nOne, lenStVarOne, NULL);
294 printError(&sciErr, 0);
295 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
299 lenStVarOne = (int *)MALLOC(sizeof(int) * (mOne * nOne));
302 sciErr = getMatrixOfString(pvApiCtx, piAddressVarOne, &mOne, &nOne, lenStVarOne, NULL);
305 printError(&sciErr, 0);
306 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
310 pStVarOne = (char **)MALLOC(sizeof(char *) * (mOne * nOne));
313 char **Output_StringMatrix = NULL;
317 for (i = 0; i < mOne * nOne; i++)
319 pStVarOne[i] = (char *)MALLOC(sizeof(char) * (lenStVarOne[i] + 1));
320 if (pStVarOne[i] == NULL)
328 freeArrayOfString(pStVarOne, i);
329 Scierror(999, _("%s: No more memory.\n"), fname);
334 sciErr = getMatrixOfString(pvApiCtx, piAddressVarOne, &mOne, &nOne, lenStVarOne, pStVarOne);
337 printError(&sciErr, 0);
338 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
347 int ierr = (int)PCRE_FINISHED_OK;
349 Output_StringMatrix = strsubst_reg(pStVarOne, mOne * nOne, pStVarTwo, pStVarThree, &ierr);
350 if ((ierr != PCRE_FINISHED_OK) && (ierr != NO_MATCH) && (ierr != PCRE_EXIT))
356 pcre_error(fname, ierr);
362 Output_StringMatrix = strsubst(pStVarOne, mOne * nOne, pStVarTwo, pStVarThree);
369 freeArrayOfString(pStVarOne, mOne * nOne);
371 sciErr = createMatrixOfString(pvApiCtx, Rhs + 1, mOne, nOne, Output_StringMatrix);
372 freeArrayOfString(Output_StringMatrix, mOne * nOne);
375 printError(&sciErr, 0);
376 Scierror(999,_("%s: Memory allocation error.\n"), fname);
392 Scierror(999, _("%s: No more memory.\n"), fname);
402 Scierror(999, _("%s: No more memory.\n"), fname);
410 Scierror(999, _("%s: No more memory.\n"), fname);
416 Scierror(999, _("%s: No more memory.\n"), fname);
420 else if (iType == sci_matrix)
422 sciErr = getVarDimension(pvApiCtx, piAddressVarOne, &mOne, &nOne);
425 printError(&sciErr, 0);
426 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
430 if ((mOne == 0) && (nOne == 0)) /* strsubst([],...) returns [] */
432 sciErr = createMatrixOfDouble(pvApiCtx, Rhs + 1, mOne, nOne, NULL);
435 printError(&sciErr, 0);
436 Scierror(999,_("%s: Memory allocation error.\n"), fname);
445 Scierror(999, _("%s: Wrong type for input argument #%d: Matrix of strings or empty matrix expected.\n"), fname);
450 Scierror(999, _("%s: Wrong type for input argument #%d: A string expected.\n"), fname, 1);
455 /*-------------------------------------------------------------------------------------*/