applayerprotocols/httptransportfw/Test/T_HttpIntegration/CCmdBase.cpp
changeset 0 b16258d2340f
equal deleted inserted replaced
-1:000000000000 0:b16258d2340f
       
     1 // Copyright (c) 2002-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 // $Header$
       
    15 // The CTEngine is the class that manages the test infrastructure
       
    16 // It knows how to process scripts and where to find command knowledge
       
    17 // The plan is:
       
    18 // Process console command line or command line supplied script
       
    19 // read in each line (or command) and process that (if appropriate)
       
    20 // rev:	mjdavey, symbian@mjdss.com, July 2002
       
    21 // for:	Typhoon (7.0s) & JetStream (8.0)
       
    22 // Include Module Definitions
       
    23 // 
       
    24 //
       
    25 
       
    26 #include "CCmdBase.h"
       
    27 
       
    28 //-----------------------------------------------------------------------------
       
    29 // format for output of data/time values
       
    30 
       
    31 //_LIT(KDateFormat,"%D%M%Y%/0%1%/1%2%/2%3%/3 %:0%H%:1%T%:2%S.%C%:3");
       
    32 _LIT(KDateFormat,"%D%M%Y%/0%1%/1%2%/2%3%/3 %:0%H%:1%T%:2%S");
       
    33 
       
    34 const TInt K_OUTOFBOUNDS_YEAR = 2499;
       
    35 
       
    36 //-----------------------------------------------------------------------------
       
    37 
       
    38 CCmdBase *CCmdBase::NewL(TInt aCommandId, const TDesC& aKeyphrase)
       
    39 {
       
    40 CCmdBase *self = NewLC(aCommandId, aKeyphrase);
       
    41 CleanupStack::Pop();
       
    42 return self; 
       
    43 }
       
    44 
       
    45 //-----------------------------------------------------------------------------
       
    46 
       
    47 CCmdBase *CCmdBase::NewLC(TInt aCommandId, const TDesC& aKeyphrase)
       
    48 {
       
    49 CCmdBase *self = new (ELeave) CCmdBase();
       
    50 CleanupStack::PushL( self );
       
    51 self->ConstructL(aCommandId, aKeyphrase);
       
    52 return self;
       
    53 }
       
    54 
       
    55 //-----------------------------------------------------------------------------
       
    56 
       
    57 CCmdBase *CCmdBase::NewL(TInt aCommandId, const TDesC& aKeyphrase, const TDesC& aHelpPhrase)
       
    58 {
       
    59 CCmdBase *self = NewLC(aCommandId, aKeyphrase, aHelpPhrase);
       
    60 CleanupStack::Pop();
       
    61 return self; 
       
    62 }
       
    63 
       
    64 //-----------------------------------------------------------------------------
       
    65 
       
    66 CCmdBase* CCmdBase::NewLC(TInt aCommandId, const TDesC& aKeyphrase, const TDesC& aHelpPhrase)
       
    67 {
       
    68 CCmdBase* self = new (ELeave) CCmdBase();
       
    69 CleanupStack::PushL(self);
       
    70 self->ConstructL(aCommandId, aKeyphrase, aHelpPhrase);
       
    71 return self;
       
    72 }
       
    73 
       
    74 //-----------------------------------------------------------------------------
       
    75 
       
    76 void CCmdBase::ConstructL( TInt aCommandId, const TDesC& aKeyphrase )
       
    77 {
       
    78 iFamily = NULL;
       
    79 iCommandId = aCommandId;
       
    80 iKeyphrase.Set(aKeyphrase);
       
    81 iHelpText.Set(NULL, 0);
       
    82 iFlags = 0x00000000;
       
    83 SetFlag( EDoLog , ETrue  );
       
    84 iStepOver = EFalse;
       
    85 }
       
    86 
       
    87 //-----------------------------------------------------------------------------
       
    88 
       
    89 void CCmdBase::ConstructL( TInt aCommandId, const TDesC& aKeyphrase, const TDesC& aHelpPhrase )
       
    90 {
       
    91 iFamily = NULL;
       
    92 iCommandId = aCommandId;
       
    93 iKeyphrase.Set(aKeyphrase);
       
    94 iHelpText.Set(aHelpPhrase);
       
    95 iFlags = 0x00000000;
       
    96 SetFlag(EDoLog, ETrue);
       
    97 iStepOver = EFalse;
       
    98 }
       
    99 
       
   100 //-----------------------------------------------------------------------------
       
   101 
       
   102 CCmdBase::~CCmdBase()
       
   103 {
       
   104 }
       
   105 
       
   106 //-----------------------------------------------------------------------------
       
   107 
       
   108 CCmdBase::CCmdBase()
       
   109 	: 
       
   110 	CBase()
       
   111 {
       
   112 }
       
   113 
       
   114 //-----------------------------------------------------------------------------
       
   115 
       
   116 TInt CCmdBase::CommandId()
       
   117 {
       
   118 return iCommandId;
       
   119 }
       
   120 
       
   121 //-----------------------------------------------------------------------------
       
   122 
       
   123 const TDesC &CCmdBase::Keyphrase()
       
   124 {
       
   125 return iKeyphrase;
       
   126 }
       
   127 
       
   128 //-----------------------------------------------------------------------------
       
   129 
       
   130 void CCmdBase::SetFamily(CCmdFamily* aCmdFamily)
       
   131 {
       
   132 iFamily = aCmdFamily;
       
   133 }
       
   134 
       
   135 //-----------------------------------------------------------------------------
       
   136 
       
   137 CCmdFamily* CCmdBase::Family( )
       
   138 {
       
   139 return iFamily;
       
   140 }
       
   141 
       
   142 //-----------------------------------------------------------------------------
       
   143 
       
   144 void CCmdBase::SetHelpText( const TDesC& aHelpText )
       
   145 {
       
   146 iHelpText.Set(aHelpText);
       
   147 }
       
   148 
       
   149 //-----------------------------------------------------------------------------
       
   150 
       
   151 const TDesC& CCmdBase::HelpText()
       
   152 {
       
   153 return iHelpText;
       
   154 }
       
   155 
       
   156 //-----------------------------------------------------------------------------
       
   157 
       
   158 void CCmdBase::SetFlag( TInt aFlag, TBool aBoolean )
       
   159 {
       
   160 if ( aFlag < 1 || aFlag > 32 ) 
       
   161 	return;
       
   162 
       
   163 TUint32 mask = ( 0x00000001<<(aFlag-1) );
       
   164 iFlags = (aBoolean) ? ( iFlags | mask ) : ( iFlags & (~mask) );
       
   165 }
       
   166 
       
   167 //-----------------------------------------------------------------------------
       
   168 
       
   169 TBool CCmdBase::Flag( TInt aFlag )
       
   170 {
       
   171 if ( aFlag < 1 || aFlag > 32 ) 
       
   172 	return EFalse;
       
   173 TUint32 mask = ( 0x00000001<<(aFlag-1) );
       
   174 return ( (iFlags & mask) != 0x00000000 );
       
   175 }
       
   176 
       
   177 //-----------------------------------------------------------------------------
       
   178 
       
   179 void CCmdBase::SetStepOver( TBool aBoolean )
       
   180 {
       
   181 iStepOver = aBoolean;
       
   182 }
       
   183 
       
   184 //-----------------------------------------------------------------------------
       
   185 
       
   186 TBool CCmdBase::StepOver( )
       
   187 {
       
   188 return iStepOver;
       
   189 }
       
   190 
       
   191 //-----------------------------------------------------------------------------
       
   192 //
       
   193 //  CCmdBase::ParamsL
       
   194 //
       
   195 //  Get command parameters = the remainder after the Keyword Phrase part in
       
   196 //    command string. Leaves ((KErrArgument) if can not recogize the command.
       
   197 //    This function accepts the Keywork Phrase if the non-space characters in
       
   198 //    it match to those which are found from the beginning of command string.
       
   199 //    Spaces (white space characters) are fully ignored in here i.e. you can
       
   200 //    have spaces where you like and how may of then as you like; they simply
       
   201 //    do not matter. The case does not matter either (folded comparison).
       
   202 //
       
   203 //    The next character after the Keyword Phrase must not be alphabetic nor
       
   204 //    a digit. The end-of-string will do fine.
       
   205 //
       
   206 //    Return Values:  TPtrC = params (or Leaves)
       
   207 //
       
   208 //-----------------------------------------------------------------------------
       
   209 
       
   210 TPtrC CCmdBase::ParamsL( const TDesC& aCommand )
       
   211 {
       
   212 // The actual beef is in the TLex version, see below.
       
   213 TLex command( aCommand );
       
   214 return ParamsL( command );
       
   215 }
       
   216 
       
   217 //-----------------------------------------------------------------------------
       
   218 
       
   219 TPtrC CCmdBase::ParamsL( TLex aCommand )
       
   220 {
       
   221 // TLex for Keyphrase
       
   222 TLex phrase( iKeyphrase );
       
   223 // Mark start point
       
   224 TLexMark mark;
       
   225 aCommand.Mark( mark );
       
   226 
       
   227 // First char in phrase
       
   228 phrase.SkipSpace();
       
   229 
       
   230 // Check whole phrase...
       
   231 while ( !phrase.Eos() )
       
   232 	{
       
   233 	// Check the next true non-space character in command. The
       
   234 	// end-of-command is not checked here. We rely on that the
       
   235 	// Get() returns 0 if that is the case (and the 0 does not
       
   236 	// match to the true character got from the phrase).
       
   237 	aCommand.SkipSpace();
       
   238 	if ( User::Fold(phrase.Get()) != User::Fold(aCommand.Get()) )
       
   239 		{
       
   240 		// No match: unget to the start point and "L E A V E".
       
   241 		aCommand.UnGetToMark( mark );
       
   242 		User::Leave( KErrArgument  );
       
   243 		}
       
   244 
       
   245 	// Next true non-space character in phrase.
       
   246 	phrase.SkipSpace();
       
   247 	}
       
   248 
       
   249 // Apparently the keyword phrase was there: check that the next
       
   250 // character is not alphabetic nor a decimal digit.
       
   251 if ( !aCommand.Eos() && aCommand.Peek().IsAlphaDigit() )
       
   252 	{
       
   253 	// What a pitty. We were almost there but not quite. Unget
       
   254 	// to the start point and "L E A V E".
       
   255 	aCommand.UnGetToMark( mark );
       
   256 	User::Leave( KErrArgument  );
       
   257 	}
       
   258 
       
   259 // Did it: return the remainder, which hopefully is just what
       
   260 // we aimed at.
       
   261 return aCommand.Remainder();
       
   262 }
       
   263 
       
   264 //-----------------------------------------------------------------------------
       
   265 //
       
   266 //    CCmdBase::Recognize
       
   267 //
       
   268 //    Recognize command string. Resets the StepOver flag from the command
       
   269 //		family. If the family is not defined, then StepOver flag remains as
       
   270 //    happens to be.
       
   271 //
       
   272 //    Return Values:  ETrue/EFalse
       
   273 //
       
   274 //-----------------------------------------------------------------------------
       
   275 
       
   276 TBool CCmdBase::Recognize(const TDesC&  aCommand)
       
   277 {
       
   278 // Obey the command family's step over mode..
       
   279 if (iFamily != NULL) 
       
   280 	SetStepOver(iFamily->StepOver());
       
   281 
       
   282 // The beef is in the ParamsL() function.
       
   283 TPtrC params;
       
   284 TRAPD(error, params.Set(ParamsL(aCommand)));
       
   285 return (error == KErrNone);
       
   286 }
       
   287 
       
   288 //-----------------------------------------------------------------------------
       
   289 
       
   290 TBool CCmdBase::Recognize(TLex aCommand)
       
   291 {
       
   292 // Reset the StepOver flag to what's in command family.
       
   293 if (iFamily != NULL) 
       
   294 	SetStepOver(iFamily->StepOver());
       
   295 
       
   296 // The beef is in the ParamsL() function.
       
   297 TPtrC params;
       
   298 TRAPD(error, params.Set(ParamsL(aCommand)));
       
   299 return (error == KErrNone);
       
   300 }
       
   301 
       
   302 //-----------------------------------------------------------------------------
       
   303 
       
   304 TInt CCmdBase::ProcessL( const TDesC& aCommand )
       
   305 {
       
   306 // Complete the test machine - will then get the next cmd
       
   307 Machine()->CompleteRequest();
       
   308 
       
   309 TPtrC params;
       
   310 TRAPD( error, params.Set( ParamsL( aCommand )) );
       
   311 return error;
       
   312 }
       
   313 
       
   314 //-----------------------------------------------------------------------------
       
   315 //	Complete process without doing anything
       
   316 
       
   317 TInt CCmdBase::ProcessStepOverL()
       
   318 {
       
   319 Machine()->CompleteRequest();
       
   320 return KErrNone;
       
   321 }
       
   322 
       
   323 //-----------------------------------------------------------------------------
       
   324 
       
   325 void CCmdBase::Printf( TRefByValue<const TDesC> aFmt, ... )
       
   326 {
       
   327 VA_LIST list;
       
   328 VA_START(list, aFmt);
       
   329 Machine()->MsgPrintfln( aFmt, list );
       
   330 }
       
   331 
       
   332 //-----------------------------------------------------------------------------
       
   333 
       
   334 void CCmdBase::Print(const TDesC& aText, const TBool &aNewLine)
       
   335 {
       
   336 if (aNewLine)
       
   337 	Machine()->MsgWriteln(aText);
       
   338 else
       
   339 	Machine()->MsgWrite(aText);
       
   340 }
       
   341 
       
   342 //-----------------------------------------------------------------------------
       
   343 
       
   344 TInt CCmdBase::Error( TInt aError, TRefByValue<const TDesC> aFmt, ... )
       
   345 {
       
   346 VA_LIST list;
       
   347 VA_START(list, aFmt);
       
   348 Machine()->MsgPrintfln(aFmt, list);
       
   349 return aError;
       
   350 }   
       
   351 
       
   352 //-----------------------------------------------------------------------------
       
   353 
       
   354 CTEngine* CCmdBase::Machine()
       
   355 {
       
   356 return iFamily->Machine();
       
   357 }
       
   358 
       
   359 //-----------------------------------------------------------------------------
       
   360 
       
   361 CConsoleBase* CCmdBase::Console()
       
   362 {
       
   363 return iFamily->Machine()->Console();
       
   364 }
       
   365 
       
   366 //-----------------------------------------------------------------------------
       
   367 //	provide general log facility (access standard Machine features)
       
   368 
       
   369 void CCmdBase::Log(TRefByValue<const TDesC> aFmt, ... )
       
   370 {
       
   371 VA_LIST list;
       
   372 VA_START(list, aFmt);
       
   373 
       
   374 Machine()->MsgPrintfln(aFmt, list);
       
   375 //Printf(aFmt, list);
       
   376 //if (Machine()->Log())
       
   377 //	Machine()->LogPrintf(aFmt, list);
       
   378 }
       
   379 
       
   380 //-----------------------------------------------------------------------------
       
   381 //	quick default to known 'invalid' date!
       
   382 
       
   383 void CCmdBase::WriteDateStamp()
       
   384 {
       
   385 TDateTime myTempDate(K_OUTOFBOUNDS_YEAR, EJanuary, 0,0,0,0,0);
       
   386 WriteDateStamp(myTempDate);
       
   387 }
       
   388 //TDateTime myTempDate;
       
   389 //myTempDate.SetMonth(0);
       
   390 //myTempDate.SetYear(K_OUTOFBOUNDS_YEAR);
       
   391 
       
   392 //-----------------------------------------------------------------------------
       
   393 //	displays the given date or current date depending on parameter
       
   394 //buf.Format(_L8 ("%04d/%02d/%02d %02d.%02d:%02d:%06d "),dateTime.Year(),dateTime.Month()+1, dateTime.Day()+1,dateTime.Hour(),dateTime.Minute(),dateTime.Second(),dateTime.MicroSecond());
       
   395 //buf.AppendFormat(_L8("%S\015\012"),&aText);
       
   396 
       
   397 void CCmdBase::WriteDateStamp(const TDateTime &aDate)
       
   398 {
       
   399 TDateTime date;
       
   400 TTime now;
       
   401 TBool iEOL = (aDate.Year() == K_OUTOFBOUNDS_YEAR);
       
   402 
       
   403 if (iEOL)
       
   404 	{
       
   405 	now.HomeTime();
       
   406 	date = now.DateTime();
       
   407 	}
       
   408 else
       
   409 	date = aDate;
       
   410 
       
   411 TTime t(date);
       
   412 TBuf<128> dateTimeString;
       
   413 TRAPD(err, t.FormatL(dateTimeString, KDateFormat));
       
   414 if (err == KErrNone)
       
   415 	{
       
   416 	if (iEOL)
       
   417 		Machine()->MsgPrintf(_L("[%S] "), &dateTimeString);
       
   418 	else
       
   419 		Log(_L("[%S]\r\n"), &dateTimeString);
       
   420 	}
       
   421 } 
       
   422 
       
   423 //-----------------------------------------------------------------------------
       
   424 //	update the completion code and set the $ResultCode$ define in Machine()
       
   425 
       
   426 void CCmdBase::SetCompletionCode(TInt aCode)
       
   427 {
       
   428 iCompletionCode = aCode;
       
   429 Machine()->SetResultDefine(aCode);
       
   430 }
       
   431 
       
   432 
       
   433 //-----------------------------------------------------------------------------
       
   434 //  End of File  
       
   435 //-----------------------------------------------------------------------------
       
   436