lowlevellibsandfws/apputils/src/BACLINE.CPP
changeset 0 e4d67989cc36
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     1 // Copyright (c) 1997-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 <bacline.h>
       
    17 
       
    18 
       
    19 EXPORT_C CCommandLineArguments* CCommandLineArguments::NewLC()
       
    20 /** Allocates and constructs a command line arguments parser, putting the returned 
       
    21 pointer onto the cleanup stack. The function leaves if there is insufficient 
       
    22 memory.
       
    23 
       
    24 @return The command line arguments parser. */
       
    25 	{
       
    26 	CCommandLineArguments* self=new (ELeave) CCommandLineArguments;
       
    27 	CleanupStack::PushL(self);
       
    28 	self->ConstructL();
       
    29 	return self;
       
    30 	}
       
    31 
       
    32 EXPORT_C CCommandLineArguments* CCommandLineArguments::NewL()
       
    33 /** Allocates and constructs a command line arguments parser. The function leaves 
       
    34 if there is insufficient memory.
       
    35 
       
    36 @return The command line arguments parser. */
       
    37 	{
       
    38 	CCommandLineArguments* self=CCommandLineArguments::NewLC();
       
    39 	CleanupStack::Pop();
       
    40 	return self;
       
    41 	}
       
    42 
       
    43 EXPORT_C CCommandLineArguments::~CCommandLineArguments()
       
    44 /** Frees resources prior to destruction. */
       
    45 	{
       
    46 	delete iArgs;
       
    47 	delete iCommandLine;
       
    48 	}
       
    49 
       
    50 CCommandLineArguments::CCommandLineArguments()
       
    51 	{
       
    52 	}
       
    53 
       
    54 void CCommandLineArguments::ConstructL()
       
    55 	{
       
    56 	// allocate args array
       
    57 	iArgs=new (ELeave) CArrayFixFlat<TPtrC> (10);
       
    58 	// get raw command line
       
    59 	RProcess me;
       
    60 	iCommandLine=HBufC::NewL(User::CommandLineLength());
       
    61 	TPtr commandLine(iCommandLine->Des());
       
    62 	User::CommandLine(commandLine);
       
    63 	iFileName=me.FileName();
       
    64 	// scan for each argument
       
    65 	TText* out=CONST_CAST(TText*,iCommandLine->Ptr());
       
    66 	const TText* scan=out;
       
    67 	const TText* end=scan+iCommandLine->Length();
       
    68 	while (scan < end) // scan one argument
       
    69 		{
       
    70 		while (scan < end && *scan==' ') // skip leading space
       
    71 			scan++;
       
    72 		if (scan == end) // ignore if blank
       
    73 			break;
       
    74 		TBool quoted=*scan=='\"'; // note leading quote
       
    75 		if (quoted)
       
    76 			scan++;
       
    77 		TText* start=out; // note start in output
       
    78 		if (!quoted) // if not quoted, scan for blank
       
    79 			{
       
    80 			while (scan < end && *scan!=' ')
       
    81 				*out++ = *scan++;
       
    82 			}
       
    83 		else // quoted, scan for quote
       
    84 			{
       
    85 			for (;;) // one quote-or-double sequence
       
    86 				{
       
    87 				while (scan < end && *scan!='\"') // all up to quote
       
    88 					*out++ = *scan++;
       
    89 				if (scan < end) // skip quote
       
    90 					scan++;
       
    91 				if (scan < end && *scan=='\"') // transfer if quote is doubled
       
    92 					*out++ = *scan++;
       
    93 				else // finished this arg
       
    94 					break;
       
    95 				}
       
    96 			}
       
    97 		TPtrC arg(start, out-start);
       
    98 		iArgs->AppendL(arg);
       
    99 		}
       
   100 	}
       
   101 
       
   102 
       
   103 EXPORT_C TPtrC CCommandLineArguments::Arg(TInt aArg) const
       
   104 /** Returns a non-modifiable pointer descriptor representing the specified command-line 
       
   105 argument.
       
   106 
       
   107 Arg(0) is the file name as specified on the command line. Arg(1), Arg(2) etc. 
       
   108 are the arguments specified to the command.
       
   109 
       
   110 The pointer descriptor is valid throughout the lifetime of the CCommandLineArguments 
       
   111 object. If you wish to retain argument values after the CCommandLineArguments 
       
   112 object is destroyed, you should copy the argument data into a different object.
       
   113 
       
   114 @param aArg The index of the desired argument. This number must be less than 
       
   115 Count(). Specify 0 for the name used to invoke the process. Specify 1, 2 etc. 
       
   116 for the arguments.
       
   117 @return Non-modifiable pointer descriptor to the specified argument text. */
       
   118 	{
       
   119 	if (aArg > 0 ) // a normal argument
       
   120 		return iArgs->operator[](aArg-1);
       
   121 	else // process name
       
   122 		return TPtrC(iFileName);
       
   123 	}
       
   124 
       
   125 EXPORT_C TInt CCommandLineArguments::Count() const
       
   126 /** Returns the number of command line arguments, including the program name.
       
   127 
       
   128 @return The number of command line arguments, plus one for the program name. 
       
   129 Returns 1, if no arguments are specified. */
       
   130 	{
       
   131 	return iArgs->Count()+1;
       
   132 	}