Replace Min, Max and Abs by std::min, std::max and std::abs
[scilab.git] / scilab / modules / core / sci_gateway / cpp / sci_getversion.cpp
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2006 - INRIA - Allan CORNET
4  * Copyright (C) 2008 - INRIA - Bruno JOFRET
5  * Copyright (C) 2010 - DIGITEO - Allan CORNET
6  *
7  * This file must be used under the terms of the CeCILL.
8  * This source file is licensed as described in the file COPYING, which
9  * you should have received as part of this distribution.  The terms
10  * are also available at
11  * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
12  *
13  */
14 /*--------------------------------------------------------------------------*/
15 #include "funcmanager.hxx"
16 #include "core_gw.hxx"
17 #include "string.hxx"
18 #include "double.hxx"
19 #include "function.hxx"
20
21 extern "C"
22 {
23 #include "sci_malloc.h"
24 #include "getversion.h"
25 #include "localization.h"
26 #include "Scierror.h"
27 #include "with_module.h"
28 #include "freeArrayOfString.h"
29 }
30 /*--------------------------------------------------------------------------*/
31 #define VERSION_STRING L"string_info"
32 /*--------------------------------------------------------------------------*/
33 static int getversion_no_rhs(char *fname, void* pvApiCtx);
34 static int getversion_one_rhs(char *fname, void* pvApiCtx);
35 static int getversion_two_rhs(char *fname, void* pvApiCtx);
36 /*--------------------------------------------------------------------------*/
37
38 using namespace types;
39
40 Function::ReturnValue sci_getversion(types::typed_list &in, int _iRetCount, types::typed_list &out)
41 {
42     if (in.size() > 2)
43     {
44         Scierror(77, _("%s: Wrong number of input argument(s): %d to %d expected.\n"), "getversion", 0, 2);
45         return Function::Error;
46     }
47
48     if (in.size() == 0)
49     {
50         if (_iRetCount != 1 && _iRetCount != 2)
51         {
52             Scierror(78, _("%s: Wrong number of output argument(s): %d to %d expected.\n"), "getveresion", 1, 2);
53             return Function::Error;
54         }
55
56         wchar_t *pwstVer = getScilabVersionAsWideString();
57         String* pOut1 = new String(pwstVer);
58         out.push_back(pOut1);
59         FREE(pwstVer);
60
61         if (_iRetCount == 2)
62         {
63             int iOption = 0;
64             wchar_t** pwstOption = getScilabVersionOptions(&iOption);
65             String* pOut2 = new String(1, iOption);
66             pOut2->set(pwstOption);
67             out.push_back(pOut2);
68             freeArrayOfWideString(pwstOption, iOption);
69         }
70
71     }
72     else if (in.size() == 1)
73     {
74         if (in[0]->isString() == false || in[0]->getAs<types::String>()->isScalar() == false)
75         {
76             Scierror(999, _("%s: Wrong size for input argument #%d: String expected.\n"), "getversion", 1);
77             return Function::Error;
78         }
79
80         if (_iRetCount != 1)
81         {
82             Scierror(78, _("%s: Wrong number of output argument(s): %d expected.\n"), "getveresion", 1);
83             return Function::Error;
84         }
85
86         wchar_t* pwstModule = in[0]->getAs<types::String>()->get()[0];
87         if (with_module(pwstModule) || (wcscmp(pwstModule, L"scilab") == 0))
88         {
89             int versionSize = 0;
90             int *version = getModuleVersion(pwstModule, &versionSize);
91             if (version == NULL)
92             {
93                 Scierror(999, _("%s: Wrong file version.xml %s.\n"), "getversion", pwstModule);
94                 return Function::Error;
95             }
96
97             Double* pOut = new Double(1, versionSize);
98             pOut->setInt(version);
99             out.push_back(pOut);
100             FREE(version);
101         }
102     }
103     else //in.size() == 2
104     {
105         if (in[0]->isString() == false || in[0]->getAs<types::String>()->getSize() != 1)
106         {
107             Scierror(999, _("%s: Wrong size for input argument #%d: String expected.\n"), "getversion", 1);
108             return Function::Error;
109         }
110
111         if (in[1]->isString() == false || in[1]->getAs<types::String>()->getSize() != 1)
112         {
113             Scierror(999, _("%s: Wrong size for input argument #%d: String expected.\n"), "getversion", 2);
114             return Function::Error;
115         }
116
117         wchar_t* pwstModule = in[0]->getAs<types::String>()->get()[0];
118         wchar_t* pwstOption = in[1]->getAs<types::String>()->get()[0];
119
120         if ( with_module(pwstModule) || (wcscmp(pwstModule, L"scilab") == 0) )
121         {
122             if ( wcscmp(pwstOption, VERSION_STRING) == 0)
123             {
124                 wchar_t *pwstInfo = getModuleVersionInfoAsString(pwstModule);
125                 String* pOut = new String(pwstInfo);
126                 out.push_back(pOut);
127                 FREE(pwstInfo);
128             }
129         }
130     }
131
132     return Function::OK;
133     //Rhs = std::max(0,Rhs);
134
135     //  CheckRhs(0,2);
136     //
137     //  if (Rhs == 0)
138     //  {
139     //          CheckLhs(1,2);
140     //          getversion_no_rhs(fname, pvApiCtx);
141     //  }
142     //  else if (Rhs == 1)
143     //  {
144     //          CheckLhs(1,1);
145     //          getversion_one_rhs(fname, pvApiCtx);
146     //  }
147     //  else /* Rhs == 2 */
148     //  {
149     //          CheckLhs(1,1);
150     //          getversion_two_rhs(fname, pvApiCtx);
151     //  }
152     //  return 0;
153     //}
154     ///*--------------------------------------------------------------------------*/
155     //int getversion_no_rhs(char *fname, void* pvApiCtx)
156     //{
157     //  char *version = getScilabVersionAsString();
158     //  if (version)
159     //  {
160     //          createSingleString(pvApiCtx, Rhs + 1, version);
161     //          LhsVar(1) = Rhs + 1;
162     //          FREE(version);
163     //          version = NULL;
164     //  }
165     //  else
166     //  {
167     //          Scierror(999,_("%s: Memory allocation error.\n"), fname);
168     //          return 0;
169     //  }
170     //
171     //  if (Lhs == 2)
172     //  {
173     //          int sizeOptions = 0;
174     //          char **ScilabOptions = getScilabVersionOptions(&sizeOptions);
175     //
176     //          if (ScilabOptions)
177     //          {
178     //                  SciErr sciErr;
179     //                  int m = 1;
180     //                  int n = sizeOptions;
181     //                  sciErr = createMatrixOfString(pvApiCtx, Rhs + 2, m, n, ScilabOptions);
182     //                  freeArrayOfString(ScilabOptions, sizeOptions);
183     //
184     //                  if(sciErr.iErr)
185     //                  {
186     //                          printError(&sciErr, 0);
187     //                          return 0;
188     //                  }
189     //
190     //                  LhsVar(2) = Rhs + 2;
191     //          }
192     //          else
193     //          {
194     //                  Scierror(999,_("%s: Memory allocation error.\n"), fname);
195     //                  return 0;
196     //          }
197     //  }
198     //
199     //  C2F(putlhsvar)();
200     //
201     //  return 0;
202     //}
203     ///*--------------------------------------------------------------------------*/
204     //int getversion_one_rhs(char *fname, void* pvApiCtx)
205     //{
206     //  SciErr sciErr;
207     //  int *piAddressVarOne = NULL;
208     //
209     //  sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddressVarOne);
210     //  if(sciErr.iErr)
211     //  {
212     //          printError(&sciErr, 0);
213     //          return 0;
214     //  }
215     //
216     //  if (isStringType(pvApiCtx, piAddressVarOne))
217     //  {
218     //          char *modulename = NULL;
219     //
220     //          if (!isScalar(pvApiCtx, piAddressVarOne))
221     //          {
222     //                  Scierror(999,_("%s: Wrong size for input argument #%d: String expected.\n"), fname, 1);
223     //                  return 0;
224     //          }
225     //
226     //          if (getAllocatedSingleString(pvApiCtx, piAddressVarOne, &modulename) == 0)
227     //          {
228     //                  if (modulename)
229     //                  {
230     //                          if ( with_module(modulename) || (strcmp(modulename, "scilab") == 0) )
231     //                          {
232     //                                  int versionSize = 0;
233     //                                  int *version = getModuleVersion(modulename, &versionSize);
234     //
235     //                                  if (version)
236     //                                  {
237     //                                          int m = 1;
238     //                                          int n = versionSize;
239     //                                          double *versionAsDouble = (double*)MALLOC(sizeof(double) * versionSize);
240     //                                          if (versionAsDouble)
241     //                                          {
242     //                                                  int i = 0;
243     //                                                  for (i = 0; i < versionSize; i++)
244     //                                                  {
245     //                                                          versionAsDouble[i] = (double)version[i];
246     //                                                  }
247     //                                                  FREE(version);
248     //                                                  version = NULL;
249     //
250     //                                                  freeAllocatedSingleString(modulename);
251     //                                                  modulename = NULL;
252     //
253     //                                                  sciErr = createMatrixOfDouble(pvApiCtx, Rhs + 1, m, n, versionAsDouble);
254     //                                                  FREE(versionAsDouble);
255     //                                                  versionAsDouble = NULL;
256     //
257     //                                                  if(sciErr.iErr)
258     //                                                  {
259     //                                                          printError(&sciErr, 0);
260     //                                                          return 0;
261     //                                                  }
262     //
263     //                                                  LhsVar(1) = Rhs + 1;
264     //                                                  C2F(putlhsvar)();
265     //                                          }
266     //                                          else
267     //                                          {
268     //                                                  Scierror(999,_("%s: Memory allocation error.\n"), fname);
269     //                                                  return 0;
270     //                                          }
271     //                                  }
272     //                                  else
273     //                                  {
274     //                                          Scierror(999,_("%s: Wrong file version.xml %s.\n"), fname, modulename);
275     //
276     //                                          freeAllocatedSingleString(modulename);
277     //                                          modulename = NULL;
278     //
279     //                                          return 0;
280     //                                  }
281     //                          }
282     //                          else
283     //                          {
284     //                                  Scierror(999,_("%s: Wrong module name %s.\n"), fname , modulename);
285     //
286     //                                  freeAllocatedSingleString(modulename);
287     //                                  modulename = NULL;
288     //
289     //                                  return 0;
290     //                          }
291     //                          freeAllocatedSingleString(modulename);
292     //                          modulename = NULL;
293     //                  }
294     //                  else
295     //                  {
296     //                          Scierror(999,_("%s: Memory allocation error.\n"), fname);
297     //                          return 0;
298     //                  }
299     //          }
300     //          else
301     //          {
302     //                  Scierror(999,_("%s: Memory allocation error.\n"), fname);
303     //                  return 0;
304     //          }
305     //  }
306     //  else
307     //  {
308     //          Scierror(999,_("%s: Wrong type for input argument #%d: String expected.\n"), fname, 1);
309     //          return 0;
310     //  }
311     //  return 0;
312     //}
313     ///*--------------------------------------------------------------------------*/
314     //int getversion_two_rhs(char *fname, void* pvApiCtx)
315     //{
316     //  SciErr sciErr;
317     //  int *piAddressVarOne = NULL;
318     //  int *piAddressVarTwo = NULL;
319     //
320     //  sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddressVarOne);
321     //  if(sciErr.iErr)
322     //  {
323     //          printError(&sciErr, 0);
324     //          return 0;
325     //  }
326     //
327     //  sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddressVarTwo);
328     //  if(sciErr.iErr)
329     //  {
330     //          printError(&sciErr, 0);
331     //          return 0;
332     //  }
333     //
334     //  if (isStringType(pvApiCtx, piAddressVarOne) && isStringType(pvApiCtx, piAddressVarTwo))
335     //  {
336     //          char *modulename = NULL;
337     //          char *optionname = NULL;
338     //
339     //          if (!isScalar(pvApiCtx, piAddressVarOne))
340     //          {
341     //                  Scierror(999,_("%s: Wrong size for input argument #%d: String expected.\n"), fname, 1);
342     //                  return 0;
343     //          }
344     //
345     //          if (!isScalar(pvApiCtx, piAddressVarTwo))
346     //          {
347     //                  Scierror(999,_("%s: Wrong size for input argument #%d: String expected.\n"), fname, 2);
348     //                  return 0;
349     //          }
350     //
351     //          if ( (getAllocatedSingleString(pvApiCtx, piAddressVarOne, &modulename) == 0) &&
352     //                  (getAllocatedSingleString(pvApiCtx, piAddressVarTwo, &optionname) == 0) )
353     //          {
354     //                  if ( (modulename) && (optionname) )
355     //                  {
356     //                          if ( with_module(modulename) || (strcmp(modulename, "scilab") == 0) )
357     //                          {
358     //                                  if ( strcmp(optionname, VERSION_STRING) == 0)
359     //                                  {
360     //                                          char *versionInfo = getModuleVersionInfoAsString(modulename);
361     //
362     //                                          if (versionInfo)
363     //                                          {
364     //                                                  createSingleString(pvApiCtx, Rhs + 1, versionInfo);
365     //
366     //                                                  FREE(versionInfo);
367     //                                                  versionInfo = NULL;
368     //
369     //                                                  LhsVar(1) = Rhs + 1;
370     //                                                  C2F(putlhsvar)();
371     //                                          }
372     //                                          else
373     //                                          {
374     //                                                  Scierror(999,_("%s: Wrong file version.xml %s.\n"), fname, modulename);
375     //
376     //                                                  freeAllocatedSingleString(modulename);
377     //                                                  modulename = NULL;
378     //                                                  freeAllocatedSingleString(optionname);
379     //                                                  optionname = NULL;
380     //
381     //                                                  return 0;
382     //                                          }
383     //                                  }
384     //                                  else
385     //                                  {
386     //                                          freeAllocatedSingleString(modulename);
387     //                                          modulename = NULL;
388     //                                          freeAllocatedSingleString(optionname);
389     //                                          optionname = NULL;
390     //
391     //                                          Scierror(999,_("%s: Wrong value for input argument #%d: '%s' expected.\n"), fname, 2, VERSION_STRING);
392     //                                          return 0;
393     //                                  }
394     //                          }
395     //                          else
396     //                          {
397     //                                  Scierror(999,_("%s: Wrong module name %s.\n"), fname , modulename);
398     //
399     //                                  freeAllocatedSingleString(modulename);
400     //                                  modulename = NULL;
401     //                                  freeAllocatedSingleString(optionname);
402     //                                  optionname = NULL;
403     //
404     //                                  return 0;
405     //                          }
406     //                  }
407     //                  else
408     //                  {
409     //                          Scierror(999,_("%s: Memory allocation error.\n"), fname);
410     //                          return 0;
411     //                  }
412     //          }
413     //          else
414     //          {
415     //                  Scierror(999,_("%s: Memory allocation error.\n"), fname);
416     //                  return 0;
417     //          }
418     //  }
419     //  else
420     //  {
421     //          Scierror(999,_("%s: Wrong type for input arguments #%d and #%d: Strings expected.\n"), fname, 1, 2);
422     //          return 0;
423     //  }
424     //
425     //  return 0;
426 }
427 /*--------------------------------------------------------------------------*/