omxil/xmltestharness/xmlclient/src/omxscript.cpp
branchOpenMAX-IL_SHAI
changeset 16 eedf2dcd43c6
equal deleted inserted replaced
15:c1e808730d6c 16:eedf2dcd43c6
       
     1 /*
       
     2 * Copyright (c) 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 <e32base.h>
       
    20 #include <e32std.h>
       
    21 #include <e32cons.h>			// Console
       
    22 #include <e32debug.h>
       
    23 #include <multimedia/omx_xml_script.h>
       
    24 
       
    25 //  Constants
       
    26 
       
    27 _LIT(KTextConsoleTitle, "Console");
       
    28 _LIT(KTextFailed, " failed, leave code = %d");
       
    29 _LIT(KTextPressAnyKey, " [press any key]\n");
       
    30 
       
    31 //  Global Variables
       
    32 
       
    33 LOCAL_D CConsoleBase* console; // write all messages to this
       
    34 
       
    35 
       
    36 //  Local Functions
       
    37 
       
    38 /**
       
    39  * Logs script output to console and RDebug.
       
    40  */
       
    41 class TConsoleLogger : public MOmxScriptTestLogger
       
    42 	{
       
    43 	void Log(const TText8* aFile, TInt aLine, TOmxScriptSeverity /*aSeverity*/, const TDes& aMessage)
       
    44 		{
       
    45 		TPtrC8 fileDes8(aFile);
       
    46 		TBuf<255> fileDes;
       
    47 		fileDes.Copy(fileDes8);
       
    48 		console->Printf(_L("%S:%d %S\n"), &fileDes, aLine, &aMessage);
       
    49 		RDebug::Print(_L("%S:%d %S"), &fileDes, aLine, &aMessage);
       
    50 		}
       
    51 	};
       
    52 
       
    53 LOCAL_C void ShowUsage()
       
    54 	{
       
    55 	console->Write(_L("Usage: omxscript <filename> <section>\n"));
       
    56 	}
       
    57 
       
    58 LOCAL_C TInt ParseSize(const TDesC& aDes)
       
    59 	{
       
    60 	TLex lex(aDes);
       
    61 	TInt val;
       
    62 	if(lex.Val(val) != KErrNone || val < 0)
       
    63 		{
       
    64 		return KErrArgument;
       
    65 		}
       
    66 	switch(lex.Get())
       
    67 		{
       
    68 	case 0:	// no modifier
       
    69 		break;
       
    70 	case 'K':
       
    71 		val <<= 10;
       
    72 		break;
       
    73 	case 'M':
       
    74 		val <<= 20;
       
    75 		break;
       
    76 	default:	// bad modifier
       
    77 		return KErrArgument;
       
    78 		}
       
    79 	if(lex.Get() != 0)
       
    80 		{
       
    81 		// trailing text
       
    82 		return KErrArgument;
       
    83 		}
       
    84 	return val;
       
    85 	}
       
    86 
       
    87 /**
       
    88  * Extracts parameters from the command line.
       
    89  * This method must not use the cleanup stack; there may not be one allocated since we may be switching heaps.
       
    90  */
       
    91 LOCAL_C TInt ParseCommandLineArgs(TDes& commandLine, TPtrC& aFilename, TPtrC& aSection, TInt &aHeapSize)
       
    92 	{
       
    93 	// copy the command line
       
    94 	if(User::CommandLineLength() > commandLine.MaxLength())
       
    95 		{
       
    96 		return KErrTooBig;
       
    97 		}
       
    98 	User::CommandLine(commandLine);
       
    99 	
       
   100 	// parse filename, section and other args from the command line
       
   101 	TInt heapSize = KErrNotFound;
       
   102 	TPtrC filename(KNullDesC);
       
   103 	TPtrC section(KNullDesC);
       
   104 	TLex lex(commandLine);
       
   105 	lex.SkipSpaceAndMark();
       
   106 	while(!lex.Eos())
       
   107 		{
       
   108 		lex.SkipCharacters();
       
   109 		TPtrC arg = lex.MarkedToken();
       
   110 		lex.SkipSpaceAndMark();
       
   111 		if(arg == _L("-heap"))
       
   112 			{
       
   113 			if(lex.Eos())
       
   114 				{
       
   115 				// no param following
       
   116 				return KErrArgument;
       
   117 				}
       
   118 			lex.SkipCharacters();
       
   119 			TPtrC heapArg = lex.MarkedToken();
       
   120 			lex.SkipSpaceAndMark();
       
   121 			heapSize = ParseSize(heapArg);
       
   122 			if(heapSize == KErrArgument)
       
   123 				{
       
   124 				return KErrArgument;
       
   125 				}
       
   126 			}
       
   127 		else if(filename.Length() == 0)
       
   128 			{
       
   129 			filename.Set(arg);
       
   130 			}
       
   131 		else if(section.Length() == 0)
       
   132 			{
       
   133 			section.Set(arg);
       
   134 			}
       
   135 		else
       
   136 			{
       
   137 			// to many unnamed params
       
   138 			return KErrArgument;
       
   139 			}
       
   140 		}
       
   141 	if(section.Length() == 0)
       
   142 		{
       
   143 		return KErrArgument;
       
   144 		}
       
   145 	aHeapSize = heapSize;
       
   146 	aFilename.Set(filename);
       
   147 	aSection.Set(section);
       
   148 	return KErrNone;
       
   149 	}
       
   150 
       
   151 LOCAL_C void MainL()
       
   152 	{
       
   153 	RBuf commandLine;
       
   154 	commandLine.CreateL(User::CommandLineLength());
       
   155 	CleanupClosePushL(commandLine);
       
   156 	TPtrC filename(KNullDesC);
       
   157 	TPtrC section(KNullDesC);
       
   158 	TInt heapSize = KErrNotFound;
       
   159 	TInt error = ParseCommandLineArgs(commandLine, filename, section, heapSize);
       
   160 	if(error)
       
   161 		{
       
   162 		ShowUsage();
       
   163 		User::Leave(error);
       
   164 		}
       
   165 	
       
   166 	//logs script output to console and RDebug
       
   167 	TConsoleLogger logger;
       
   168 	
       
   169 	COmxXmlScript* script = COmxXmlScript::NewL(logger);
       
   170 	CleanupStack::PushL(script);
       
   171 	script->RunScriptL(filename, section);
       
   172 	CleanupStack::PopAndDestroy(2, &commandLine);
       
   173 	}
       
   174 
       
   175 LOCAL_C void DoStartL()
       
   176 	{
       
   177 	// Create active scheduler (to run active objects)
       
   178 	CActiveScheduler* scheduler = new (ELeave) CActiveScheduler();
       
   179 	CleanupStack::PushL(scheduler);
       
   180 	CActiveScheduler::Install(scheduler);
       
   181 
       
   182 	MainL();
       
   183 
       
   184 	// Delete active scheduler
       
   185 	CleanupStack::PopAndDestroy(scheduler);
       
   186 	}
       
   187 
       
   188 /**
       
   189  * Invokes ParseCommandLineArgs() to retrieve any heap size specified on the command line.
       
   190  * The command line descriptor is allocated on the stack (since we are avoiding heap allocations at this point).
       
   191  * The descriptors are then thrown away (to avoid consuming too much stack space).
       
   192  * Later, the command line will be parsed again but on the heap.
       
   193  */
       
   194 LOCAL_C TInt ParseHeapSize()
       
   195 	{
       
   196 	TInt heapSize = KErrNotFound;
       
   197 	TPtrC filename(KNullDesC);
       
   198 	TPtrC section(KNullDesC);
       
   199 	TBuf<255> commandLine;
       
   200 	// ignore error
       
   201 	ParseCommandLineArgs(commandLine, filename, section, heapSize);
       
   202 	return heapSize;
       
   203 	}
       
   204 
       
   205 //  Global Functions
       
   206 
       
   207 GLDEF_C TInt E32Main()
       
   208 	{
       
   209 	
       
   210 
       
   211 	TInt heapSize = ParseHeapSize();
       
   212 
       
   213 
       
   214 	// switch heap if specified
       
   215 	RHeap* oldHeap = NULL;
       
   216 	RHeap* newHeap = NULL;
       
   217 
       
   218 	if(heapSize != KErrNotFound)
       
   219 		{
       
   220 		const TInt KMinHeapGrowBy = 1;
       
   221 		const TInt KByteAlignment = 0;
       
   222 		const TBool KSingleThreaded = EFalse;
       
   223 		newHeap = User::ChunkHeap(NULL, heapSize, heapSize, KMinHeapGrowBy, KByteAlignment, KSingleThreaded);
       
   224 		if(newHeap == NULL)
       
   225 			{
       
   226 			return KErrNoMemory;
       
   227 			}
       
   228 		oldHeap = User::SwitchHeap(newHeap);		
       
   229 		}
       
   230 
       
   231 	// Create cleanup stack
       
   232 	__UHEAP_MARK;
       
   233 	CTrapCleanup* cleanup = CTrapCleanup::New();
       
   234 
       
   235 	// Create output console
       
   236 	TRAPD(createError, console = Console::NewL(KTextConsoleTitle, TSize(KConsFullScreen,KConsFullScreen)));
       
   237 	if (createError)
       
   238 	return createError;
       
   239 	
       
   240 	// Run application code inside TRAP harness, wait keypress when terminated
       
   241 	TRAPD(mainError, DoStartL());
       
   242 	
       
   243 
       
   244 	if(mainError)
       
   245 		{
       
   246 		console->Printf(KTextFailed, mainError);
       
   247 		}
       
   248 	console->Printf(KTextPressAnyKey);
       
   249 	console->Getch();
       
   250 
       
   251 	delete console;
       
   252 	delete cleanup;
       
   253 	__UHEAP_MARKEND;
       
   254 	
       
   255 	if(newHeap != NULL)
       
   256 		{
       
   257 		User::SwitchHeap(oldHeap);
       
   258 		newHeap->Close();
       
   259 		}
       
   260 
       
   261 	return KErrNone;
       
   262 	}
       
   263