2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2010-2010 - DIGITEO - Bruno JOFRET
4 * Copyright (C) 2013 - Scilab Enterprises - Cedric Delamarre
6 * Copyright (C) 2012 - 2016 - Scilab Enterprises
8 * This file is hereby licensed under the terms of the GNU GPL v2.0,
9 * pursuant to article 5.3.4 of the CeCILL v.2.1.
10 * This file was originally licensed under the terms of the CeCILL v2.1,
11 * and continues to be available under such terms.
12 * For more information, see the COPYING file which you should have received
13 * along with this program.
17 #include "core_gw.hxx"
18 #include "function.hxx"
19 #include "context.hxx"
26 #include "localization.h"
38 static ScopeRange getScopeFromOption(const wchar_t *_psScope)
40 if (wcscmp(_psScope, L"all") == 0 || wcscmp(_psScope, L"a") == 0)
44 if (wcscmp(_psScope, L"local") == 0 || wcscmp(_psScope, L"l") == 0)
48 if (wcscmp(_psScope, L"nolocal") == 0 || wcscmp(_psScope, L"n") == 0)
56 static types::Function::ReturnValue isdef(types::typed_list& in, int _iRetCount, types::typed_list& out, const char* fname)
58 types::String* pStrIn = NULL;
60 if (in.size() != 1 && in.size() != 2)
62 Scierror(77, _("%s: Wrong number of input argument(s): %d to %d expected."), fname, 1, 2);
63 return types::Function::Error;
66 if (in[0]->isDouble() && in[0]->getAs<types::Double>()->isEmpty())
68 out.push_back(types::Double::Empty());
69 return types::Function::OK;
72 if (!in[0]->isString())
74 Scierror(999, _("%s: Wrong type for argument #%d: Matrix of strings expected.\n"), fname, 1);
75 return types::Function::Error;
78 if (in.size() == 2 && (!in[1]->isString() || in[1]->getAs<types::String>()->getSize() != 1))
80 Scierror(999, _("%s: Wrong type for input argument #%d: A single string expected.\n"), fname, 2);
81 return types::Function::Error;
84 const wchar_t *psScope = L"all"; // Default option is "all"
87 psScope = in[1]->getAs<types::String>()->get(0);
90 pStrIn = in[0]->getAs<types::String>();
92 types::InternalType *pIT;
93 types::Bool* pBOut = new types::Bool(pStrIn->getDims(), pStrIn->getDimsArray());
95 switch (getScopeFromOption(psScope))
98 for (int i = 0; i < pStrIn->getSize(); i++)
100 pIT = symbol::Context::getInstance()->get(symbol::Symbol(pStrIn->get(i)));
101 pBOut->set(i, pIT != NULL && pIT->getType() != types::InternalType::ScilabVoid);
105 for (int i = 0; i < pStrIn->getSize(); i++)
107 pIT = symbol::Context::getInstance()->getCurrentLevel(symbol::Symbol(pStrIn->get(i)));
108 pBOut->set(i, pIT != NULL && pIT->getType() != types::InternalType::ScilabVoid);
112 for (int i = 0; i < pStrIn->getSize(); i++)
114 pIT = symbol::Context::getInstance()->getAllButCurrentLevel(symbol::Symbol(pStrIn->get(i)));
115 pBOut->set(i, pIT != NULL && pIT->getType() != types::InternalType::ScilabVoid);
119 Scierror(36, _("%s: Wrong input argument %d.\n"), fname, 2);
120 return types::Function::Error;
123 out.push_back(pBOut);
125 return types::Function::OK;
128 types::Function::ReturnValue sci_isdef(types::typed_list &in, int _iRetCount, types::typed_list &out)
130 return isdef(in, _iRetCount, out, "isdef");
133 types::Function::ReturnValue sci_exists(types::typed_list &in, int _iRetCount, types::typed_list &out)
135 types::Function::ReturnValue retVal = isdef(in, _iRetCount, out, "exists");
137 if (retVal == types::Function::OK)
139 if (out[0]->isDouble() == false)
141 types::Bool* pBOut = out[0]->getAs<types::Bool>();
142 types::Double* pDblOut = new types::Double(pBOut->getDims(), pBOut->getDimsArray());
143 for (int i = 0; i < pBOut->getSize(); i++)
145 pDblOut->set(i, (double)pBOut->get(i));
150 out.push_back(pDblOut);