testexecfw/tef/utils/src/tefscriptutils.cpp
changeset 0 3e07fef1e154
equal deleted inserted replaced
-1:000000000000 0:3e07fef1e154
       
     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 * Implementation for utitity class used for parsing script files
       
    16 * Collect details on counts of test steps and test cases in a script file
       
    17 *
       
    18 */
       
    19 
       
    20 
       
    21 
       
    22 /**
       
    23  @file TEFScriptUtils.cpp
       
    24 */
       
    25 
       
    26 #include "TEFScriptUtils.h"
       
    27 
       
    28 /**
       
    29  * CScriptPreProcess(const TDesC aScriptFilePath, TCommandForCounting* aCommandsAndCounts, TInt aStructSize) - Constructor
       
    30  * Overloaded constructor used for collecting the script file path along with the structure array and size for storing count values
       
    31  * @param aScriptFilePath - Path of the script file to be loaded for reading
       
    32  * @param aCommandsAndCounts - Pointer to array of TCommandForCounting structures for collecting counts of commands
       
    33  * @param aStructSize - Integer value representing the size of the structure array passed in
       
    34  */
       
    35 CScriptPreProcess::CScriptPreProcess(const TDesC& aScriptFilePath, TPtrC aCommandList[KTEFCommandCountsStructSize])
       
    36 : iScriptFilePath(aScriptFilePath), iStructSize(KTEFCommandCountsStructSize), iScriptData(NULL)
       
    37 	{
       
    38 	for (TInt index=0; index < iStructSize; index++)
       
    39 		{
       
    40 		iCommandsAndCounts[index].iCommandName.Copy(aCommandList[index]);
       
    41 		iCommandsAndCounts[index].iCommandCount = KTEFZeroValue;
       
    42 		}
       
    43 	}
       
    44 
       
    45 /**
       
    46  * ~CScriptPreProcess() - Destructor
       
    47  * Deallocates heap allocation if done
       
    48  */
       
    49 CScriptPreProcess::~CScriptPreProcess()
       
    50 	{
       
    51 	if (iScriptData != 0)
       
    52 		{
       
    53 		delete iScriptData;
       
    54 		}
       
    55 	}
       
    56 
       
    57 /**
       
    58  * CountNoOfOccurences()
       
    59  * Member function used for parsing each of the script line
       
    60  * Updates the structure array for any command of interest found during parsing
       
    61  * @return TInt - Return system-wide error codes
       
    62  */
       
    63 TInt CScriptPreProcess::CountNoOfOccurences()
       
    64 	{
       
    65 	// Construct the buffer with contents of script file
       
    66 	TRAPD(err,CreateScriptDataFromScriptFileL());
       
    67 	if (err != KErrNone)
       
    68 		{
       
    69 		// Return the error value, if the we fail to retieve the script file contents
       
    70 		return err;
       
    71 		}
       
    72 	
       
    73 	TBool statusOfWhile=ETrue;
       
    74 	while (statusOfWhile)
       
    75 		{
       
    76 		// Get the individual script line contents until EOF
       
    77 		statusOfWhile=GetNextScriptLine();
       
    78 
       
    79 		// If the script line is available, parse it and update the command counts in the structure array, if any
       
    80 		if (statusOfWhile)
       
    81 			{
       
    82 			UpdateCommandCounts();
       
    83 			}
       
    84 		}
       
    85 	// Return KErrNone if the process is completed safely
       
    86 	return KErrNone;
       
    87 	}
       
    88 
       
    89 /**
       
    90  * CreateScriptDataFromScriptFileL()
       
    91  * Funtion used to copy the contents of the script file to a buffer for reading
       
    92  */
       
    93 void CScriptPreProcess::CreateScriptDataFromScriptFileL()
       
    94 	{
       
    95 	// Create a file session and connect
       
    96 	RFs fS;
       
    97 	User::LeaveIfError(fS.Connect());
       
    98 	CleanupClosePushL(fS);
       
    99 
       
   100 	// Create a file object and try to open the script file for reading
       
   101 	RFile scriptFile;
       
   102 	User::LeaveIfError(scriptFile.Open(fS,iScriptFilePath,EFileRead | EFileShareAny));
       
   103 	CleanupClosePushL(scriptFile);
       
   104 
       
   105 	// Extract the size of the script file and create a heap buffer to the particular size
       
   106 	TInt fileSize;
       
   107 	User::LeaveIfError(scriptFile.Size(fileSize));
       
   108 
       
   109 	// Create a 16bit heap buffer
       
   110 	iScriptData = HBufC::NewL(fileSize);
       
   111 
       
   112 	// Create a 8 bit heap buffer for temporily copying the contents of the script file from the file object
       
   113 	HBufC8* narrowData = HBufC8::NewL(fileSize);
       
   114 	CleanupStack::PushL(narrowData);
       
   115 	TPtr8 narrowPtr=narrowData->Des();
       
   116 	// Read the file into an 8bit heap buffer
       
   117 	User::LeaveIfError(scriptFile.Read(narrowPtr));
       
   118 
       
   119 	TPtr widePtr(iScriptData->Des());
       
   120 	// Copy it to the 16bit buffer
       
   121 	widePtr.Copy(narrowData->Des());
       
   122 
       
   123 	// Cleanup all heap allocations
       
   124 	CleanupStack::PopAndDestroy(narrowData);
       
   125 	CleanupStack::Pop(2);
       
   126 
       
   127 	// Close the file object and file session
       
   128 	scriptFile.Close();
       
   129 	fS.Close();
       
   130 
       
   131 	// Set up the instance token parser
       
   132 	iScriptLex = iScriptData->Des();
       
   133 	}
       
   134 
       
   135 /**
       
   136  * GetNextScriptLine()
       
   137  * Funtion used to retrieve individual script line and store them to a member buffer for parsing
       
   138  * @return TBool - Returns EFalse if the cursor position reaches end of file. ETrue otherwise
       
   139  */
       
   140 TBool CScriptPreProcess::GetNextScriptLine()
       
   141 	{
       
   142 	// Place the lex marker for the next read
       
   143 	while(!iScriptLex.Eos())
       
   144 		{
       
   145 		// Peek and place the cursor position to the end of line
       
   146 		TChar peek = iScriptLex.Peek();
       
   147 		if(peek == '\n')
       
   148 			{
       
   149 			iScriptLex.Inc();
       
   150 			break;
       
   151 			}
       
   152 		else
       
   153 			{
       
   154 			iScriptLex.Inc();
       
   155 			}
       
   156 		}
       
   157 	
       
   158 	// Store the current line to a member variable
       
   159 	iCurrentScriptLine.Set(iScriptLex.MarkedToken());
       
   160 	iScriptLex.Mark();
       
   161 	// Replace the remainder of the script contents into the original buffer
       
   162 	// This is done to enable retrieving of next line during successive calls to the same function
       
   163 	iScriptLex.Assign(iScriptLex.RemainderFromMark());
       
   164 
       
   165 	// Check for end of file and return status accordingly
       
   166 	if(iCurrentScriptLine.Length() || !iScriptLex.Eos())
       
   167 		{
       
   168 		return ETrue;
       
   169 		}
       
   170 	else
       
   171 		{
       
   172 		return EFalse;
       
   173 		}
       
   174 	}
       
   175 
       
   176 /**
       
   177  * UpdateCommandCounts()
       
   178  * Funtion used to parse the script line and update the the command struture with incrementing counts within the structure array
       
   179  */
       
   180 void CScriptPreProcess::UpdateCommandCounts()
       
   181 	{
       
   182 	// Copy the current script line contents to a TLex for parsing
       
   183 	TLex scriptLine(iCurrentScriptLine);
       
   184 
       
   185 	// Extract the first token from the current script line lex
       
   186 	TPtrC commandName(scriptLine.NextToken());
       
   187 
       
   188 	// Run through a loop and check if the first token of the current script line
       
   189 	// matches the command names provided in the structure array
       
   190 	// If it matches any of the commands in array, increment the counter correspondingly
       
   191 	for (TInt index=0;index<iStructSize;index++)
       
   192 		{
       
   193 		if (iCommandsAndCounts[index].iCommandName.CompareF(commandName) == 0)
       
   194 			{
       
   195 			iCommandsAndCounts[index].iCommandCount++;
       
   196 			}
       
   197 		}
       
   198 	}
       
   199 
       
   200 /**
       
   201  * RetrieveCount(TInt& aIndex, TDes& aCommandName, TInt& aCommandCount)
       
   202  * Funtion used to retrieve individual command counts from the structure array and store them to reference parameters
       
   203  * @param aIndex - TInt value representing the aray index whose details needs to be fetched
       
   204  * @param aCommandName - Reference to a buffer used to retrieve the command whose count is being updated
       
   205  * @param aCount - Reference to TInt descriptor used to retrieve the counts of the command from script file
       
   206  */
       
   207 void CScriptPreProcess::RetrieveValues(TInt& aIndex, TDes& aCommandName, TInt& aCommandCount)
       
   208 	{
       
   209 	aCommandName.Copy(iCommandsAndCounts[aIndex].iCommandName);
       
   210 	aCommandCount = iCommandsAndCounts[aIndex].iCommandCount;
       
   211 	}