IMPSengine/datautils/src/impskey.cpp
changeset 0 094583676ce7
equal deleted inserted replaced
-1:000000000000 0:094583676ce7
       
     1 /*
       
     2 * Copyright (c) 2002 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: 
       
    15 * class for imps key.
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include    <e32base.h>
       
    22 #include    "impskey.h"
       
    23 #include    "impserrors.h"
       
    24 #include    "impsconst.h"
       
    25 
       
    26 #ifdef _DEBUG
       
    27 #include    <f32file.h>
       
    28 #endif
       
    29 
       
    30 // CONSTANTS
       
    31 const TInt KDefaultKeySize = 20;
       
    32 const TInt KAddRoom        = 20;
       
    33 
       
    34 // ================= MEMBER FUNCTIONS =======================
       
    35 
       
    36 // C++ default constructor can NOT contain any code, that
       
    37 // might leave.
       
    38 //
       
    39 CImpsKey::CImpsKey() :
       
    40 iBuffer(NULL),
       
    41 iCount(0)
       
    42     {
       
    43     }
       
    44 
       
    45 // Symbian OS default constructor can leave.
       
    46 void CImpsKey::ConstructL( )
       
    47     {
       
    48     iBuffer = HBufC16::NewL( KDefaultKeySize ); // Insert room for the elements
       
    49     }
       
    50 
       
    51 // Two-phased constructor.
       
    52 EXPORT_C CImpsKey* CImpsKey::NewL( )
       
    53     {
       
    54     CImpsKey* self = new (ELeave) CImpsKey;
       
    55     CleanupStack::PushL( self );
       
    56     self->ConstructL( );
       
    57     CleanupStack::Pop( );
       
    58     return self;
       
    59     }
       
    60 
       
    61 // Two-phased constructor.
       
    62 EXPORT_C CImpsKey* CImpsKey::NewLC( )
       
    63     {
       
    64     CImpsKey* self = new (ELeave) CImpsKey;
       
    65     CleanupStack::PushL( self );
       
    66     self->ConstructL( );
       
    67     return self;
       
    68     }
       
    69 
       
    70     
       
    71 // Destructor
       
    72 EXPORT_C CImpsKey::~CImpsKey()
       
    73     {
       
    74     delete iBuffer;    
       
    75     }
       
    76 
       
    77 
       
    78 // ---------------------------------------------------------
       
    79 // CImpsKey::AddL
       
    80 // Adds new element value the key
       
    81 // ---------------------------------------------------------
       
    82 //
       
    83 void CImpsKey::AddL(
       
    84     const TInt aElemvalue ,
       
    85     enum TImpsKeyType aKeyType)
       
    86     {
       
    87     if( iBuffer->Length( ) + 2 > iBuffer->Des().MaxLength() )
       
    88         {
       
    89         iBuffer = iBuffer->ReAllocL( iBuffer->Length( ) + KAddRoom ); // Insert room
       
    90         }
       
    91     TPtr16 buffer = iBuffer->Des();
       
    92     buffer.AppendFill( 0,2 );
       
    93     TUint16 val = STATIC_CAST(TUint16,GET_INDEX(aElemvalue));
       
    94     if(aKeyType == EImpsKeyTypeIM)
       
    95         val &= 0x7FFF; // High bit is zero
       
    96     else
       
    97         val |= 0x8000; // High bit is one
       
    98     buffer[iCount*2] = val;  // Index value
       
    99     buffer[iCount*2+1] = STATIC_CAST(TUint16,GET_ENUM(aElemvalue));       // Enum value
       
   100     iCount++;
       
   101     }
       
   102 
       
   103 // ---------------------------------------------------------
       
   104 // CImpsKey::PopL
       
   105 // Remove the last item from the key
       
   106 // ---------------------------------------------------------
       
   107 //
       
   108 void CImpsKey::PopL( TInt aCount )
       
   109    {
       
   110     if( iCount - aCount < 0 )
       
   111         User::Leave( KImpsErrorKeyEmpty );
       
   112     TPtr16 buffer = iBuffer->Des( );
       
   113     TInt size = buffer.Length( );
       
   114     buffer.SetLength( size - 2*aCount );
       
   115     iCount -= aCount;
       
   116    }
       
   117 
       
   118 // ---------------------------------------------------------
       
   119 // CImpsKey::GetElementL
       
   120 // Remove the last item from the key
       
   121 // ---------------------------------------------------------
       
   122 //
       
   123 TInt CImpsKey::GetElementL( 
       
   124    const TInt aIndex, TImpsKeyType& aKeyType ) const
       
   125    {
       
   126     if( aIndex < 0 )
       
   127         User::Leave( KImpsErrorKeyIndexInvalid );
       
   128     if( iCount == 0 )  
       
   129         User::Leave( KImpsErrorKeyEmpty );
       
   130     if( aIndex>=iCount )
       
   131         User::Leave( KImpsErrorKeyIndexTooLarge );
       
   132     TInt highval = (*iBuffer)[aIndex*2];
       
   133     if( IS_PRESENCE( highval*0x10000 ) )
       
   134         aKeyType = EImpsKeyTypePre;
       
   135     else 
       
   136         aKeyType = EImpsKeyTypeIM;
       
   137     highval &= 0x7FFF; // Leave out the high bit
       
   138     TInt ret = STATIC_CAST( TInt, highval * 0x10000 + (*iBuffer)[aIndex*2+1] );
       
   139     return ret;
       
   140    }
       
   141 
       
   142 // ---------------------------------------------------------
       
   143 // CImpsKey::ReplaceLastL
       
   144 // Replace last with new key
       
   145 // ---------------------------------------------------------
       
   146 //
       
   147 void CImpsKey::ReplaceLastL( 
       
   148    const TInt aElemvalue, 
       
   149    enum TImpsKeyType aKeyType )
       
   150    {
       
   151     PopL( );
       
   152     AddL( aElemvalue, aKeyType );
       
   153    }
       
   154 
       
   155 // ---------------------------------------------------------
       
   156 // CImpsKey::Reset
       
   157 // Reset to empty key
       
   158 // ---------------------------------------------------------
       
   159 //
       
   160 void CImpsKey::Reset()
       
   161    {
       
   162     TPtr16 buffer = iBuffer->Des( );
       
   163     buffer.SetLength( 0 );
       
   164     iCount = 0;
       
   165     }
       
   166 
       
   167 // ---------------------------------------------------------
       
   168 // CImpsKey::Compare
       
   169 // ---------------------------------------------------------
       
   170 //
       
   171 TInt CImpsKey::Compare( MImpsKey& aKey )
       
   172    {
       
   173    CImpsKey& alien = (CImpsKey&)aKey;
       
   174    return (*this==alien);
       
   175    }
       
   176 
       
   177 // ---------------------------------------------------------
       
   178 // CImpsKey::Copy
       
   179 // Copy the key
       
   180 // ---------------------------------------------------------
       
   181 //
       
   182 CImpsKey* CImpsKey::CopyL( ) const
       
   183     {
       
   184     CImpsKey* newKey = new (ELeave) CImpsKey;
       
   185     CleanupStack::PushL( newKey );
       
   186     HBufC16* newString = iBuffer->AllocL( );
       
   187     newKey->iBuffer = newString;
       
   188     newKey->iCount = iCount;
       
   189     CleanupStack::Pop( );
       
   190     return newKey;
       
   191     }
       
   192 
       
   193 // ---------------------------------------------------------
       
   194 // CImpsKey::CheckKey
       
   195 // ---------------------------------------------------------
       
   196 //
       
   197 TBool CImpsKey::CheckKey(
       
   198     const CImpsKey *aKey,
       
   199     TImpsKeyOptions aOptions ) const
       
   200     {
       
   201     // Find the string from the buffer
       
   202     // if the string is not found return EFalse
       
   203     TInt index = iBuffer->Find( aKey->iBuffer->Des() );
       
   204     if(index == KErrNotFound )
       
   205       return EFalse;
       
   206 
       
   207     // If aOptions is PARTIALKEY and index is 0 we know that the
       
   208     // key matched to the start of the key.
       
   209     // If aOptionms is PARTIALKEYLONGER we want the key to little bit more
       
   210     // than just the plain key. The key is not wholekey
       
   211     // If aOptions is EWholeKey and the index is 0 and also the
       
   212     // lengths must be the same then we have macthed whole key
       
   213     switch ( aOptions )
       
   214         {
       
   215         case EPartialKey:
       
   216             if( index==0 )
       
   217               return ETrue;
       
   218             break;
       
   219         case EPartialKeyLonger:
       
   220             if( index==0 && aKey->Count( ) < Count( ))
       
   221               return ETrue;
       
   222             break;
       
   223 
       
   224         case EWholeKey:
       
   225             if( index == 0 && aKey->Count( ) == Count( ) )
       
   226               return ETrue;
       
   227             break;
       
   228         default:
       
   229           break;
       
   230         }
       
   231     return EFalse;
       
   232     }
       
   233 
       
   234 // ---------------------------------------------------------
       
   235 // CImpsKey::Size
       
   236 // Size of the key
       
   237 // ---------------------------------------------------------
       
   238 //
       
   239 TInt CImpsKey::Size( ) const
       
   240     {
       
   241     TInt size = sizeof (TInt); // iCount
       
   242     size += iBuffer->Size( );
       
   243     return size;
       
   244     }
       
   245 
       
   246 // ---------------------------------------------------------
       
   247 // CImpsKey::Pack
       
   248 // Pack the key
       
   249 // ---------------------------------------------------------
       
   250 //
       
   251 TInt CImpsKey::Pack(
       
   252     const TUint8*& aPtrStart ) const
       
   253     {
       
   254     Mem::Copy( (void*)aPtrStart, &iCount, sizeof( iCount ) );
       
   255     aPtrStart = aPtrStart + sizeof( iCount );
       
   256     
       
   257     if( iCount )
       
   258         {
       
   259 //        TInt32 tempSize = 0;
       
   260 /*
       
   261         tempSize = iBuffer->Size();
       
   262         Mem::Copy( (void*)aPtrStart, &tempSize, sizeof( tempSize) );
       
   263         aPtrStart = aPtrStart + sizeof( tempSize );
       
   264         if ( iCount )
       
   265             {
       
   266             Mem::Copy( (void*)aPtrStart, iBuffer->Ptr(), tempSize);
       
   267             aPtrStart = aPtrStart + tempSize ;
       
   268             }
       
   269 */
       
   270         //        tempSize = iBuffer->Size();
       
   271         //        Mem::Copy( (void*)aPtrStart, &tempSize, sizeof( tempSize) );
       
   272         //        aPtrStart = aPtrStart + sizeof( tempSize );
       
   273         if ( iCount )
       
   274             {
       
   275             Mem::Copy( (void*)aPtrStart, iBuffer->Ptr(), iCount*4);
       
   276             aPtrStart = aPtrStart + iCount*4 ;
       
   277             }
       
   278         }
       
   279     return KErrNone;
       
   280     }
       
   281 
       
   282 // ---------------------------------------------------------
       
   283 // CImpsKey::UnPackL
       
   284 // Pack the key
       
   285 // ---------------------------------------------------------
       
   286 //
       
   287 void CImpsKey::UnPackL(
       
   288     TUint8*& aPtr )
       
   289     {
       
   290     Mem::Copy( &iCount, aPtr, sizeof( iCount ) );
       
   291     aPtr += sizeof( iCount );
       
   292     if( iCount )
       
   293         {
       
   294         const TUint8* textPtr = aPtr;
       
   295 //        TInt32 tempSize = 0;
       
   296 
       
   297 /*
       
   298         Mem::Copy( &tempSize, textPtr, sizeof( tempSize) );
       
   299         textPtr = textPtr + sizeof( tempSize );
       
   300         if ( iCount > 0 )
       
   301             {
       
   302             delete iBuffer;
       
   303             iBuffer = NULL;
       
   304             iBuffer = HBufC16::NewL( tempSize );  
       
   305             Mem::Copy( (void*)iBuffer->Ptr(), textPtr, tempSize );
       
   306             iBuffer->Des().SetLength( tempSize ); 
       
   307             textPtr = textPtr + tempSize; 
       
   308 */
       
   309         //        Mem::Copy( &tempSize, textPtr, sizeof( tempSize) );
       
   310         //        textPtr = textPtr + sizeof( tempSize );
       
   311         if ( iCount > 0 )
       
   312             {
       
   313             delete iBuffer;
       
   314             iBuffer = NULL;
       
   315             iBuffer = HBufC16::NewL( iCount*2 );  
       
   316             Mem::Copy( (void*)iBuffer->Ptr(), textPtr, iCount*4 );
       
   317             iBuffer->Des().SetLength( iCount*2 ); 
       
   318             textPtr = textPtr + iCount*4; 
       
   319             }
       
   320         if ( iCount < 0 )
       
   321             {
       
   322             User::Leave( KErrCorrupt );
       
   323             }
       
   324         aPtr = (TUint8*) textPtr;
       
   325         }
       
   326     }
       
   327 
       
   328 
       
   329 // ---------------------------------------------------------
       
   330 // CImpsKey::Destroy
       
   331 // Destroy the key
       
   332 // ---------------------------------------------------------
       
   333 //
       
   334 void CImpsKey::Destroy()
       
   335     {
       
   336     delete this;
       
   337     }
       
   338 
       
   339 #ifdef _DEBUG
       
   340 
       
   341 // ---------------------------------------------------------
       
   342 // CImpsKey::DumpToFileL
       
   343 // ---------------------------------------------------------
       
   344 //
       
   345 void CImpsKey::DumpToFileL( RFile& aFile ) const
       
   346     {
       
   347     _LIT8( KImpsKeyDumpBegin, "      ImpsKey dump begin\r\n");
       
   348     aFile.Write( KImpsKeyDumpBegin );
       
   349   
       
   350     TInt count = iCount;
       
   351     TBuf8<256> message; 
       
   352     _LIT8(KCount,"        Impskey number of elements = %d\r\n");
       
   353     _LIT8(KElement,"       Impskey element %d\r\n");
       
   354     _LIT8(KElementIndex,"       Impskey element index %d\r\n");
       
   355     _LIT8(KElementEnum,"       Impskey element enum %x\r\n");
       
   356 
       
   357     message.Format( KCount, count );
       
   358     aFile.Write( message );
       
   359 
       
   360 
       
   361     for( TInt k = 0; k< count; k++ )
       
   362         {
       
   363           TImpsKeyType keyType;
       
   364           TInt elem = GetElementL( k, keyType );
       
   365           message.Format( KElement, k );
       
   366           aFile.Write( message );
       
   367           if( keyType == EImpsKeyTypeIM )
       
   368               {
       
   369               _LIT8( KImpsKeyTypeIM, "      ImpsKey type = EImpsKeyTypeIM\r\n");
       
   370               aFile.Write( KImpsKeyTypeIM );
       
   371               }
       
   372          else
       
   373               {
       
   374               _LIT8( KImpsKeyTypePresence, "      ImpsKey type = EImpsKeyTypePre\r\n");
       
   375               aFile.Write( KImpsKeyTypePresence );
       
   376               }
       
   377           message.Format( KElementIndex, GET_INDEX(elem) );
       
   378           aFile.Write( message );
       
   379           message.Format( KElementEnum, GET_ENUM(elem) );
       
   380           aFile.Write( message );
       
   381         }
       
   382     
       
   383     _LIT8( KImpsKeyDumpEnd, "      ImpsKey dump end\r\n");
       
   384     aFile.Write( KImpsKeyDumpEnd );
       
   385     }
       
   386 #endif
       
   387 
       
   388 // ---------------------------------------------------------
       
   389 // CImpsKey::SetElementIndex
       
   390 // ---------------------------------------------------------
       
   391 //
       
   392 void CImpsKey::SetElementIndex( TInt aCount, TInt aNewIndex )
       
   393     {
       
   394     if( iCount == 0 )  
       
   395         return;
       
   396     if( aCount < 0 )
       
   397         return;
       
   398     if( aCount >= iCount )
       
   399         return;
       
   400     TInt highval = (*iBuffer)[aCount*2];
       
   401     TUint16 val = STATIC_CAST(TUint16,aNewIndex );
       
   402     if( IS_PRESENCE( highval*0x10000 ) )
       
   403         val |= 0x8000;
       
   404     else 
       
   405         val &= 0x7FFF;
       
   406     TPtr16 buffer = iBuffer->Des();
       
   407     buffer[aCount*2] = val;  // Index value
       
   408     }
       
   409 
       
   410 //  End of File  
       
   411