srsf/sicc/src/nsssicustomcommanddata.cpp
branchRCL_3
changeset 19 e36f3802f733
parent 0 bf1d17376201
equal deleted inserted replaced
18:cad71a31b7fc 19:e36f3802f733
       
     1 /*
       
     2 * Copyright (c) 2004-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:  Private serialization functions for Custom Commands
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include <s32mem.h>
       
    21 #include "nsssicustomcommanddata.h"
       
    22 #include "rubydebug.h"
       
    23 
       
    24 // LOCAL FUNCTION PROTOTYPES
       
    25 void CleanupForPointerArrays( TAny* anArray );
       
    26 void CleanupResetAndDestroyAndDeletePushL( RPointerArray<MDesCArray>* anArray );
       
    27 
       
    28 
       
    29 // ============================= LOCAL FUNCTIONS ===============================
       
    30 
       
    31 // -----------------------------------------------------------------------------
       
    32 // CleanupForPointerArrays
       
    33 //
       
    34 // Cleanup function for RPointerArray, which calls ResetAndDestroy()
       
    35 // before deleting.
       
    36 //
       
    37 // Returns: None
       
    38 // -----------------------------------------------------------------------------
       
    39 //
       
    40 void CleanupForPointerArrays(
       
    41     TAny* anArray) // Pointer to RPointerArray<MDesCArray>
       
    42     {
       
    43     RPointerArray<MDesCArray> *array = (RPointerArray<MDesCArray> *)anArray;
       
    44     array->ResetAndDestroy();
       
    45     delete array;
       
    46     }
       
    47 
       
    48 // -----------------------------------------------------------------------------
       
    49 // CleanupResetAndDestroyAndDeletePushL
       
    50 //
       
    51 // Pushes the RPointerArray to the cleanup stack, so that
       
    52 // both the container and its contents are deleted in case of a leave.
       
    53 //
       
    54 // Returns: None
       
    55 // -----------------------------------------------------------------------------
       
    56 //
       
    57 void CleanupResetAndDestroyAndDeletePushL(
       
    58     RPointerArray<MDesCArray> *anArray) // Array to be destroyed and deleted
       
    59     {
       
    60     TCleanupItem cleanupItem( CleanupForPointerArrays, (TAny *)anArray );
       
    61     CleanupStack::PushL( cleanupItem );
       
    62     }
       
    63 
       
    64 
       
    65 // ============================ MEMBER FUNCTIONS ===============================
       
    66 
       
    67 // -----------------------------------------------------------------------------
       
    68 // CSpeakerIndependentCustomCommandParser::ExternalizeDesCArrayL
       
    69 // Externalizes a string array.
       
    70 // (other items were commented in a header).
       
    71 // -----------------------------------------------------------------------------
       
    72 //	
       
    73 CBufFlat* SICustomCommandData::ExternalizeDesCArrayL(
       
    74 	const MDesCArray& aArray)
       
    75 	{
       
    76 
       
    77 	CBufFlat* dataCopyBuffer = CBufFlat::NewL(KExpandSize);
       
    78 	CleanupStack::PushL(dataCopyBuffer);
       
    79 	RBufWriteStream stream;
       
    80 	stream.Open(*dataCopyBuffer);
       
    81 	CleanupClosePushL(stream);
       
    82 
       
    83     // Write the number of strings
       
    84 	stream.WriteInt32L(aArray.MdcaCount());
       
    85 
       
    86     // Write strings: First string length, then the characters
       
    87     for( TInt k = 0; k < aArray.MdcaCount(); k++ )
       
    88         {
       
    89         TPtrC text( aArray.MdcaPoint( k ) );
       
    90 
       
    91         stream.WriteInt32L( text.Length() );
       
    92 
       
    93         if ( text.Length() > 0 )
       
    94             {
       
    95             stream.WriteL( text );
       
    96             }
       
    97         }
       
    98 
       
    99 	CleanupStack::PopAndDestroy(1); //stream
       
   100 	CleanupStack::Pop(); //dataCopyBuffer;
       
   101 	return dataCopyBuffer;	
       
   102 	}
       
   103 
       
   104 // -----------------------------------------------------------------------------
       
   105 // CSpeakerIndependentCustomCommandParser::InternalizeDesCArrayL
       
   106 // Internalizes a string array.
       
   107 // (other items were commented in a header).
       
   108 // -----------------------------------------------------------------------------
       
   109 //	
       
   110 MDesCArray *SICustomCommandData::InternalizeDesCArrayL(
       
   111 	const TDesC8& aBuffer)
       
   112 	{
       
   113 	CDesCArrayFlat* phraseArray = new CDesCArrayFlat(KExpandSize);
       
   114 	CleanupStack::PushL(phraseArray);
       
   115 
       
   116 	RDesReadStream stream;
       
   117 	stream.Open(aBuffer);
       
   118 	CleanupClosePushL(stream);
       
   119 
       
   120 	TInt count = stream.ReadInt32L();
       
   121 
       
   122     TInt   phraseBufLength = KDefaultPhraseLen;
       
   123     HBufC *phraseBuf = HBufC::NewLC( phraseBufLength );
       
   124     TPtr   phrase    = phraseBuf->Des();
       
   125 
       
   126     for( TInt k = 0; k < count; k++ )
       
   127         {
       
   128         TInt length = stream.ReadInt32L();
       
   129 
       
   130         if ( length > phraseBufLength )
       
   131             {
       
   132             CleanupStack::PopAndDestroy( phraseBuf );
       
   133             phraseBuf = HBufC::NewLC( length );
       
   134             phraseBufLength = length;
       
   135             phrase.Set( phraseBuf->Des() );
       
   136             }
       
   137 
       
   138         stream.ReadL( phrase, length );
       
   139         phraseArray->AppendL( phrase );
       
   140         phrase.Zero();
       
   141         }
       
   142 
       
   143 	CleanupStack::PopAndDestroy(2); //phraseBuf, stream
       
   144 	CleanupStack::Pop(phraseArray); //phraseArray;
       
   145 	return phraseArray;	
       
   146 	}
       
   147 
       
   148 // -----------------------------------------------------------------------------
       
   149 // CSpeakerIndependentCustomCommandParser::ExternalizeDesCArrayArrayL
       
   150 // Externalizes an array of string arrays.
       
   151 // (other items were commented in a header).
       
   152 // -----------------------------------------------------------------------------
       
   153 //	
       
   154 CBufFlat* SICustomCommandData::ExternalizeDesCArrayArrayL(
       
   155 	const RPointerArray<MDesCArray>& aArrayArray)
       
   156 	{
       
   157 	CBufFlat* dataCopyBuffer = CBufFlat::NewL(KExpandSize);
       
   158 	CleanupStack::PushL(dataCopyBuffer);
       
   159 	RBufWriteStream stream;
       
   160 	stream.Open(*dataCopyBuffer);
       
   161 	CleanupClosePushL(stream);
       
   162 
       
   163     stream.WriteInt32L(aArrayArray.Count());
       
   164 
       
   165     for( TInt arrayK = 0; arrayK < aArrayArray.Count(); arrayK++ )
       
   166         {
       
   167         const MDesCArray& aArray = *aArrayArray[ arrayK ];
       
   168         // Write the number of strings
       
   169 	    stream.WriteInt32L(aArray.MdcaCount());
       
   170 
       
   171         // Write strings: First string length, then the characters
       
   172         for( TInt k = 0; k < aArray.MdcaCount(); k++ )
       
   173             {
       
   174             TPtrC text( aArray.MdcaPoint( k ) );
       
   175 
       
   176             stream.WriteInt32L( text.Length() );
       
   177 
       
   178             if ( text.Length() > 0 )
       
   179                 {
       
   180                 stream.WriteL( text );
       
   181                 }
       
   182             }
       
   183         }
       
   184 
       
   185 	CleanupStack::PopAndDestroy(1); //stream
       
   186 	CleanupStack::Pop(); //dataCopyBuffer;
       
   187 	return dataCopyBuffer;	
       
   188 	
       
   189 	}
       
   190 
       
   191 
       
   192 // -----------------------------------------------------------------------------
       
   193 // CSpeakerIndependentCustomCommandParser::InternalizeDesCArrayArrayL
       
   194 // Internalizes an array of string arrays array.
       
   195 // (other items were commented in a header).
       
   196 // -----------------------------------------------------------------------------
       
   197 //
       
   198 RPointerArray<MDesCArray> *SICustomCommandData::InternalizeDesCArrayArrayL(
       
   199 	const TDesC8& aBuffer)
       
   200 	{
       
   201     RPointerArray<MDesCArray>* phraseArrayArray = new (ELeave) RPointerArray<MDesCArray>(KExpandSize);
       
   202     CleanupResetAndDestroyAndDeletePushL(phraseArrayArray);
       
   203 
       
   204 	RDesReadStream stream;
       
   205 	stream.Open(aBuffer);
       
   206 	CleanupClosePushL(stream);
       
   207 
       
   208 	TInt arrayCount = stream.ReadInt32L();
       
   209 
       
   210     TInt   phraseBufLength = KDefaultPhraseLen;
       
   211     HBufC *phraseBuf = HBufC::NewL( phraseBufLength );
       
   212     TPtr   phrase    = phraseBuf->Des();
       
   213     CleanupStack::PushL( phraseBuf );
       
   214 
       
   215     for( TInt arrayK = 0; arrayK < arrayCount; arrayK++ )
       
   216         {
       
   217         
       
   218 	    TInt count = stream.ReadInt32L();
       
   219 
       
   220     	CDesCArrayFlat* phraseArray = new CDesCArrayFlat(KExpandSize);
       
   221         CleanupStack::PushL( phraseArray );
       
   222 
       
   223         for( TInt k = 0; k < count; k++ )
       
   224             {
       
   225             TInt length = stream.ReadInt32L();
       
   226 
       
   227             if ( length > phraseBufLength )
       
   228                 {
       
   229                 HBufC* newPhraseBuf = HBufC::NewL( length );
       
   230 
       
   231                 CleanupStack::Pop( phraseArray );
       
   232                 CleanupStack::PopAndDestroy( phraseBuf );
       
   233 
       
   234                 phraseBuf = newPhraseBuf;
       
   235                 phraseBufLength = length;
       
   236                 phrase.Set( phraseBuf->Des() );
       
   237 
       
   238                 CleanupStack::PushL( phraseBuf );
       
   239                 CleanupStack::PushL( phraseArray );
       
   240                 }
       
   241 
       
   242             stream.ReadL( phrase, length );
       
   243             phraseArray->AppendL( phrase );
       
   244             phrase.Zero();
       
   245             }
       
   246 
       
   247         User::LeaveIfError( phraseArrayArray->Append( phraseArray ) );
       
   248         CleanupStack::Pop( phraseArray );
       
   249         }
       
   250 
       
   251 	CleanupStack::PopAndDestroy(2); //phraseBuf, stream
       
   252 	CleanupStack::Pop(1); //phraseArrayArray
       
   253 	return phraseArrayArray;	
       
   254 	}
       
   255 
       
   256 
       
   257 // -----------------------------------------------------------------------------
       
   258 // CSpeakerIndependentCustomCommandParser::InternalizeDesCArrayArrayL
       
   259 // Internalizes an array of string arrays array.
       
   260 // (other items were commented in a header).
       
   261 // -----------------------------------------------------------------------------
       
   262 //
       
   263 HBufC8* SICustomCommandData::ReadMessageDataLC( TMMFMessage& aMessage, TInt aSlot )
       
   264     {
       
   265     // MMF messages have 2 slots
       
   266     if ( aSlot != 1 && aSlot != 2 )
       
   267     	{
       
   268         RUBY_DEBUG0( "SICustomCommandData::ReadMessageDataLC Leaving with KErrArgument" );
       
   269     	User::Leave( KErrArgument );
       
   270     	}
       
   271         
       
   272 
       
   273     TInt size = 0;
       
   274 
       
   275     if ( aSlot == 1 )
       
   276         {
       
   277         size = aMessage.SizeOfData1FromClient();
       
   278         }
       
   279     else
       
   280         {
       
   281         size = aMessage.SizeOfData2FromClient();
       
   282         }
       
   283 
       
   284     HBufC8* data = HBufC8::NewLC( size );
       
   285     TPtr8 ptr = data->Des();
       
   286 
       
   287     if ( aSlot == 1 )
       
   288         {
       
   289         aMessage.ReadData1FromClientL( ptr );
       
   290         }
       
   291     else{
       
   292         aMessage.ReadData2FromClientL( ptr );
       
   293         }
       
   294 
       
   295     return( data );
       
   296     }
       
   297 
       
   298 
       
   299 // -----------------------------------------------------------------------------
       
   300 // CSpeakerIndependentCustomCommandParser::InternalizePronunArrayL
       
   301 // Internalizes an array of string arrays array.
       
   302 // (other items were commented in a header).
       
   303 // -----------------------------------------------------------------------------
       
   304 //
       
   305 void SICustomCommandData::InternalizePronunArrayL(
       
   306     const TDesC8& aData,
       
   307     TInt aCount,
       
   308     RArray<TSIPronunciationID>& aArray )
       
   309     {
       
   310     const TSIPronunciationID* pronunPtr = (const TSIPronunciationID*)aData.Ptr();
       
   311 
       
   312     for ( TInt k = 0; k < aCount; k++ )
       
   313         {
       
   314         User::LeaveIfError( aArray.Append( *pronunPtr ) );
       
   315         pronunPtr++;
       
   316         }
       
   317     }
       
   318 
       
   319 
       
   320 // End of File