update whereis to return several libraries names 99/15899/1
Antoine ELIAS [Mon, 2 Feb 2015 16:04:03 +0000 (17:04 +0100)]
Change-Id: I6b80a98a8fb9e3342a280adedaa3177fc0f3af00

scilab/modules/ast/includes/symbol/context.hxx
scilab/modules/ast/includes/symbol/libraries.hxx
scilab/modules/ast/src/cpp/symbol/context.cpp
scilab/modules/functions/sci_gateway/cpp/sci_whereis.cpp
scilab/modules/functions/tests/unit_tests/whereis.dia.ref
scilab/modules/functions/tests/unit_tests/whereis.tst

index fdec3a6..0b48765 100644 (file)
@@ -79,6 +79,7 @@ public :
     std::list<std::wstring>* getFunctionsName();
     std::list<std::wstring>* getVarsNameForWho(bool sorted);
     std::list<std::wstring>* getGlobalNameForWho(bool sorted);
+    std::list<std::wstring>* getWhereIs(const std::wstring& _str);
 
     /* global functions */
 
index 9647bfd..8759e11 100644 (file)
@@ -257,6 +257,19 @@ struct Libraries
         return true;
     }
 
+    std::list<std::wstring>* whereis(const Symbol& _key)
+    {
+        std::list<std::wstring>* ret = new std::list<std::wstring>();
+
+        for (auto lib : libs)
+        {
+            if (lib.second->get(_key) != NULL)
+            {
+                ret->push_back(lib.first.getName());
+            }
+        }
+        return ret;
+    }
 
 private:
     MapLibs libs;
index 343b2b4..7254991 100644 (file)
@@ -244,6 +244,11 @@ std::list<std::wstring>* Context::getGlobalNameForWho(bool bSorted)
     return lstVar;
 }
 
+std::list<std::wstring>* Context::getWhereIs(const std::wstring& _str)
+{
+    return libraries.whereis(Symbol(_str));
+}
+
 void Context::put(Variable* _var, types::InternalType* _pIT)
 {
     if (_pIT->isLibrary())
index 099f631..97ec84d 100644 (file)
@@ -28,9 +28,6 @@ using namespace std;
 
 Function::ReturnValue sci_whereis(types::typed_list &in, int _iRetCount, types::typed_list &out)
 {
-    InternalType *pIT = NULL;
-    wstring stModule;
-
     /* Check the number of input argument */
     if (in.size() != 1)
     {
@@ -49,38 +46,48 @@ Function::ReturnValue sci_whereis(types::typed_list &in, int _iRetCount, types::
     {
         types::String* pS = in[0]->getAs<types::String>();
 
-        if (pS->getSize() != 1)
+        if (pS->isScalar() == false)
         {
             Scierror(999, _("%s: Wrong type for input argument #%d: A String expected.\n"), "whereis", 1);
             return Function::Error;
         }
 
-        ;
-        pIT = symbol::Context::getInstance()->getFunction(symbol::Symbol(pS->get(0)));
-        if (pIT == NULL)
+        std::list<std::wstring>* lst = symbol::Context::getInstance()->getWhereIs(pS->get(0));
+        if (lst == NULL || lst->empty())
         {
             out.push_back(types::Double::Empty());
+            if (lst)
+            {
+                delete lst;
+            }
             return Function::OK;
         }
+
+        types::String* pOut = new types::String(static_cast<int>(lst->size()), 1);
+        int i = 0;
+        for (std::wstring l : *lst)
+        {
+            pOut->set(i++, l.c_str());
+        }
+
+        out.push_back(pOut);
+        delete lst;
     }
     else
     {
-        pIT = in[0];
-    }
-
-    switch (pIT->getType())
-    {
-        case InternalType::ScilabFunction :
-        case InternalType::ScilabMacro :
-        case InternalType::ScilabMacroFile :
-            stModule = pIT->getAs<Callable>()->getModule();
-            break;
-        default :
-            out.push_back(types::Double::Empty());
-            return Function::OK;
+        wstring stModule;
+        switch (in[0]->getType())
+        {
+            case InternalType::ScilabFunction:
+            case InternalType::ScilabMacro:
+            case InternalType::ScilabMacroFile:
+                out.push_back(new types::String(in[0]->getAs<Callable>()->getModule().c_str()));
+                break;
+            default:
+                out.push_back(types::Double::Empty());
+        }
     }
 
-    out.push_back(new types::String(stModule.c_str()));
     return Function::OK;
 }
 /*--------------------------------------------------------------------------*/
index 51293c6..a24df69 100644 (file)
@@ -5,5 +5,8 @@
 //  This file is distributed under the same license as the Scilab package.
 // =============================================================================
 //whereis
-if whereis("sci2exp") <> "stringlib" then bugmes();quit;end
-if whereis('aaa')     <> []  then bugmes();quit;end
+assert_checkequal(whereis("sci2exp"), "stringlib");
+assert_checkequal(whereis("aaa"), []);
+assert_checkequal(whereis("with_tk"), "corelib");
+a = corelib;
+assert_checkequal(whereis("with_tk"), ["a" ; "corelib"]);
index 0432971..a80e8ea 100644 (file)
@@ -7,5 +7,11 @@
 
 //whereis
 
-if whereis("sci2exp") <> "stringlib" then pause,end
-if whereis('aaa')     <> []  then pause,end
+assert_checkequal(whereis("sci2exp"), "stringlib");
+assert_checkequal(whereis("aaa"), []);
+
+assert_checkequal(whereis("with_tk"), "corelib");
+a = corelib;
+assert_checkequal(whereis("with_tk"), ["a" ; "corelib"]);
+
+