codhandler/codeng/inc/FileExt.h
changeset 0 dd21522fd290
equal deleted inserted replaced
-1:000000000000 0:dd21522fd290
       
     1 /*
       
     2 * Copyright (c) 2002-2004 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 the License "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:  Extension to RFile class to load custom data from file.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 #ifndef RFILEEXT_H
       
    21 #define RFILEEXT_H
       
    22 
       
    23 //  INCLUDES
       
    24 #include <e32base.h>
       
    25 #include <f32file.h>
       
    26 
       
    27 // CONSTANTS
       
    28 
       
    29 // MACROS
       
    30 #define MIN( a, b ) ( (a) < (b) ? (a) : (b) )
       
    31 #define MAX( a, b ) ( (a) > (b) ? (a) : (b) )
       
    32 
       
    33 
       
    34 #define APPEND_BUF_INT( buf, a ) { buf.Append( (TUint8*)&(a), sizeof(a) ); }
       
    35 
       
    36 #define READ_INT_L( fs, a ) { TPtr8 buf( (TUint8*)&(a), sizeof(a) ); \
       
    37                               User::LeaveIfError( fs.Read( buf ) ); \
       
    38                               if( buf.Length() != sizeof(a) ) \
       
    39                                 {   \
       
    40                                 User::Leave( KErrEof ); \
       
    41                                 }   \
       
    42                           }
       
    43 
       
    44 #define READ_CUST_L( fs, a ) { TPtr8 buff( (TUint8*)&(a), sizeof(a) ); \
       
    45                                fs.Read( buff, sizeof(a) ); \
       
    46                                if( buff.Length() == 0 ) \
       
    47                                     { \
       
    48                                     User::Leave( KErrEof ); \
       
    49                                     } \
       
    50                             }
       
    51 
       
    52 // indicates that the passed HBufC(8)* pointer is/was NULL
       
    53 const TInt KNullBuffer = -1;
       
    54 
       
    55 inline void ReadHBufCL( RFile& aInFile, HBufC* &aBuf )
       
    56     {
       
    57     TInt length;
       
    58 
       
    59     delete aBuf;
       
    60     aBuf = NULL;
       
    61 
       
    62     // read length of the buffer
       
    63     READ_INT_L( aInFile, length );
       
    64 
       
    65     if( length > 10*1024 )
       
    66         // we surely didn't have so huge block
       
    67         {
       
    68         User::Leave( KErrCorrupt );
       
    69         }
       
    70 
       
    71     if( length != KNullBuffer )
       
    72         {
       
    73         aBuf = HBufC::NewL( length / 2 );   // byte -> dbyte
       
    74         if( length )
       
    75             {
       
    76             TPtr8 buf( (TUint8*)aBuf->Ptr(), length );
       
    77 
       
    78             User::LeaveIfError( aInFile.Read( buf, length ) );
       
    79 
       
    80             if( buf.Length() == length )
       
    81                 {
       
    82                 aBuf->Des().SetLength( length / 2 );
       
    83                 }
       
    84             else
       
    85                 // couldn't read enough byte -> eof
       
    86                 {
       
    87                 delete aBuf;
       
    88                 aBuf = NULL;
       
    89 
       
    90                 User::Leave( KErrEof );
       
    91                 }
       
    92             }
       
    93         }
       
    94     }
       
    95 
       
    96 inline void ReadHBufCL( RFile& aInFile, HBufC8* &aBuf )
       
    97     {
       
    98     TInt length;
       
    99 
       
   100     delete aBuf;
       
   101     aBuf = NULL;
       
   102 
       
   103     // read length of the buffer
       
   104     READ_INT_L( aInFile, length );
       
   105 
       
   106     if( length > 10*1024 )
       
   107         // we surely didn't have so huge block
       
   108         {
       
   109         User::Leave( KErrCorrupt );
       
   110         }
       
   111 
       
   112     if( length != KNullBuffer )
       
   113         {
       
   114         aBuf = HBufC8::NewL( length );   // byte -> dbyte
       
   115         if( length )
       
   116             {
       
   117             TPtr8 buf( (TUint8*)aBuf->Ptr(), length );
       
   118 
       
   119             User::LeaveIfError( aInFile.Read( buf, length ) );
       
   120 
       
   121             if( buf.Length() == length )
       
   122                 {
       
   123                 aBuf->Des().SetLength( length );
       
   124                 }
       
   125             else
       
   126                 // couldn't read enough byte -> eof
       
   127                 {
       
   128                 delete aBuf;
       
   129                 aBuf = NULL;
       
   130 
       
   131                 User::Leave( KErrEof );
       
   132                 }
       
   133             }
       
   134         }
       
   135     }
       
   136 
       
   137 /**
       
   138  * Appends data and data length info to buffer
       
   139  * @param aDest destinaion buffer
       
   140  * @param aSrc  source of data
       
   141  * @return None. Leaves on error.
       
   142  */
       
   143 void AppendBufL( TPtr8& aDest, HBufC* aSrc );
       
   144 
       
   145 /**
       
   146  * Appends data and data length info to buffer
       
   147  * @param aDest destinaion buffer
       
   148  * @param aSrc  source of data
       
   149  * @return None. Leaves on error.
       
   150  */
       
   151 void AppendBufL( TPtr8& aDest, HBufC8* aSrc );
       
   152 
       
   153 inline void ReallocateStringL( HBufC* &aBuff, const TDesC& aValue, TInt aMaxSize = 0 )
       
   154     {
       
   155     delete aBuff;
       
   156     aBuff = NULL;
       
   157     TInt min;
       
   158     TInt max;
       
   159 
       
   160     if( aMaxSize )
       
   161         {
       
   162         min = MIN( aValue.Length(), aMaxSize );
       
   163         max = MAX( aValue.Length(), aMaxSize );
       
   164         }
       
   165     else
       
   166         {
       
   167         min = aValue.Length();
       
   168         max = aValue.Length();
       
   169         }
       
   170     
       
   171     aBuff = HBufC::NewL( max );
       
   172     aBuff->Des().Copy( aValue );
       
   173     aBuff->Des().SetLength( min );
       
   174     }
       
   175 
       
   176 inline void ReallocateStringL( HBufC8* &aBuff, const TDesC8& aValue, TInt aMaxSize = 0 )
       
   177     {
       
   178     delete aBuff;
       
   179     aBuff = NULL;
       
   180     TInt min;
       
   181     TInt max;
       
   182 
       
   183     if( aMaxSize )
       
   184         {
       
   185         min = MIN( aValue.Length(), aMaxSize );
       
   186         max = MAX( aValue.Length(), aMaxSize );
       
   187         }
       
   188     else
       
   189         {
       
   190         min = aValue.Length();
       
   191         max = aValue.Length();
       
   192         }
       
   193     
       
   194     aBuff = HBufC8::NewL( max );
       
   195     aBuff->Des().Copy( aValue );
       
   196     aBuff->Des().SetLength( min );
       
   197     }
       
   198 
       
   199 inline void ReallocateStringL( HBufC8* &aBuff, const TDesC& aValue, TInt aMaxSize = 0 )
       
   200     {
       
   201     delete aBuff;
       
   202     aBuff = NULL;
       
   203     TInt min;
       
   204     TInt max;
       
   205 
       
   206     if( aMaxSize )
       
   207         {
       
   208         min = MIN( aValue.Length(), aMaxSize );
       
   209         max = MAX( aValue.Length(), aMaxSize );
       
   210         }
       
   211     else
       
   212         {
       
   213         min = aValue.Length();
       
   214         max = aValue.Length();
       
   215         }
       
   216     
       
   217     aBuff = HBufC8::NewL( max );
       
   218     aBuff->Des().Copy( aValue );
       
   219     aBuff->Des().SetLength( min );
       
   220     }
       
   221 
       
   222 inline void ReallocateStringL( HBufC* &aBuff, const TDesC8& aValue, TInt aMaxSize = 0 )
       
   223     {
       
   224     delete aBuff;
       
   225     aBuff = NULL;
       
   226     TInt min;
       
   227     TInt max;
       
   228 
       
   229     if( aMaxSize )
       
   230         {
       
   231         min = MIN( aValue.Length(), aMaxSize );
       
   232         max = MAX( aValue.Length(), aMaxSize );
       
   233         }
       
   234     else
       
   235         {
       
   236         min = aValue.Length();
       
   237         max = aValue.Length();
       
   238         }
       
   239     
       
   240     aBuff = HBufC::NewL( max );
       
   241     aBuff->Des().Copy( aValue );
       
   242     aBuff->Des().SetLength( min );
       
   243     }
       
   244 
       
   245 
       
   246 #endif      // RFILEEXT_H
       
   247             
       
   248 // End of File