srsf/vcommandhandler/src/desarrayconverter.cpp
branchRCL_3
changeset 23 e36f3802f733
parent 0 bf1d17376201
equal deleted inserted replaced
22:cad71a31b7fc 23:e36f3802f733
       
     1 /*
       
     2 * Copyright (c) 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:  A utility object to convert descriptors to 
       
    15 *                VAS savable arrays and vise versa
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 #include "rubydebug.h"
       
    21 #include "desarrayconverter.h"
       
    22 
       
    23 /**
       
    24 * Converts the stream data into the array of fixed size descriptors
       
    25 * @param aSource 8-bit descriptor to convert
       
    26 */
       
    27 CArrayFixFlat<NssRRDText>* CDesArrayConverter::DesC8ToArrayLC( 
       
    28 											  const TDesC8& aSource )
       
    29 	{
       
    30     NssRRDText buf;
       
    31     
       
    32     const TInt fieldSize = buf.MaxLength(); 
       
    33     TInt textFieldCount = aSource.Length() / fieldSize;
       
    34     if( aSource.Length() % fieldSize != 0 )
       
    35         {
       
    36         textFieldCount++;
       
    37         }
       
    38     RUBY_DEBUG1( "CDesArrayConverter::DesC8ToArrayLC [%d] slots are needed \
       
    39     			to store the streamed command", textFieldCount );
       
    40     
       
    41     
       
    42     CArrayFixFlat<NssRRDText>* array = new (ELeave) CArrayFixFlat<NssRRDText>( textFieldCount );
       
    43     CleanupStack::PushL( array );
       
    44     
       
    45     TInt appendedSize = fieldSize;
       
    46     for( TInt i(0); i < textFieldCount; i++ )
       
    47         {
       
    48         // the last piece
       
    49         if( i == textFieldCount - 1 ) 
       
    50             {
       
    51             appendedSize = aSource.Length() - fieldSize * i;
       
    52             }
       
    53         buf.Copy( aSource.Mid( fieldSize*i, appendedSize ) );
       
    54         array->AppendL( buf );
       
    55         }
       
    56     return array;
       
    57 	}
       
    58 
       
    59 /**
       
    60 * Constructs Des8 descriptor from the given array slice. Almost a 
       
    61 * counterpart of the DesC8ToArrayLC
       
    62 * @param aArray Array to convert
       
    63 * @param aStartIndex First element to convert. Zero-based
       
    64 * @param aCount number of elements to convert. 
       
    65 * @leave KErrArgument if aStartIndex or aCount are out of the array bounds
       
    66 */
       
    67 TDesC8* CDesArrayConverter::ArrayToDesC8LC( const CArrayFixFlat<NssRRDText>& aArray, 
       
    68 													TInt aStartIndex, TInt aCount )
       
    69 	{
       
    70     __ASSERT_ALWAYS( aStartIndex < aArray.Count(), User::Leave( KErrArgument) );
       
    71     __ASSERT_ALWAYS( aStartIndex + aCount <= aArray.Count(), User::Leave( KErrArgument) );
       
    72     // Calculate the required descriptor length
       
    73     TInt storageLength (0);
       
    74     for( TInt i = aStartIndex; i < aStartIndex + aCount; i++ )
       
    75         {
       
    76         storageLength += aArray[i].Length();
       
    77         }
       
    78     HBufC8* storage = HBufC8::NewLC( storageLength );
       
    79     
       
    80     for( TInt i = aStartIndex; i < aStartIndex + aCount; i++ ) 
       
    81         {
       
    82         // 16 bit chars are converted to 8-bit chars
       
    83         storage->Des().Append( aArray[i] );
       
    84         }
       
    85     return storage;
       
    86 	}
       
    87 
       
    88 			
       
    89 //End of file