allow using "relative" path in get function, fix trouble when two handles have the... 72/13972/2
Antoine ELIAS [Tue, 11 Mar 2014 10:52:19 +0000 (11:52 +0100)]
clean get function
get([]) returns error instead of hazardous results like console handle or [], ...

Change-Id: If2cfcf97019b6acaed6a28bd4cf681e639f7368b

scilab/modules/graphic_objects/includes/ScilabView.hxx
scilab/modules/graphic_objects/src/cpp/ScilabView.cpp
scilab/modules/graphics/sci_gateway/c/sci_get.c

index 90c774a..e215b17 100644 (file)
@@ -109,13 +109,14 @@ public :
 
     static PathItem* getItem(int uid);
     static PathItem* getItem(std::string _pstTag);
+    static PathItem* getItem(std::string _pstTag, std::list<int>& _ignoredList);
     static PathItem* getFigureItem(std::string _pstTag);
 
     static int search_path(char* _pstPath);
     static std::string get_path(int uid);
 
 private :
-    static PathItem* search_children(PathItem* _path, std::string _subPath, bool _bDeep);
+    static PathItem* search_children(PathItem* _path, std::string _subPath, bool _bDeep, std::list<int>& _ignoredList);
 
 };
 
index d92d3f1..23ddc0e 100644 (file)
@@ -12,6 +12,7 @@
 
 #include <iostream>
 #include <map>
+#include <list>
 #include <cstring>
 #include <limits.h>
 
