symport/bafl/src/bacline.cpp
changeset 1 0a7b44b10206
child 2 806186ab5e14
equal deleted inserted replaced
0:c55016431358 1:0a7b44b10206
       
     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 the License "Symbian Foundation License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.symbianfoundation.org/legal/sfl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #ifdef __TOOLS2__
       
    17 #ifndef _WIN32
       
    18 #define __TOOLS__
       
    19 #endif
       
    20 #endif
       
    21 
       
    22 #include <bacline.h>
       
    23 
       
    24 #ifdef __TOOLS2__
       
    25 #include <cstring>
       
    26 extern char **gArgv;
       
    27 #endif
       
    28 
       
    29 EXPORT_C CCommandLineArguments* CCommandLineArguments::NewLC()
       
    30 /** Allocates and constructs a command line arguments parser, putting the returned 
       
    31 pointer onto the cleanup stack. The function leaves if there is insufficient 
       
    32 memory.
       
    33 
       
    34 @return The command line arguments parser. */
       
    35 	{
       
    36 	CCommandLineArguments* self=new (ELeave) CCommandLineArguments;
       
    37 	CleanupStack::PushL(self);
       
    38 	self->ConstructL();
       
    39 	return self;
       
    40 	}
       
    41 
       
    42 EXPORT_C CCommandLineArguments* CCommandLineArguments::NewL()
       
    43 /** Allocates and constructs a command line arguments parser. The function leaves 
       
    44 if there is insufficient memory.
       
    45 
       
    46 @return The command line arguments parser. */
       
    47 	{
       
    48 	CCommandLineArguments* self=CCommandLineArguments::NewLC();
       
    49 	CleanupStack::Pop();
       
    50 	return self;
       
    51 	}
       
    52 
       
    53 EXPORT_C CCommandLineArguments::~CCommandLineArguments()
       
    54 /** Frees resources prior to destruction. */
       
    55 	{
       
    56 	delete iArgs;
       
    57 	delete iCommandLine;
       
    58 	}
       
    59 
       
    60 CCommandLineArguments::CCommandLineArguments()
       
    61 	{
       
    62 	}
       
    63 
       
    64 void CCommandLineArguments::ConstructL()
       
    65 	{
       
    66 	// allocate args array
       
    67 	iArgs=new (ELeave) CArrayFixFlat<TPtrC> (10);
       
    68 	// get raw command line
       
    69 
       
    70 #ifndef __TOOLS2__
       
    71 	RProcess me;
       
    72 #endif
       
    73 	iCommandLine=HBufC::NewL(User::CommandLineLength());
       
    74 	TPtr commandLine(iCommandLine->Des());
       
    75 	User::CommandLine(commandLine);
       
    76 #ifndef __TOOLS2__
       
    77 	iFileName=me.FileName();
       
    78 #else
       
    79 	iFileName.Copy(TPtrC8((TUint8*)gArgv[0], strlen(gArgv[0])));
       
    80 #endif
       
    81 
       
    82 	// scan for each argument
       
    83 	TText* out=CONST_CAST(TText*,iCommandLine->Ptr());
       
    84 	const TText* scan=out;
       
    85 	const TText* end=scan+iCommandLine->Length();
       
    86 	while (scan < end) // scan one argument
       
    87 		{
       
    88 		while (scan < end && *scan==' ') // skip leading space
       
    89 			scan++;
       
    90 		if (scan == end) // ignore if blank
       
    91 			break;
       
    92 		TBool quoted=*scan=='\"'; // note leading quote
       
    93 		if (quoted)
       
    94 			scan++;
       
    95 		TText* start=out; // note start in output
       
    96 		if (!quoted) // if not quoted, scan for blank
       
    97 			{
       
    98 			while (scan < end && *scan!=' ')
       
    99 				*out++ = *scan++;
       
   100 			}
       
   101 		else // quoted, scan for quote
       
   102 			{
       
   103 			for (;;) // one quote-or-double sequence
       
   104 				{
       
   105 				while (scan < end && *scan!='\"') // all up to quote
       
   106 					*out++ = *scan++;
       
   107 				if (scan < end) // skip quote
       
   108 					scan++;
       
   109 				if (scan < end && *scan=='\"') // transfer if quote is doubled
       
   110 					*out++ = *scan++;
       
   111 				else // finished this arg
       
   112 					break;
       
   113 				}
       
   114 			}
       
   115 		TPtrC arg(start, out-start);
       
   116 		iArgs->AppendL(arg);
       
   117 		}
       
   118 	}
       
   119 
       
   120 
       
   121 EXPORT_C TPtrC CCommandLineArguments::Arg(TInt aArg) const
       
   122 /** Returns a non-modifiable pointer descriptor representing the specified command-line 
       
   123 argument.
       
   124 
       
   125 Arg(0) is the file name as specified on the command line. Arg(1), Arg(2) etc. 
       
   126 are the arguments specified to the command.
       
   127 
       
   128 The pointer descriptor is valid throughout the lifetime of the CCommandLineArguments 
       
   129 object. If you wish to retain argument values after the CCommandLineArguments 
       
   130 object is destroyed, you should copy the argument data into a different object.
       
   131 
       
   132 @param aArg The index of the desired argument. This number must be less than 
       
   133 Count(). Specify 0 for the name used to invoke the process. Specify 1, 2 etc. 
       
   134 for the arguments.
       
   135 @return Non-modifiable pointer descriptor to the specified argument text. */
       
   136 	{
       
   137 	if (aArg > 0 ) // a normal argument
       
   138 		return iArgs->operator[](aArg-1);
       
   139 	else // process name
       
   140 		return TPtrC(iFileName);
       
   141 	}
       
   142 
       
   143 EXPORT_C TInt CCommandLineArguments::Count() const
       
   144 /** Returns the number of command line arguments, including the program name.
       
   145 
       
   146 @return The number of command line arguments, plus one for the program name. 
       
   147 Returns 1, if no arguments are specified. */
       
   148 	{
       
   149 	return iArgs->Count()+1;
       
   150 	}