hti/HtiCfg/src/HtiCfg.cpp
changeset 36 813b186005b6
parent 30 86a2e675b80a
child 41 838cdffd57ce
equal deleted inserted replaced
30:86a2e675b80a 36:813b186005b6
     1 /*
       
     2 * Copyright (c) 2009 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:  CHtiCfg implementation
       
    15 *
       
    16 */
       
    17 
       
    18 // INCLUDE FILES
       
    19 #include "HtiCfg.h"
       
    20 #include <badesca.h>
       
    21 #include <f32file.h>
       
    22 
       
    23 // CONSTANTS
       
    24 const static TInt KCfgArrayGranularity = 5;
       
    25 
       
    26 // MACROS
       
    27 
       
    28 // LOCAL CONSTANTS AND MACROS
       
    29 
       
    30 // MODULE DATA STRUCTURES
       
    31 
       
    32 // LOCAL FUNCTION PROTOTYPES
       
    33 
       
    34 // FORWARD DECLARATIONS
       
    35 
       
    36 // ============================ MEMBER FUNCTIONS ===============================
       
    37 
       
    38 // -----------------------------------------------------------------------------
       
    39 // CHtiCfg::NewL
       
    40 // Two-phased constructor.
       
    41 // -----------------------------------------------------------------------------
       
    42 EXPORT_C CHtiCfg* CHtiCfg::NewL()
       
    43     {
       
    44     CHtiCfg* self = NewLC();
       
    45     CleanupStack::Pop( self );
       
    46     return self;
       
    47     }
       
    48 
       
    49 // -----------------------------------------------------------------------------
       
    50 // CHtiCfg::NewLC
       
    51 // Two-phased constructor.
       
    52 // -----------------------------------------------------------------------------
       
    53 EXPORT_C CHtiCfg* CHtiCfg::NewLC()
       
    54     {
       
    55     CHtiCfg* self = new ( ELeave ) CHtiCfg();
       
    56     CleanupStack::PushL( self );
       
    57     self->ConstructL();
       
    58     return self;
       
    59     }
       
    60 
       
    61 // -----------------------------------------------------------------------------
       
    62 // CHtiCfg::CHtiCfg
       
    63 // Constructor
       
    64 // -----------------------------------------------------------------------------
       
    65 CHtiCfg::CHtiCfg():iCfgParameters( NULL )
       
    66     {
       
    67     }
       
    68 
       
    69 // -----------------------------------------------------------------------------
       
    70 // CHtiCfg::CHtiCfg
       
    71 // Destructor
       
    72 // -----------------------------------------------------------------------------
       
    73 EXPORT_C CHtiCfg::~CHtiCfg()
       
    74     {
       
    75     if ( iCfgParameters )
       
    76         {
       
    77         iCfgParameters->Reset();
       
    78         delete iCfgParameters;
       
    79         iCfgParameters = NULL;
       
    80         }
       
    81     }
       
    82 
       
    83 // -----------------------------------------------------------------------------
       
    84 // CHtiCfg::ConstructL
       
    85 // Second phase constructor. Private.
       
    86 // -----------------------------------------------------------------------------
       
    87 void CHtiCfg::ConstructL()
       
    88     {
       
    89     iCfgParameters = new ( ELeave ) CDesC8ArrayFlat( KCfgArrayGranularity );
       
    90     }
       
    91 
       
    92 // -----------------------------------------------------------------------------
       
    93 // CHtiCfg::LoadCfgL
       
    94 // Searches the file in all drives and if found reads all parameter lines to the
       
    95 // iCfgParameters array.
       
    96 // -----------------------------------------------------------------------------
       
    97 EXPORT_C void CHtiCfg::LoadCfgL( const TDesC& aCfgFilePath,
       
    98                                  const TDesC& aCfgFileName )
       
    99     {
       
   100     // Reset parameters
       
   101     if ( iCfgParameters )
       
   102         {
       
   103         iCfgParameters->Reset();
       
   104         delete iCfgParameters;
       
   105         iCfgParameters = NULL;
       
   106         }
       
   107     iCfgParameters = new ( ELeave ) CDesC8ArrayFlat( KCfgArrayGranularity );
       
   108 
       
   109     // Open & read file
       
   110     RFs fsSession;
       
   111     User::LeaveIfError( fsSession.Connect() );
       
   112     CleanupClosePushL( fsSession );
       
   113 
       
   114     TFindFile finder( fsSession );
       
   115     TFileName path( aCfgFilePath );
       
   116     TInt err = finder.FindByPath( aCfgFileName, &path );
       
   117     if ( err != KErrNone )
       
   118         {
       
   119         User::Leave( err );
       
   120         }
       
   121 
       
   122     TFileName cfgFile = finder.File();
       
   123 
       
   124     RFile file;
       
   125     User::LeaveIfError( file.Open( fsSession, cfgFile, EFileRead ) );
       
   126     CleanupClosePushL( file );
       
   127 
       
   128     TInt fileLength;
       
   129     User::LeaveIfError( file.Size( fileLength ) );
       
   130 
       
   131     HBufC8* fileData = HBufC8::NewLC( fileLength );
       
   132     TPtr8 fileDes = fileData->Des();
       
   133     User::LeaveIfError( file.Read( fileDes ) );
       
   134 
       
   135     // Get parameters
       
   136     TBool eof = EFalse;
       
   137     while ( !eof )
       
   138         {
       
   139         TInt strEndIndex = fileDes.Locate( KCfgNewLine );
       
   140         if ( strEndIndex == KErrNotFound )
       
   141             {
       
   142                 strEndIndex = fileDes.Length();
       
   143                 eof = ETrue;
       
   144             }
       
   145 
       
   146         TPtrC8 line = fileDes.Left( strEndIndex );
       
   147 
       
   148         if ( line.Locate( KCfgComment ) != 0 )
       
   149             {
       
   150             if ( line.Locate( KCfgSeparator ) > 0 )
       
   151                 {
       
   152                 TBuf8<KMaxParameterLength> parameter;
       
   153                 parameter.Copy( line );
       
   154                 parameter.Trim();
       
   155                 iCfgParameters->AppendL( parameter );
       
   156                 }
       
   157             }
       
   158 
       
   159         if ( !eof )
       
   160             fileDes = fileDes.Right( fileDes.Length() - ( strEndIndex + 1 ) );
       
   161 
       
   162         }
       
   163 
       
   164     CleanupStack::PopAndDestroy( 3 ); // fsSession, fileData, file
       
   165     }
       
   166 
       
   167 // -----------------------------------------------------------------------------
       
   168 // CHtiCfg::SaveCfgL
       
   169 // Writes all parameter lines from iCfgParameters array to the file.
       
   170 // Existing file is searched from all drives and if found file will be replaced.
       
   171 // If existing file is not found file will be created to c-drive.
       
   172 // -----------------------------------------------------------------------------
       
   173 EXPORT_C void CHtiCfg::SaveCfgL( const TDesC& aCfgFilePath,
       
   174                                  const TDesC& aCfgFileName )
       
   175     {
       
   176     RFs fs;
       
   177     User::LeaveIfError( fs.Connect() );
       
   178     CleanupClosePushL( fs );
       
   179 
       
   180     RFile file;
       
   181     CleanupClosePushL( file );
       
   182 
       
   183     // Find the file
       
   184     TFindFile finder( fs );
       
   185     TFileName path( aCfgFilePath );
       
   186     TFileName cfgFile;
       
   187     TInt err = finder.FindByPath( aCfgFileName, &path );
       
   188     if ( err != KErrNone )
       
   189         {
       
   190         cfgFile.Append( _L ( "c:" ) );
       
   191         cfgFile.Append( path );
       
   192         cfgFile.Append( aCfgFileName );
       
   193         }
       
   194     else
       
   195         {
       
   196         cfgFile = finder.File();
       
   197         // If the file was found from ROM, we must save to C-drive.
       
   198         if ( cfgFile[0] == 'z' || cfgFile[0] == 'Z' )
       
   199             {
       
   200             cfgFile[0] = 'c';
       
   201             }
       
   202         }
       
   203 
       
   204     // Replace or create the file
       
   205     User::LeaveIfError( file.Replace( fs, cfgFile, EFileWrite | EFileShareAny ) );
       
   206 
       
   207     // Write the file
       
   208     for ( TInt i = 0; i < iCfgParameters->Count(); i++ )
       
   209         {
       
   210         file.Write( iCfgParameters->MdcaPoint( i ) );
       
   211         file.Write( _L8( "\n" ) );
       
   212         }
       
   213 
       
   214     // Close
       
   215     CleanupStack::PopAndDestroy( 2 ); // fs, file
       
   216     }
       
   217 
       
   218 // -----------------------------------------------------------------------------
       
   219 // CHtiCfg::SetParameterL
       
   220 // -----------------------------------------------------------------------------
       
   221 EXPORT_C TInt CHtiCfg::SetParameterL( const TDesC8& aName,
       
   222                                       const TDesC8& aValue )
       
   223     {
       
   224     // Does the parameter exist?
       
   225     for ( TInt i = 0; i < iCfgParameters->Count(); i++ )
       
   226         {
       
   227         TInt sepIndex =
       
   228             ( iCfgParameters->MdcaPoint( i ) ).Locate( KCfgSeparator );
       
   229         if ( sepIndex <= 0 )
       
   230             User::Leave( KErrGeneral ); // should not happen
       
   231 
       
   232         TPtrC8 name = ( iCfgParameters->MdcaPoint( i ) ).Left( sepIndex );
       
   233         if ( name.Compare( aName ) == 0 )
       
   234             {
       
   235             iCfgParameters->Delete( i );
       
   236             TBuf8<KMaxParameterLength> parameter;
       
   237             parameter.Append( aName );
       
   238             parameter.Append( KCfgSeparator );
       
   239             parameter.Append( aValue );
       
   240             iCfgParameters->AppendL( parameter );
       
   241             return KErrNone;
       
   242             }
       
   243         }
       
   244 
       
   245     // Apparently not.. add it
       
   246     TBuf8<KMaxParameterLength> parameter;
       
   247     parameter.Append( aName );
       
   248     parameter.Append( KCfgSeparator );
       
   249     parameter.Append( aValue );
       
   250     iCfgParameters->AppendL( parameter );
       
   251 
       
   252     return KErrNone;
       
   253     }
       
   254 
       
   255 // -----------------------------------------------------------------------------
       
   256 // CHtiCfg::RemoveParameterL
       
   257 // -----------------------------------------------------------------------------
       
   258 EXPORT_C TInt CHtiCfg::RemoveParameterL( const TDesC8& aName )
       
   259     {
       
   260     for ( TInt i = 0; i < iCfgParameters->Count(); i++ )
       
   261         {
       
   262         TInt sepIndex =
       
   263             ( iCfgParameters->MdcaPoint( i ) ).Locate( KCfgSeparator );
       
   264         if ( sepIndex <= 0 )
       
   265             User::Leave( KErrGeneral ); // should not happen
       
   266 
       
   267         TPtrC8 name = ( iCfgParameters->MdcaPoint( i ) ).Left( sepIndex );
       
   268         if ( name.Compare( aName ) == 0 )
       
   269             {
       
   270             iCfgParameters->Delete( i );
       
   271             return KErrNone;
       
   272             }
       
   273         }
       
   274 
       
   275     return KErrNotFound;
       
   276     }
       
   277 
       
   278 // -----------------------------------------------------------------------------
       
   279 // CHtiCfg::GetParameterL
       
   280 // -----------------------------------------------------------------------------
       
   281 EXPORT_C TPtrC8 CHtiCfg::GetParameterL( const TDesC8& aName )
       
   282     {
       
   283     for ( TInt i = 0; i < iCfgParameters->Count(); i++ )
       
   284         {
       
   285         TInt sepIndex =
       
   286             ( iCfgParameters->MdcaPoint( i ) ).Locate( KCfgSeparator );
       
   287         if ( sepIndex <= 0 )
       
   288             User::Leave( KErrGeneral ); // should not happen
       
   289 
       
   290         TPtrC8 name = ( iCfgParameters->MdcaPoint( i ) ).Left( sepIndex );
       
   291         if ( name.Compare( aName ) == 0 )
       
   292             {
       
   293             TPtrC8 value = ( iCfgParameters->MdcaPoint( i ) ).Right(
       
   294                 ( iCfgParameters->MdcaPoint( i ) ).Length() - ( sepIndex + 1 ) );
       
   295             return value;
       
   296             }
       
   297         }
       
   298     User::Leave( KErrNotFound );
       
   299     return 0;
       
   300     }
       
   301 
       
   302 // -----------------------------------------------------------------------------
       
   303 // CHtiCfg::GetParameterIntL
       
   304 // -----------------------------------------------------------------------------
       
   305 EXPORT_C TInt CHtiCfg::GetParameterIntL( const TDesC8& aName )
       
   306     {
       
   307     for ( TInt i = 0; i < iCfgParameters->Count(); i++ )
       
   308         {
       
   309         TInt sepIndex =
       
   310             ( iCfgParameters->MdcaPoint( i ) ).Locate( KCfgSeparator );
       
   311         if ( sepIndex <= 0 )
       
   312             User::Leave( KErrGeneral ); // should not happen
       
   313 
       
   314         TPtrC8 name = ( iCfgParameters->MdcaPoint( i ) ).Left( sepIndex );
       
   315         if ( name.Compare( aName ) == 0 )
       
   316             {
       
   317             TPtrC8 value = ( iCfgParameters->MdcaPoint( i ) ).Right(
       
   318                 ( iCfgParameters->MdcaPoint( i ) ).Length() - ( sepIndex + 1 ) );
       
   319 
       
   320             TLex8 lex( value );
       
   321             TInt result;
       
   322             User::LeaveIfError( lex.Val( result ) );
       
   323             return result;
       
   324             }
       
   325         }
       
   326     User::Leave( KErrNotFound );
       
   327     return 0;
       
   328     }
       
   329 
       
   330 
       
   331 // End of file