landmarks/locationlandmarks/clientlib/src/epos_cposreadbufstorage.cpp
changeset 0 667063e416a2
equal deleted inserted replaced
-1:000000000000 0:667063e416a2
       
     1 /*
       
     2 * Copyright (c) 2005 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:  CPosReadBufStorage class implementation
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "epos_cposreadbufstorage.h"
       
    20 
       
    21 // ======== MEMBER FUNCTIONS ========
       
    22 
       
    23 // ---------------------------------------------------------------------------
       
    24 // C++ constructor
       
    25 // ---------------------------------------------------------------------------
       
    26 //
       
    27 CPosReadBufStorage::CPosReadBufStorage( const TDesC8& aBuffer ) 
       
    28     : iBuffer( aBuffer ) 
       
    29     {
       
    30     iPointer = iBuffer.Ptr();
       
    31     iBoundary = iPointer + iBuffer.Size();
       
    32     }
       
    33 
       
    34 // ---------------------------------------------------------------------------
       
    35 // Static constructor
       
    36 // ---------------------------------------------------------------------------
       
    37 //
       
    38 CPosReadBufStorage* CPosReadBufStorage::NewLC( const TDesC8& aBuffer ) 
       
    39     {
       
    40     CPosReadBufStorage* self = new ( ELeave ) CPosReadBufStorage( aBuffer );
       
    41     CleanupStack::PushL( self );
       
    42     return self;
       
    43     }
       
    44 
       
    45 // ---------------------------------------------------------------------------
       
    46 // Copies aSize bytes from internal buffer to aTrg address
       
    47 // Leaves if buffer is shorter than requested amount of data.
       
    48 // ---------------------------------------------------------------------------
       
    49 //
       
    50 void CPosReadBufStorage::GetL( TUint8* aTrg, TUint aSize ) 
       
    51     {
       
    52     if ( iPointer + aSize <= iBoundary ) 
       
    53         {
       
    54         memcpy( aTrg, iPointer, aSize );
       
    55         iPointer += aSize;
       
    56         }
       
    57     else
       
    58         {
       
    59         User::Leave( KErrCorrupt );
       
    60         }
       
    61     }
       
    62 
       
    63 // ---------------------------------------------------------------------------
       
    64 // Copies TInt from internal buffer
       
    65 // ---------------------------------------------------------------------------
       
    66 //
       
    67 void CPosReadBufStorage::GetL( TInt& aData ) 
       
    68     {
       
    69     GetL( (TUint8*) &aData, sizeof( TInt ) );
       
    70     }
       
    71 
       
    72 // ---------------------------------------------------------------------------
       
    73 // Copies TUint32 from internal buffer
       
    74 // ---------------------------------------------------------------------------
       
    75 //
       
    76 void CPosReadBufStorage::GetL( TUint32& aData ) 
       
    77   {
       
    78   GetL( (TUint8*) &aData, sizeof( TUint32 ) );
       
    79   }
       
    80 
       
    81 // ---------------------------------------------------------------------------
       
    82 // Copies TUint16 from internal buffer
       
    83 // ---------------------------------------------------------------------------
       
    84 //
       
    85 void CPosReadBufStorage::GetL( TUint16& aData ) 
       
    86   {
       
    87   GetL( (TUint8*) &aData, sizeof( TUint16 ) );
       
    88   }
       
    89 
       
    90 // ---------------------------------------------------------------------------
       
    91 // Copies descriptor from internal buffer to aData buffer.
       
    92 // Size of the data to be copied is read from the buffer.
       
    93 // Leaves if target descriptor cannot accept descriptor in buffer.
       
    94 // ---------------------------------------------------------------------------
       
    95 //
       
    96 void CPosReadBufStorage::GetL( TDes8& aData ) 
       
    97   {
       
    98   TInt size;
       
    99   GetL( size );
       
   100   if ( size >= 0 && 
       
   101      aData.MaxSize() >= size &&
       
   102      iPointer + size <= iBoundary ) 
       
   103     {
       
   104     aData = TPtrC8( (TUint8*) iPointer, size );
       
   105     iPointer += size;
       
   106     }
       
   107   else
       
   108     {
       
   109     User::Leave( KErrCorrupt );
       
   110     }
       
   111   }
       
   112 
       
   113 // ---------------------------------------------------------------------------
       
   114 // Sets passed descriptor to data from internal buffer.
       
   115 // Size and length of the data are read from the buffer.
       
   116 // ---------------------------------------------------------------------------
       
   117 //
       
   118 void CPosReadBufStorage::GetL( TPtrC& aData ) 
       
   119   {
       
   120   TInt size, length;
       
   121   GetL( length );
       
   122   GetL( size );
       
   123   if ( size >= 0 && length >= 0 && size >= length && iPointer + size <= iBoundary ) 
       
   124     {
       
   125     aData.Set( (TUint16*) iPointer, length );
       
   126     iPointer += size;  
       
   127     }
       
   128   else
       
   129     {
       
   130     User::Leave( KErrCorrupt );
       
   131     }
       
   132   }