idlehomescreen/xmluirendering/dom/src/xndomstringpool.cpp
changeset 2 08c6ee43b396
parent 0 f72a12da539e
equal deleted inserted replaced
1:5315654608de 2:08c6ee43b396
    17 
    17 
    18 
    18 
    19 
    19 
    20 // INCLUDE FILES
    20 // INCLUDE FILES
    21 #include    "xndomstringpool.h"
    21 #include    "xndomstringpool.h"
    22 
    22 #include    "xndomstringpooloptimizer.h"
    23 
    23 
    24 // ============================ LOCAL FUNCTIONS ================================
    24 // ============================ LOCAL FUNCTIONS ================================
    25 
    25 
    26 // ============================ MEMBER FUNCTIONS ===============================
    26 // ============================ MEMBER FUNCTIONS ===============================
    27 // -----------------------------------------------------------------------------
    27 // -----------------------------------------------------------------------------
    28 // CXnDomStringPool::CXnDomStringPool
    28 // CXnDomStringPool::CXnDomStringPool
    29 // C++ default constructor can NOT contain any code, that
    29 // C++ default constructor can NOT contain any code, that
    30 // might leave.
    30 // might leave.
    31 // -----------------------------------------------------------------------------
    31 // -----------------------------------------------------------------------------
    32 //
    32 //
    33 CXnDomStringPool::CXnDomStringPool()
    33 CXnDomStringPool::CXnDomStringPool( const TBool aAllowDuplicates ) :
    34     {
    34     iAllowDuplicates( aAllowDuplicates )
    35     iStringPoolOffsetCurrent = iStringPoolOffsetNext = 0;
    35     {
    36     }
    36     }
    37 
    37 
    38 // -----------------------------------------------------------------------------
    38 // -----------------------------------------------------------------------------
    39 // CXnDomStringPool::ConstructL
    39 // CXnDomStringPool::ConstructL
    40 // Symbian 2nd phase constructor can leave.
    40 // Symbian 2nd phase constructor can leave.
    47 // -----------------------------------------------------------------------------
    47 // -----------------------------------------------------------------------------
    48 // CXnDomStringPool::NewL
    48 // CXnDomStringPool::NewL
    49 // Two-phased constructor.
    49 // Two-phased constructor.
    50 // -----------------------------------------------------------------------------
    50 // -----------------------------------------------------------------------------
    51 //
    51 //
    52 CXnDomStringPool* CXnDomStringPool::NewL()
    52 CXnDomStringPool* CXnDomStringPool::NewL( const TBool aAllowDuplicates )
    53     {
    53     {
    54     CXnDomStringPool* self = new( ELeave ) CXnDomStringPool;
    54     CXnDomStringPool* self =
       
    55             new( ELeave ) CXnDomStringPool( aAllowDuplicates );
    55     
    56     
    56     CleanupStack::PushL( self );
    57     CleanupStack::PushL( self );
    57     self->ConstructL();
    58     self->ConstructL();
    58     CleanupStack::Pop(self);
    59     CleanupStack::Pop(self);
    59 
    60 
    64 // -----------------------------------------------------------------------------
    65 // -----------------------------------------------------------------------------
    65 // CXnDomStringPool::NewL
    66 // CXnDomStringPool::NewL
    66 // Two-phased stream constructor.
    67 // Two-phased stream constructor.
    67 // -----------------------------------------------------------------------------
    68 // -----------------------------------------------------------------------------
    68 //
    69 //
    69 CXnDomStringPool* CXnDomStringPool::NewL( RReadStream& aStream )
    70 CXnDomStringPool* CXnDomStringPool::NewL( RReadStream& aStream,
    70     {
    71         const TBool aAllowDuplicates )
    71     CXnDomStringPool* self = new( ELeave ) CXnDomStringPool;
    72     {
       
    73     CXnDomStringPool* self = new( ELeave ) CXnDomStringPool( aAllowDuplicates );
    72     CleanupStack::PushL( self );
    74     CleanupStack::PushL( self );
    73     aStream >> *self;
    75     aStream >> *self;
    74     CleanupStack::Pop(self);
    76     CleanupStack::Pop(self);
    75 
    77 
    76     return self;
    78     return self;
    82 // -----------------------------------------------------------------------------
    84 // -----------------------------------------------------------------------------
    83 //
    85 //
    84 CXnDomStringPool::~CXnDomStringPool()
    86 CXnDomStringPool::~CXnDomStringPool()
    85     {
    87     {
    86     iStringPool.ResetAndDestroy();
    88     iStringPool.ResetAndDestroy();
       
    89     iStringPoolOptimizer.Close();
    87     }
    90     }
    88 
    91 
    89 // -----------------------------------------------------------------------------
    92 // -----------------------------------------------------------------------------
    90 // CXnDomStringPool::CloneL
    93 // CXnDomStringPool::CloneL
    91 // -----------------------------------------------------------------------------
    94 // -----------------------------------------------------------------------------
    92 //
    95 //
    93 CXnDomStringPool* CXnDomStringPool::CloneL()
    96 CXnDomStringPool* CXnDomStringPool::CloneL()
    94     {
    97     {    
    95     CXnDomStringPool* clone = CXnDomStringPool::NewL();
    98     CXnDomStringPool* clone = NULL;    
       
    99     if( iAllowDuplicates )
       
   100         {
       
   101         clone = CXnDomStringPool::NewL( ETrue );
       
   102         }
       
   103     else
       
   104         {
       
   105         clone = CXnDomStringPool::NewL( EFalse );
       
   106         }    
    96     CleanupStack::PushL( clone );
   107     CleanupStack::PushL( clone );
    97     
   108     
    98     TInt count( iStringPool.Count() );
   109     TInt count( iStringPool.Count() );
    99     for ( TInt i=0; i<count; i++ )
   110     for ( TInt i = 0; i < count; i++ )
   100         {
   111         {    
   101         HBufC8* tmp = iStringPool[i]->Des().AllocLC();
   112         HBufC8* tmp = iStringPool[i]->Des().AllocLC();
   102         clone->iStringPool.AppendL( tmp );
   113         clone->DoAddStringL( tmp );
   103         CleanupStack::Pop( tmp );
   114         CleanupStack::Pop( tmp );
   104         }
   115         }
   105     CleanupStack::Pop( clone );
   116     CleanupStack::Pop( clone );
   106     return clone;
   117     return clone;
   107     }
   118     }
   108 
   119 
   109 // -----------------------------------------------------------------------------
   120 // -----------------------------------------------------------------------------
   110 // CXnDomNode::AddStringL
   121 // CXnDomNode::AddStringL
   111 // -----------------------------------------------------------------------------
   122 // -----------------------------------------------------------------------------
   112 //
   123 //
   113 EXPORT_C TInt CXnDomStringPool::AddStringL( const TDesC8& aString )
   124 TInt CXnDomStringPool::AddStringL( const TDesC8& aString )
   114     {
   125     {   
   115     TBool found( EFalse );
   126     TInt index = iStringPoolOptimizer.GetIndex( aString );
   116     TInt index( 0 );
   127 
   117      
   128     if( index == KErrNotFound )
   118     TInt count( iStringPool.Count() );
   129         {
   119     for (; index < count && !found;  )
   130         HBufC8* string = aString.AllocLC();
   120         {
   131         index = DoAddStringL( string );        
   121         if ( iStringPool[ index ]->Des().Compare( aString ) == 0 )
   132         CleanupStack::Pop( string );        
   122             {
   133         }    
   123             found = ETrue;
   134     
   124             }
   135     return index;    
   125         else
   136     }
   126             {
   137 
   127             index++;
   138 // -----------------------------------------------------------------------------
   128             }    
   139 // CXnDomNode::AddStringL
   129         }
   140 // -----------------------------------------------------------------------------
   130     if ( !found )
   141 //
   131         {
   142 TInt CXnDomStringPool::AddStringL( HBufC8* aString )
   132         HBufC8* tmp = aString.AllocLC();
   143     {   
   133         iStringPool.AppendL( tmp );
   144     if( !aString )
   134         CleanupStack::Pop( tmp );
   145         {
   135         index = iStringPool.Count()-1;    //Last item
   146         User::Leave( KErrArgument );
   136         iStringPoolOffsetNext++;
   147         }
   137         }
   148     
   138     return index;
   149     TInt index = iStringPoolOptimizer.GetIndex( *aString );    
       
   150     
       
   151     if( index == KErrNotFound )
       
   152         {
       
   153         index = DoAddStringL( aString );       
       
   154         }
       
   155     else
       
   156         {
       
   157         delete aString;
       
   158         }
       
   159     
       
   160     return index;    
       
   161     }
       
   162 
       
   163 // -----------------------------------------------------------------------------
       
   164 // CXnDomNode::AddStringL
       
   165 // -----------------------------------------------------------------------------
       
   166 //
       
   167 void CXnDomStringPool::AddAllL( CXnDomStringPool& aStringPool )
       
   168     {
       
   169     const TInt count = aStringPool.Count();
       
   170     for( TInt i = 0; i < count; i++ )
       
   171         {
       
   172         AddStringL( aStringPool.String( i ) );
       
   173         }    
   139     }
   174     }
   140 
   175 
   141 // -----------------------------------------------------------------------------
   176 // -----------------------------------------------------------------------------
   142 // CXnDomNode::String
   177 // CXnDomNode::String
   143 // -----------------------------------------------------------------------------
   178 // -----------------------------------------------------------------------------
   144 //
   179 //
   145 const TDesC8& CXnDomStringPool::String( const TInt aStringRef )
   180 const TDesC8& CXnDomStringPool::String( const TInt aStringRef )
   146     {
   181     {       
   147     return (*iStringPool[ aStringRef ]);
   182     if( aStringRef >= 0 && aStringRef < iStringPool.Count() )
   148     }
   183         {
   149         
   184         return (*iStringPool[ aStringRef ]);
       
   185         }
       
   186     else
       
   187         {
       
   188         return KNullDesC8;
       
   189         }
       
   190     }
       
   191 
   150 // -----------------------------------------------------------------------------
   192 // -----------------------------------------------------------------------------
   151 // CXnDomStringPool::Size
   193 // CXnDomStringPool::Size
   152 // -----------------------------------------------------------------------------
   194 // -----------------------------------------------------------------------------
   153 //
   195 //
   154 TInt CXnDomStringPool::Size() const
   196 TInt CXnDomStringPool::Size() const
   165         }
   207         }
   166     return size;    
   208     return size;    
   167     }
   209     }
   168 
   210 
   169 // -----------------------------------------------------------------------------
   211 // -----------------------------------------------------------------------------
       
   212 // CXnDomStringPool::Count
       
   213 // -----------------------------------------------------------------------------
       
   214 //
       
   215 TInt CXnDomStringPool::Count() const
       
   216     {
       
   217     return iStringPool.Count();
       
   218     }
       
   219 
       
   220 // -----------------------------------------------------------------------------
   170 // CXnDomStringPool::ExternalizeL
   221 // CXnDomStringPool::ExternalizeL
   171 // -----------------------------------------------------------------------------
   222 // -----------------------------------------------------------------------------
   172 //
   223 //
   173 void CXnDomStringPool::ExternalizeL( RWriteStream& aStream ) const
   224 void CXnDomStringPool::ExternalizeL( RWriteStream& aStream ) const
   174     {
   225     {
   187 // -----------------------------------------------------------------------------
   238 // -----------------------------------------------------------------------------
   188 //
   239 //
   189 void CXnDomStringPool::InternalizeL( RReadStream& aStream )
   240 void CXnDomStringPool::InternalizeL( RReadStream& aStream )
   190     {
   241     {
   191     TInt len(0);
   242     TInt len(0);
   192     TInt16 count ( aStream.ReadInt16L() );
   243     TInt16 count ( aStream.ReadInt16L() );    
   193     
       
   194     iStringPoolOffsetCurrent = iStringPoolOffsetNext;
       
   195     iStringPoolOffsetNext += count;
       
   196 
   244 
   197     for ( TInt i=0; i<count; i++ )
   245     for ( TInt i=0; i<count; i++ )
   198         {
   246         {
   199         len = aStream.ReadInt16L();
   247         len = aStream.ReadInt16L();
   200         HBufC8* tmp = HBufC8::NewLC( aStream, len );
   248         HBufC8* tmp = HBufC8::NewLC( aStream, len );
   201         iStringPool.AppendL( tmp );
   249         AddStringL( tmp ); // OWNERSHIP TRANSFERRED!        
   202         CleanupStack::Pop( tmp );
   250         CleanupStack::Pop( tmp );
   203         }
   251         }
   204     }
   252     }
   205 
   253 
   206 // -----------------------------------------------------------------------------
   254 // -----------------------------------------------------------------------------
   207 // CXnDomStringPool::Offset
   255 // CXnDomStringPool::DoAddStringL
   208 // -----------------------------------------------------------------------------
   256 // -----------------------------------------------------------------------------
   209 //
   257 //
   210 TUint CXnDomStringPool::Offset() const
   258 TInt CXnDomStringPool::DoAddStringL( HBufC8* aNewString )
   211     {
   259     {
   212     return iStringPoolOffsetCurrent;
   260     if( !aNewString )
       
   261         {
       
   262         User::Leave( KErrArgument );
       
   263         }       
       
   264     
       
   265     TInt index = iStringPool.Count();
       
   266     
       
   267     if( !iAllowDuplicates )
       
   268         {
       
   269         TXnDomStringPoolOptimizerEntry tmp( index, *aNewString );
       
   270         iStringPoolOptimizer.AddEntryL( tmp );
       
   271         }
       
   272     
       
   273     iStringPool.AppendL( aNewString );       
       
   274         
       
   275     return index;
   213     }
   276     }
   214 
   277 
   215 //  End of File  
   278 //  End of File