smartinstaller/common/config.cpp
branchADM
changeset 14 343c622c9f65
equal deleted inserted replaced
13:8ed1157e9487 14:343c622c9f65
       
     1 /*
       
     2 * Copyright (c) 2009-2010 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: 
       
    15 *     Implementation to read from the config file.
       
    16 *
       
    17 *
       
    18 */
       
    19 
       
    20 
       
    21 #include "config.h"
       
    22 
       
    23 // ---------------------------------------------------------------------------
       
    24 // ReadConfigFile
       
    25 //
       
    26 // Reads the configuration file to aBuffer, aLineBuffer containing the pointers to lines.
       
    27 //
       
    28 // @return TInt normal Symbian error code or KErrNone if all went OK.
       
    29 // ---------------------------------------------------------------------------
       
    30 //
       
    31 TInt ReadConfigFile(RFile& aFile, TDes& aBuffer, RArray< TPtrC >& aLineBuffer )
       
    32 	{
       
    33 	// Read text file into buffers
       
    34 	TInt ret( KErrNone );
       
    35 	TFileText tf;
       
    36 	tf.Set( aFile );
       
    37 	aBuffer.SetMax();
       
    38 	TPtr ptr( aBuffer.MidTPtr( 0 ) );
       
    39 	TInt used( 0 );
       
    40 	do
       
    41 		{
       
    42 		ret = tf.Read( ptr );
       
    43 		TInt len( ptr.Length() );
       
    44 		if ( ( ret == KErrNone || ret == KErrEof ) && len > 0 )
       
    45 			{
       
    46 			// Store non-empty text line
       
    47 			TInt err( aLineBuffer.Append( ptr ) );
       
    48 			if ( err == KErrNone )
       
    49 				{
       
    50 				ptr.SetMax();
       
    51 				ptr.Set( ptr.MidTPtr( len ) );
       
    52 				ptr.Zero();
       
    53 				used += len;
       
    54 				}
       
    55 			else
       
    56 				{
       
    57 				ret = err;
       
    58 				}
       
    59 			}
       
    60 		}
       
    61 	while ( ret == KErrNone );
       
    62 	if ( ret == KErrEof )
       
    63 		{
       
    64 		// reached the end of file without any other error => this is OK
       
    65 		ret = KErrNone;
       
    66 		}
       
    67 	aBuffer.SetLength( used );
       
    68 
       
    69 	return ret;
       
    70 	}
       
    71 
       
    72 // ---------------------------------------------------------------------------
       
    73 // ReadConfigFile
       
    74 //
       
    75 // @return HBufC containg the read config file. aLineBuffer arrays pointing to lines.
       
    76 // ---------------------------------------------------------------------------
       
    77 //
       
    78 HBufC* ReadConfigFile( RFs& aFs, const TDesC& aFullPath, RArray< TPtrC >& aLineBuffer, TInt& aError )
       
    79 	{
       
    80 	RFile file;
       
    81 	aError = file.Open( aFs, aFullPath, EFileRead | EFileStreamText | EFileShareReadersOnly );
       
    82 	if ( aError != KErrNone )
       
    83 		{
       
    84 		return NULL;
       
    85 		}
       
    86 	TInt size( 0 );
       
    87 	aError = file.Size( size );
       
    88 	if ( aError != KErrNone )
       
    89 		{
       
    90 		file.Close();
       
    91 		return NULL;
       
    92 		}
       
    93 	// Get text size, create buffer for text and read text file
       
    94 	HBufC* ret = HBufC::New( ( size + sizeof( TText ) - 1 ) / sizeof( TText ) );
       
    95 	if ( !ret )
       
    96 		{
       
    97 		aError = KErrNoMemory;
       
    98 		file.Close();
       
    99 		return NULL;
       
   100 		}
       
   101 	TPtr ptr( ret->Des() );
       
   102 	aError = ReadConfigFile( file, ptr, aLineBuffer );
       
   103 	file.Close();
       
   104 	if ( aError != KErrNone )
       
   105 		{
       
   106 		delete ret;
       
   107 		aLineBuffer.Reset();
       
   108 		return NULL;
       
   109 		}
       
   110 	return ret;
       
   111 	}
       
   112 
       
   113 // ---------------------------------------------------------------------------
       
   114 // GetConfigValue
       
   115 //
       
   116 // Gets string specified by tag. For examples:
       
   117 // Tag1=Value1  Tag1 = aTag, Value1 will be copied to aTag
       
   118 // aError Normal Symbian error code or KErrNone if all went OK.
       
   119 // ---------------------------------------------------------------------------
       
   120 //
       
   121 TPtrC GetConfigValue( const TDesC& aTag, const RArray< TPtrC >& aLineBuffer, TInt& aError )
       
   122 	{
       
   123 	aError = KErrNotFound;
       
   124 	TPtrC ret( KNullDesC );
       
   125 	const TInt tagLen( aTag.Length() );
       
   126 	const TInt count( aLineBuffer.Count() );
       
   127 	for( TInt i( 0 ); i < count; ++i )
       
   128 		{
       
   129 		TPtrC line( aLineBuffer[ i ] );
       
   130 		if ( !line.Left( tagLen ).CompareF( aTag ) )
       
   131 			{
       
   132 			ret.Set( line.Mid( tagLen ) );
       
   133 			aError = KErrNone;
       
   134 			break;
       
   135 			}
       
   136 		}
       
   137 
       
   138 	return ret;
       
   139 	}
       
   140 
       
   141 TInt CompareVersions(TVersion& version1,TVersion& version2)
       
   142 	{
       
   143 	// Compare the versions based on major,minor and build number.
       
   144 	if((version1.iMajor == version2.iMajor)&&(version1.iMinor == version2.iMinor)&&(version1.iBuild == version2.iBuild))
       
   145 		{
       
   146 		return EEqualVersion;
       
   147 		}
       
   148 	else if ((version1.iMajor > version2.iMajor) ||
       
   149 			((version1.iMajor == version2.iMajor)&&(version1.iMinor > version2.iMinor)) ||
       
   150 			((version1.iMajor == version2.iMajor)&&(version1.iMinor == version2.iMinor)&&(version1.iBuild >= version2.iBuild)))
       
   151 		{
       
   152 		return EGreaterFirstVersion;
       
   153 		}
       
   154 	else
       
   155 		{
       
   156 		return EGreaterSecondVersion;
       
   157 		}
       
   158 	}
       
   159 
       
   160 TBool SetVersion(const TDesC8& aVersionPtr, TVersion& aVer)
       
   161 	{
       
   162 	// Function will return EFalse if aVersionPtr is not a valid
       
   163 	// version string
       
   164 	TLex8 lex(aVersionPtr);
       
   165 	TInt count = 0;
       
   166 	lex.SkipSpace();
       
   167 
       
   168 	// Get Major Version (max length 3)
       
   169 	const TInt maxMajorVersionLength = 3;
       
   170 	lex.Mark();
       
   171 	while ( (count<maxMajorVersionLength) && (lex.Peek() != '.') )
       
   172 		{
       
   173 		lex.Inc();
       
   174 		++count;
       
   175 		}
       
   176 
       
   177 	if (!(lex.Peek() == '.'))
       
   178 		{
       
   179 		return EFalse;
       
   180 		}
       
   181 
       
   182 	TLex8 lexToken(lex.MarkedToken());
       
   183 	if ( lexToken.Val(aVer.iMajor) != KErrNone )
       
   184 		{
       
   185 		return EFalse;
       
   186 		}
       
   187 
       
   188 	// Get Minor Version(max length 2)
       
   189 	const TInt maxMinorVersionLength = 2;
       
   190 	count = 0;
       
   191 	lex.Inc();
       
   192 	lex.Mark();
       
   193 	while ( (count<maxMinorVersionLength) && (lex.Peek() != '.') )
       
   194 		{
       
   195 		lex.Inc();
       
   196 		++count;
       
   197 		}
       
   198 
       
   199 	lexToken.Assign(lex.MarkedToken());
       
   200 	if ( lexToken.Val(aVer.iMinor) != KErrNone )
       
   201 		{
       
   202 		return EFalse;
       
   203 		}
       
   204 
       
   205 	// Check if Build Number exists,
       
   206 	// otherwise return
       
   207 	if (!(lex.Peek() == '.'))
       
   208 	   {
       
   209 	   lex.Mark();
       
   210 	   lex.SkipCharacters();
       
   211 	   if (lex.TokenLength() > 0)
       
   212 		   {
       
   213 		   return EFalse;
       
   214 		   }
       
   215 	   else
       
   216 		   {
       
   217 		   aVer.iBuild = 0;
       
   218 		   return ETrue;
       
   219 		   }
       
   220 	   }
       
   221 
       
   222 	// Get Build Number(max length 5)
       
   223 	const TInt maxBuildNoLength = 5;
       
   224 	lex.Inc();
       
   225 	lex.Mark();
       
   226 	lex.SkipCharacters();
       
   227 
       
   228 	if (lex.TokenLength() > maxBuildNoLength)
       
   229 		{
       
   230 		return EFalse;
       
   231 		}
       
   232 
       
   233 	lexToken.Assign(lex.MarkedToken());
       
   234 	if ( lexToken.Val(aVer.iBuild) != KErrNone )
       
   235 		{
       
   236 		return EFalse;
       
   237 		}
       
   238 	return ETrue;
       
   239 	}