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