xml/xmldomandxpath/src/xmlenginedom/xmlengownednodescontainer.cpp
changeset 0 e35f40988205
equal deleted inserted replaced
-1:000000000000 0:e35f40988205
       
     1 // Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // Node container implementation
       
    15 //
       
    16 
       
    17 #include <xml/utils/xmlengmem.h>
       
    18 #include "xmlengownednodescontainer.h"
       
    19 #include "xmlengdomdefs.h"
       
    20 #include <stdapis/libxml2/libxml2_tree.h>
       
    21 #include <stdapis/libxml2/libxml2_globals.h>
       
    22 
       
    23 // --------------------------------------------------------------------------------------
       
    24 // Creates an instance of CXmlEngOwnedNodesContainer
       
    25 // --------------------------------------------------------------------------------------
       
    26 //
       
    27 CXmlEngOwnedNodesContainer* CXmlEngOwnedNodesContainer::NewL()
       
    28     {
       
    29     CXmlEngOwnedNodesContainer* obj = new (ELeave) CXmlEngOwnedNodesContainer();
       
    30     obj->iNodes = NULL;
       
    31     obj->iLast = NULL;
       
    32     obj->iCount = 0;
       
    33     return obj;
       
    34     }
       
    35 
       
    36 CXmlEngOwnedNodesContainer::~CXmlEngOwnedNodesContainer()
       
    37     {
       
    38     }
       
    39 // --------------------------------------------------------------------------------------
       
    40 // Searches for a node in the list
       
    41 //
       
    42 // @param   aNode Pointer to a libxml2 node
       
    43 // @return Index in the list of nodes [0; iCount-1] or iCount if not found
       
    44 // --------------------------------------------------------------------------------------
       
    45 //
       
    46 TUint CXmlEngOwnedNodesContainer::Lookup(xmlNodePtr aNode)
       
    47     {
       
    48     TUint i = 0;
       
    49     xmlNodePtr tmp = iNodes;
       
    50     for(; tmp; tmp = tmp->next, i++)
       
    51         if(tmp == aNode)
       
    52             return i;
       
    53     return iCount;
       
    54     }
       
    55 
       
    56 // --------------------------------------------------------------------------------------
       
    57 // Performs clean up of the list
       
    58 // --------------------------------------------------------------------------------------
       
    59 //
       
    60 void CXmlEngOwnedNodesContainer::RemoveAll()
       
    61     {
       
    62     iNodes = NULL;
       
    63     iCount = 0;
       
    64     }
       
    65 
       
    66 // --------------------------------------------------------------------------------------
       
    67 // Performs clean up of all owned nodes:
       
    68 // xmlFreeNode is called on every contained pointer
       
    69 // --------------------------------------------------------------------------------------
       
    70 //
       
    71 void CXmlEngOwnedNodesContainer::FreeAll()
       
    72     {
       
    73     xmlNodePtr tmp = iNodes;
       
    74     xmlNodePtr ptr;
       
    75     for(;tmp;)
       
    76         {
       
    77         ptr = tmp;
       
    78         tmp = tmp->next;
       
    79         if(ptr)
       
    80             xmlFreeNode(ptr);
       
    81         }
       
    82     iNodes = NULL;
       
    83     iCount = 0;
       
    84     }
       
    85 
       
    86 
       
    87 // --------------------------------------------------------------------------------------
       
    88 // Adds an owned node.
       
    89 //
       
    90 // @param aNode  A pointer to some node to be added into the container
       
    91 //
       
    92 // @note This method does not check whether the pointer is already in the container
       
    93 // @note In OOM situation the node is destroyed before leave occurs
       
    94 // --------------------------------------------------------------------------------------
       
    95 //
       
    96 void CXmlEngOwnedNodesContainer::Add(
       
    97     xmlNodePtr aNode )
       
    98     {
       
    99     if(iLast)
       
   100         {
       
   101         iLast->next = aNode;
       
   102         aNode->prev = iLast;        
       
   103         }
       
   104     else
       
   105         {
       
   106         iNodes = iLast = aNode;
       
   107         }
       
   108     iLast = aNode;
       
   109     ++iCount;
       
   110     }
       
   111 
       
   112 // --------------------------------------------------------------------------------------
       
   113 // Excludes node pointer from the list of owned nodes.
       
   114 // 
       
   115 // does nothing if provided node pointer is not on the list.
       
   116 // --------------------------------------------------------------------------------------
       
   117 //
       
   118 void CXmlEngOwnedNodesContainer::Remove(
       
   119     xmlNodePtr aNode )
       
   120     {
       
   121     if(aNode == iLast)
       
   122         {
       
   123         iLast = aNode->prev;
       
   124         }
       
   125     else
       
   126         {
       
   127         aNode->next->prev = aNode->prev;    
       
   128         }
       
   129     if(aNode == iNodes)
       
   130         {
       
   131         iNodes = aNode->next;
       
   132         }
       
   133     else
       
   134         {
       
   135         aNode->prev->next = aNode->next;
       
   136         }    
       
   137     
       
   138     aNode->prev = NULL;
       
   139     aNode->next = NULL;
       
   140     --iCount;
       
   141     }