os/kernelhwsrv/baseapitest/basesvs/validation/f32/sfsrv/src/T_FileTextData.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 /*
     2 * Copyright (c) 2005-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: 
    15 *
    16 */
    17 
    18 
    19 #include "T_FileTextData.h"
    20 
    21 // constants
    22 const TInt	KReadLength		= 64;
    23 
    24 const TBool KMandatory			= EFalse;
    25 
    26 _LIT( KStrSeekEnd,				"ESeekEnd" );
    27 _LIT( KStrSeekStart,			"ESeekStart" );
    28 _LIT( KStrSeekAddress,			"ESeekAddress");
    29 _LIT( KStrSeekCurrent,			"ESeekCurrent");
    30 // Commands
    31 _LIT( KCmdDestructor,			"~" );
    32 _LIT( KCmdNew,					"new" );
    33 _LIT( KCmdRead,					"Read" );
    34 _LIT( KCmdSeek,					"Seek" );
    35 _LIT( KCmdSet,					"Set" );
    36 _LIT( KCmdWrite,				"Write" );
    37 
    38 // Parameters
    39 _LIT( KParamExpectedString,		"expected_str" );
    40 _LIT( KParamObjectName,			"object_name" );
    41 _LIT( KParamSeekMode,			"seek_mode" );
    42 _LIT( KParamText,				"text" );
    43 _LIT( KParamBufferLength,		"buffer_length");
    44 
    45 CT_FileTextData* CT_FileTextData::NewL()
    46 /**
    47 * Two phase constructor
    48 */
    49 	{
    50 	CT_FileTextData* ret = new (ELeave) CT_FileTextData();
    51 	CleanupStack::PushL( ret );
    52 	ret->ConstructL();
    53 	CleanupStack::Pop( ret );
    54 	return ret;
    55 	}
    56 
    57 CT_FileTextData::CT_FileTextData():
    58 iFileText(NULL)
    59 /**
    60 * Protected constructor. First phase construction
    61 */
    62 	{
    63 	}
    64 	
    65 void CT_FileTextData::ConstructL()
    66 /**
    67 * Protected constructor. Second phase construction
    68 */
    69 	{
    70 	}
    71 	
    72 /**
    73 * Destructor.
    74 */
    75 CT_FileTextData::~CT_FileTextData()
    76 	{
    77 	DoCleanup();
    78 	}
    79 	
    80 TAny* CT_FileTextData::GetObject()
    81 /**
    82 * Return a pointer to the object that the data wraps
    83 *
    84 * @return pointer to the object that the data wraps
    85 */
    86 	{
    87 	return iFileText;
    88 	}
    89 
    90 TBool CT_FileTextData::DoCommandL( const TTEFFunction& aCommand, const TTEFSectionName& aSection, const TInt /*aAsyncErrorIndex*/ )
    91 /**
    92 * Process a command read from the ini file
    93 *
    94 * @param aCommand	the command to process
    95 * @param aSection		the entry in the ini file requiring the command to be processed
    96 *
    97 * @return ETrue if the command is processed
    98 */
    99 	{
   100 	TBool retVal = ETrue;
   101 	
   102 	if ( aCommand == KCmdDestructor )
   103 		{
   104 		DoCleanup();
   105 		}
   106 	else if ( aCommand == KCmdNew )
   107 		{
   108 		DoCmdNew();
   109 		}
   110 	else if ( aCommand == KCmdRead )
   111 		{
   112 		DoCmdRead( aSection );
   113 		}
   114 	else if ( aCommand == KCmdSeek )
   115 		{
   116 		DoCmdSeek( aSection );
   117 		}
   118 	else if ( aCommand == KCmdSet )
   119 		{
   120 		DoCmdSet( aSection );
   121 		}
   122 	else if ( aCommand == KCmdWrite )
   123 		{
   124 		DoCmdWrite( aSection );
   125 		}
   126 	else
   127 	    {
   128 	    retVal = EFalse;    
   129 	    }
   130 	
   131 	return retVal;
   132 	}
   133 	
   134 void CT_FileTextData::DoCleanup()
   135 /**
   136 * Deletes TFileText class instance
   137 */
   138     {
   139 	INFO_PRINTF1( _L( "Delete TFileText class instance" ) );
   140 	delete iFileText;
   141 	iFileText = NULL;
   142 	}
   143 	
   144 void CT_FileTextData::DoCmdNew()
   145 /**
   146 * Creates new TFileText class instance
   147 */
   148 	{
   149 	DoCleanup();
   150 	
   151 	INFO_PRINTF1( _L( "Create new TFileText class instance." ) );
   152 	TRAPD( err, iFileText = new (ELeave) TFileText() );
   153 	if ( err != KErrNone )
   154 		{
   155 		ERR_PRINTF2( _L( "new TFileText() error %d" ), err );
   156 		SetError( err );
   157 		}
   158 	}
   159 	
   160 void CT_FileTextData::DoCmdRead( const TDesC& aSection )
   161 /**
   162 * Reads one line form file using Read()
   163 */
   164 	{
   165 	INFO_PRINTF1( _L( "Read one line from file." ) );
   166 	
   167 	RBuf readLine;
   168 	TInt bufferLength = KReadLength;
   169 	
   170 	GET_OPTIONAL_INT_PARAMETER( KParamBufferLength, aSection, bufferLength );
   171 	TInt err = readLine.Create(bufferLength);
   172 		
   173 	if(err == KErrNone)
   174 		{
   175 		err = iFileText->Read( readLine );
   176 	
   177 		INFO_PRINTF2( _L( "FileText::Read() result - %S" ), &readLine );
   178 		
   179 		if ( err != KErrNone )
   180 			{
   181 			ERR_PRINTF2( _L( "Function returned error %d." ), err );
   182 			SetError( err );
   183 			}
   184 		else
   185 			{
   186 			TPtrC expectedLine;
   187 			if ( GET_OPTIONAL_STRING_PARAMETER( KParamExpectedString, aSection, expectedLine ) )
   188 				{
   189 				if ( readLine.Compare(expectedLine)!=0 )
   190 					{
   191 					ERR_PRINTF3( _L( "Read line \"%S\", expected \"%S\"." ), &readLine, &expectedLine );
   192 					SetBlockResult( EFail );
   193 					}
   194 				}
   195 			}
   196 		readLine.Close();
   197 		}
   198 	else
   199 		{
   200 		SetBlockResult( EFail );
   201 		ERR_PRINTF1( _L( "RBuf initialization failed." ));
   202 		}
   203 	}
   204 	
   205 void CT_FileTextData::DoCmdSeek( const TDesC& aSection )
   206 /**
   207 * performs a seek to start or end of file using Seek()
   208 */
   209 	{
   210 	TSeek mode;
   211 	if ( GetSeekMode( aSection, mode, KMandatory ) )
   212 		{
   213 		TInt err = iFileText->Seek( mode );
   214 	
   215 		if ( err != KErrNone )
   216 			{
   217 			ERR_PRINTF2( _L( "Function returned %d." ), err);
   218 			SetError( err );
   219 			}
   220 		}
   221 	}
   222 
   223 void CT_FileTextData::DoCmdSet( const TDesC& aSection )
   224 /**
   225 * Sets the file to be read from or written to using Set()
   226 */
   227 	{
   228 	TPtrC fileObjectName;
   229 	if ( GET_MANDATORY_STRING_PARAMETER( KParamObjectName, aSection, fileObjectName ) )
   230 		{
   231 		INFO_PRINTF2( _L( "Set( %S )" ), &fileObjectName );
   232 		
   233 		RFile* fileObject = NULL;
   234 		TRAPD( err, fileObject = (RFile*)GetDataObjectL( fileObjectName ) );
   235 
   236 		if ( err == KErrNone )
   237 			{
   238 			iFileText->Set( *fileObject );
   239 			}
   240 		else
   241 			{
   242 			ERR_PRINTF3( _L( "Unable to access object %S (error = %d)"), &fileObjectName, err );
   243 			SetBlockResult( EFail );
   244 			}
   245 		
   246 		}
   247 	}
   248 	
   249 void CT_FileTextData::DoCmdWrite( const TDesC& aSection )
   250 /**
   251 * writes one line of text into file using Write()
   252 */
   253 	{
   254 	TPtrC writeLine;
   255 	if ( GET_MANDATORY_STRING_PARAMETER( KParamText, aSection, writeLine ) )
   256 		{
   257 		
   258 		INFO_PRINTF2( _L( "Write \"%S\" into file" ), &writeLine );
   259 		TInt err = iFileText->Write( writeLine );
   260 		if ( err != KErrNone)
   261 			{
   262 			ERR_PRINTF2( _L( "Function returned %d." ), err);
   263 			SetError( err );
   264 			}
   265 		}
   266 	}
   267 
   268 	
   269 
   270 TBool CT_FileTextData::GetSeekMode( const TDesC& aSection, TSeek& aMode, TBool aOptional )
   271 /** 
   272 * retrieves "seek_mode" parameter value and converts it into its TSeek representation
   273 * @param aSection 	- the entry in the ini file requiring the command to be processed
   274 * @param aMode		- the returned TSeek representation of "seek_mode" command parameter
   275 * @param aOptional	- represents the function which is called to retrieve the value
   276 *					KOptional	- GET_OPTIONAL_STRING_PARAMETER is called
   277 *					KMandatory	- GET_MANDATORY_STRING_PARAMETER is called
   278 *
   279 * @return ETrue if "seek_mode" parameter is present and has been sucessfully converted
   280 */
   281 	{
   282 	TBool result = ETrue;
   283 	
   284 	TPtrC strSeekMode;
   285 	
   286 	// Get "seek_mode" command parameter string value
   287 	if ( aOptional )
   288 		{
   289 		result = GET_OPTIONAL_STRING_PARAMETER( KParamSeekMode, aSection, strSeekMode );
   290 		}
   291 	else
   292 		{
   293 		result = GET_MANDATORY_STRING_PARAMETER( KParamSeekMode, aSection, strSeekMode );
   294 		}
   295 	
   296 	// Convert it into TSeek representation
   297 	if ( result )
   298 		{
   299 		if ( strSeekMode == KStrSeekEnd )
   300 			{
   301 			aMode = ESeekEnd;
   302 			}
   303 		else if ( strSeekMode == KStrSeekStart )
   304 			{
   305 			aMode = ESeekStart;
   306 			}
   307 		else if ( strSeekMode == KStrSeekAddress )
   308 			{
   309 			aMode = ESeekAddress;
   310 			}
   311 		else if ( strSeekMode == KStrSeekCurrent )
   312 			{
   313 			aMode = ESeekCurrent;
   314 			}
   315 		else
   316 			{
   317 			ERR_PRINTF2( _L( "Unrecognized seek_mode value: %S" ), &strSeekMode );
   318 			result = EFalse;
   319 			}
   320 		}
   321 			
   322 	return result;
   323 	}
   324  
   325