homescreenpluginsrv/hspsdom/src/hspsdomstringpool.cpp
branchRCL_3
changeset 114 a5a39a295112
equal deleted inserted replaced
113:0efa10d348c0 114:a5a39a295112
       
     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 // ============================ LOCAL FUNCTIONS ================================
       
    24 
       
    25 // ============================ MEMBER FUNCTIONS ===============================
       
    26 // -----------------------------------------------------------------------------
       
    27 // ChspsDomStringPool::ChspsDomStringPool
       
    28 // C++ default constructor can NOT contain any code, that
       
    29 // might leave.
       
    30 // -----------------------------------------------------------------------------
       
    31 //
       
    32 ChspsDomStringPool::ChspsDomStringPool( const TBool aAllowDuplicates ) :
       
    33     iAllowDuplicates( aAllowDuplicates )
       
    34     {
       
    35     }
       
    36 
       
    37 // -----------------------------------------------------------------------------
       
    38 // ChspsDomStringPool::ConstructL
       
    39 // Symbian 2nd phase constructor can leave.
       
    40 // -----------------------------------------------------------------------------
       
    41 //
       
    42 void ChspsDomStringPool::ConstructL()
       
    43     {
       
    44     }
       
    45 
       
    46 // -----------------------------------------------------------------------------
       
    47 // ChspsDomStringPool::NewL
       
    48 // Two-phased constructor.
       
    49 // -----------------------------------------------------------------------------
       
    50 //
       
    51 ChspsDomStringPool* ChspsDomStringPool::NewL( const TBool aAllowDuplicates )
       
    52     {
       
    53     ChspsDomStringPool* self =
       
    54             new( ELeave ) ChspsDomStringPool( aAllowDuplicates );
       
    55     
       
    56     CleanupStack::PushL( self );
       
    57     self->ConstructL();
       
    58     CleanupStack::Pop(self);
       
    59 
       
    60     return self;
       
    61     }    
       
    62 
       
    63 
       
    64 // -----------------------------------------------------------------------------
       
    65 // ChspsDomStringPool::NewL
       
    66 // Two-phased stream constructor.
       
    67 // -----------------------------------------------------------------------------
       
    68 //
       
    69 ChspsDomStringPool* ChspsDomStringPool::NewL( RReadStream& aStream,
       
    70         const TBool aAllowDuplicates )
       
    71     {
       
    72     ChspsDomStringPool* self = new( ELeave ) ChspsDomStringPool( aAllowDuplicates );
       
    73     CleanupStack::PushL( self );
       
    74     aStream >> *self;
       
    75     CleanupStack::Pop(self);
       
    76 
       
    77     return self;
       
    78     }    
       
    79     
       
    80 // -----------------------------------------------------------------------------
       
    81 // ChspsDomStringPool::~ChspsDomStringPool
       
    82 // Destructor
       
    83 // -----------------------------------------------------------------------------
       
    84 //
       
    85 ChspsDomStringPool::~ChspsDomStringPool()
       
    86     {
       
    87     iStringPool.ResetAndDestroy();
       
    88     iStringPoolOptimizer.Close();
       
    89     }
       
    90 
       
    91 // -----------------------------------------------------------------------------
       
    92 // ChspsDomStringPool::CloneL
       
    93 // -----------------------------------------------------------------------------
       
    94 //
       
    95 ChspsDomStringPool* ChspsDomStringPool::CloneL()
       
    96     {    
       
    97     ChspsDomStringPool* clone = NULL;    
       
    98     if( iAllowDuplicates )
       
    99         {
       
   100         clone = ChspsDomStringPool::NewL( ETrue );
       
   101         }
       
   102     else
       
   103         {
       
   104         clone = ChspsDomStringPool::NewL( EFalse );
       
   105         }    
       
   106     CleanupStack::PushL( clone );
       
   107     
       
   108     TInt count( iStringPool.Count() );
       
   109     for ( TInt i = 0; i < count; i++ )
       
   110         {    
       
   111         HBufC8* tmp = iStringPool[i]->Des().AllocLC();
       
   112         clone->DoAddStringL( tmp );
       
   113         CleanupStack::Pop( tmp );
       
   114         }
       
   115     CleanupStack::Pop( clone );
       
   116     return clone;
       
   117     }
       
   118 
       
   119 // -----------------------------------------------------------------------------
       
   120 // ChspsDomStringPool::Reset
       
   121 // -----------------------------------------------------------------------------
       
   122 //
       
   123 void ChspsDomStringPool::Reset()
       
   124     {    
       
   125     if( iStringPool.Count() > 0 )
       
   126         {
       
   127         iStringPool.ResetAndDestroy();        
       
   128         }
       
   129     
       
   130     if( iStringPoolOptimizer.Count() > 0 )
       
   131         {
       
   132         iStringPoolOptimizer.Reset();
       
   133         }
       
   134     }
       
   135 
       
   136 // -----------------------------------------------------------------------------
       
   137 // ChspsDomStringPool::AddStringL
       
   138 // -----------------------------------------------------------------------------
       
   139 //
       
   140 EXPORT_C TInt ChspsDomStringPool::AddStringL( const TDesC8& aString )
       
   141     {   
       
   142     TInt index = iStringPoolOptimizer.GetIndex( aString );
       
   143 
       
   144     if( index == KErrNotFound )
       
   145         {
       
   146         HBufC8* string = aString.AllocLC();
       
   147         index = DoAddStringL( string );        
       
   148         CleanupStack::Pop( string );        
       
   149         }    
       
   150     
       
   151     return index;    
       
   152     }
       
   153 
       
   154 // -----------------------------------------------------------------------------
       
   155 // ChspsDomStringPool::AddStringL
       
   156 // -----------------------------------------------------------------------------
       
   157 //
       
   158 TInt ChspsDomStringPool::AddStringL( HBufC8* aString )
       
   159     {   
       
   160     if( !aString )
       
   161         {
       
   162         User::Leave( KErrArgument );
       
   163         }
       
   164     
       
   165     TInt index = iStringPoolOptimizer.GetIndex( *aString );    
       
   166     
       
   167     if( index == KErrNotFound )
       
   168         {
       
   169         index = DoAddStringL( aString );       
       
   170         }
       
   171     else
       
   172         {
       
   173         delete aString;
       
   174         }
       
   175     
       
   176     return index;    
       
   177     }
       
   178 
       
   179 // -----------------------------------------------------------------------------
       
   180 // ChspsDomStringPool::AddStringL
       
   181 // -----------------------------------------------------------------------------
       
   182 //
       
   183 void ChspsDomStringPool::AddAllL( ChspsDomStringPool& aStringPool )
       
   184     {
       
   185     const TInt count = aStringPool.Count();
       
   186     for( TInt i = 0; i < count; i++ )
       
   187         {
       
   188         AddStringL( aStringPool.String( i ) );
       
   189         }    
       
   190     }
       
   191 
       
   192 // -----------------------------------------------------------------------------
       
   193 // ChspsDomStringPool::String
       
   194 // -----------------------------------------------------------------------------
       
   195 //
       
   196 const TDesC8& ChspsDomStringPool::String( const TInt aStringRef )
       
   197     {       
       
   198     if( aStringRef >= 0 && aStringRef < iStringPool.Count() )
       
   199         {
       
   200         return (*iStringPool[ aStringRef ]);
       
   201         }
       
   202     else
       
   203         {
       
   204         return KNullDesC8;
       
   205         }
       
   206     }
       
   207 
       
   208 // -----------------------------------------------------------------------------
       
   209 // ChspsDomStringPool::Size
       
   210 // -----------------------------------------------------------------------------
       
   211 //
       
   212 TInt ChspsDomStringPool::Size() const
       
   213     {
       
   214     TInt size( 0 );
       
   215     
       
   216     TInt count( iStringPool.Count() );
       
   217     for ( TInt i=0; i<count; i++ )
       
   218         {
       
   219         size += sizeof(TInt16);     //Length
       
   220         size++;                     //HBufC control mark
       
   221         size++;                     //HBufC control mark
       
   222         size += iStringPool[i]->Size();  //Buffer sixe in bytes     
       
   223         }
       
   224     return size;    
       
   225     }
       
   226 
       
   227 // -----------------------------------------------------------------------------
       
   228 // ChspsDomStringPool::Count
       
   229 // -----------------------------------------------------------------------------
       
   230 //
       
   231 TInt ChspsDomStringPool::Count() const
       
   232     {
       
   233     return iStringPool.Count();
       
   234     }
       
   235 
       
   236 // -----------------------------------------------------------------------------
       
   237 // ChspsDomStringPool::ExternalizeL
       
   238 // -----------------------------------------------------------------------------
       
   239 //
       
   240 void ChspsDomStringPool::ExternalizeL( RWriteStream& aStream ) const
       
   241     {
       
   242     TInt count( iStringPool.Count() );
       
   243     aStream.WriteInt16L( count );
       
   244     
       
   245     for ( TInt i=0; i<count; i++ )
       
   246         {
       
   247         aStream.WriteInt16L( iStringPool[i]->Length() );
       
   248         aStream << *iStringPool[i];      
       
   249         }
       
   250     }
       
   251 
       
   252 // -----------------------------------------------------------------------------
       
   253 // ChspsDomStringPool::InternalizeL
       
   254 // -----------------------------------------------------------------------------
       
   255 //
       
   256 void ChspsDomStringPool::InternalizeL( RReadStream& aStream )
       
   257     {
       
   258     TInt len(0);
       
   259     TInt16 count ( aStream.ReadInt16L() );    
       
   260 
       
   261     for ( TInt i=0; i<count; i++ )
       
   262         {
       
   263         len = aStream.ReadInt16L();
       
   264         HBufC8* tmp = HBufC8::NewLC( aStream, len );
       
   265         AddStringL( tmp ); // OWNERSHIP TRANSFERRED!        
       
   266         CleanupStack::Pop( tmp );
       
   267         }
       
   268     }
       
   269 
       
   270 // -----------------------------------------------------------------------------
       
   271 // ChspsDomStringPool::DoAddStringL
       
   272 // -----------------------------------------------------------------------------
       
   273 //
       
   274 TInt ChspsDomStringPool::DoAddStringL( HBufC8* aNewString )
       
   275     {
       
   276     if( !aNewString )
       
   277         {
       
   278         User::Leave( KErrArgument );
       
   279         }       
       
   280     
       
   281     TInt index = iStringPool.Count();
       
   282     
       
   283     if( !iAllowDuplicates )
       
   284         {
       
   285         ThspsDomStringPoolOptimizerEntry tmp( index, *aNewString );
       
   286         iStringPoolOptimizer.AddEntryL( tmp );
       
   287         }
       
   288     
       
   289     iStringPool.AppendL( aNewString );       
       
   290         
       
   291     return index;
       
   292     }
       
   293 
       
   294 //  End of File