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