@@ -487,21 +488,44 @@ PathItem* ScilabView::getItem(int uid)
 
 PathItem* ScilabView::getItem(std::string _pstTag)
 {
+    std::list<int> ignored;
+    return getItem(_pstTag, ignored);
+}
+
+PathItem* ScilabView::getItem(std::string _pstTag, std::list<int>& _ignoredList)
+{
+    /*
+    */
     __pathList_iterator it = m_pathList.begin();
     for (; it != m_pathList.end(); it++)
     {
         PathItem * item = it->second;
         if (item->tag == _pstTag)
         {
-            return item;
+            bool ignored = false;
+            //check if this handle is not in ignoredList
+            std::list<int>::iterator itIgnored = _ignoredList.begin();
+            for (; itIgnored != _ignoredList.end(); itIgnored++)
+            {
+                if ((*itIgnored) == item->uid)
+                {
+                    ignored = true;
+                    break;
+                }
+            }
+
+            if (ignored == false)
+            {
+                return item;
+            }
         }
     }
-
     return NULL;
 }
 
 PathItem* ScilabView::getFigureItem(std::string _pstTag)
 {
+
     __pathFigList_iterator it = m_pathFigList.find(_pstTag);
     if (it != m_pathFigList.end())
     {
@@ -513,8 +537,11 @@ PathItem* ScilabView::getFigureItem(std::string _pstTag)
 
 int ScilabView::search_path(char* _pstPath)
 {
+    //copy string to protect it against strtok
+    char* pstPath = strdup(_pstPath);
+    std::list<int> ignoredList;
     PathItem* path = NULL;
-    char* pstSubPath = strtok(_pstPath, "/");
+    char* pstSubPath = strtok(pstPath, "/");
     bool bDeep = false;
     while (pstSubPath != NULL)
     {
@@ -529,10 +556,10 @@ int ScilabView::search_path(char* _pstPath)
             //search in direct children
             if (path == NULL)
             {
-                path = ScilabView::getFigureItem(_pstPath);
+                path = ScilabView::getFigureItem(pstSubPath);
                 if (path == NULL)
                 {
-                    path = ScilabView::getItem(_pstPath);
+                    path = ScilabView::getItem(pstSubPath, ignoredList);
                     if (path == NULL)
                     {
                         break;
@@ -541,10 +568,19 @@ int ScilabView::search_path(char* _pstPath)
             }
             else
             {
-                path = search_children(path, pstSubPath, bDeep);
-                if (path == NULL)
+                PathItem* newPath = search_children(path, pstSubPath, bDeep, ignoredList);
+                if (newPath == NULL)
                 {
-                    break;
+                    //flag handle to ingnore and restart parsing
+                    ignoredList.push_back(path->uid);
+                    pstPath = strdup(_pstPath);
+                    pstSubPath = strtok(pstPath, "/");
+                    path = NULL;
+                    continue;
+                }
+                else
+                {
+                    path = newPath;
                 }
 
                 bDeep = false;
@@ -567,7 +603,7 @@ int ScilabView::search_path(char* _pstPath)
     return path->uid;
 }
 
-PathItem* ScilabView::search_children(PathItem* _path, std::string _subPath, bool _bDeep)
+PathItem* ScilabView::search_children(PathItem* _path, std::string _subPath, bool _bDeep, std::list<int>& _ignoredList)
 {
     PathItem::__child_iterator it = _path->children.begin();
     for (; it != _path->children.end() ; it++)
@@ -575,11 +611,26 @@ PathItem* ScilabView::search_children(PathItem* _path, std::string _subPath, boo
         PathItem* child = ScilabView::getItem(*it);
         if (child->tag == _subPath)
         {
-            return child;
+            bool ignored = false;
+            //check if this handle is not in ignoredList
+            std::list<int>::iterator itIgnored = _ignoredList.begin();
+            for (; itIgnored != _ignoredList.end(); itIgnored++)
+            {
+                if ((*itIgnored) == child->uid)
+                {
+                    ignored = true;
+                    break;
+                }
+            }
+
+            if (ignored == false)
+            {
+                return child;
+            }
         }
         else if (_bDeep)
         {
-            PathItem *item = search_children(child, _subPath, _bDeep);
+            PathItem *item = search_children(child, _subPath, _bDeep, _ignoredList);
             if (item)
             {
                 return item;
index 637ea8f..8412667 100644 (file)
@@ -55,8 +55,6 @@ int sci_get(char *fname, unsigned long fname_len)
     SciErr sciErr;
 
     int* piAddrl1 = NULL;
-    double* pdbll1 = NULL;
-    int* piAddrstkAdr = NULL;
     long long* l1 = NULL;
     int* piAddrl2 = NULL;
     char* l2 = NULL;
@@ -67,50 +65,53 @@ int sci_get(char *fname, unsigned long fname_len)
     int lw = 0;
     int iObjUID = 0;
 
-    /* Root properties */
-    char **stkAdr = NULL;
     int status = SET_PROPERTY_ERROR;
 
-    if (((checkInputArgumentType(pvApiCtx, 1, sci_mlist))) || ((checkInputArgumentType(pvApiCtx, 1, sci_tlist))))
+    CheckInputArgument(pvApiCtx, 1, 2);
+    CheckOutputArgument(pvApiCtx, 0, 1);
+
+    sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddrl1);
+    if (sciErr.iErr)
+    {
+        printError(&sciErr, 0);
+        return 1;
+    }
+
+    if (isMListType(pvApiCtx, piAddrl1) || isTListType(pvApiCtx, piAddrl1))
     {
-        //lw = 1 + nbArgumentOnStack(pvApiCtx) - nbInputArgument(pvApiCtx);
         OverLoad(1);
         return 0;
     }
 
-    CheckInputArgument(pvApiCtx, 1, 2);
-    CheckOutputArgument(pvApiCtx, 0, 1);
-
-    /*  set or create a graphic window */
-
     /*
      * The first input argument can be an ID or a marker (in this case, get returns the value of the current object */
     switch (getInputArgumentType(pvApiCtx, 1))
     {
-        case 1:                    /* tclsci handle */
-            sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddrl1);
-            if (sciErr.iErr)
+        case sci_matrix: //console handle
+        {
+            double dbll1 = 0;
+
+            if (isScalar(pvApiCtx, piAddrl1) == 0)
             {
-                printError(&sciErr, 0);
+                Scierror(202, _("%s: Wrong type for argument #%d: A real expected.\n"), fname, 1);
                 return 1;
             }
 
             // Retrieve a matrix of double at position 1.
-            sciErr = getMatrixOfDouble(pvApiCtx, piAddrl1, &m1, &n1, &pdbll1);
-            if (sciErr.iErr)
+            if (getScalarDouble(pvApiCtx, piAddrl1, &dbll1))
             {
-                printError(&sciErr, 0);
                 Scierror(202, _("%s: Wrong type for argument #%d: A real expected.\n"), fname, 1);
                 return 1;
             }
 
-            if ((int)pdbll1[0] == 0) /* Root property */
+            if ((int)dbll1 == 0) /* Console property */
             {
+                int* piAddrstkAdr = NULL;
+                char *stkAdr = NULL;
                 if (nbInputArgument(pvApiCtx) == 1)
                 {
                     if (sciReturnHandle(pvApiCtx, getHandle(getConsoleIdentifier())) != 0)    /* Get Console handle */
                     {
-                        /* An error has occurred */
                         ReturnArguments(pvApiCtx);
                         return 0;
                     }
@@ -118,64 +119,38 @@ int sci_get(char *fname, unsigned long fname_len)
                     ReturnArguments(pvApiCtx);
                     return 0;
                 }
+
                 CheckInputArgument(pvApiCtx, 2, 2);
-                if ((checkInputArgumentType(pvApiCtx, 2, sci_strings)))
+                sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddrstkAdr);
+                if (sciErr.iErr)
                 {
-                    sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddrstkAdr);
-                    if (sciErr.iErr)
-                    {
-                        printError(&sciErr, 0);
-                        return 1;
-                    }
-
-                    // Retrieve a matrix of string at position 2.
-                    if (getAllocatedMatrixOfString(pvApiCtx, piAddrstkAdr, &m1, &n1, &stkAdr))
-                    {
-                        Scierror(202, _("%s: Wrong type for argument #%d: String matrix expected.\n"), fname, 2);
-                        return 1;
-                    }
-
-
-                    if (m1 * n1 != 1)
-                    {
-                        freeAllocatedMatrixOfString(m1, n1, stkAdr);
-                        Scierror(999, _("%s: Wrong type for input argument #%d: Single string expected.\n"), "get", 2);
-                        return SET_PROPERTY_ERROR;
-                    }
-
-                    status = GetScreenProperty(pvApiCtx, stkAdr[0]);
+                    printError(&sciErr, 0);
+                    return 1;
+                }
 
-                    if (status != SET_PROPERTY_SUCCEED) /* Return property */
-                    {
-                        Scierror(999, _("%s: Could not read property '%s' for root object.\n"), "get", stkAdr[0]);
-                        freeAllocatedMatrixOfString(m1, n1, stkAdr);
-                        return FALSE;
-                    }
-                    freeAllocatedMatrixOfString(m1, n1, stkAdr);
+                // Retrieve a matrix of string at position 2.
+                if (getAllocatedSingleString(pvApiCtx, piAddrstkAdr, &stkAdr))
+                {
+                    Scierror(202, _("%s: Wrong type for argument #%d: Single string expected.\n"), fname, 2);
+                    return 1;
                 }
-                else
+
+                if (GetScreenProperty(pvApiCtx, stkAdr) != SET_PROPERTY_SUCCEED)
                 {
-                    Scierror(999, _("%s: Wrong type for input argument #%d: Single string expected.\n"), "get", 2);
-                    return FALSE;
+                    Scierror(999, _("%s: Could not read property '%s' for console object.\n"), "get", stkAdr[0]);
+                    freeAllocatedSingleString(stkAdr);
+                    return 1;
                 }
+                freeAllocatedSingleString(stkAdr);
                 AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;
                 ReturnArguments(pvApiCtx);
             }
-            else                    /* tclsci handle: should no more happen */
-            {
-                //lw = 1 + nbArgumentOnStack(pvApiCtx) - nbInputArgument(pvApiCtx);
-                OverLoad(1);
-            }
+
             return 0;
             break;
+        }
         case sci_handles:          /* scalar argument (hdl + string) */
             CheckInputArgument(pvApiCtx, 1, 2);
-            sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddrl1);
-            if (sciErr.iErr)
-            {
-                printError(&sciErr, 0);
-                return 1;
-            }
 
             // Retrieve a matrix of handle at position 1.
             sciErr = getMatrixOfHandle(pvApiCtx, piAddrl1, &m1, &n1, &l1);
@@ -219,6 +194,12 @@ int sci_get(char *fname, unsigned long fname_len)
                 return 1;
             }
 
+            if (isScalar(pvApiCtx, piAddrl2) == 0 || isStringType(pvApiCtx, piAddrl2) == 0)
+            {
+                Scierror(202, _("%s: Wrong type for argument #%d: A string expected.\n"), fname, 2);
+                return 1;
+            }
+
             // Retrieve a matrix of double at position 2.
             if (getAllocatedSingleString(pvApiCtx, piAddrl2, &l2))
             {
@@ -230,18 +211,17 @@ int sci_get(char *fname, unsigned long fname_len)
             break;
         case sci_strings:          /* string argument (string) */
         {
-            int* piAddr = NULL;
             char* pstFirst = NULL;
             CheckInputArgument(pvApiCtx, 1, 2);
-            sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddr);
-            if (sciErr.iErr)
+
+            if (isScalar(pvApiCtx, piAddrl1) == 0)
             {
-                printError(&sciErr, 0);
+                Scierror(202, _("%s: Wrong type for argument #%d: A real expected.\n"), fname, 1);
                 return 1;
             }
 
             // Retrieve a matrix of double at position 1.
-            if (getAllocatedSingleString(pvApiCtx, piAddr, &pstFirst))
+            if (getAllocatedSingleString(pvApiCtx, piAddrl1, &pstFirst))
             {
                 Scierror(202, _("%s: Wrong type for argument #%d: A string expected.\n"), fname, 1);
                 return 1;
@@ -281,6 +261,12 @@ int sci_get(char *fname, unsigned long fname_len)
                         return 1;
                     }
 
+                    if (isScalar(pvApiCtx, piAddrl2) == 0 || isStringType(pvApiCtx, piAddrl2) == 0)
+                    {
+                        Scierror(202, _("%s: Wrong type for argument #%d: A string expected.\n"), fname, 2);
+                        return 1;
+                    }
+
                     if (getAllocatedSingleString(pvApiCtx, piAddrl2, &l2))
                     {
                         Scierror(202, _("%s: Wrong type for argument #%d: A string expected.\n"), fname, 2);