2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2015 - Scilab Enterprises - Antoine ELIAS
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 "libraries.hxx"
22 void Library::put(types::Library* _pLib, int _iLevel)
24 if (empty() || top()->m_iLevel < _iLevel)
27 stack.push(new ScopedLibrary(_iLevel, _pLib));
31 //update current level
32 types::Library* pLib = top()->m_pLib;
35 // data is manage by variables.
36 // So if this library have to be killed
37 // this is alredy done by variables.
39 top()->m_pLib = _pLib;
44 types::MacroFile* Library::get(const Symbol& _keyMacro) const
48 return top()->getMacroFile(_keyMacro);
54 int Library::getMacrosName(std::list<std::wstring>& lst)
58 top()->m_pLib->getMacrosName(lst);
61 return static_cast<int>(lst.size());
64 Library* Libraries::getOrCreate(const Symbol& _key)
66 MapLibs::const_iterator it = libs.find(_key);
69 //create an empty StackedValues
70 Library* lib = new Library(_key);
78 int Libraries::getLevel(const Symbol& _key) const
80 MapLibs::const_iterator it = libs.find(_key);
83 if (!it->second->empty())
85 return it->second->top()->m_iLevel;
90 for (auto i = libs.rbegin(), end = libs.rend(); i != end; ++i)
92 Library * lib = i->second;
95 types::MacroFile * pMF = lib->get(_key);
98 return lib->top()->m_iLevel;
107 void Libraries::put(const Symbol& _keyLib, types::Library* _pLib, int _iLevel)
109 Library* lib = getOrCreate(_keyLib);
110 lib->put(_pLib, _iLevel);
113 bool Libraries::putInPreviousScope(const Symbol& _keyLib, types::Library* _pLib, int _iLevel)
115 Library* lib = getOrCreate(_keyLib);
119 lib->put(_pLib, _iLevel);
121 else if (lib->top()->m_iLevel > _iLevel)
123 ScopedLibrary* pLib = lib->top();
125 putInPreviousScope(_keyLib, _pLib, _iLevel);
130 lib->put(_pLib, _iLevel);
136 types::InternalType* Libraries::get(const Symbol& _key, int _iLevel)
138 //does _key is a lib name
139 auto lib = libs.find(_key);
140 if (lib != libs.end())
142 if (lib->second->empty() == false)
144 if (_iLevel == SCOPE_ALL || lib->second->top()->m_iLevel == _iLevel)
146 return lib->second->top()->m_pLib;
151 //does _key is a macro in a lib
152 auto it = libs.rbegin();
153 for (auto it = libs.rbegin(), itEnd = libs.rend(); it != itEnd; ++it)
155 Library* lib = it->second;
156 if (it->second->empty() == false)
158 if (_iLevel == SCOPE_ALL || it->second->top()->m_iLevel == _iLevel)
160 types::MacroFile* pMF = it->second->get(_key);
163 return (types::InternalType*)pMF;
172 bool Libraries::remove(const Symbol& _key, int _iLevel)
174 MapLibs::iterator it = libs.find(_key);
175 if (it != libs.end())
177 if (it->second->empty() == false)
179 if (it->second->top()->m_iLevel == _iLevel)
181 ScopedLibrary * pSL = it->second->top();
192 int Libraries::getMacrosName(std::list<std::wstring>& lst)
194 MapLibs::iterator it = libs.begin();
195 MapLibs::iterator itEnd = libs.end();
198 it.second->getMacrosName(lst);
201 return static_cast<int>(lst.size());
204 int Libraries::getVarsName(std::list<std::wstring>& lst)
208 if (it.second->empty() == false)
210 lst.push_back(it.first.getName().c_str());
214 return static_cast<int>(lst.size());
217 int Libraries::getVarsToVariableBrowser(std::list<Library*>& lst)
219 for (auto lib : libs)
221 if (lib.second->empty() == false)
223 lst.push_back(lib.second);
227 return static_cast<int>(lst.size());
230 void Libraries::clearAll()
232 for (auto lib : libs)
234 while (!lib.second->empty())
236 ScopedLibrary * pSL = lib.second->top();
237 types::InternalType * pIT = pSL->m_pLib;
247 bool Libraries::getVarsNameForWho(std::list<std::wstring>* lstVarName, int* iVarLenMax, bool bSorted) const
249 for (auto it = libs.begin(), itEnd = libs.end(); it != itEnd; ++it)
251 std::wstring wstrVarName(it->first.getName().c_str());
252 if (lstVarName && it->second->empty() == false)
254 lstVarName->push_back(wstrVarName);
255 *iVarLenMax = std::max(*iVarLenMax, (int)wstrVarName.size());
270 int Libraries::whereis(std::list<std::wstring>& lst, const Symbol& _key)
272 for (auto lib : libs)
274 if (lib.second->get(_key) != NULL)
276 lst.push_back(lib.first.getName());
279 return static_cast<int>(lst.size());
282 int Libraries::librarieslist(std::list<std::wstring>& lst)
284 for (auto lib : libs)
286 if (lib.second->empty() == false)
288 lst.push_back(lib.first.getName());
292 return static_cast<int>(lst.size());