1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2012 - Scilab Enterprises - Antoine ELIAS
4  *
5  * Copyright (C) 2012 - 2016 - Scilab Enterprises
6  *
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.
13  */
14 
15 #include "function.hxx"
16 #include "gatewaystruct.hxx"
17 #include "alltypes.hxx"
18 
19 extern "C"
20 {
21 #include <string.h>
22 #include "api_scilab.h"
23 #include "localization.h"
24 #include "Scierror.h"
25 #include "call_scilab.h"
26 #include "sciprint.h"
27 #include "sci_malloc.h"
28 }
29 
30 
31 static int findOptional(void* _pvCtx, char *fname, rhs_opts opts[]);
32 static void printOptionalNames(void* _pvCtx, rhs_opts opts[]);
33 
34 /**************************/
35 /*   optional functions   */
36 /**************************/
getOptionals(void * _pvCtx,char * pstFuncName,rhs_opts opts[])37 int getOptionals(void* _pvCtx, char* pstFuncName, rhs_opts opts[])
38 {
39     types::GatewayStruct* pStr = (types::GatewayStruct*)_pvCtx;
40     types::optional_list opt = *pStr->m_pOpt;
41     int i = 0;
42 
43     /* reset first field since opts is declared static in calling function */
44 
45     while (opts[i].pstName != NULL)
46     {
47         opts[i].iPos = -1;
48         i++;
49     }
50 
51 
52     for (const auto& o : opt)
53     {
54         int typeOfOpt = -1;
55         char* pstOpts = wide_string_to_UTF8(o.first.c_str());
56         int index = findOptional(_pvCtx, pstOpts, opts);
57         FREE(pstOpts);
58 
59         if (index < 0)
60         {
61             sciprint(_("%ls: Unrecognized optional arguments %ls.\n"), pStr->m_pstName, o.first.c_str());
62             printOptionalNames(_pvCtx, opts);
63             return 0;
64         }
65 
66         opts[index].iPos = i + 1;
67         types::GenericType* pGT = (types::GenericType*)o.second;
68         getVarType(_pvCtx, (int*)pGT, &typeOfOpt);
69         opts[index].iType = typeOfOpt;
70 
71         if (typeOfOpt == sci_implicit_poly)
72         {
73             types::InternalType* pIT = NULL;
74             types::ImplicitList* pIL = pGT->getAs<types::ImplicitList>();
75             pIT = pIL->extractFullMatrix();
76             types::Double* impResult = (types::Double*)pIT;
77             opts[index].iRows = impResult->getRows();
78             opts[index].iCols = impResult->getCols();
79             opts[index].piAddr = (int*)impResult;
80             opts[index].iType = sci_matrix;
81         }
82         else
83         {
84             opts[index].iRows = pGT->getRows();
85             opts[index].iCols = pGT->getCols();
86             opts[index].piAddr = (int*)pGT;
87         }
88     }
89     //   int index = -1;
90     //GatewayStruct* pStr = (GatewayStruct*)_pvCtx;
91 
92     //   wchar_t* pwstProperty = to_wide_string(pstProperty);
93 
94     //   for(int i = 0 ; i < pStr->m_pOpt->size() ; i++)
95     //   {
96     //       std::pair<std::wstring, InternalType*> current = (*pStr->m_pOpt)[i];
97     //       if(wcscmp(current.first.c_str(), pwstProperty) == 0)
98     //       {
99     //           index = i;
100     //           break;
101     //       }
102     //   }
103 
104     //   FREE(pwstProperty);
105 
106     return 1;
107 }
108 
FirstOpt(void * _pvCtx)109 int FirstOpt(void* _pvCtx)
110 {
111     types::GatewayStruct* pStr = (types::GatewayStruct*)_pvCtx;
112     return (int)pStr->m_pIn->size() + 1;
113 }
114 
NumOpt(void * _pvCtx)115 int NumOpt(void* _pvCtx)
116 {
117     types::GatewayStruct* pStr = (types::GatewayStruct*)_pvCtx;
118     return (int)pStr->m_pOpt->size();
119 }
120 
FindOpt(void * _pvCtx,char * pstProperty,rhs_opts opts[])121 int FindOpt(void* _pvCtx, char* pstProperty, rhs_opts opts[])
122 {
123     int i = findOptional(_pvCtx, pstProperty, opts);
124     if (i >= 0 && opts[i].iPos > 0)
125     {
126         return i;
127     }
128 
129     return -1;
130 }
131 
findOptional(void * _pvCtx,char * pstProperty,rhs_opts opts[])132 static int findOptional(void* _pvCtx, char *pstProperty, rhs_opts opts[])
133 {
134     int rep = -1, i = 0;
135 
136     while (opts[i].pstName != NULL)
137     {
138         int cmp;
139         /* name is terminated by white space and we want to ignore them */
140         if ((cmp = strcmp(pstProperty, opts[i].pstName)) == 0)
141         {
142             rep = i;
143             break;
144         }
145         else
146         {
147             i++;
148         }
149     }
150     return rep;
151 }
152 
printOptionalNames(void * _pvCtx,rhs_opts opts[])153 void printOptionalNames(void* _pvCtx, rhs_opts opts[])
154 /* array of optinal names (in alphabetical order)
155 * the array is null terminated */
156 {
157     int i = 0;
158 
159     if (opts[i].pstName == NULL)
160     {
161         sciprint(_("Optional argument list is empty.\n"));
162         return;
163     }
164 
165     sciprint(_("Optional arguments list: \n"));
166     while (opts[i + 1].pstName != NULL)
167     {
168         sciprint("%s, ", opts[i].pstName);
169         i++;
170     }
171     sciprint(_("and %s.\n"), opts[i].pstName);
172 }
173