testtoolsconn/stat/desktop/source/lib/src/statscriptdecoder.cpp
changeset 0 3da2a79470a7
equal deleted inserted replaced
-1:000000000000 0:3da2a79470a7
       
     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 
       
    20 
       
    21 #include "stdafx.h"
       
    22 #include "statscriptdecoder.h"
       
    23 
       
    24 //----------------------------------------------------------------------------
       
    25 // Contructor
       
    26 CSTATScriptCommand::CSTATScriptCommand()
       
    27 : cCommandID(0), pCommand(NULL), ulLength(0)
       
    28 {
       
    29 }
       
    30 
       
    31 //----------------------------------------------------------------------------
       
    32 // Destructor
       
    33 CSTATScriptCommand::~CSTATScriptCommand()
       
    34 {
       
    35 	Reset();
       
    36 }
       
    37 
       
    38 
       
    39 //----------------------------------------------------------------------------
       
    40 // Set command data
       
    41 bool CSTATScriptCommand::SetData(const char *pData, const unsigned long theLength)
       
    42 {
       
    43 	Reset();
       
    44 
       
    45 	if (pData && theLength)
       
    46 	{
       
    47 		pCommand = new char[theLength + 1];
       
    48 		if (!pCommand)
       
    49 			return false;
       
    50 
       
    51 		memcpy(pCommand, pData, theLength);
       
    52 		*(pCommand + theLength) = (char)0;
       
    53 		ulLength = theLength;
       
    54 	}
       
    55 
       
    56 	return true;
       
    57 }
       
    58 
       
    59 
       
    60 //----------------------------------------------------------------------------
       
    61 // Destructor
       
    62 void CSTATScriptCommand::Reset()
       
    63 {
       
    64 	if (pCommand)
       
    65 	{
       
    66 		delete [] pCommand;
       
    67 		pCommand = NULL;
       
    68 	}
       
    69 
       
    70 	ulLength = 0;
       
    71 }
       
    72 
       
    73 
       
    74 
       
    75 ////////////////////////////////////////////////////////////////////////////////////////
       
    76 // Script Decoder class
       
    77 ////////////////////////////////////////////////////////////////////////////////////////
       
    78 
       
    79 //----------------------------------------------------------------------------
       
    80 // Constructor
       
    81 CSTATScriptDecoder::CSTATScriptDecoder(CSTATLogFile *theLog)
       
    82 : lFileLength(0), iCounter(0),
       
    83   lpBuffer(NULL)
       
    84 {
       
    85 	pLog = theLog;
       
    86 }
       
    87 
       
    88 
       
    89 //----------------------------------------------------------------------------
       
    90 // Destructor
       
    91 CSTATScriptDecoder::~CSTATScriptDecoder()
       
    92 {
       
    93 	Release();
       
    94 }
       
    95 
       
    96 
       
    97 //----------------------------------------------------------------------------
       
    98 // Read the script into memory
       
    99 int
       
   100 CSTATScriptDecoder::Initialise(const CString& file, bool bIsFile)
       
   101 {
       
   102 	lpBuffer = NULL;
       
   103 	int ret = OPEN_SCRIPT_FILE_COMPLETE_FAILURE;
       
   104 
       
   105 	// read the commands into memory
       
   106 	if (bIsFile)
       
   107 	{
       
   108 		CFile script_file;
       
   109 		if (script_file.Open(file, CFile::modeRead))
       
   110 		{
       
   111 			ret = ReadFileIntoMemory(script_file);
       
   112 			script_file.Abort();
       
   113 		}
       
   114 	}
       
   115 	else
       
   116 		ret = ReadDataIntoMemory(file);
       
   117 
       
   118 	// make sure we have valid start/end points in the script
       
   119 	if (ret == ITS_OK)
       
   120 		ret = ValidateScript();
       
   121 
       
   122 	if (ret == ITS_OK)
       
   123 	{
       
   124 		pLog->WriteTimeToLog();
       
   125 
       
   126 		if(bIsFile)
       
   127 		{
       
   128 			CString cBuffer = _T("Script file is : ");
       
   129 			cBuffer += file;
       
   130 			pLog->Write(cBuffer);
       
   131 		}
       
   132 		else
       
   133 			pLog->Write(_T("Interpreting command script..."));
       
   134 
       
   135 		ret = ITS_OK;
       
   136 	}
       
   137 	else
       
   138 		Release();
       
   139 
       
   140 	return ret;
       
   141 }
       
   142 
       
   143 
       
   144 //----------------------------------------------------------------------------
       
   145 // Release resources
       
   146 void CSTATScriptDecoder::Release()
       
   147 {
       
   148 	LastCommand.Reset();
       
   149 
       
   150 	// release the script
       
   151 	if (lpBuffer && lFileLength)
       
   152 	{
       
   153 		delete [] lpBuffer;
       
   154 		lpBuffer = NULL;
       
   155 		lFileLength = iCounter = 0;
       
   156 	}
       
   157 }
       
   158 
       
   159 
       
   160 //----------------------------------------------------------------------------
       
   161 // get the contents of a single command
       
   162 int
       
   163 CSTATScriptDecoder::GetNextCommand(CSTATScriptCommand **ppCommand)
       
   164 {
       
   165 	// get to the start of the command
       
   166 	while (lpBuffer[iCounter] != '<')
       
   167 	{
       
   168 		// comment
       
   169 		if (lpBuffer[iCounter] == '/' && lpBuffer[iCounter + 1] == '*')
       
   170 		{
       
   171 			if (ExtractComment())
       
   172 			{
       
   173 				*ppCommand = &LastCommand;
       
   174 				return LastCommand.cCommandID;
       
   175 			}
       
   176 
       
   177 			return 0;
       
   178 		}
       
   179 
       
   180 		if (!IncrementCounter())
       
   181 			return 0;
       
   182 	}
       
   183 
       
   184 	// step over the '<'
       
   185 	if (!IncrementCounter())
       
   186 		return 0;
       
   187 
       
   188 	// save the command ID
       
   189 	LastCommand.cCommandID = (char)toupper(lpBuffer[iCounter]);
       
   190 	if (!IncrementCounter())
       
   191 		return 0;
       
   192 
       
   193 	// need to step over 'image' with '#image' command
       
   194 	if (LastCommand.cCommandID == '#')
       
   195 	{
       
   196 		while (lpBuffer[iCounter] != ' ')
       
   197 			if (!IncrementCounter())
       
   198 				return 0;
       
   199 
       
   200 		// step over the space
       
   201 		if (!IncrementCounter())
       
   202 			return 0;
       
   203 	}
       
   204 
       
   205 	int iBeginning = iCounter;
       
   206 
       
   207 	// extract the command
       
   208 	while (lpBuffer[iCounter] != '>')
       
   209 		if (!IncrementCounter())
       
   210 			return 0;
       
   211 
       
   212 	// save our command
       
   213 	LastCommand.SetData(lpBuffer + iBeginning, iCounter - iBeginning);
       
   214 
       
   215 	// step over the '>'
       
   216 	IncrementCounter();
       
   217 
       
   218 	// set pointer to the command
       
   219 	*ppCommand = &LastCommand;
       
   220 	return LastCommand.cCommandID;
       
   221 }
       
   222 
       
   223 
       
   224 //----------------------------------------------------------------------------
       
   225 // PRIVATE METHODS
       
   226 //----------------------------------------------------------------------------
       
   227 
       
   228 //----------------------------------------------------------------------------
       
   229 //reads string data into memory so that it can be decoded
       
   230 int CSTATScriptDecoder::ReadDataIntoMemory(const CString& commands)
       
   231 {
       
   232 	// Convert CString to char *
       
   233 	lFileLength = commands.GetLength();		//set length
       
   234 	lpBuffer = new char [lFileLength + 1];		//allocate temporary buffer
       
   235 	if (lpBuffer)
       
   236 	{
       
   237 		if (!pLog->ToAnsi(commands, 
       
   238 								  lpBuffer, 
       
   239 								  lFileLength))
       
   240 		{
       
   241 			return INTERPRET_COMMANDS_FAILURE;		//unable to convert to 8-bit
       
   242 		}
       
   243 
       
   244 		lpBuffer[lFileLength] = 0;
       
   245 		return ITS_OK;
       
   246 	}
       
   247 
       
   248 	return E_OUTOFMEM;
       
   249 }
       
   250 //----------------------------------------------------------------------------
       
   251 //reads file data into memory so that it can be decoded
       
   252 int CSTATScriptDecoder::ReadFileIntoMemory(CFile& scriptdata)
       
   253 {
       
   254 	try
       
   255 	{
       
   256 		lFileLength = scriptdata.GetLength();		//file length
       
   257 		lpBuffer = new char [lFileLength + 1];		//allocate temporary buffer
       
   258 		if (lpBuffer)
       
   259 		{
       
   260 			scriptdata.Read(lpBuffer, lFileLength);	//read in from the file and store in the buffer
       
   261 			lpBuffer[lFileLength] = 0;
       
   262 			return ITS_OK;
       
   263 		}
       
   264 	}
       
   265 	catch(CFileException *e)
       
   266 	{
       
   267 		e -> Delete();
       
   268 		return OPEN_SCRIPT_FILE_COMPLETE_FAILURE;
       
   269 	}
       
   270 
       
   271 	return E_OUTOFMEM;
       
   272 }
       
   273 
       
   274 
       
   275 //----------------------------------------------------------------------------
       
   276 // checks that start and end codes exist in the script
       
   277 // on success, iCounter will be left ready for the first STAT command
       
   278 int CSTATScriptDecoder::ValidateScript()
       
   279 {
       
   280 	CSTATScriptCommand *pCommand;
       
   281 	bool bFound = false;
       
   282 
       
   283 	// reset
       
   284 	iCounter = 0;
       
   285 
       
   286 	// look for end
       
   287 	while (GetNextCommand(&pCommand))
       
   288 	{
       
   289 		if (pCommand->cCommandID == 'E')
       
   290 		{
       
   291 			bFound = true;
       
   292 			break;
       
   293 		}
       
   294 	}
       
   295 
       
   296 	if (bFound)
       
   297 	{
       
   298 		// reset
       
   299 		iCounter = 0;
       
   300 		bFound = false;
       
   301 
       
   302 		// look for begin
       
   303 		while (GetNextCommand(&pCommand))
       
   304 		{
       
   305 			if (pCommand->cCommandID == 'B')
       
   306 			{
       
   307 				bFound = true;
       
   308 				iCounter -= 3;	// step back so this is the first command sent
       
   309 				break;
       
   310 			}
       
   311 		}
       
   312 	}
       
   313 
       
   314 	if (!bFound)
       
   315 		return BEGIN_COMMAND_FAILURE;
       
   316 
       
   317 	return ITS_OK;
       
   318 }
       
   319 
       
   320 
       
   321 //----------------------------------------------------------------------------
       
   322 // get a comment
       
   323 int
       
   324 CSTATScriptDecoder::ExtractComment()
       
   325 {
       
   326 	int iBeginning = iCounter;
       
   327 
       
   328 	LastCommand.cCommandID = lpBuffer[iCounter];
       
   329 	iCounter += 2;
       
   330 
       
   331 	while (((iCounter + 1) < lFileLength) &&
       
   332 		   !(lpBuffer[iCounter] == '*' && lpBuffer[iCounter + 1] == '/') )
       
   333 	{
       
   334 		if (!IncrementCounter())
       
   335 			return 0;
       
   336 	}
       
   337 
       
   338 	// get the last bit of the comment '*/'
       
   339 	if ((iCounter + 1) < lFileLength)
       
   340 		iCounter += 2;
       
   341 
       
   342 	// save our comment
       
   343 	LastCommand.SetData(lpBuffer + iBeginning, iCounter - iBeginning);
       
   344 	return LastCommand.cCommandID;
       
   345 }
       
   346 
       
   347 
       
   348 //----------------------------------------------------------------------------
       
   349 // increment counter but make sure we don't go over the end of the script
       
   350 bool CSTATScriptDecoder::IncrementCounter(void)
       
   351 {
       
   352 	return (++iCounter < lFileLength);
       
   353 }