2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) Digiteo 2011 - Cedric DELAMARRE
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.
17 #include "function.hxx"
21 #include "function.hxx"
22 #include "string_gw.hxx"
26 #include "sci_malloc.h"
27 #include "os_string.h"
29 #include "core_math.h"
30 #include "localization.h"
36 types::Function::ReturnValue sci_tokens(types::typed_list &in, int _iRetCount, types::typed_list &out)
38 types::String* pOutString = NULL;
39 types::String* pString = NULL;
40 types::String* pCharSample = NULL;
44 if (in.size() > 2 || in.size() == 0)
46 Scierror(77, _("%s: Wrong number of input argument(s): %d to %d expected.\n"), "tokens", 1, 2);
47 return types::Function::Error;
51 Scierror(78, _("%s: Wrong number of output argument(s): %d expected.\n"), "tokens", 1);
52 return types::Function::Error;
56 if (in[0]->isString() == false)
58 Scierror(999, _("%s: Wrong type for input argument #%d: String expected.\n"), "tokens", 1);
59 return types::Function::Error;
61 pString = in[0]->getAs<types::String>();
62 if (pString->isScalar() == false)
64 Scierror(999, _("%s: Wrong size for input argument #%d.\n"), "tokens", 1);
65 return types::Function::Error;
67 if (wcslen(pString->get(0)) == 0)
69 types::Double* pOutDouble = types::Double::Empty();
70 out.push_back(pOutDouble);
71 return types::Function::OK;
77 if (in[1]->isString() == false)
79 Scierror(999, _("%s: Wrong type for input argument #%d: String expected.\n"), "tokens", 2);
80 return types::Function::Error;
82 pCharSample = in[1]->getAs<types::String>();
84 if (pCharSample->getSize() == 0)
86 Scierror(999, _("%s: Wrong size for input argument #%d.\n"), "tokens", 2);
87 return types::Function::Error;
89 sizeSeps = pCharSample->getSize();
90 seps = (wchar_t*)MALLOC((sizeSeps + 1) * sizeof(wchar_t));
91 for (int i = 0; i < sizeSeps ; i++)
93 int iLen = (int)wcslen(pCharSample->get(i));
94 if (iLen > 1 || iLen < 0)
96 Scierror(999, _("%s: Wrong type for input argument #%d: Char(s) expected.\n"), "tokens", 2);
99 return types::Function::Error;
101 seps[i] = pCharSample->get(i)[0];
104 else // default delimiters are ' ' and Tabulation
107 seps = (wchar_t*)MALLOC((sizeSeps + 1) * sizeof(wchar_t));
111 seps[sizeSeps] = L'\0';
114 int dimsArray[2] = {0, 1};
117 wchar_t** Output_Strings = stringTokens(pString->get(0), seps, &dimsArray[0]);
119 if (Output_Strings == NULL)
121 //return empty matrix
122 out.push_back(types::Double::Empty());
123 return types::Function::OK;
127 pOutString = new types::String(dims, dimsArray);
128 pOutString->set(Output_Strings);
130 for (int i = 0 ; i < dimsArray[0] ; i++)
132 FREE(Output_Strings[i]);
134 FREE(Output_Strings);
137 out.push_back(pOutString);
138 return types::Function::OK;