smartinstaller/common/config.cpp
branchADM
changeset 48 364021cecc90
equal deleted inserted replaced
47:3f419852be07 48:364021cecc90
       
     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 #include "config.h"
       
    21 
       
    22 // ---------------------------------------------------------------------------
       
    23 // WriteIntToConfigFile
       
    24 //
       
    25 // Write the given TUint32 value and tag into the given file.
       
    26 //
       
    27 // @return TInt normal Symbian error code or KErrNone if all went OK.
       
    28 // ---------------------------------------------------------------------------
       
    29 //
       
    30 TInt WriteIntToConfigFile( RFile& aFile, const TDesC& aTag, const TUint32 aValue )
       
    31 	{
       
    32 	const TInt valueLength = 10; //Length of TUint32
       
    33 	TBufC<valueLength> value;
       
    34 	TPtr valuePtr = value.Des();
       
    35 
       
    36 	valuePtr.AppendNum(aValue);
       
    37 	TInt err = WriteToConfigFile( aFile, aTag, value );
       
    38 
       
    39 	return err;
       
    40 	}
       
    41 
       
    42 // ---------------------------------------------------------------------------
       
    43 // WriteToConfigFile
       
    44 //
       
    45 // Write the given value-tag pair into the given file.
       
    46 //
       
    47 // @return TInt normal Symbian error code or KErrNone if all went OK.
       
    48 // ---------------------------------------------------------------------------
       
    49 //
       
    50 TInt WriteToConfigFile( RFile& aFile, const TDesC& aTag, const TDesC& aValue )
       
    51 	{
       
    52 	TFileText outTextFile;
       
    53 	outTextFile.Set(aFile);
       
    54 
       
    55 	HBufC* content = HBufC::New( aTag.Length() + aValue.Length());
       
    56 	if(!content)
       
    57 		{
       
    58 		return KErrNoMemory;
       
    59 		}
       
    60 	CleanupStack::PushL(content);
       
    61 	TPtr ptr(content->Des());
       
    62 
       
    63 	ptr.Copy(aTag);
       
    64 	ptr.Append(aValue);
       
    65 
       
    66 	TInt err = outTextFile.Write(ptr);
       
    67 	CleanupStack::PopAndDestroy(content);
       
    68 
       
    69 	if( err != KErrNone)
       
    70 		{
       
    71 		return err;
       
    72 		}
       
    73 	return KErrNone;
       
    74 	}
       
    75 
       
    76 // ---------------------------------------------------------------------------
       
    77 // EnsureNewLineAtEnd
       
    78 //
       
    79 // Add a newline character as a last character in the file.
       
    80 //
       
    81 // @return TInt normal Symbian error code or KErrNone if all went OK.
       
    82 // ---------------------------------------------------------------------------
       
    83 //
       
    84 TInt EnsureNewLineAtEnd(RFile& aFile)
       
    85 	{
       
    86 	// Get last 2-bytes before EOF
       
    87 	// to check for new line character
       
    88 	TInt pos = -2;
       
    89 	TInt err = aFile.Seek(ESeekEnd, pos);
       
    90 	if(err != KErrNone)
       
    91 		{
       
    92 		return err;
       
    93 		}
       
    94 
       
    95 	TBuf8<2> buf;
       
    96 	err = aFile.Read(buf);
       
    97 	if(err != KErrNone)
       
    98 		{
       
    99 		return err;
       
   100 		}
       
   101 
       
   102 	// Ascii value of new line character is 10
       
   103 	if(buf[0] != 10 )
       
   104 		{
       
   105 		TFileText outTextFile;
       
   106 		outTextFile.Set(aFile);
       
   107 
       
   108 		// Write will append newline character
       
   109 		err = outTextFile.Write(KNullDesC);
       
   110 		if( err != KErrNone)
       
   111 			{
       
   112 			return err;
       
   113 			}
       
   114 		}
       
   115 	return KErrNone;
       
   116 	}
       
   117 
       
   118 // ---------------------------------------------------------------------------
       
   119 // ReadConfigFile
       
   120 //
       
   121 // Reads the configuration file to aBuffer, aLineBuffer containing the pointers to lines.
       
   122 //
       
   123 // @return TInt normal Symbian error code or KErrNone if all went OK.
       
   124 // ---------------------------------------------------------------------------
       
   125 //
       
   126 TInt ReadConfigFile(RFile& aFile, TDes& aBuffer, RArray< TPtrC >& aLineBuffer )
       
   127 	{
       
   128 	// Read text file into buffers
       
   129 	TInt ret( KErrNone );
       
   130 	TFileText tf;
       
   131 	tf.Set( aFile );
       
   132 	aBuffer.SetMax();
       
   133 	TPtr ptr( aBuffer.MidTPtr( 0 ) );
       
   134 	TInt used( 0 );
       
   135 	do
       
   136 		{
       
   137 		ret = tf.Read( ptr );
       
   138 		TInt len( ptr.Length() );
       
   139 		if ( ( ret == KErrNone || ret == KErrEof ) && len > 0 )
       
   140 			{
       
   141 			// Store non-empty text line
       
   142 			TInt err( aLineBuffer.Append( ptr ) );
       
   143 			if ( err == KErrNone )
       
   144 				{
       
   145 				ptr.SetMax();
       
   146 				ptr.Set( ptr.MidTPtr( len ) );
       
   147 				ptr.Zero();
       
   148 				used += len;
       
   149 				}
       
   150 			else
       
   151 				{
       
   152 				ret = err;
       
   153 				}
       
   154 			}
       
   155 		}
       
   156 	while ( ret == KErrNone );
       
   157 	if ( ret == KErrEof )
       
   158 		{
       
   159 		// reached the end of file without any other error => this is OK
       
   160 		ret = KErrNone;
       
   161 		}
       
   162 	aBuffer.SetLength( used );
       
   163 
       
   164 	return ret;
       
   165 	}
       
   166 
       
   167 // ---------------------------------------------------------------------------
       
   168 // ReadConfigFile
       
   169 //
       
   170 // @return HBufC containg the read config file. aLineBuffer arrays pointing to lines.
       
   171 // ---------------------------------------------------------------------------
       
   172 //
       
   173 HBufC* ReadConfigFile( RFs& aFs, const TDesC& aFullPath, RArray< TPtrC >& aLineBuffer, TInt& aError )
       
   174 	{
       
   175 	RFile file;
       
   176 	aError = file.Open( aFs, aFullPath, EFileRead | EFileStreamText | EFileShareReadersOnly );
       
   177 	if ( aError != KErrNone )
       
   178 		{
       
   179 		return NULL;
       
   180 		}
       
   181 	TInt size( 0 );
       
   182 	aError = file.Size( size );
       
   183 	if ( aError != KErrNone )
       
   184 		{
       
   185 		file.Close();
       
   186 		return NULL;
       
   187 		}
       
   188 	// Get text size, create buffer for text and read text file
       
   189 	HBufC* ret = HBufC::New( ( size + sizeof( TText ) - 1 ) / sizeof( TText ) );
       
   190 	if ( !ret )
       
   191 		{
       
   192 		aError = KErrNoMemory;
       
   193 		file.Close();
       
   194 		return NULL;
       
   195 		}
       
   196 	TPtr ptr( ret->Des() );
       
   197 	aError = ReadConfigFile( file, ptr, aLineBuffer );
       
   198 	file.Close();
       
   199 	if ( aError != KErrNone )
       
   200 		{
       
   201 		delete ret;
       
   202 		aLineBuffer.Reset();
       
   203 		return NULL;
       
   204 		}
       
   205 	return ret;
       
   206 	}
       
   207 
       
   208 // ---------------------------------------------------------------------------
       
   209 // GetConfigValue
       
   210 //
       
   211 // Gets string specified by tag. For examples:
       
   212 // Tag1=Value1  Tag1 = aTag, Value1 will be copied to aTag
       
   213 // aError Normal Symbian error code or KErrNone if all went OK.
       
   214 // ---------------------------------------------------------------------------
       
   215 //
       
   216 TPtrC GetConfigValue( const TDesC& aTag, const RArray< TPtrC >& aLineBuffer, TInt& aError )
       
   217 	{
       
   218 	aError = KErrNotFound;
       
   219 	TPtrC ret( KNullDesC );
       
   220 	const TInt tagLen( aTag.Length() );
       
   221 	const TInt count( aLineBuffer.Count() );
       
   222 	for( TInt i( 0 ); i < count; ++i )
       
   223 		{
       
   224 		TPtrC line( aLineBuffer[ i ] );
       
   225 		if ( !line.Left( tagLen ).CompareF( aTag ) )
       
   226 			{
       
   227 			ret.Set( line.Mid( tagLen ) );
       
   228 			aError = KErrNone;
       
   229 			break;
       
   230 			}
       
   231 		}
       
   232 
       
   233 	return ret;
       
   234 	}
       
   235 
       
   236 TInt CompareVersions(TVersion& version1,TVersion& version2)
       
   237 	{
       
   238 	// Compare the versions based on major,minor and build number.
       
   239 	if((version1.iMajor == version2.iMajor)&&(version1.iMinor == version2.iMinor)&&(version1.iBuild == version2.iBuild))
       
   240 		{
       
   241 		return EEqualVersion;
       
   242 		}
       
   243 	else if ((version1.iMajor > version2.iMajor) ||
       
   244 			((version1.iMajor == version2.iMajor)&&(version1.iMinor > version2.iMinor)) ||
       
   245 			((version1.iMajor == version2.iMajor)&&(version1.iMinor == version2.iMinor)&&(version1.iBuild >= version2.iBuild)))
       
   246 		{
       
   247 		return EGreaterFirstVersion;
       
   248 		}
       
   249 	else
       
   250 		{
       
   251 		return EGreaterSecondVersion;
       
   252 		}
       
   253 	}
       
   254 
       
   255 TBool SetVersion(const TDesC8& aVersionPtr, TVersion& aVer)
       
   256 	{
       
   257 	// Function will return EFalse if aVersionPtr is not a valid
       
   258 	// version string
       
   259 	TLex8 lex(aVersionPtr);
       
   260 	TInt count = 0;
       
   261 	lex.SkipSpace();
       
   262 
       
   263 	// Get Major Version (max length 3)
       
   264 	const TInt maxMajorVersionLength = 3;
       
   265 	lex.Mark();
       
   266 	while ( (count<maxMajorVersionLength) && (lex.Peek() != '.') )
       
   267 		{
       
   268 		lex.Inc();
       
   269 		++count;
       
   270 		}
       
   271 
       
   272 	if (!(lex.Peek() == '.'))
       
   273 		{
       
   274 		return EFalse;
       
   275 		}
       
   276 
       
   277 	TLex8 lexToken(lex.MarkedToken());
       
   278 	if ( lexToken.Val(aVer.iMajor) != KErrNone )
       
   279 		{
       
   280 		return EFalse;
       
   281 		}
       
   282 
       
   283 	// Get Minor Version(max length 2)
       
   284 	const TInt maxMinorVersionLength = 2;
       
   285 	count = 0;
       
   286 	lex.Inc();
       
   287 	lex.Mark();
       
   288 	while ( (count<maxMinorVersionLength) && (lex.Peek() != '.') )
       
   289 		{
       
   290 		lex.Inc();
       
   291 		++count;
       
   292 		}
       
   293 
       
   294 	lexToken.Assign(lex.MarkedToken());
       
   295 	if ( lexToken.Val(aVer.iMinor) != KErrNone )
       
   296 		{
       
   297 		return EFalse;
       
   298 		}
       
   299 
       
   300 	// Check if Build Number exists,
       
   301 	// otherwise return
       
   302 	if (!(lex.Peek() == '.'))
       
   303 		 {
       
   304 		 lex.Mark();
       
   305 		 lex.SkipCharacters();
       
   306 		 if (lex.TokenLength() > 0)
       
   307 			 {
       
   308 			 return EFalse;
       
   309 			 }
       
   310 		 else
       
   311 			 {
       
   312 			 aVer.iBuild = 0;
       
   313 			 return ETrue;
       
   314 			 }
       
   315 		 }
       
   316 
       
   317 	// Get Build Number(max length 5)
       
   318 	const TInt maxBuildNoLength = 5;
       
   319 	lex.Inc();
       
   320 	lex.Mark();
       
   321 	lex.SkipCharacters();
       
   322 
       
   323 	if (lex.TokenLength() > maxBuildNoLength)
       
   324 		{
       
   325 		return EFalse;
       
   326 		}
       
   327 
       
   328 	lexToken.Assign(lex.MarkedToken());
       
   329 	if ( lexToken.Val(aVer.iBuild) != KErrNone )
       
   330 		{
       
   331 		return EFalse;
       
   332 		}
       
   333 	return ETrue;
       
   334 	}