filesystemuis/memstatepopup/src/MSPUtil.cpp
branchRCL_3
changeset 21 65326cf895ed
equal deleted inserted replaced
20:491b3ed49290 21:65326cf895ed
       
     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: CMSPUtil Implementation
       
    15 *
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 //  INCLUDE FILES
       
    21 #include <e32std.h>
       
    22 #include "MSPPanic.hrh"
       
    23 #include "msputil.h"
       
    24 
       
    25 // ---------------------------------------------------------------------------
       
    26 
       
    27 CMSPUtil* CMSPUtil::NewL( CDesCArrayFlat* aArray )
       
    28 	{
       
    29 	__ASSERT_DEBUG( aArray,
       
    30 					User::Panic( KPanicMSP, EInvalidParameter ) );
       
    31 	CMSPUtil* self = new( ELeave ) CMSPUtil;
       
    32 	self->iUnitArray = aArray;
       
    33 	return self;
       
    34 	}
       
    35 
       
    36 // ---------------------------------------------------------------------------
       
    37 
       
    38 CMSPUtil::~CMSPUtil( )
       
    39 	{
       
    40 	if( iUnitArray )
       
    41 	    {
       
    42 	    iUnitArray->Delete( 0, iUnitArray->Count( ) );
       
    43 	    }
       
    44 	delete iUnitArray;
       
    45 	}
       
    46 
       
    47 // ---------------------------------------------------------------------------
       
    48 
       
    49 CMSPUtil::CMSPUtil( ): iUnitArray( NULL )
       
    50 	{
       
    51 	}
       
    52 
       
    53 // ---------------------------------------------------------------------------
       
    54 
       
    55 void CMSPUtil::SolveUnitAndSize( TInt64& aNumber, TInt& aUnit )
       
    56 	{
       
    57 	TInt count( iUnitArray->Count( ) );
       
    58 	const TInt KMaxDigits = 10000;
       
    59 	const TInt KKiloByte = 1024;
       
    60 	const TInt KRoundLimit = 512;
       
    61 	const TInt KShiftValue = 10;
       
    62 	aUnit = EByte;
       
    63 
       
    64 	// If number is 1024 or more, it can be divided and unit changed
       
    65 	while( aNumber >= 1024 && (aUnit + 1) < count )
       
    66 		{
       
    67 		// Show kilobytes and megabytes with 4 digits
       
    68 		if( ( aUnit == EKiloByte || aUnit == EMegaByte ) && aNumber < KMaxDigits )
       
    69 			{
       
    70 			break;
       
    71 			}
       
    72 		// Check how the rounding should be done
       
    73 		if( aNumber % KKiloByte < KRoundLimit )
       
    74 			{
       
    75 			// Round downwards
       
    76 			aNumber >>= KShiftValue;	// 2^10 = 1024
       
    77 			}
       
    78 		else
       
    79 			{
       
    80 			aNumber = ( aNumber >> KShiftValue ) + 1;
       
    81 			}
       
    82 		aUnit++;		// Next enum
       
    83 		}
       
    84 	}
       
    85 
       
    86 
       
    87 // ---------------------------------------------------------------------------
       
    88 
       
    89 EXPORT_C TPtrC CMSPUtil::SolveUnitAndSize( TInt64& aNumber )
       
    90 	{
       
    91 	TInt unitType( EByte );
       
    92 	SolveUnitAndSize( aNumber, unitType );
       
    93     if( unitType >= iUnitArray->Count() )
       
    94         {
       
    95         if( iUnitArray->Count() )
       
    96             {
       
    97             unitType = iUnitArray->Count() - 1;
       
    98             }
       
    99         else
       
   100             {
       
   101             unitType = 0;
       
   102             }
       
   103         }
       
   104         
       
   105 	return ( *iUnitArray )[ unitType ];
       
   106 	}
       
   107 
       
   108 // End of File
       
   109