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