installationservices/swi/test/testexes/testappinuse/console_app.cpp
changeset 0 ba25891c3a9e
equal deleted inserted replaced
-1:000000000000 0:ba25891c3a9e
       
     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 the License "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 * This console executable runs for a limited time (default 5 seconds but
       
    16 * can be specified on the command line) and optionally holds open
       
    17 * a file from the same SIS file.
       
    18 * Used for testing INC058929
       
    19 *
       
    20 */
       
    21 
       
    22 
       
    23 #include <e32base.h>
       
    24 #include <e32cons.h>
       
    25 #include <bacline.h>
       
    26 #include <f32file.h>
       
    27  
       
    28 
       
    29 _LIT(KTxtEPOC32EX,"console_app: mainL failed");
       
    30 _LIT(KTxtExampleCode,"console_app");
       
    31 _LIT(KFormatRunning ,"Running for %d microseconds. ");
       
    32 _LIT(KSetShutDownTimeOption, "-s");
       
    33 _LIT(KHoldFileOpenSwitch, "-h");
       
    34 _LIT(KDefaultHeldFileName, "\\documents\\insttest\\file.txt");
       
    35 _LIT(KCommandSwitch, "-");
       
    36 _LIT(KHoldingFileStartString, "Holding file  ");
       
    37 _LIT(KHoldingFileEndString, " open.");
       
    38 
       
    39 
       
    40 
       
    41 #ifdef _CONSOLE_APP_LONG_RUNNING_DEFAULT_
       
    42 // Default run time exceeds default runwait timeout value.
       
    43 const TInt KDefaultTimeout = 185000000;
       
    44 #else
       
    45 const TInt KDefaultTimeout = 5000000;
       
    46 #endif
       
    47 
       
    48 // private
       
    49 LOCAL_C void mainL();
       
    50 
       
    51 TInt E32Main() // main function called by E32
       
    52 	{
       
    53 	__UHEAP_MARK;
       
    54 	CTrapCleanup* cleanup=CTrapCleanup::New(); // get clean-up stack
       
    55 	TRAPD(error,mainL()); // Run main method
       
    56 	__ASSERT_ALWAYS(!error,User::Panic(KTxtEPOC32EX,error));
       
    57 	delete cleanup; // destroy clean-up stack
       
    58 	__UHEAP_MARKEND;
       
    59 	return 0; // and return
       
    60 	}
       
    61 
       
    62 LOCAL_C void mainL()
       
    63 	{
       
    64 	TBool holdFileOpen(EFalse);
       
    65 
       
    66 	TInt timerDuration(KDefaultTimeout);
       
    67  
       
    68  	TBuf<256> nextArg(KNullDesC); 
       
    69 	TBuf<256> fileToHold(KNullDesC);
       
    70 		
       
    71     CCommandLineArguments* cmdLine = CCommandLineArguments::NewL();
       
    72 	TInt argTotal=cmdLine->Count();
       
    73 	for (TInt loop=1 ; loop < argTotal ; ++loop)
       
    74 		{
       
    75 		TPtrC arg(cmdLine->Arg(loop));
       
    76 		
       
    77 		// Process the file holding option
       
    78 		if (arg==KHoldFileOpenSwitch)
       
    79 		{	
       
    80 			// The -h option has been issued at the command line so we need to 
       
    81 			// hold a file open. 		
       
    82 			holdFileOpen = ETrue;
       
    83 			
       
    84 			// If there is another argument it may be the filename of 
       
    85 			// the file to be held.
       
    86 			if (loop++ < (argTotal-1)) 
       
    87 			{	
       
    88 				// Read	the argument in so that it can be checked.				
       
    89 				fileToHold = cmdLine->Arg(loop);
       
    90 			
       
    91 	 
       
    92 				if (fileToHold.Left(1) == KCommandSwitch)
       
    93 				{
       
    94 					// The argument is another switch and not a filename
       
    95 					// Decrement the loop counter so that the argument can 
       
    96 					// be processed subsequently.
       
    97 					loop--;		
       
    98 					fileToHold = KNullDesC;
       
    99 				}
       
   100 			
       
   101 			}
       
   102 		 	
       
   103 		}
       
   104 		 
       
   105 		// Process the timer option
       
   106 		else if ((arg==KSetShutDownTimeOption))
       
   107 			{
       
   108 				// If there is another argument it may be the timer value
       
   109 				if (loop++ < (argTotal-1)) 
       
   110 				{	
       
   111 					// Read	the argument in so that it can be checked.					
       
   112 			 		nextArg = cmdLine->Arg(loop);						   	 
       
   113 				
       
   114 	 
       
   115 					if (nextArg.Left(1) == KCommandSwitch) 
       
   116 					{
       
   117 						// The argument is another switch and not a filename
       
   118 						// Decrement the loop counter so that the argument can 
       
   119 						// be processed subsequently .
       
   120 						loop--;		
       
   121 					}
       
   122 					else 
       
   123 					{
       
   124 						// The argument must be the timer value. Extract the value.
       
   125 						TLex timeoutLex(cmdLine->Arg(loop));
       
   126 						timeoutLex.Val(timerDuration);
       
   127 					}
       
   128 				}
       
   129 			 
       
   130 			}
       
   131 		}
       
   132 	delete cmdLine;
       
   133 
       
   134 	// There's only something useful to do if the timeDuration is
       
   135 	// a positive value.
       
   136 	if(timerDuration > 0)
       
   137 		{
       
   138 		CConsoleBase* console; // write all your messages to this
       
   139 
       
   140 		console=Console::NewL(KTxtExampleCode,TSize(KConsFullScreen,KConsFullScreen));
       
   141 		
       
   142 		console->Printf(KFormatRunning, timerDuration);
       
   143 
       
   144 		RTimer timer;
       
   145 		TRequestStatus timerStatus;
       
   146 		timer.CreateLocal();
       
   147 		timer.After(timerStatus, timerDuration);
       
   148 		
       
   149 		RFs fileSession;
       
   150 		fileSession.Connect();
       
   151 
       
   152 		// Timer started, now open the file if "-h" switch specified.
       
   153 		if (holdFileOpen)
       
   154 			{
       
   155 			RFile heldFile;
       
   156 			TDriveUnit sysDrive (RFs::GetSystemDrive());
       
   157 			TFileName fileName (sysDrive.Name());
       
   158 		 
       
   159 			console->Printf(KHoldingFileStartString);
       
   160 
       
   161 
       
   162 			if (fileToHold != KNullDesC)
       
   163 			{
       
   164 				// Use the filename from the command line if provided.
       
   165 				fileName.Append(fileToHold);
       
   166 				console->Printf(fileToHold);
       
   167 				RDebug::Print(_L("Holding file %S open for %d microseconds"), &fileName, timerDuration);	
       
   168 			}
       
   169 			else 
       
   170 			{
       
   171 				// Otherwise use the default file name.
       
   172 				fileName.Append(KDefaultHeldFileName);	
       
   173 				console->Printf(KDefaultHeldFileName);
       
   174 				RDebug::Print(_L("Holding default file %S open for %d microseconds"), &fileName, timerDuration);
       
   175 			}
       
   176 	 
       
   177 	 		console->Printf(KHoldingFileEndString);
       
   178 
       
   179 			// Hold the file
       
   180 			heldFile.Open(fileSession, fileName, EFileRead);
       
   181  
       
   182 			User::WaitForRequest(timerStatus);
       
   183 			heldFile.Close();
       
   184 			}
       
   185 		else 
       
   186 			{
       
   187 			User::WaitForRequest(timerStatus);	
       
   188 			}
       
   189 
       
   190 		timer.Close();
       
   191 
       
   192 		_LIT(KFlaggingFileName, "\\testrun_flag_file.out");
       
   193 		TDriveUnit sysDrive (RFs::GetSystemDrive());
       
   194 		RFile flaggingFile;
       
   195 		TBuf<128> flaggingFileName (sysDrive.Name());
       
   196 		flaggingFileName.Append(KFlaggingFileName);				
       
   197 		flaggingFile.Replace(fileSession, flaggingFileName, EFileWrite);
       
   198 		flaggingFile.Close();
       
   199 			
       
   200 		fileSession.Close();
       
   201 		delete console; // delete console
       
   202 		
       
   203 		}
       
   204     }
       
   205 
       
   206 // End of file