homescreenpluginsrv/hspsdom/src/hspsdomstringpool.cpp
changeset 0 79c6a41cd166
child 4 1a2a00e78665
equal deleted inserted replaced
-1:000000000000 0:79c6a41cd166
       
     1 /*
       
     2 * Copyright (c) 2005,2006 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Represent string pool for dom strings
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include    "hspsdomstringpool.h"
       
    22 
       
    23 
       
    24 // ============================ LOCAL FUNCTIONS ================================
       
    25 // -----------------------------------------------------------------------------
       
    26 // Adds string to string pool. If string doesn't appear yet, it is added to 
       
    27 // the string pool and index to pool is returned. 
       
    28 // @param aString String to add 
       
    29 // @param aArray A pool which holds strings
       
    30 // @return Index to string pool 
       
    31 // -----------------------------------------------------------------------------
       
    32 //
       
    33 LOCAL_C TInt AddToStringPoolL( const TDesC8& aString, RPointerArray<HBufC8>& aArray )
       
    34     {
       
    35     TBool found( EFalse );
       
    36     TInt index( 0 );
       
    37      
       
    38     TInt count( aArray.Count() );
       
    39     for (; index < count && !found;  )
       
    40         {
       
    41         if ( aArray[ index ]->Des().Compare( aString ) == 0 )
       
    42             {
       
    43             found = ETrue;
       
    44             }
       
    45         else
       
    46             {
       
    47             index++;
       
    48             }    
       
    49         }
       
    50     if ( !found )
       
    51         {
       
    52         HBufC8* tmp = aString.AllocLC();
       
    53         aArray.AppendL( tmp );
       
    54         CleanupStack::Pop( tmp );
       
    55         index = aArray.Count()-1;    //Last item
       
    56         }
       
    57     return index;
       
    58     }
       
    59 // ============================ MEMBER FUNCTIONS ===============================
       
    60 // -----------------------------------------------------------------------------
       
    61 // ChspsDomStringPool::ChspsDomStringPool
       
    62 // C++ default constructor can NOT contain any code, that
       
    63 // might leave.
       
    64 // -----------------------------------------------------------------------------
       
    65 //
       
    66 ChspsDomStringPool::ChspsDomStringPool()
       
    67     {
       
    68     }
       
    69 
       
    70 // -----------------------------------------------------------------------------
       
    71 // ChspsDomStringPool::ConstructL
       
    72 // Symbian 2nd phase constructor can leave.
       
    73 // -----------------------------------------------------------------------------
       
    74 //
       
    75 void ChspsDomStringPool::ConstructL()
       
    76     {
       
    77     }
       
    78 
       
    79 // -----------------------------------------------------------------------------
       
    80 // ChspsDomStringPool::NewL
       
    81 // Two-phased constructor.
       
    82 // -----------------------------------------------------------------------------
       
    83 //
       
    84 ChspsDomStringPool* ChspsDomStringPool::NewL()
       
    85     {
       
    86     ChspsDomStringPool* self = new( ELeave ) ChspsDomStringPool;
       
    87     
       
    88     CleanupStack::PushL( self );
       
    89     self->ConstructL();
       
    90     CleanupStack::Pop(self);
       
    91 
       
    92     return self;
       
    93     }    
       
    94 
       
    95 
       
    96 // -----------------------------------------------------------------------------
       
    97 // ChspsDomStringPool::NewL
       
    98 // Two-phased stream constructor.
       
    99 // -----------------------------------------------------------------------------
       
   100 //
       
   101 ChspsDomStringPool* ChspsDomStringPool::NewL( RReadStream& aStream )
       
   102     {
       
   103     ChspsDomStringPool* self = new( ELeave ) ChspsDomStringPool;
       
   104     CleanupStack::PushL( self );
       
   105     aStream >> *self;
       
   106     CleanupStack::Pop(self);
       
   107 
       
   108     return self;
       
   109     }    
       
   110     
       
   111 // -----------------------------------------------------------------------------
       
   112 // ChspsDomStringPool::~ChspsDomStringPool
       
   113 // Destructor
       
   114 // -----------------------------------------------------------------------------
       
   115 //
       
   116 ChspsDomStringPool::~ChspsDomStringPool()
       
   117     {
       
   118     iStringPool.ResetAndDestroy();
       
   119     }
       
   120 
       
   121 // -----------------------------------------------------------------------------
       
   122 // ChspsDomStringPool::CloneL
       
   123 // -----------------------------------------------------------------------------
       
   124 //
       
   125 ChspsDomStringPool* ChspsDomStringPool::CloneL()
       
   126     {
       
   127     ChspsDomStringPool* clone = ChspsDomStringPool::NewL();
       
   128     CleanupStack::PushL( clone );
       
   129     
       
   130     TInt count( iStringPool.Count() );
       
   131     for ( TInt i=0; i<count; i++ )
       
   132         {
       
   133         HBufC8* tmp = iStringPool[i]->Des().AllocLC();
       
   134         clone->iStringPool.AppendL( tmp );
       
   135         CleanupStack::Pop( tmp );
       
   136         }
       
   137     CleanupStack::Pop( clone );
       
   138     return clone;
       
   139     }
       
   140 
       
   141 // -----------------------------------------------------------------------------
       
   142 // ChspsDomNode::AddStringL
       
   143 // -----------------------------------------------------------------------------
       
   144 //
       
   145 EXPORT_C TInt ChspsDomStringPool::AddStringL( const TDesC8& aString )
       
   146     {
       
   147     return AddToStringPoolL( aString, iStringPool );
       
   148     }
       
   149 
       
   150 // -----------------------------------------------------------------------------
       
   151 // ChspsDomNode::String
       
   152 // -----------------------------------------------------------------------------
       
   153 //
       
   154 const TDesC8& ChspsDomStringPool::String( const TInt aStringRef )
       
   155     {
       
   156     return (*iStringPool[ aStringRef ]);
       
   157     }
       
   158         
       
   159 // -----------------------------------------------------------------------------
       
   160 // ChspsDomStringPool::Size
       
   161 // -----------------------------------------------------------------------------
       
   162 //
       
   163 TInt ChspsDomStringPool::Size() const
       
   164     {
       
   165     TInt size( 0 );
       
   166     
       
   167     TInt count( iStringPool.Count() );
       
   168     for ( TInt i=0; i<count; i++ )
       
   169         {
       
   170         size += sizeof(TInt16);     //Length
       
   171         size++;                     //HBufC control mark
       
   172         size++;                     //HBufC control mark
       
   173         size += iStringPool[i]->Size();  //Buffer sixe in bytes     
       
   174         }
       
   175     return size;    
       
   176     }
       
   177 
       
   178 // -----------------------------------------------------------------------------
       
   179 // ChspsDomStringPool::ExternalizeL
       
   180 // -----------------------------------------------------------------------------
       
   181 //
       
   182 void ChspsDomStringPool::ExternalizeL( RWriteStream& aStream ) const
       
   183     {
       
   184     TInt count( iStringPool.Count() );
       
   185     aStream.WriteInt16L( count );
       
   186     
       
   187     for ( TInt i=0; i<count; i++ )
       
   188         {
       
   189         aStream.WriteInt16L( iStringPool[i]->Length() );
       
   190         aStream << *iStringPool[i];      
       
   191         }
       
   192     }
       
   193 
       
   194 // -----------------------------------------------------------------------------
       
   195 // ChspsDomStringPool::InternalizeL
       
   196 // -----------------------------------------------------------------------------
       
   197 //
       
   198 void ChspsDomStringPool::InternalizeL( RReadStream& aStream )
       
   199     {
       
   200     TInt len(0);
       
   201     TInt16 count ( aStream.ReadInt16L() );
       
   202     
       
   203     for ( TInt i=0; i<count; i++ )
       
   204         {
       
   205         len = aStream.ReadInt16L();
       
   206         HBufC8* tmp = HBufC8::NewLC( aStream, len );
       
   207         iStringPool.AppendL( tmp );
       
   208         CleanupStack::Pop( tmp );
       
   209         }
       
   210    
       
   211     }
       
   212 //  End of File