messagingappbase/obexmtms/TObexMTM/testutils/msgth/src/testframeparse.cpp
changeset 0 72b543305e3a
equal deleted inserted replaced
-1:000000000000 0:72b543305e3a
       
     1 // Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include "testframeparse.h"
       
    17 
       
    18 
       
    19 _LIT(KTestErrorUnknown, "No more details available");
       
    20 _LIT(KInvalidNumberOfArguments, "[%4d] Invalid number of arguments, actual: %d, expected: %d");
       
    21 _LIT(KGeneralParseError, "[%4d] Error in command: %S, section: %S");
       
    22 _LIT(KNotParsedError, "[%4d] Command not recognised: %S, in section: %S");
       
    23 
       
    24 //
       
    25 //
       
    26 // CTestBaseCommandParser
       
    27 //
       
    28 
       
    29 EXPORT_C CBaseCommandParser::~CBaseCommandParser()
       
    30 	{
       
    31 	delete iArgumentList;
       
    32 	delete iSupportedCommands;
       
    33 	delete iCurrentCommand;
       
    34 	delete iLastError;
       
    35 	}
       
    36 
       
    37 EXPORT_C void CBaseCommandParser::ConstructL()
       
    38 	{
       
    39 	iArgumentList = new (ELeave) CDesCArrayFlat(3);
       
    40 	iSupportedCommands = new (ELeave) CDesCArrayFlat(1);
       
    41 	iLastError = HBufC::NewL(0);
       
    42 	iCurrentCommand = 0;
       
    43 	}
       
    44 
       
    45 // TODO: this function should really use TLex!
       
    46 EXPORT_C void CBaseCommandParser::ParseL(const TDesC& aCommand, TTestDebugInfo aDebugInfo)
       
    47 // Extract the command and any argments from the given string
       
    48 	{
       
    49 	iDebugInfo = aDebugInfo;
       
    50 	iArgumentList->Reset();
       
    51 
       
    52 	delete iCurrentCommand;
       
    53 	iCurrentCommand = 0;
       
    54 	TInt commandIter = 0;
       
    55 	TInt commandStart = 0;
       
    56 	TBool inQuotes = EFalse;
       
    57 	while (commandIter <= aCommand.Length())
       
    58 		{
       
    59 		TBool commandFound = EFalse;
       
    60 
       
    61 		if (commandIter < aCommand.Length())
       
    62 			{
       
    63 			if (aCommand[commandIter] == '"')
       
    64 				{
       
    65 				// We assume that quotes only occur at the beginning and end
       
    66 				// of arguments. Hence, if we've just found a new quote, we should
       
    67 				// not make it part of the argument.
       
    68 				if (!inQuotes)
       
    69 					{
       
    70 					inQuotes = ETrue;
       
    71 					commandStart++;
       
    72 					}
       
    73 				else
       
    74 					{
       
    75 					// Assume we've got to the end of the argument and skip the
       
    76 					// following space.
       
    77 					commandFound = ETrue;
       
    78 					commandIter++;
       
    79 					}
       
    80 				}
       
    81 			else if ((aCommand[commandIter] == ' ') && (!inQuotes))
       
    82 				{
       
    83 				commandFound = ETrue;
       
    84 				}
       
    85 			}
       
    86 		else
       
    87 			{
       
    88 			commandFound = ETrue;
       
    89 			}
       
    90 		
       
    91 		if (commandFound)
       
    92 			{
       
    93 			if (commandIter > commandStart)
       
    94 			// Command or argument found
       
    95 				{
       
    96 				TInt commandLength = commandIter - commandStart;
       
    97 
       
    98 				// If the argument ended in quotes, modify the length to exclude them.
       
    99 				if (inQuotes)
       
   100 					{
       
   101 					inQuotes = EFalse;
       
   102 					commandLength--;
       
   103 					}
       
   104 				
       
   105 				if (iCurrentCommand == NULL)
       
   106 				// This is the main command
       
   107 					{
       
   108 					iCurrentCommand = HBufC::NewL(commandLength);
       
   109 					(*iCurrentCommand) = aCommand.Mid(commandStart, commandLength);
       
   110 					}
       
   111 				else
       
   112 				// This is a command argument
       
   113 					{
       
   114 					iArgumentList->AppendL(aCommand.Mid(commandStart, commandLength));
       
   115 					}
       
   116 
       
   117 				// Set the start of the next argument.
       
   118 				commandStart = commandIter + 1;
       
   119 				}
       
   120 			}
       
   121 
       
   122 		commandIter++;
       
   123 		}
       
   124 
       
   125 	ProcessL();
       
   126 	}
       
   127 
       
   128 EXPORT_C TBool CBaseCommandParser::CanParse(const TDesC& aCommand) const
       
   129 	{
       
   130 	TBool canParse = EFalse;
       
   131 
       
   132 	TInt commandCounter = iSupportedCommands->Count();
       
   133 	while ((commandCounter-- > 0) && (!canParse))
       
   134 	// Scan through each of the supported commands
       
   135 		{
       
   136 		TInt commandSize = (*iSupportedCommands)[commandCounter].Length();
       
   137 		if (commandSize <= aCommand.Length())
       
   138 		// The given command must be at least as long as the one it is being compared to
       
   139 			{
       
   140 			if (aCommand.Left(commandSize) == (*iSupportedCommands)[commandCounter])
       
   141 			// If the first character match
       
   142 				{
       
   143 				// Check that there are no more characters or that the next one is a whitespace
       
   144 				if (commandSize < aCommand.Length())
       
   145 					{
       
   146 					if (aCommand[commandSize] == ' ')
       
   147 						{
       
   148 						canParse = ETrue;
       
   149 						}
       
   150 					}
       
   151 				else
       
   152 					{
       
   153 					canParse = ETrue;
       
   154 					}
       
   155 				}
       
   156 			}
       
   157 		}
       
   158 
       
   159 	return canParse;
       
   160 	}
       
   161 
       
   162 EXPORT_C CBaseCommandParser::CBaseCommandParser()
       
   163 	{
       
   164 	}
       
   165 
       
   166 EXPORT_C void CBaseCommandParser::AddCommandL(const TDesC& aCommand)
       
   167 	{
       
   168 	iSupportedCommands->AppendL(aCommand);
       
   169 	}
       
   170 
       
   171 
       
   172 EXPORT_C TDesC& CBaseCommandParser::Error()
       
   173 	{
       
   174 	if (iLastError->Length() == 0)
       
   175 		{
       
   176 		delete iLastError;
       
   177 		iLastError = 0;
       
   178 		iLastError = HBufC::NewL(KTestErrorUnknown().Length());
       
   179 		(*iLastError) = KTestErrorUnknown;
       
   180 		}
       
   181 
       
   182 	return *iLastError;
       
   183 	}
       
   184 
       
   185 EXPORT_C void CBaseCommandParser::CheckNumberOfArgumentsL(TInt aNumberOfArguments)
       
   186 	{
       
   187 	if (iArgumentList->Count() != aNumberOfArguments)
       
   188 		{
       
   189 		TBuf<128> errorString;
       
   190 		errorString.Format(KInvalidNumberOfArguments, iDebugInfo.LineNumber(), iArgumentList->Count(), aNumberOfArguments);
       
   191 		SetErrorL(errorString);
       
   192 		User::Leave(KErrArgument);
       
   193 		}
       
   194 	}
       
   195 
       
   196 EXPORT_C void CBaseCommandParser::SetErrorL(const TDesC& aError)
       
   197 	{
       
   198 	delete iLastError;
       
   199 	iLastError = 0;
       
   200 	iLastError = HBufC::NewL(aError.Length());
       
   201 	(*iLastError) = aError;
       
   202 	}
       
   203 
       
   204 
       
   205 //
       
   206 //
       
   207 // CBaseSectionParser
       
   208 //
       
   209 
       
   210 void CBaseSectionParser::SetSectionL(const TDesC& aSectionName)
       
   211 	{
       
   212 	delete iSection;
       
   213 	iSection = 0;
       
   214 	iSection = iScript.GetSectionL(aSectionName);
       
   215 	}
       
   216 
       
   217 EXPORT_C void CBaseSectionParser::ParseL()
       
   218 	{
       
   219 	// Go through each command in the section and parse it
       
   220 	TBool parsed = EFalse;
       
   221 	TBuf<512> currentCommand;
       
   222 	TBool commandNotRecognised = EFalse;
       
   223 	TInt lineNumber = 0;
       
   224 
       
   225 	while (((lineNumber = iSection->GetCurrentCommand(currentCommand)) > 0) && (!commandNotRecognised))
       
   226 		{
       
   227 		TInt commandParserIndex = iCommandParsers->Count();
       
   228 		parsed = EFalse;
       
   229 		while (commandParserIndex--)
       
   230 			// Look for a command parser that can parse the current command
       
   231 			{
       
   232 			if ((*iCommandParsers)[commandParserIndex]->CanParse(currentCommand))
       
   233 				{
       
   234 				TTestDebugInfo debugInfo(iScript, iSection->SectionPosition(), iSection->CurrentCommandPosition(), lineNumber);
       
   235 				TRAPD(err, (*iCommandParsers)[commandParserIndex]->ParseL(currentCommand, debugInfo));
       
   236 				if (err != KErrNone)
       
   237 					{
       
   238 					TBuf<128> errorString;
       
   239 					errorString.Format(KGeneralParseError, lineNumber, &currentCommand, &(iSection->SectionName()));
       
   240 					LogCommentL(errorString);
       
   241 					LogCommentL((*iCommandParsers)[commandParserIndex]->Error());
       
   242 					User::Leave(err);
       
   243 					}
       
   244 				parsed = ETrue;
       
   245 				}
       
   246 			}
       
   247 
       
   248 		if (!parsed)
       
   249 			{
       
   250 			commandNotRecognised = ETrue;
       
   251 			TBuf<256> aError;
       
   252 			aError.Format(KNotParsedError, lineNumber, &currentCommand, &(iSection->SectionName()));
       
   253 			LogCommentL(aError);
       
   254 			User::Leave(KErrNotFound);
       
   255 			}
       
   256 
       
   257 		iSection->NextCommand();
       
   258 		}
       
   259 	}
       
   260 
       
   261 EXPORT_C CBaseSectionParser::~CBaseSectionParser()
       
   262 	{
       
   263 	iCommandParsers->ResetAndDestroy();
       
   264 	delete iCommandParsers;
       
   265 	delete iSection;
       
   266 	}
       
   267 
       
   268 EXPORT_C CBaseSectionParser::CBaseSectionParser(CTestScript& aScript) : iScript(aScript)
       
   269 	{
       
   270 	}
       
   271 
       
   272 EXPORT_C void CBaseSectionParser::AddCommandParserL(CBaseCommandParser* aParser)
       
   273 	{
       
   274 	TRAPD(err, iCommandParsers->AppendL(aParser));
       
   275 	// If the parser can't be appended then delete it.
       
   276 	// Then leave with the original error.
       
   277 	if (err != KErrNone)
       
   278 		{
       
   279 		delete aParser;
       
   280 		User::Leave(err);
       
   281 		}
       
   282 	}
       
   283 
       
   284 EXPORT_C void CBaseSectionParser::ConstructL(const TDesC& aSectionName)
       
   285 	{
       
   286 	iCommandParsers = new (ELeave) CArrayPtrFlat<CBaseCommandParser>(5);
       
   287 	SetSectionL(aSectionName);
       
   288 	}
       
   289