a1b8dddc5fe7196a59b35050c05007e2d086f054
[scilab.git] / scilab / modules / fileio / sci_gateway / cpp / sci_pathconvert.cpp
1 /*
2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2009 - DIGITEO - Allan CORNET
4 *
5 * This file must be used under the terms of the CeCILL.
6 * This source file is licensed as described in the file COPYING, which
7 * you should have received as part of this distribution.  The terms
8 * are also available at
9 * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
10 *
11 */
12 /*--------------------------------------------------------------------------*/
13 #include "fileio_gw.hxx"
14 #include "function.hxx"
15 #include "string.hxx"
16 #include "double.hxx"
17 #include "bool.hxx"
18
19 extern "C"
20 {
21 #include "MALLOC.h"
22 #include "localization.h"
23 #include "Scierror.h"
24 #include "pathconvert.h"
25 }
26
27 #define UNIX_TYPE       L"u"
28 #define WINDOWS_TYPE    L"w"
29
30 using namespace types;
31
32 /*--------------------------------------------------------------------------*/
33 Function::ReturnValue sci_pathconvert(typed_list &in, int _iRetCount, typed_list &out)
34 {
35         PathConvertType PType   = AUTO_STYLE;
36     int iPathExpand         = 1;
37     int iPathTrail          = 1;
38
39     if(in.size() < 1 && in.size() > 4)
40     {
41         ScierrorW(999, _W("%ls: Wrong number of input arguments: %d to %d expected.\n"), L"pathconvert" , 1, 4);
42         return Function::Error;
43     }
44
45     if(_iRetCount != 1)
46     {
47         ScierrorW(78,_W("%ls: Wrong number of output argument(s): %d expected.\n"), L"pathconvert", 1);
48         return Function::Error;
49     }
50
51     //get type
52     if(in.size() > 3)
53     {
54         if(in[3]->isString() == false || in[3]->getAs<types::String>()->getSize() != 1)
55         {
56             ScierrorW(999, _W("%ls: Wrong type for input argument #%d: A string expected.\n"), L"pathconvert", 4);
57             return Function::Error;
58         }
59
60         wchar_t* pwstType = in[3]->getAs<types::String>()->get(0);
61         if(wcscmp(pwstType, WINDOWS_TYPE) == 0)
62         {
63             PType = WINDOWS_STYLE;
64         }
65         else if(wcscmp(pwstType, UNIX_TYPE) == 0)
66         {
67             PType = UNIX_STYLE;
68         }
69         else
70         {
71             ScierrorW(999,_W("%ls: Wrong value for input argument #%d: '%ls' or '%ls' expected.\n"), L"pathconvert", 4, UNIX_TYPE, WINDOWS_TYPE);
72             return Function::Error;
73         }
74     }
75
76     if(in.size() > 2)
77     {
78         if(in[2]->isBool() == false || in[2]->getAs<types::Bool>()->getSize() != 1)
79         {
80             ScierrorW(999, _W("%ls: Wrong type for input argument #%d: A boolean expected.\n"), L"pathconvert", 3);
81             return Function::Error;
82         }
83
84         iPathExpand = in[2]->getAs<types::Bool>()->get()[0];
85     }
86
87     if(in.size() > 1)
88     {
89         if(in[1]->isBool() == false || in[1]->getAs<types::Bool>()->getSize() != 1)
90         {
91             ScierrorW(999, _W("%ls: Wrong type for input argument #%d: A boolean expected.\n"), L"pathconvert", 2);
92             return Function::Error;
93         }
94
95         iPathTrail = in[1]->getAs<types::Bool>()->get()[0];
96     }
97
98     if(in[0]->isDouble() && in[0]->getAs<Double>()->isEmpty())
99     {
100         out.push_back(Double::Empty());
101         return Function::OK;
102     }
103
104     if(in[0]->isString() == false)
105     {
106         ScierrorW(999, _W("%ls: Wrong type for input argument #%d: Matrix of strings expected.\n"), L"pathconvert", 1);
107         return Function::Error;
108     }
109
110     String* pS      = in[0]->getAs<types::String>();
111     String* pOut    = new String(pS->getRows(), pS->getCols());
112     wchar_t** pStr  = pOut->get();
113
114
115     for(int i = 0 ; i < pS->getSize() ; i++)
116     {
117         pStr[i] = pathconvertW(pS->get(i), (BOOL) iPathTrail, (BOOL) iPathExpand, PType);
118     }
119
120     out.push_back(pOut);
121     return Function::OK;
122         //SciErr sciErr;
123         //int *piAddressVarOne = NULL;
124         //wchar_t **pStVarOne = NULL;
125         //int iType1 = 0;
126         //int *lenStVarOne = NULL;
127         //int m1 = 0, n1 = 0;
128
129         //wchar_t **results = NULL;
130         //int i = 0;
131
132         //BOOL flagtrail = TRUE;
133         //BOOL flagexpand = TRUE;
134
135         //PathConvertType PType = AUTO_STYLE;
136
137         ///* Check Input & Output parameters */
138         //CheckRhs(1,4);
139         //CheckLhs(1,1);
140
141
142         //if (Rhs > 3)
143         //{
144         //      int *piAddressVarFour = NULL;
145         //      wchar_t *pStVarFour = NULL;
146         //      int iType4 = 0;
147         //      int lenStVarFour = 0;
148         //      int m4 = 0, n4 = 0;
149
150         //      sciErr = getVarAddressFromPosition(_piKey, 4, &piAddressVarFour);
151         //      if(sciErr.iErr)
152         //      {
153         //              printError(&sciErr, 0);
154         //              return 0;
155         //      }
156
157         //      sciErr = getVarType(_piKey, piAddressVarFour, &iType4);
158         //      if(sciErr.iErr)
159         //      {
160         //              printError(&sciErr, 0);
161         //              return 0;
162         //      }
163
164         //      if (iType4 != sci_strings)
165         //      {
166         //              Scierror(999,_("%s: Wrong type for input argument #%d: A string expected.\n"), fname, 4);
167         //              return 0;
168         //      }
169
170         //      sciErr = getVarDimension(_piKey, piAddressVarFour, &m4, &n4);
171         //      if(sciErr.iErr)
172         //      {
173         //              printError(&sciErr, 0);
174         //              return 0;
175         //      }
176
177         //      if ( (m4 != n4) && (n4 != 1) )
178         //      {
179         //              Scierror(999,_("%s: Wrong size for input argument #%d: A string expected.\n"), fname, 4);
180         //              return 0;
181         //      }
182
183         //      sciErr = getMatrixOfWideString(_piKey, piAddressVarFour, &m4, &n4, &lenStVarFour, &pStVarFour);
184         //      if(sciErr.iErr)
185         //      {
186         //              printError(&sciErr, 0);
187         //              return 0;
188         //      }
189
190         //      pStVarFour = (wchar_t*)MALLOC(sizeof(wchar_t)*(lenStVarFour + 1));
191         //      if (pStVarFour == NULL)
192         //      {
193         //              Scierror(999,_("%s: Memory allocation error.\n"),fname);
194         //              return 0;
195         //      }
196
197         //      sciErr = getMatrixOfWideString(_piKey, piAddressVarFour, &m4, &n4, &lenStVarFour, &pStVarFour);
198         //      if(sciErr.iErr)
199         //      {
200         //              printError(&sciErr, 0);
201         //              return 0;
202         //      }
203
204
205         //      if ( (wcscmp(pStVarFour, L"w") == 0) || (wcscmp(pStVarFour, L"u") == 0) )
206         //      {
207         //              if (wcscmp(pStVarFour, L"w") == 0)
208         //              {
209         //                      PType = WINDOWS_STYLE;
210         //              }
211         //              else
212         //              {
213         //                      PType = UNIX_STYLE;
214         //              }
215         //              if (pStVarFour) {FREE(pStVarFour); pStVarFour = NULL;}
216         //      }
217         //      else
218         //      {
219         //              if (pStVarFour) {FREE(pStVarFour); pStVarFour = NULL;}
220         //              Scierror(999,_("%s: Wrong value for input argument #%d: ''w'' or ''u'' string expected.\n"), fname, 4);
221         //              return 0;
222         //      }
223         //}
224
225         //if (Rhs > 2)
226         //{
227         //      int *piAddressVarThree = NULL;
228         //      int *piData = NULL;
229         //      int iType3      = 0;
230         //      int m3 = 0, n3 = 0;
231
232         //      sciErr = getVarAddressFromPosition(_piKey, 3, &piAddressVarThree);
233         //      if(sciErr.iErr)
234         //      {
235         //              printError(&sciErr, 0);
236         //              return 0;
237         //      }
238
239         //      sciErr = getVarType(_piKey, piAddressVarThree, &iType3);
240         //      if(sciErr.iErr)
241         //      {
242         //              printError(&sciErr, 0);
243         //              return 0;
244         //      }
245
246         //      if (iType3 != sci_boolean)
247         //      {
248         //              Scierror(999,_("%s: Wrong type for input argument #%d: A boolean expected.\n"), fname, 3);
249         //              return 0;
250         //      }
251
252         //      sciErr = getVarDimension(_piKey, piAddressVarThree, &m3, &n3);
253         //      if(sciErr.iErr)
254         //      {
255         //              printError(&sciErr, 0);
256         //              return 0;
257         //      }
258
259
260         //      if ( (m3 != n3) && (n3 != 1) )
261         //      {
262         //              Scierror(999,_("%s: Wrong size for input argument #%d: A boolean expected.\n"), fname, 3);
263         //              return 0;
264         //      }
265
266         //      sciErr = getMatrixOfBoolean(_piKey, piAddressVarThree, &m3, &n3,  &piData);
267         //      if(sciErr.iErr)
268         //      {
269         //              printError(&sciErr, 0);
270         //              return 0;
271         //      }
272
273         //      flagexpand = piData[0];
274         //}
275
276         //if (Rhs > 1)
277         //{
278         //      int *piAddressVarTwo = NULL;
279         //      int *piData = NULL;
280         //      int iType2      = 0;
281         //      int m2 = 0, n2 = 0;
282
283         //      sciErr = getVarAddressFromPosition(_piKey, 2, &piAddressVarTwo);
284         //      if(sciErr.iErr)
285         //      {
286         //              printError(&sciErr, 0);
287         //              return 0;
288         //      }
289
290         //      sciErr = getVarType(_piKey, piAddressVarTwo, &iType2);
291         //      if(sciErr.iErr)
292         //      {
293         //              printError(&sciErr, 0);
294         //              return 0;
295         //      }
296
297         //      if (iType2 != sci_boolean)
298         //      {
299         //              Scierror(999,_("%s: Wrong type for input argument #%d: A boolean expected.\n"), fname, 2);
300         //              return 0;
301         //      }
302
303         //      sciErr = getVarDimension(_piKey, piAddressVarTwo, &m2, &n2);
304         //      if(sciErr.iErr)
305         //      {
306         //              printError(&sciErr, 0);
307         //              return 0;
308         //      }
309
310         //      if ( (m2 != n2) && (n2 != 1) )
311         //      {
312         //              Scierror(999,_("%s: Wrong size for input argument #%d: A boolean expected.\n"), fname, 2);
313         //              return 0;
314         //      }
315
316         //      sciErr = getMatrixOfBoolean(_piKey, piAddressVarTwo, &m2, &n2,  &piData);
317         //      if(sciErr.iErr)
318         //      {
319         //              printError(&sciErr, 0);
320         //              return 0;
321         //      }
322
323         //      flagtrail = piData[0];
324         //}
325
326         //sciErr = getVarAddressFromPosition(_piKey, 1, &piAddressVarOne);
327         //if(sciErr.iErr)
328         //{
329         //      printError(&sciErr, 0);
330         //      return 0;
331         //}
332
333         //sciErr = getVarType(_piKey, piAddressVarOne, &iType1);
334         //if(sciErr.iErr)
335         //{
336         //      printError(&sciErr, 0);
337         //      return 0;
338         //}
339
340         //if (iType1 == sci_matrix)
341         //{
342         //      sciErr = getVarDimension(_piKey, piAddressVarOne, &m1, &n1);
343         //      if(sciErr.iErr)
344         //      {
345         //              printError(&sciErr, 0);
346         //              return 0;
347         //      }
348
349         //      if ( (m1 == n1) && (m1 == 0) )
350         //      {
351         //              sciErr = createMatrixOfDouble(_piKey, Rhs + 1, m1, n1, NULL);
352         //              if(sciErr.iErr)
353         //              {
354         //                      printError(&sciErr, 0);
355         //                      return 0;
356         //              }
357
358         //              LhsVar(1) = Rhs + 1;
359         //              C2F(putlhsvar)();
360         //      }
361         //      else
362         //      {
363         //              Scierror(999,_("%s: Wrong type for input argument #%d: String array expected.\n"), fname, 1);
364         //      }
365         //}
366         //else if (iType1 == sci_strings)
367         //{
368         //      sciErr = getVarDimension(_piKey, piAddressVarOne, &m1, &n1);
369         //      if(sciErr.iErr)
370         //      {
371         //              printError(&sciErr, 0);
372         //              return 0;
373         //      }
374
375         //      lenStVarOne = (int*)MALLOC(sizeof(int) * (m1 * n1));
376         //      if (lenStVarOne == NULL)
377         //      {
378         //              Scierror(999,_("%s: Memory allocation error.\n"),fname);
379         //              return 0;
380         //      }
381
382         //      results = (wchar_t **)MALLOC(sizeof(wchar_t*) * (m1 * n1));
383
384         //      if (results == NULL)
385         //      {
386         //              if (lenStVarOne) {FREE(lenStVarOne); lenStVarOne = NULL;}
387         //              freeArrayOfWideString(pStVarOne, m1 * n1);
388         //              Scierror(999,_("%s: Memory allocation error.\n"),fname);
389         //              return 0;
390         //      }
391
392         //      sciErr = getMatrixOfWideString(_piKey, piAddressVarOne, &m1, &n1, lenStVarOne, pStVarOne);
393         //      if(sciErr.iErr)
394         //      {
395         //              printError(&sciErr, 0);
396         //              return 0;
397         //      }
398
399         //      pStVarOne = (wchar_t**)MALLOC(sizeof(wchar_t*) * (m1 * n1));
400         //      if (pStVarOne == NULL)
401         //      {
402         //              if (lenStVarOne) {FREE(lenStVarOne); lenStVarOne = NULL;}
403         //              Scierror(999,_("%s: Memory allocation error.\n"),fname);
404         //              return 0;
405         //      }
406
407         //      for( i = 0; i < m1 * n1; i++)
408         //      {
409         //              pStVarOne[i] = (wchar_t*)MALLOC(sizeof(wchar_t)*(lenStVarOne[i] + 1));
410         //              if (pStVarOne[i] == NULL)
411         //              {
412         //                      if (lenStVarOne) {FREE(lenStVarOne); lenStVarOne = NULL;}
413         //                      freeArrayOfWideString(pStVarOne, m1 * n1);
414         //                      Scierror(999,_("%s: Memory allocation error.\n"),fname);
415         //                      return 0;
416         //              }
417         //      }
418
419         //      sciErr = getMatrixOfWideString(_piKey, piAddressVarOne, &m1, &n1, lenStVarOne, pStVarOne);
420         //      if(sciErr.iErr)
421         //      {
422         //              printError(&sciErr, 0);
423         //              return 0;
424         //      }
425
426         //      for( i = 0; i < m1 * n1; i++)
427         //      {
428         //              results[i] = pathconvertW(pStVarOne[i], flagtrail, flagexpand, PType);
429         //      }
430
431         //      sciErr = createMatrixOfWideString(_piKey, Rhs + 1, m1, n1, results);
432         //      if(sciErr.iErr)
433         //      {
434         //              printError(&sciErr, 0);
435         //              return 0;
436         //      }
437
438         //      LhsVar(1) = Rhs + 1;
439         //      C2F(putlhsvar)();
440
441         //      if (lenStVarOne) {FREE(lenStVarOne); lenStVarOne = NULL;}
442         //      freeArrayOfWideString(results, m1 * n1);
443         //      freeArrayOfWideString(pStVarOne, m1 * n1);
444         //}
445         //else
446         //{
447         //      Scierror(999,_("%s: Wrong type for input argument #%d: A string expected.\n"), fname, 1);
448         //      return 0;
449         //}
450         //return 0;
451 }
452 /*--------------------------------------------------------------------------*/