skins/AknSkins/srvsrc/AknsSrvFileBuffer.cpp
changeset 0 05e9090e2422
equal deleted inserted replaced
-1:000000000000 0:05e9090e2422
       
     1 /*
       
     2 * Copyright (c) 2003-2008 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:  Skin serv's file buffer.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 
       
    21 #include "AknsSrvFileBuffer.h"
       
    22 
       
    23 // CONSTANTS
       
    24 
       
    25 static const int KAknsSrvFileBufSize = 10*1024;
       
    26 
       
    27 // ============================ MEMBER FUNCTIONS ===============================
       
    28 
       
    29 // -----------------------------------------------------------------------------
       
    30 // C++ Constructor
       
    31 // -----------------------------------------------------------------------------
       
    32 //
       
    33 CAknsSrvFileBuffer::CAknsSrvFileBuffer()
       
    34     // CBase initializes: iBuffer(0), iPosition(0), iLength(0)
       
    35     {
       
    36     }
       
    37 
       
    38 // -----------------------------------------------------------------------------
       
    39 // Destructor
       
    40 // -----------------------------------------------------------------------------
       
    41 //
       
    42 CAknsSrvFileBuffer::~CAknsSrvFileBuffer()
       
    43     {
       
    44     delete iData;
       
    45     delete iContent;
       
    46     delete iBuffer;
       
    47     }
       
    48 
       
    49 // -----------------------------------------------------------------------------
       
    50 // Two-phased constructor
       
    51 // -----------------------------------------------------------------------------
       
    52 //
       
    53 void CAknsSrvFileBuffer::ConstructL(RFile& aFilehandle)
       
    54     {
       
    55     iBuffer = HBufC8::NewL( KAknsSrvFileBufSize );
       
    56     iContent = ContentAccess::CContent::NewL(aFilehandle);
       
    57     iData = iContent->OpenContentL(ContentAccess::EView, ContentAccess::EContentShareReadOnly);
       
    58     FillBufferL();
       
    59     }
       
    60 
       
    61 // -----------------------------------------------------------------------------
       
    62 // CAknsSrvFileBuffer::NewL
       
    63 // -----------------------------------------------------------------------------
       
    64 //
       
    65 CAknsSrvFileBuffer* CAknsSrvFileBuffer::NewL( RFile& aFilehandle )
       
    66     {
       
    67     CAknsSrvFileBuffer* self = new (ELeave) CAknsSrvFileBuffer( );
       
    68     CleanupStack::PushL( self );
       
    69     self->ConstructL(aFilehandle);
       
    70     CleanupStack::Pop( self );
       
    71     return self;
       
    72     }
       
    73 
       
    74 
       
    75 // -----------------------------------------------------------------------------
       
    76 // CAknsSrvFileBuffer::GetInt32L
       
    77 // -----------------------------------------------------------------------------
       
    78 //
       
    79 TInt32 CAknsSrvFileBuffer::GetInt32L( const TUint aOffset )
       
    80     {
       
    81     ReadToBufferL( aOffset, 4 );
       
    82     TInt32 value;
       
    83     Mem::Copy( &value, PtrToBuffer(aOffset), 4 );
       
    84     return value;
       
    85     }
       
    86 
       
    87 // -----------------------------------------------------------------------------
       
    88 // CAknsSrvFileBuffer::GetUint16L
       
    89 // -----------------------------------------------------------------------------
       
    90 //
       
    91 TUint16 CAknsSrvFileBuffer::GetUint16L( const TUint aOffset )
       
    92     {
       
    93     ReadToBufferL( aOffset, 2 );
       
    94     TUint16 value;
       
    95     Mem::Copy( &value, PtrToBuffer(aOffset), 2 );
       
    96     return value;
       
    97     }
       
    98 
       
    99 // -----------------------------------------------------------------------------
       
   100 // CAknsSrvFileBuffer::GetInt16L
       
   101 // -----------------------------------------------------------------------------
       
   102 //
       
   103 TInt16 CAknsSrvFileBuffer::GetInt16L( const TUint aOffset )
       
   104     {
       
   105     ReadToBufferL( aOffset, 2 );
       
   106     TInt16 value;
       
   107     Mem::Copy( &value, PtrToBuffer(aOffset), 2 );
       
   108     return value;
       
   109     }
       
   110 
       
   111 // -----------------------------------------------------------------------------
       
   112 // CAknsSrvFileBuffer::GetUint8L
       
   113 // -----------------------------------------------------------------------------
       
   114 //
       
   115 TUint8 CAknsSrvFileBuffer::GetUint8L( const TUint aOffset )
       
   116     {
       
   117     ReadToBufferL( aOffset, 1 );
       
   118     TUint8 value;
       
   119     Mem::Copy( &value, PtrToBuffer(aOffset), 1 );
       
   120     return value;
       
   121     }
       
   122 
       
   123 // -----------------------------------------------------------------------------
       
   124 // CAknsSrvFileBuffer::PtrToBuffer
       
   125 // -----------------------------------------------------------------------------
       
   126 //
       
   127 const TAny* CAknsSrvFileBuffer::PtrToBuffer( const TUint aOffset ) const
       
   128     {
       
   129     return (iBuffer->Ptr()) + aOffset - iPosition;
       
   130     }
       
   131 
       
   132 // -----------------------------------------------------------------------------
       
   133 // CAknsSrvFileBuffer::ReadToBufferL
       
   134 // -----------------------------------------------------------------------------
       
   135 //
       
   136 void CAknsSrvFileBuffer::ReadToBufferL( const TUint aOffset, const TUint aSize )
       
   137     {
       
   138     if( IsWithin( aOffset, aSize ) )
       
   139         {
       
   140         return;
       
   141         }
       
   142 
       
   143     iPosition = static_cast<TInt>(aOffset);
       
   144     FillBufferL();
       
   145 
       
   146     if( !IsWithin( aOffset, aSize ) )
       
   147         {
       
   148         User::Leave( KErrEof );
       
   149         }
       
   150     }
       
   151 
       
   152 // -----------------------------------------------------------------------------
       
   153 // CAknsSrvFileBuffer::FillBufferL
       
   154 // -----------------------------------------------------------------------------
       
   155 //
       
   156 void CAknsSrvFileBuffer::FillBufferL()
       
   157     {
       
   158     TPtr8 ptr( iBuffer->Des() );
       
   159 
       
   160     iLength = 0;
       
   161 
       
   162     User::LeaveIfError( iData->Seek(ESeekStart, iPosition) );
       
   163     User::LeaveIfError( iData->Read(ptr, KAknsSrvFileBufSize));
       
   164     iLength = ptr.Length();
       
   165     }
       
   166 
       
   167 // -----------------------------------------------------------------------------
       
   168 // CAknsSrvFileBuffer::IsWithin
       
   169 // -----------------------------------------------------------------------------
       
   170 //
       
   171 TBool CAknsSrvFileBuffer::IsWithin( const TUint aOffset, const TUint aSize ) const
       
   172     {
       
   173     if( (static_cast<TInt>(aOffset) >= iPosition) &&
       
   174         (static_cast<TInt>(aOffset+aSize) <= (iPosition+iLength)) )
       
   175         {
       
   176         return ETrue;
       
   177         }
       
   178     return EFalse;
       
   179     }
       
   180 
       
   181 //  End of File