2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2009 - DIGITEO - Allan CORNET
5 * Copyright (C) 2012 - 2016 - Scilab Enterprises
7 * This file is hereby licensed under the terms of the GNU GPL v2.0,
8 * pursuant to article 5.3.4 of the CeCILL v.2.1.
9 * This file was originally licensed under the terms of the CeCILL v2.1,
10 * and continues to be available under such terms.
11 * For more information, see the COPYING file which you should have received
12 * along with this program.
16 /*--------------------------------------------------------------------------*/
17 #include "fileio_gw.hxx"
18 #include "function.hxx"
25 #include "sci_malloc.h"
26 #include "localization.h"
28 #include "pathconvert.h"
31 #define UNIX_TYPE L"u"
32 #define WINDOWS_TYPE L"w"
33 #define _UNIX_TYPE "u"
34 #define _WINDOWS_TYPE "w"
36 /*--------------------------------------------------------------------------*/
37 types::Function::ReturnValue sci_pathconvert(types::typed_list &in, int _iRetCount, types::typed_list &out)
39 PathConvertType PType = AUTO_STYLE;
43 if (in.size() < 1 && in.size() > 4)
45 Scierror(999, _("%s: Wrong number of input arguments: %d to %d expected.\n"), "pathconvert" , 1, 4);
46 return types::Function::Error;
51 Scierror(78, _("%s: Wrong number of output argument(s): %d expected.\n"), "pathconvert", 1);
52 return types::Function::Error;
58 if (in[3]->isString() == false || in[3]->getAs<types::String>()->getSize() != 1)
60 Scierror(999, _("%s: Wrong type for input argument #%d: string expected.\n"), "pathconvert", 4);
61 return types::Function::Error;
64 wchar_t* pwstType = in[3]->getAs<types::String>()->get(0);
65 if (wcscmp(pwstType, WINDOWS_TYPE) == 0)
67 PType = WINDOWS_STYLE;
69 else if (wcscmp(pwstType, UNIX_TYPE) == 0)
75 Scierror(999, _("%s: Wrong value for input argument #%d: '%s' or '%s' expected.\n"), "pathconvert", 4, _UNIX_TYPE, _WINDOWS_TYPE);
76 return types::Function::Error;
82 if (in[2]->isBool() == false || in[2]->getAs<types::Bool>()->getSize() != 1)
84 Scierror(999, _("%s: Wrong type for input argument #%d: A boolean expected.\n"), "pathconvert", 3);
85 return types::Function::Error;
88 iPathExpand = in[2]->getAs<types::Bool>()->get()[0];
93 if (in[1]->isBool() == false || in[1]->getAs<types::Bool>()->getSize() != 1)
95 Scierror(999, _("%s: Wrong type for input argument #%d: A boolean expected.\n"), "pathconvert", 2);
96 return types::Function::Error;
99 iPathTrail = in[1]->getAs<types::Bool>()->get()[0];
102 if (in[0]->isDouble() && in[0]->getAs<types::Double>()->isEmpty())
104 out.push_back(types::Double::Empty());
105 return types::Function::OK;
108 if (in[0]->isString() == false)
110 Scierror(999, _("%s: Wrong type for input argument #%d: Matrix of strings expected.\n"), "pathconvert", 1);
111 return types::Function::Error;
114 types::String* pS = in[0]->getAs<types::String>();
115 types::String* pOut = new types::String(pS->getRows(), pS->getCols());
116 wchar_t** pStr = pOut->get();
119 for (int i = 0 ; i < pS->getSize() ; i++)
121 pStr[i] = pathconvertW(pS->get(i), (BOOL) iPathTrail, (BOOL) iPathExpand, PType);
125 return types::Function::OK;
127 //int *piAddressVarOne = NULL;
128 //wchar_t **pStVarOne = NULL;
130 //int *lenStVarOne = NULL;
131 //int m1 = 0, n1 = 0;
133 //wchar_t **results = NULL;
136 //BOOL flagtrail = TRUE;
137 //BOOL flagexpand = TRUE;
139 //PathConvertType PType = AUTO_STYLE;
141 ///* Check Input & Output parameters */
148 // int *piAddressVarFour = NULL;
149 // wchar_t *pStVarFour = NULL;
151 // int lenStVarFour = 0;
152 // int m4 = 0, n4 = 0;
154 // sciErr = getVarAddressFromPosition(pvApiCtx, 4, &piAddressVarFour);
157 // printError(&sciErr, 0);
161 // sciErr = getVarType(pvApiCtx, piAddressVarFour, &iType4);
164 // printError(&sciErr, 0);
168 // if (iType4 != sci_strings)
170 // Scierror(999,_("%s: Wrong type for input argument #%d: string expected.\n"), fname, 4);
174 // sciErr = getVarDimension(pvApiCtx, piAddressVarFour, &m4, &n4);
177 // printError(&sciErr, 0);
181 // if ( (m4 != n4) && (n4 != 1) )
183 // Scierror(999,_("%s: Wrong size for input argument #%d: string expected.\n"), fname, 4);
187 // sciErr = getMatrixOfWideString(pvApiCtx, piAddressVarFour, &m4, &n4, &lenStVarFour, &pStVarFour);
190 // printError(&sciErr, 0);
194 // pStVarFour = (wchar_t*)MALLOC(sizeof(wchar_t)*(lenStVarFour + 1));
195 // if (pStVarFour == NULL)
197 // Scierror(999,_("%s: Memory allocation error.\n"),fname);
201 // sciErr = getMatrixOfWideString(pvApiCtx, piAddressVarFour, &m4, &n4, &lenStVarFour, &pStVarFour);
204 // printError(&sciErr, 0);
209 // if ( (wcscmp(pStVarFour, L"w") == 0) || (wcscmp(pStVarFour, L"u") == 0) )
211 // if (wcscmp(pStVarFour, L"w") == 0)
213 // PType = WINDOWS_STYLE;
217 // PType = UNIX_STYLE;
219 // if (pStVarFour) {FREE(pStVarFour); pStVarFour = NULL;}
223 // if (pStVarFour) {FREE(pStVarFour); pStVarFour = NULL;}
224 // Scierror(999,_("%s: Wrong value for input argument #%d: ''w'' or ''u'' string expected.\n"), fname, 4);
231 // int *piAddressVarThree = NULL;
232 // int *piData = NULL;
234 // int m3 = 0, n3 = 0;
236 // sciErr = getVarAddressFromPosition(pvApiCtx, 3, &piAddressVarThree);
239 // printError(&sciErr, 0);
243 // sciErr = getVarType(pvApiCtx, piAddressVarThree, &iType3);
246 // printError(&sciErr, 0);
250 // if (iType3 != sci_boolean)
252 // Scierror(999,_("%s: Wrong type for input argument #%d: A boolean expected.\n"), fname, 3);
256 // sciErr = getVarDimension(pvApiCtx, piAddressVarThree, &m3, &n3);
259 // printError(&sciErr, 0);
264 // if ( (m3 != n3) && (n3 != 1) )
266 // Scierror(999,_("%s: Wrong size for input argument #%d: A boolean expected.\n"), fname, 3);
270 // sciErr = getMatrixOfBoolean(pvApiCtx, piAddressVarThree, &m3, &n3, &piData);
273 // printError(&sciErr, 0);
277 // flagexpand = piData[0];
282 // int *piAddressVarTwo = NULL;
283 // int *piData = NULL;
285 // int m2 = 0, n2 = 0;
287 // sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddressVarTwo);
290 // printError(&sciErr, 0);
294 // sciErr = getVarType(pvApiCtx, piAddressVarTwo, &iType2);
297 // printError(&sciErr, 0);
301 // if (iType2 != sci_boolean)
303 // Scierror(999,_("%s: Wrong type for input argument #%d: A boolean expected.\n"), fname, 2);
307 // sciErr = getVarDimension(pvApiCtx, piAddressVarTwo, &m2, &n2);
310 // printError(&sciErr, 0);
314 // if ( (m2 != n2) && (n2 != 1) )
316 // Scierror(999,_("%s: Wrong size for input argument #%d: A boolean expected.\n"), fname, 2);
320 // sciErr = getMatrixOfBoolean(pvApiCtx, piAddressVarTwo, &m2, &n2, &piData);
323 // printError(&sciErr, 0);
327 // flagtrail = piData[0];
330 //sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddressVarOne);
333 // printError(&sciErr, 0);
337 //sciErr = getVarType(pvApiCtx, piAddressVarOne, &iType1);
340 // printError(&sciErr, 0);
344 //if (iType1 == sci_matrix)
346 // sciErr = getVarDimension(pvApiCtx, piAddressVarOne, &m1, &n1);
349 // printError(&sciErr, 0);
353 // if ( (m1 == n1) && (m1 == 0) )
355 // sciErr = createMatrixOfDouble(pvApiCtx, Rhs + 1, m1, n1, NULL);
358 // printError(&sciErr, 0);
362 // LhsVar(1) = Rhs + 1;
367 // Scierror(999,_("%s: Wrong type for input argument #%d: String array expected.\n"), fname, 1);
370 //else if (iType1 == sci_strings)
372 // sciErr = getVarDimension(pvApiCtx, piAddressVarOne, &m1, &n1);
375 // printError(&sciErr, 0);
379 // lenStVarOne = (int*)MALLOC(sizeof(int) * (m1 * n1));
380 // if (lenStVarOne == NULL)
382 // Scierror(999,_("%s: Memory allocation error.\n"),fname);
386 // results = (wchar_t **)MALLOC(sizeof(wchar_t*) * (m1 * n1));
388 // if (results == NULL)
390 // if (lenStVarOne) {FREE(lenStVarOne); lenStVarOne = NULL;}
391 // freeArrayOfWideString(pStVarOne, m1 * n1);
392 // Scierror(999,_("%s: Memory allocation error.\n"),fname);
396 // sciErr = getMatrixOfWideString(pvApiCtx, piAddressVarOne, &m1, &n1, lenStVarOne, pStVarOne);
399 // printError(&sciErr, 0);
403 // pStVarOne = (wchar_t**)MALLOC(sizeof(wchar_t*) * (m1 * n1));
404 // if (pStVarOne == NULL)
406 // if (lenStVarOne) {FREE(lenStVarOne); lenStVarOne = NULL;}
407 // Scierror(999,_("%s: Memory allocation error.\n"),fname);
411 // for( i = 0; i < m1 * n1; i++)
413 // pStVarOne[i] = (wchar_t*)MALLOC(sizeof(wchar_t)*(lenStVarOne[i] + 1));
414 // if (pStVarOne[i] == NULL)
416 // if (lenStVarOne) {FREE(lenStVarOne); lenStVarOne = NULL;}
417 // freeArrayOfWideString(pStVarOne, m1 * n1);
418 // Scierror(999,_("%s: Memory allocation error.\n"),fname);
423 // sciErr = getMatrixOfWideString(pvApiCtx, piAddressVarOne, &m1, &n1, lenStVarOne, pStVarOne);
426 // printError(&sciErr, 0);
430 // for( i = 0; i < m1 * n1; i++)
432 // results[i] = pathconvertW(pStVarOne[i], flagtrail, flagexpand, PType);
435 // sciErr = createMatrixOfWideString(pvApiCtx, Rhs + 1, m1, n1, results);
438 // printError(&sciErr, 0);
442 // LhsVar(1) = Rhs + 1;
445 // if (lenStVarOne) {FREE(lenStVarOne); lenStVarOne = NULL;}
446 // freeArrayOfWideString(results, m1 * n1);
447 // freeArrayOfWideString(pStVarOne, m1 * n1);
451 // Scierror(999,_("%s: Wrong type for input argument #%d: string expected.\n"), fname, 1);
456 /*--------------------------------------------------------------------------*/